src/Domain/Registry/Controller/ViolationController.php line 270

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the MADIS - RGPD Management application.
  4.  *
  5.  * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
  6.  * @author Donovan Bourlard <donovan@awkan.fr>
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify
  9.  * it under the terms of the GNU Affero General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU Affero General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Affero General Public License
  19.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20.  */
  21. declare(strict_types=1);
  22. namespace App\Domain\Registry\Controller;
  23. use App\Application\Controller\CRUDController;
  24. use App\Application\Symfony\Security\UserProvider;
  25. use App\Application\Traits\ServersideDatatablesTrait;
  26. use App\Domain\Documentation\Model\Category;
  27. use App\Domain\Registry\Dictionary\ViolationCauseDictionary;
  28. use App\Domain\Registry\Dictionary\ViolationGravityDictionary;
  29. use App\Domain\Registry\Dictionary\ViolationNatureDictionary;
  30. use App\Domain\Registry\Dictionary\ViolationNotificationListDictionary;
  31. use App\Domain\Registry\Form\Type\ViolationType;
  32. use App\Domain\Registry\Model;
  33. use App\Domain\Registry\Repository;
  34. use App\Domain\Reporting\Handler\WordHandler;
  35. use App\Domain\User\Dictionary\UserRoleDictionary;
  36. use Doctrine\Common\Collections\ArrayCollection;
  37. use Doctrine\ORM\EntityManagerInterface;
  38. use Knp\Snappy\Pdf;
  39. use Symfony\Component\HttpFoundation\JsonResponse;
  40. use Symfony\Component\HttpFoundation\Request;
  41. use Symfony\Component\HttpFoundation\RequestStack;
  42. use Symfony\Component\HttpFoundation\Response;
  43. use Symfony\Component\Routing\RouterInterface;
  44. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  45. use Symfony\Contracts\Translation\TranslatorInterface;
  46. /**
  47.  * @property Repository\Violation $repository
  48.  */
  49. class ViolationController extends CRUDController
  50. {
  51.     use ServersideDatatablesTrait;
  52.     /**
  53.      * @var RequestStack
  54.      */
  55.     protected $requestStack;
  56.     /**
  57.      * @var WordHandler
  58.      */
  59.     protected $wordHandler;
  60.     /**
  61.      * @var AuthorizationCheckerInterface
  62.      */
  63.     protected $authorizationChecker;
  64.     /**
  65.      * @var UserProvider
  66.      */
  67.     protected $userProvider;
  68.     /**
  69.      * @var RouterInterface
  70.      */
  71.     protected $router;
  72.     public function __construct(
  73.         EntityManagerInterface $entityManager,
  74.         TranslatorInterface $translator,
  75.         Repository\Violation $repository,
  76.         RequestStack $requestStack,
  77.         WordHandler $wordHandler,
  78.         AuthorizationCheckerInterface $authorizationChecker,
  79.         UserProvider $userProvider,
  80.         Pdf $pdf,
  81.         RouterInterface $router
  82.     ) {
  83.         parent::__construct($entityManager$translator$repository$pdf$userProvider$authorizationChecker);
  84.         $this->requestStack         $requestStack;
  85.         $this->wordHandler          $wordHandler;
  86.         $this->authorizationChecker $authorizationChecker;
  87.         $this->userProvider         $userProvider;
  88.         $this->router               $router;
  89.     }
  90.     protected function getDomain(): string
  91.     {
  92.         return 'registry';
  93.     }
  94.     protected function getModel(): string
  95.     {
  96.         return 'violation';
  97.     }
  98.     protected function getModelClass(): string
  99.     {
  100.         return Model\Violation::class;
  101.     }
  102.     protected function getFormType(): string
  103.     {
  104.         return ViolationType::class;
  105.     }
  106.     protected function isSoftDelete(): bool
  107.     {
  108.         return true;
  109.     }
  110.     /**
  111.      * Generate a word report of contractors.
  112.      *
  113.      * @throws \PhpOffice\PhpWord\Exception\Exception
  114.      */
  115.     public function reportAction(): Response
  116.     {
  117.         $objects $this->repository->findAllByCollectivity(
  118.             $this->userProvider->getAuthenticatedUser()->getCollectivity(),
  119.             false,
  120.             ['date' => 'asc']
  121.         );
  122.         return $this->wordHandler->generateRegistryViolationReport($objects);
  123.     }
  124.     public function listAction(): Response
  125.     {
  126.         $criteria $this->getRequestCriteria();
  127.         $category $this->entityManager->getRepository(Category::class)->findOneBy([
  128.             'name' => 'Violation',
  129.         ]);
  130.         return $this->render($this->getTemplatingBasePath('list'), [
  131.             'totalItem' => $this->repository->count($criteria),
  132.             'category'  => $category,
  133.             'route'     => $this->router->generate('registry_violation_list_datatables', ['archive' => $criteria['archive']]),
  134.         ]);
  135.     }
  136.     public function listDataTables(Request $request): JsonResponse
  137.     {
  138.         $criteria $this->getRequestCriteria();
  139.         $users    $this->getResults($request$criteria);
  140.         $reponse  $this->getBaseDataTablesResponse($request$users$criteria);
  141.         $yes '<span class="badge bg-red">' $this->translator->trans('label.yes') . '</span>';
  142.         $no  '<span class="badge bg-green">' $this->translator->trans('label.no') . '</span>';
  143.         /** @var Model\Violation $violation */
  144.         foreach ($users as $violation) {
  145.             $violationLink '<a aria-label="' \date_format($violation->getDate(), 'd/m/Y') . '" href="' $this->router->generate('registry_violation_show', ['id' => $violation->getId()->toString()]) . '">
  146.                 ' \date_format($violation->getDate(), 'd/m/Y') . '
  147.             </a>';
  148.             $allNatures   ViolationNatureDictionary::getNatures();
  149.             $natures      '';
  150.             $naturesArray = new ArrayCollection($violation->getViolationNatures());
  151.             if (count($naturesArray) > 0) {
  152.                 $natures $naturesArray->map(function ($name) use ($allNatures) {
  153.                     return $allNatures[$name] ?? null;
  154.                 })->filter(function ($r) {return null !== $r; });
  155.                 $natures join(', '$natures->toArray());
  156.             }
  157.             $reponse['data'][] = [
  158.                 'id'           => $violation->getId(),
  159.                 'collectivite' => $violation->getCollectivity()->getName(),
  160.                 'date'         => $violationLink,
  161.                 'nature'       => $natures,
  162.                 'cause'        => !\is_null($violation->getCause()) ? ViolationCauseDictionary::getNatures()[$violation->getCause()] : null,
  163.                 'gravity'      => !\is_null($violation->getGravity()) ? ViolationGravityDictionary::getGravities()[$violation->getGravity()] : null,
  164.                 'createdAt'    => date_format($violation->getCreatedAt(), 'd-m-Y H:i'),
  165.                 'updatedAt'    => date_format($violation->getUpdatedAt(), 'd-m-Y H:i'),
  166.                 'inProgress'   => $violation->isInProgress() ? $yes $no,
  167.                 'actions'      => $this->getActionCellsContent($violation),
  168.                 'notification' => !\is_null($violation->getNotification()) && isset(ViolationNotificationListDictionary::getNotificationsList()[$violation->getNotification()]) ? ViolationNotificationListDictionary::getNotificationsList()[$violation->getNotification()] : null,
  169.             ];
  170.         }
  171.         $jsonResponse = new JsonResponse();
  172.         $jsonResponse->setJson(\json_encode($reponse));
  173.         return $jsonResponse;
  174.     }
  175.     private function isRequestInUserServices(Model\Violation $violation): bool
  176.     {
  177.         $user $this->userProvider->getAuthenticatedUser();
  178.         if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  179.             return true;
  180.         }
  181.         return $violation->isInUserServices($user);
  182.     }
  183.     protected function getLabelAndKeysArray(): array
  184.     {
  185.         if ($this->authorizationChecker->isGranted('ROLE_REFERENT')) {
  186.             return [
  187.                 => 'date',
  188.                 => 'collectivite',
  189.                 => 'nature',
  190.                 => 'inProgress',
  191.                 => 'cause',
  192.                 => 'gravity',
  193.                 => 'notification',
  194.                 => 'createdAt',
  195.                 => 'updatedAt',
  196.                 => 'actions',
  197.             ];
  198.         }
  199.         return [
  200.             => 'date',
  201.             => 'nature',
  202.             => 'inProgress',
  203.             => 'cause',
  204.             => 'gravity',
  205.             => 'notification',
  206.             => 'createdAt',
  207.             => 'updatedAt',
  208.             => 'actions',
  209.         ];
  210.     }
  211.     private function getActionCellsContent(Model\Violation $violation)
  212.     {
  213.         $cellContent '';
  214.         $user        $this->userProvider->getAuthenticatedUser();
  215.         if ($this->authorizationChecker->isGranted('ROLE_USER')
  216.         && \is_null($violation->getDeletedAt())
  217.         && ($user->getServices()->isEmpty() || $this->isRequestInUserServices($violation))) {
  218.             $cellContent .= '<a aria-label="' $this->translator->trans('action.edit') . '" href="' $this->router->generate('registry_violation_edit', ['id' => $violation->getId()]) . '">
  219.                     <i class="fa fa-pencil-alt"></i> ' .
  220.                     $this->translator->trans('action.edit') . '
  221.                 </a>
  222.                 <a aria-label="' $this->translator->trans('action.archive') . '" href="' $this->router->generate('registry_violation_delete', ['id' => $violation->getId()]) . '">
  223.                     <i class="fa fa-archive"></i> ' .
  224.                     $this->translator->trans('action.archive') . '
  225.                 </a>';
  226.         }
  227.         return $cellContent;
  228.     }
  229.     private function getRequestCriteria()
  230.     {
  231.         $criteria            = [];
  232.         $request             $this->requestStack->getMasterRequest();
  233.         $criteria['archive'] = $request->query->getBoolean('archive');
  234.         $user                $this->userProvider->getAuthenticatedUser();
  235.         if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
  236.             $criteria['collectivity'] = $user->getCollectivity();
  237.         }
  238.         if (\in_array(UserRoleDictionary::ROLE_REFERENT$user->getRoles())) {
  239.             $criteria['collectivity'] = $user->getCollectivitesReferees();
  240.         }
  241.         return $criteria;
  242.     }
  243. }