Commit 919c4208 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Traitement des données des FreeFields centralisés (modulable)

parent 43b2fb29
Loading
Loading
Loading
Loading
+4 −22
Original line number Diff line number Diff line
@@ -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;
@@ -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([
+29 −18
Original line number Diff line number Diff line
@@ -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
{
@@ -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)
    {
@@ -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");
            }
        }


@@ -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");
            }
        }

+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
+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
+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