Loading module/Oscar/src/Oscar/Form/ProjectGrantForm.php +4 −22 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ use Oscar\Service\OscarConfigurationService; use Oscar\Service\ProjectGrantService; use Oscar\Traits\UseServiceContainer; use Oscar\Traits\UseServiceContainerTrait; use Oscar\Utils\FreeField\FreeFieldUtilsFactory; use Oscar\Validator\EOTP; use UnicaenApp\Util; use Laminas\Filter\StringTrim; Loading Loading @@ -176,28 +177,9 @@ class ProjectGrantForm extends Form implements InputFilterProviderInterface, Use $this->_freeFieldsKey = $freeFields; foreach ($freeFields as $key=>$freeField) { $type = 'Text'; $attributes = [ 'class' => 'form-control', ]; $options = [ 'label' => $freeField['label'], 'required' => $freeField['required'] === true, // 'config' => $fieldConfig, //'use' => $fieldConfig['use'], 'label_attributes' => [ 'class' => 'control-label' ] ]; $this->add([ 'name' => $key, 'label' => $freeField['label'], 'help' => "Some helping message", 'required' => $freeField['required'] === true, 'type' => $type, 'attributes' => $attributes, 'options' => $options, ]); $field = FreeFieldUtilsFactory::create($freeField, $key); $this->add($field->getFormParams()); } $this->add([ Loading module/Oscar/src/Oscar/Hydrator/ProjectGrantFormHydrator.php +29 −18 Original line number Diff line number Diff line Loading @@ -8,17 +8,19 @@ namespace Oscar\Hydrator; use Doctrine\ORM\EntityManager; use Exception; use Oscar\Entity\Activity; use Oscar\Entity\ActivityFreeField; use Oscar\Entity\ActivityFreeFieldValue; use Oscar\Entity\Repository\ActivityFreeFieldRepository; use Oscar\Hydrator\Hydrator; use Oscar\Service\OscarConfigurationService; use Oscar\Service\ProjectGrantService; use Oscar\Traits\UseServiceContainer; use Oscar\Traits\UseServiceContainerTrait; use Oscar\Utils\DateTimeUtils; use Laminas\Hydrator\HydratorInterface; use Oscar\Utils\FreeField\FreeFieldUtils; use Oscar\Utils\FreeField\FreeFieldUtilsFactory; class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer { Loading Loading @@ -84,6 +86,8 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer /** * @param array $data * @param Activity $object * @return Activity * @throws Exception */ public function hydrate(array $data, $object) { Loading @@ -101,30 +105,32 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer $freeFields = $this->getFreeFields(); foreach ($freeFields as $key => $fieldInfos) { $fieldConf = FreeFieldUtilsFactory::create($fieldInfos, $key); $typeObj = $ffRepo->find($fieldInfos['id']); switch ($fieldInfos['type']) { case 'text' : case FreeFieldUtils::TYPE_TEXT : $value = [ 'label' => $data[$key], 'value' => $data[$key], ]; break; case FreeFieldUtils::TYPE_LIST : $value = $fieldConf->hydrateFromData($data); break; default : throw new Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas géré"); } $freeField = $object->getFreeFieldOfType($typeObj); if (!$freeField) { $freeField = new ActivityFreeFieldValue(); $freeField->setType($typeObj); $freeField->setActivity($object); $object->getFreeFields()->add($freeField); } else { //$freeField = $this->getEntityManager()->getRepository(ActivityFreeFieldValue::class)->find($freeField->getId()); } $freeField->setData($value); break; default : throw new \Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); } } Loading Loading @@ -318,15 +324,20 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer $ffRepo = $this->getActivityFreeFieldRepository(); foreach ($freeFields as $key => $fieldInfos) { switch ($fieldInfos['type']) { case 'text' : $typeObj = $ffRepo->find($fieldInfos['id']); $value = $object->getFreeFieldsValues($typeObj); switch ($fieldInfos['type']) { case FreeFieldUtils::TYPE_TEXT : $out[$key] = $value; break; case FreeFieldUtils::TYPE_LIST : $out[$key] = $value; break; default : throw new \Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); throw new Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); } } Loading module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtils.php 0 → 100644 +65 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; /** * Forme générique de champs libre */ class FreeFieldUtils { protected string $key; protected string $type; protected string $typeForm; const TYPE_TEXT = 'text'; const TYPE_DATE = 'date'; const TYPE_INTEGER = 'integer'; const TYPE_LIST = 'list'; public function __construct(array $freeFieldOptions, string $key) { $this->freeFieldOptions = $freeFieldOptions; $this->key = $key; } public function getFormParams(): array { return [ 'name' => $this->key, 'label' => $this->freeFieldOptions['label'], 'help' => "Some helping message", 'required' => $this->freeFieldOptions['required'] === true, 'type' => $this->typeForm, 'attributes' => $this->getFormParamsAttributes(), 'options' => $this->getFormParamsOptions(), ]; } protected function getFormParamsOptions(): array { return [ 'label' => $this->freeFieldOptions['label'], 'required' => $this->freeFieldOptions['required'] === true, 'label_attributes' => [ 'class' => 'control-label' ] ]; } protected function getFormParamsAttributes(): array { return [ 'class' => 'form-control', ]; } public function hydrateFromData(array $data) :array { return [ 'label' => $data[$this->key], 'value' => $data[$this->key], ]; } } No newline at end of file module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtilsFactory.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; use Oscar\Exception\OscarException; class FreeFieldUtilsFactory { public static function create(array $config, string $key): FreeFieldUtils { $type = $config['type']; switch ($type) { case FreeFieldUtils::TYPE_TEXT : return new FreeFieldUtilsText($config, $key); case FreeFieldUtils::TYPE_LIST : return new FreeFieldUtilsList($config, $key); default: throw new OscarException("Type '$type' not supported"); } } } No newline at end of file module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtilsList.php 0 → 100644 +46 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; class FreeFieldUtilsList extends FreeFieldUtils { protected string $type = FreeFieldUtils::TYPE_LIST; protected string $typeForm = 'Select'; protected function getFormParamsOptions(): array { $options = parent::getFormParamsOptions(); $options['value_options'] = $this->getOptionsValues(); return $options; } protected function getOptionsValues(): array { $options = []; if (array_key_exists('params', $this->freeFieldOptions) && array_key_exists('values', $this->freeFieldOptions['params'])) { $options = $this->freeFieldOptions['params']['values']; } return $options; } public function getOptionsLabel($value): string { foreach ($this->getOptionsValues() as $option) { if ($option['value'] == $value) { return $option['label']; } } return ""; } public function hydrateFromData(array $data): array { $value = array_key_exists($this->key, $data) ? $data[$this->key] : null; return [ 'label' => $this->getOptionsLabel($value), 'value' => $value, ]; } } No newline at end of file Loading
module/Oscar/src/Oscar/Form/ProjectGrantForm.php +4 −22 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ use Oscar\Service\OscarConfigurationService; use Oscar\Service\ProjectGrantService; use Oscar\Traits\UseServiceContainer; use Oscar\Traits\UseServiceContainerTrait; use Oscar\Utils\FreeField\FreeFieldUtilsFactory; use Oscar\Validator\EOTP; use UnicaenApp\Util; use Laminas\Filter\StringTrim; Loading Loading @@ -176,28 +177,9 @@ class ProjectGrantForm extends Form implements InputFilterProviderInterface, Use $this->_freeFieldsKey = $freeFields; foreach ($freeFields as $key=>$freeField) { $type = 'Text'; $attributes = [ 'class' => 'form-control', ]; $options = [ 'label' => $freeField['label'], 'required' => $freeField['required'] === true, // 'config' => $fieldConfig, //'use' => $fieldConfig['use'], 'label_attributes' => [ 'class' => 'control-label' ] ]; $this->add([ 'name' => $key, 'label' => $freeField['label'], 'help' => "Some helping message", 'required' => $freeField['required'] === true, 'type' => $type, 'attributes' => $attributes, 'options' => $options, ]); $field = FreeFieldUtilsFactory::create($freeField, $key); $this->add($field->getFormParams()); } $this->add([ Loading
module/Oscar/src/Oscar/Hydrator/ProjectGrantFormHydrator.php +29 −18 Original line number Diff line number Diff line Loading @@ -8,17 +8,19 @@ namespace Oscar\Hydrator; use Doctrine\ORM\EntityManager; use Exception; use Oscar\Entity\Activity; use Oscar\Entity\ActivityFreeField; use Oscar\Entity\ActivityFreeFieldValue; use Oscar\Entity\Repository\ActivityFreeFieldRepository; use Oscar\Hydrator\Hydrator; use Oscar\Service\OscarConfigurationService; use Oscar\Service\ProjectGrantService; use Oscar\Traits\UseServiceContainer; use Oscar\Traits\UseServiceContainerTrait; use Oscar\Utils\DateTimeUtils; use Laminas\Hydrator\HydratorInterface; use Oscar\Utils\FreeField\FreeFieldUtils; use Oscar\Utils\FreeField\FreeFieldUtilsFactory; class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer { Loading Loading @@ -84,6 +86,8 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer /** * @param array $data * @param Activity $object * @return Activity * @throws Exception */ public function hydrate(array $data, $object) { Loading @@ -101,30 +105,32 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer $freeFields = $this->getFreeFields(); foreach ($freeFields as $key => $fieldInfos) { $fieldConf = FreeFieldUtilsFactory::create($fieldInfos, $key); $typeObj = $ffRepo->find($fieldInfos['id']); switch ($fieldInfos['type']) { case 'text' : case FreeFieldUtils::TYPE_TEXT : $value = [ 'label' => $data[$key], 'value' => $data[$key], ]; break; case FreeFieldUtils::TYPE_LIST : $value = $fieldConf->hydrateFromData($data); break; default : throw new Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas géré"); } $freeField = $object->getFreeFieldOfType($typeObj); if (!$freeField) { $freeField = new ActivityFreeFieldValue(); $freeField->setType($typeObj); $freeField->setActivity($object); $object->getFreeFields()->add($freeField); } else { //$freeField = $this->getEntityManager()->getRepository(ActivityFreeFieldValue::class)->find($freeField->getId()); } $freeField->setData($value); break; default : throw new \Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); } } Loading Loading @@ -318,15 +324,20 @@ class ProjectGrantFormHydrator implements HydratorInterface, UseServiceContainer $ffRepo = $this->getActivityFreeFieldRepository(); foreach ($freeFields as $key => $fieldInfos) { switch ($fieldInfos['type']) { case 'text' : $typeObj = $ffRepo->find($fieldInfos['id']); $value = $object->getFreeFieldsValues($typeObj); switch ($fieldInfos['type']) { case FreeFieldUtils::TYPE_TEXT : $out[$key] = $value; break; case FreeFieldUtils::TYPE_LIST : $out[$key] = $value; break; default : throw new \Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); throw new Exception("Le type de champs personnalisé '" . $fieldInfos['type'] . "' n'est pas pris en charge"); } } Loading
module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtils.php 0 → 100644 +65 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; /** * Forme générique de champs libre */ class FreeFieldUtils { protected string $key; protected string $type; protected string $typeForm; const TYPE_TEXT = 'text'; const TYPE_DATE = 'date'; const TYPE_INTEGER = 'integer'; const TYPE_LIST = 'list'; public function __construct(array $freeFieldOptions, string $key) { $this->freeFieldOptions = $freeFieldOptions; $this->key = $key; } public function getFormParams(): array { return [ 'name' => $this->key, 'label' => $this->freeFieldOptions['label'], 'help' => "Some helping message", 'required' => $this->freeFieldOptions['required'] === true, 'type' => $this->typeForm, 'attributes' => $this->getFormParamsAttributes(), 'options' => $this->getFormParamsOptions(), ]; } protected function getFormParamsOptions(): array { return [ 'label' => $this->freeFieldOptions['label'], 'required' => $this->freeFieldOptions['required'] === true, 'label_attributes' => [ 'class' => 'control-label' ] ]; } protected function getFormParamsAttributes(): array { return [ 'class' => 'form-control', ]; } public function hydrateFromData(array $data) :array { return [ 'label' => $data[$this->key], 'value' => $data[$this->key], ]; } } No newline at end of file
module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtilsFactory.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; use Oscar\Exception\OscarException; class FreeFieldUtilsFactory { public static function create(array $config, string $key): FreeFieldUtils { $type = $config['type']; switch ($type) { case FreeFieldUtils::TYPE_TEXT : return new FreeFieldUtilsText($config, $key); case FreeFieldUtils::TYPE_LIST : return new FreeFieldUtilsList($config, $key); default: throw new OscarException("Type '$type' not supported"); } } } No newline at end of file
module/Oscar/src/Oscar/Utils/FreeField/FreeFieldUtilsList.php 0 → 100644 +46 −0 Original line number Diff line number Diff line <?php namespace Oscar\Utils\FreeField; class FreeFieldUtilsList extends FreeFieldUtils { protected string $type = FreeFieldUtils::TYPE_LIST; protected string $typeForm = 'Select'; protected function getFormParamsOptions(): array { $options = parent::getFormParamsOptions(); $options['value_options'] = $this->getOptionsValues(); return $options; } protected function getOptionsValues(): array { $options = []; if (array_key_exists('params', $this->freeFieldOptions) && array_key_exists('values', $this->freeFieldOptions['params'])) { $options = $this->freeFieldOptions['params']['values']; } return $options; } public function getOptionsLabel($value): string { foreach ($this->getOptionsValues() as $option) { if ($option['value'] == $value) { return $option['label']; } } return ""; } public function hydrateFromData(array $data): array { $value = array_key_exists($this->key, $data) ? $data[$this->key] : null; return [ 'label' => $this->getOptionsLabel($value), 'value' => $value, ]; } } No newline at end of file