Commit c8cb4764 authored by Jean-Baptiste Oellers's avatar Jean-Baptiste Oellers
Browse files

Ajout mots clés à la création d'une activité

parent 22cb8536
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -357,6 +357,12 @@ return array(
                    'roles' => ['user'],
                ],

                [
                    'controller' => 'ActivityMotsCles',
                    'action' => ['api'],
                    'roles' => ['user'],
                ],

                ////////////////////////////////////////////////////////////////
                // PERSON
                ///////////////////////////////////////////////////////////////
@@ -612,6 +618,7 @@ return array(
        'factories' => [
            'Activity' => \Oscar\Controller\ProjectGrantControllerFactory::class,
            'ActivityDate' => \Oscar\Controller\ActivityDateControllerFactory::class,
            'ActivityMotsCles' => \Oscar\Controller\ActivityMotsClesControllerFactory::class,
            'ActivityNotes' => \Oscar\Controller\ActivityNotesControllerFactory::class,
            'ActivityPayment' => \Oscar\Controller\ActivityPaymentControllerFactory::class,
            'ActivityType' => \Oscar\Controller\ActivityTypeControllerFactory::class,
+15 −0
Original line number Diff line number Diff line
@@ -2156,3 +2156,18 @@ activity-notes:
        route: /api
        defaults:
          action: api

activity-mots-cles:
  type: literal
  options:
    route: /activity-mots-cles
    defaults:
      controller: ActivityMotsCles
  may_terminate: false
  child_routes:
    api:
      type: segment
      options:
        route: /api
        defaults:
          action: api
+78 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Controller;

use Laminas\Http\Response;
use Laminas\View\Model\JsonModel;
use Oscar\Entity\ActivityMotCle;
use Oscar\Traits\UseLoggerService;
use Oscar\Traits\UseLoggerServiceTrait;
use Throwable;

class ActivityMotsClesController extends AbstractOscarController implements UseLoggerService
{
    use UseLoggerServiceTrait;

    public function apiAction()
    {
        try {

            if ($this->getRequest()->getMethod() == "GET") {
                return $this->getMotsCles();
            }

            if ($this->getRequest()->getMethod() == "POST") {
                $action_mot_cle_json = json_decode($this->getRequest()->getContent());

                if ($action_mot_cle_json->action == "create") {
                    return $this->createMotCle($action_mot_cle_json);
                }
            }

            $response = new Response();
            $response->setStatusCode(Response::STATUS_CODE_405);
            $response->setContent("Method Not Allowed.");
            return $response;

        } catch (Throwable $e) {
            $response = new Response();
            $response->setStatusCode(Response::STATUS_CODE_500);
            $response->setContent($e->getMessage());
            return $response;
        }
    }

    private function getMotsCles() {

        /** @var ActivityMotCleRepository $activityMotCleRepository */
        $activityMotCleRepository = $this->getEntityManager()->getRepository(ActivityMotCle::class);

        $motsCles = [];
        foreach ($activityMotCleRepository->getAll() as $motCle) {
            $motsCles[] = [
                'id'    => $motCle->getId(),
                'label' => $motCle->getLabel(),
            ];
        }

        $response = new JsonModel();
        $response->setVariables(['motscles' => $motsCles]);
        return $response;
    }

    private function createMotCle($action_mot_cle_json) {

        $this->getLoggerService()->info("createMotCle");
        $this->getLoggerService()->info("by: " . $this->getCurrentPerson());
        $motCle = new ActivityMotCle();
        $motCle->setLabel($action_mot_cle_json->label);
        $motCle->setCreatedBy($this->getCurrentPerson());
        $this->getEntityManager()->persist($motCle);
        $this->getEntityManager()->flush();

        $response = new JsonModel();
        $response->setVariables(['id' => $motCle->getId(), 'label' => $motCle->getLabel()]);
        return $response;
    }

}
+20 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Controller;

use Doctrine\ORM\EntityManager;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Oscar\Service\OscarUserContext;

class ActivityMotsClesControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $c = new ActivityMotsClesController();
        $c->setEntityManager($container->get(EntityManager::class));
        $c->setOscarUserContextService($container->get(OscarUserContext::class));
        $c->setLoggerService($container->get('Logger'));
        return $c;
    }
}
+76 −0
Original line number Diff line number Diff line
@@ -389,6 +389,14 @@ class Activity implements ResourceInterface
     */
    protected $disciplines;

    /**
     * Mots clés
     *
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="ActivityMotCle", cascade={"detach"}, inversedBy="activities")
     */
    protected $motscles;

    /**
     * Liste des personnes impliquées dans cette activité
     *
@@ -1469,6 +1477,7 @@ class Activity implements ResourceInterface
        $this->milestones = new ArrayCollection();
        $this->payments = new ArrayCollection();
        $this->disciplines = new ArrayCollection();
        $this->motscles = new ArrayCollection();
        $this->estimatedSpentLines = new ArrayCollection();
        $this->timesheetFormat = TimeSheet::TIMESHEET_FORMAT_NONE;
        $this->validatorsPrj = new ArrayCollection();
@@ -1654,6 +1663,73 @@ class Activity implements ResourceInterface
        return $this;
    }

    /**
     * @param ActivityMotCle $motcle
     * @return bool
     */
    public function hasMotcle(ActivityMotCle $motcle)
    {
        return $this->motscles->contains($motcle);
    }

    /**
     * @param ActivityMotCle $motcle
     * @return $this
     */
    public function addMotcle(ActivityMotCle $motcle)
    {
        if (!$this->hasMotcle($motcle)) {
            $this->motscles->add($motcle);
        }
        return $this;
    }

    /**
     * Retourne la liste des mots clés sous la forme d'un tableau de chaînes.
     *
     * @return array
     */
    public function getMotsclesArray()
    {
        $motscles = [];
        foreach ($this->getMotscles() as $m) {
            $motscles[] = (string)$m;
        }
        return $motscles;
    }

    /**
     * @return self
     */
    public function setMotscles($motsclesparam)
    {
        $this->motscles = new ArrayCollection();
        foreach ($motsclesparam as $m) {
            $this->addMotcle($m);
        }
        return $this;
    }

    /**
     * @return integer[]
     */
    public function getMotsclesIds()
    {
        $ids = [];
        foreach ($this->getMotscles() as $motcle) {
            $ids[] = $motcle->getId();
        }
        return $ids;
    }

    /**
     * @return ActivityMotCle[]
     */
    public function getMotscles()
    {
        return $this->motscles;
    }

    public function newPerson(Person $person, $role, $start = null, $to = null)
    {
        if (!$this->hasPerson($person, $role)) {
Loading