Commit fabe01ea authored by Herve Marie's avatar Herve Marie
Browse files

Ajout relation DateType vers Roles, modif controlleur, DateTypeForm, hydrator, et template

parent 14a77c80
Loading
Loading
Loading
Loading
Loading
+42 −9
Original line number Diff line number Diff line
@@ -9,15 +9,25 @@ namespace Oscar\Controller;


use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Oscar\Entity\ActivityDate;
use Oscar\Entity\DateType;
use Oscar\Entity\DateTypeRepository;
use Oscar\Exception\OscarException;
use Oscar\Form\DateTypeForm;
use Oscar\Service\OscarUserContext;
use Oscar\Traits\UseEntityManager;
use Oscar\Traits\UseEntityManagerTrait;
use Oscar\Traits\UseOscarUserContextService;
use Oscar\Traits\UseOscarUserContextServiceTrait;
use Zend\Http\Response;
use Zend\View\Model\ViewModel;

class DateTypeController extends AbstractOscarController
class DateTypeController extends AbstractOscarController implements UseOscarUserContextService
{
    use UseOscarUserContextServiceTrait;

    public function indexAction()
    {
        return [
@@ -25,9 +35,15 @@ class DateTypeController extends AbstractOscarController
        ];
    }

    public function newAction()
    /**
     * Ajoute un nouveau type de jalons
     * @return ViewModel
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function newAction(): ViewModel
    {
        $form = new DateTypeForm();
        $form = new DateTypeForm($this->getOscarUserContextService()->getOscarRoles(), $this->getEntityManager());
        $request = $this->getRequest();
        $entity = new DateType();
        $form->setObject($entity);
@@ -52,7 +68,14 @@ class DateTypeController extends AbstractOscarController
        return $view;
    }

    public function deleteAction()
    /**
     * Supprime un jalon et la relation de ce jalon avec des roles associés
     * @return Response
     * @throws ORMException
     * @throws OptimisticLockException
     * @throws OscarException
     */
    public function deleteAction(): Response
    {
        $id = $this->params()->fromRoute('id');
        $dateType = $this->getEntityManager()->getRepository(DateType::class)->find($id);
@@ -74,16 +97,27 @@ class DateTypeController extends AbstractOscarController
        }
    }

    public function editAction()
    /**
     * Modification d'un jalon
     * @return ViewModel
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function editAction(): ViewModel
    {
        $form = new DateTypeForm();
        $entity = $this->getEntityManager()->getRepository(DateType::class)->find($this->params()->fromRoute('id'));
        $rolesCheck = $entity->getRoles();
        $arrayRoles = [];
        foreach ($rolesCheck as $role){
            $arrayRoles [] = $role->getId();
        }

        $form = new DateTypeForm($this->getOscarUserContextService()->getOscarRoles(), $this->getEntityManager(), $arrayRoles);
        $request = $this->getRequest();
        $form->setAttribute('action', $this->url()->fromRoute('datetype/edit', ['id' => $entity->getId()]));
        //$form->init();
        $form->bind($entity);
        $form->get('finishable')->setAttribute('checked', ($form->get('finishable')->getValue()) ? 'checked' : '');

        // Traitement des données envoyées
        if ($request->isPost()) {
            $form->setData($request->getPost());
@@ -102,5 +136,4 @@ class DateTypeController extends AbstractOscarController
        $view->setTemplate('oscar/date-type/form.phtml');
        return $view;
    }

}
+49 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
namespace Oscar\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
@@ -61,18 +62,26 @@ class DateType implements ITrackable
    private $finishable = false;

    /**
     * @var \Doctrine\Common\Collections\Collection
     * @var Collection
     * @ORM\OneToMany(targetEntity="ActivityDate", mappedBy="type")
     */
    private $milestones;

    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="datesType")
     * @ORM\JoinTable(name="role_datetype")
     *      joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="date_type_id", referencedColumnName="id")})
     */
    protected $roles;

    /**
     * DateType constructor.
     * @param \Doctrine\Common\Collections\Collection $milestones
     */
    public function __construct()
    {
        $this->milestones = new ArrayCollection();
        $this->roles = new ArrayCollection();
    }


@@ -181,6 +190,44 @@ class DateType implements ITrackable
        ];
    }


    /**
     * @return Collection|Role[]
     */
    public function getRoles(): Collection
    {
        return $this->roles;
    }

    /**
     * @param Role $role
     * @return $this
     */
    public function addRole(Role $role): self
    {
        if (!$this->roles->contains($role)) {
            $this->roles[] = $role;
            $role->addDateType($this);
        }

        return $this;
    }

    /**
     * @param Role $role
     * @return $this
     */
    public function removeRole(Role $role): self
    {
        if ($this->roles->contains($role)) {
            $this->roles->removeElement($role);
            $role->removeDateType($this);
        }

        return $this;
    }


    function __toString()
    {
        return $this->getLabel();
+47 −1
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ namespace Oscar\Entity;
use BjyAuthorize\Acl\HierarchicalRoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use UnicaenAuth\Acl\NamedRole;

/**
@@ -33,6 +34,7 @@ class Role implements HierarchicalRoleInterface
    public function __construct()
    {
        $this->privileges = new ArrayCollection();
        $this->datesType = new ArrayCollection();
    }

    public function asArray()
@@ -149,6 +151,15 @@ class Role implements HierarchicalRoleInterface
    protected $accessibleExterieur = true;


    /**
     * @ORM\ManyToMany(targetEntity="DateType", mappedBy="roles", fetch="EAGER")
     */

    private $datesType;






    ///////////////////////////////////////////////////////////////// PRIVILEGES
@@ -466,4 +477,39 @@ class Role implements HierarchicalRoleInterface
    {
        return $this->isLevel(self::LEVEL_ACTIVITY);
    }

    /**
     * @return Collection|DateType[]
     */
    public function getDatesType(): Collection
    {
        return $this->datesType;
    }

    /**
     * @param DateType $dateType
     * @return $this
     */
    public function addDateType(DateType  $dateType): self
    {
        if (!$this->datesType->contains($dateType)) {
            $this->datesType[] = $dateType;
        }
        return $this;
    }

    /**
     * @param DateType $dateType
     * @return $this
     */
    public function removeDateType(DateType $dateType): self
    {
        if ($this->datesType->contains($dateType)) {
            $this->datesType->removeElement($dateType);
        }
        return $this;
    }



}
+49 −7
Original line number Diff line number Diff line
@@ -7,20 +7,22 @@

namespace Oscar\Form;


use Doctrine\ORM\EntityManager;
use Oscar\Entity\OscarFacet;
use Oscar\Hydrator\DateTypeFormHydrator;
use Zend\Form\Element\Checkbox;
use Zend\Form\Element\MultiCheckbox;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class DateTypeForm extends Form implements InputFilterProviderInterface
{
    public function __construct()

    public function __construct(array $roles, EntityManager $em, array $idsRolesCheck = [])
    {
        parent::__construct('datetype');

        $this->setHydrator(new DateTypeFormHydrator());
        $this->setHydrator(new DateTypeFormHydrator($em));

        $this->add(array(
            'name'  => 'id',
@@ -104,22 +106,62 @@ class DateTypeForm extends Form implements InputFilterProviderInterface
            'type'=>'Textarea'
        ]);

        // ROLES
        $label = 'Roles';
        $this->add(
            [
                'type'=>MultiCheckbox::class,
                'name'   => 'roles',
                'attributes'    => [
                    'class'       => 'form-control',
                    'multiple' => 'multiple',
                ],
                'options' => [
                    'label_attributes' => ['style'=>'display: block; vertical-align: middle']
                ]
            ]
        );


        $this->add(array(
            'name'  => 'secure',
            'type'  => 'Csrf',
        ));
    }

        $this->get('roles')->setValueOptions($this->checkedRoles($roles, $idsRolesCheck));

    }


    public function getInputFilterSpecification()
    /**
     * @return array
     */
    public function getInputFilterSpecification(): array
    {
        return [
            'label' => [ 'required' => true ],
            'description' => [ 'required' => false ],
            'recursivity' => [ 'required' => false ],
            'finishable' => [ 'required' => false ],

            'roles' => ['required' => false],
        ];
    }

    /**
     * Compare-les id roles déjà attribués (edit) et retour la config pour le champ MultiCheckbox au niveau valuesoptions
     * @param $roles
     * @param $idsRolesCheck
     * @return array
     */
    private function checkedRoles($roles, $idsRolesCheck):array{
        $checkboxRoles = [];
        foreach ($roles as $key => $entityRole) {
            if (in_array($entityRole->getId(), $idsRolesCheck)){
                $checkboxRoles [] = ['value'=>$entityRole->getId(), 'label' => $key, 'selected' => true];
            }else{
                $checkboxRoles [] = ['value'=>$entityRole->getId(), 'label' => $key, 'selected' => false];
            }
        }
        return $checkboxRoles;
    }
}
+24 −4
Original line number Diff line number Diff line
@@ -8,9 +8,11 @@
namespace Oscar\Hydrator;


use Doctrine\ORM\EntityManager;
use Oscar\Entity\ActivityType;
use Oscar\Entity\DateType;
use Oscar\Entity\OscarFacet;
use Oscar\Entity\Role;
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Hydrator\HydratorInterface;
@@ -19,11 +21,21 @@ class DateTypeFormHydrator implements HydratorInterface, ServiceLocatorAwareInte
{
    use ServiceLocatorAwareTrait;

    /**
     * @var EntityManager
     */
    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @param DateType $object
     * @return array
     */
    public function extract($object)
    public function extract($object): array
    {
        $data = [
            'id' => $object->getId(),
@@ -32,6 +44,7 @@ class DateTypeFormHydrator implements HydratorInterface, ServiceLocatorAwareInte
            'label' => $object->getLabel(),
            'recursivity' => $object->getRecursivity(),
        ];

        if( $object->isFinishable() ){
            $data['finishable'] = 1;
        }
@@ -42,13 +55,20 @@ class DateTypeFormHydrator implements HydratorInterface, ServiceLocatorAwareInte
    /**
     * @param array $data
     * @param DateType $object
     * @return DateType
     */
    public function hydrate(array $data, $object)
    public function hydrate(array $data, $object): DateType
    {
        return $object->setDescription($data['description'])
        foreach ($data['roles'] as $idRole){
            $entityRole = $this->em->getRepository(Role::class)->findOneBy(["id"=>$idRole]);
            $object->addRole($entityRole);
        }

        $object->setDescription($data['description'])
            ->setFacet(OscarFacet::getFacets()[$data['facet']])
            ->setRecursivity($data['recursivity'])
            ->setFinishable(array_key_exists('finishable', $data) ? true : false)
            ->setLabel($data['label']);
        return $object;
    }
}
Loading