src/Controller/FicheActionController.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Entity\Document;
  5. use App\Entity\FicheAction;
  6. use App\Entity\Keyword;
  7. use App\Entity\PartnerAction;
  8. use App\Form\FicheActionFilterType;
  9. use Cocur\Slugify\Slugify;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Knp\Snappy\Pdf;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  20. /**
  21. * FicheAction controller.
  22. */
  23. #[Route(path: '/fiche-action')]
  24. class FicheActionController extends AbstractController
  25. {
  26. private PaginatorInterface $paginator;
  27. private ManagerRegistry $em;
  28. private Pdf $pdf;
  29. private AuthorizationCheckerInterface $authorizationChecker;
  30. public function __construct(PaginatorInterface $paginator, AuthorizationCheckerInterface $authorizationChecker, ManagerRegistry $em, Pdf $pdf)
  31. {
  32. $this->paginator = $paginator;
  33. $this->authorizationChecker = $authorizationChecker;
  34. $this->em = $em;
  35. $this->pdf = $pdf;
  36. }
  37. /**
  38. * Lists all FicheAction entities.
  39. */
  40. #[Route(path: '/', name: 'fiche-action', methods: ['GET'])]
  41. public function index(Request $request): Response
  42. {
  43. $filter = $this->filterForm($request);
  44. $filterData = $filter['filterData'];
  45. $filterForm = $filter['filterForm'];
  46. $queryRequest = $request->query;
  47. // gestion ordre affichage
  48. $sort = $queryRequest->get('sort', 'fa.created');
  49. $direction = $queryRequest->get('direction', 'DESC');
  50. // prise en compte du champ de recherche additionnel TermSearch
  51. $filtreActionFilter = $request->query->get('ficheactionfilter');
  52. if (is_array($filtreActionFilter) && array_key_exists('TermSearch', $filtreActionFilter)) {
  53. $filterData['TermSearch'] = $filtreActionFilter['TermSearch'];
  54. }
  55. if ($request->get('formPartneCloud')) {
  56. $filterData['formPartnerCloud'] = 1;
  57. }
  58. $query = $this->em->getRepository(FicheAction::class)->search($filterData, $sort, $direction);
  59. $paginator = $this->paginator;
  60. $pagination = $paginator->paginate(
  61. $query,
  62. $request->query->getInt('page', 1), /* page number */
  63. 25/* limit per page */
  64. );
  65. $pagination->setTemplate('Pagination/pagination.html.twig');
  66. return $this->renderForm('FicheAction/index.html.twig', [
  67. 'pagination' => $pagination,
  68. 'filterForm' => $filterForm,
  69. 'direction' => $direction,
  70. ]);
  71. }
  72. /**
  73. * update nbviews fields when fiche action is open.
  74. */
  75. #[Route(path: '/updateNbViews', name: 'updateNbViews', methods: ['POST'])]
  76. public function updateNbViews(Request $request): Response
  77. {
  78. $ficheAction = $this->em->getRepository(FicheAction::class)->find($request->request->get('id'));
  79. if (!$ficheAction) {
  80. throw $this->createNotFoundException('Unable to find FicheAction entity.');
  81. }
  82. $ficheAction->setNbViews($ficheAction->getNbViews() + 1);
  83. $this->em->getManager()->persist($ficheAction);
  84. $this->em->getManager()->flush();
  85. $return = 'nbviews of fiche action '.$ficheAction->getId().' was updated with success';
  86. return new Response($return, Response::HTTP_OK);
  87. }
  88. private function filterForm(Request $request): array
  89. {
  90. $session = $request->getSession();
  91. $filterData = [];
  92. $filterForm = $this->createForm(FicheActionFilterType::class);
  93. if (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  94. $filterForm->remove('applicants');
  95. }
  96. // Reset filter
  97. if ('reset' == $request->get('filter_action')) {
  98. $session->remove('ficheactionfilter');
  99. }
  100. // Filter action
  101. if ('filter' == $request->get('filter_action')) {
  102. if ($request->get('ficheactionfilter')) {
  103. $ficheActionFilter = $request->get('ficheactionfilter');
  104. if (array_key_exists('department', $ficheActionFilter)) {
  105. $filterData['department'] = $ficheActionFilter['department'];
  106. }
  107. if (array_key_exists('financialPartners', $ficheActionFilter)) {
  108. $filterData['financialPartners'] = $ficheActionFilter['financialPartners'];
  109. }
  110. if (array_key_exists('actionPartners', $ficheActionFilter)) {
  111. $filterData['actionPartners'] = $ficheActionFilter['actionPartners'];
  112. }
  113. if ($request->get('formPartnerCloud')) {
  114. $filterData['formPartnerCloud'] = $request->get('formPartnerCloud');
  115. }
  116. }
  117. // Bind values from the request
  118. $filterForm->handleRequest($request);
  119. if ($filterForm->isSubmitted() && $filterForm->isValid()) {
  120. if ($filterForm->has('department') && ($dep = $filterForm->get('department')->getData())) {
  121. $filterData['department'] = $dep->getId();
  122. }
  123. if ($filterForm->has('region') && ($dep = $filterForm->get('region')->getData())) {
  124. $filterData['region'] = $dep->getId();
  125. }
  126. if ($filterForm->has('public') && ($dep = $filterForm->get('public')->getData())) {
  127. $filterData['public'] = $dep->getId();
  128. }
  129. if ($filterForm->has('dimension') && ($dep = $filterForm->get('dimension')->getData())) {
  130. $filterData['dimension'] = $dep->getId();
  131. }
  132. if ($filterForm->has('actionPartners') && ($dep = $filterForm->get('actionPartners')->getData())) {
  133. $ids = $dep->map(fn ($entity) => $entity->getId());
  134. if ((is_countable($ids) ? count($ids) : 0) > 0) {
  135. $filterData['actionPartners'] = $ids->toArray();
  136. }
  137. }
  138. if ($filterForm->has('financialPartners') && ($dep = $filterForm->get('financialPartners')->getData())) {
  139. $ids = $dep->map(fn ($entity) => $entity->getId());
  140. if ((is_countable($ids) ? count($ids) : 0) > 0) {
  141. $filterData['financialPartners'] = $ids->toArray();
  142. }
  143. }
  144. if ($filterForm->has('applicants') && ($dep = $filterForm->get('applicants')->getData())) {
  145. $ids = $dep->map(fn ($entity) => $entity->getId());
  146. if ((is_countable($ids) ? count($ids) : 0) > 0) {
  147. $filterData['applicants'] = $ids->toArray();
  148. }
  149. }
  150. if ($filterForm->has('keywords') && ($dep = $filterForm->get('keywords')->getData())) {
  151. $ids = $dep->map(fn ($entity) => $entity->getId());
  152. if ((is_countable($ids) ? count($ids) : 0) > 0) {
  153. $filterData['keywords'] = $ids->toArray();
  154. }
  155. }
  156. }
  157. }
  158. // dd($filterData);
  159. return [
  160. 'filterForm' => $filterForm,
  161. 'filterData' => $filterData,
  162. ];
  163. }
  164. /**
  165. * Recherche geographique et par mots cles.
  166. */
  167. #[Route(path: '/recherche-geographique-mot-cle', name: 'fiche-action_geographical_and_tag_search')]
  168. public function geographicalAndTagSearch(): Response
  169. {
  170. $connection = $this->em->getConnection();
  171. // tags
  172. $keywords = $this->em->getRepository(Keyword::class)->findAllUsedKeyword();
  173. // partnerActions
  174. $partnerActions = $this->em->getRepository(PartnerAction::class)->findAllUsedPartnerAction();
  175. $baseFontSize = '0.8';
  176. $maxFontSize = '2.5';
  177. // map
  178. $BaseQuery = 'SELECT * FROM FicheAction a LEFT JOIN Structure s ON a.structure_id = s.id LEFT JOIN Departement d ON s.departement_id=d.id ';
  179. $statement1 = $connection->prepare('SELECT * FROM imap_departements');
  180. $GetImapDepartements = $statement1->execute()->fetchAll();
  181. $map_array = [];
  182. foreach ($GetImapDepartements as $result) {
  183. $statement2 = $connection->prepare($BaseQuery." WHERE d.num='".$result['id']."'");
  184. $Get_All = $statement2->execute()->fetchAll();
  185. $total_resultats = is_countable($Get_All) ? count($Get_All) : 0;
  186. $map_array[$result['id']]['id'] = $result['id'];
  187. $map_array[$result['id']]['departement'] = $result['departement'];
  188. $map_array[$result['id']]['total_resultats'] = $total_resultats;
  189. $map_array[$result['id']]['coords'] = $result['coords'];
  190. }
  191. return $this->render('FicheAction/geographical_and_tag_search.html.twig', [
  192. 'results_tags' => $keywords,
  193. 'results_partnerActions' => $partnerActions,
  194. 'baseFontSize' => $baseFontSize,
  195. 'maxFontSize' => $maxFontSize,
  196. 'map_array' => $map_array,
  197. ]);
  198. }
  199. /**
  200. * Finds and displays a FicheAction entity.
  201. */
  202. #[Route(path: '/{id}', name: 'fiche-action_show', methods: ['GET'])]
  203. public function show($id): Response
  204. {
  205. $entity = $this->em->getRepository(FicheAction::class)->find($id);
  206. if (!$entity) {
  207. throw $this->createNotFoundException('Unable to find FicheAction entity.');
  208. }
  209. $entity->setNbViews($entity->getNbViews() + 1);
  210. $this->em->getManager()->persist($entity);
  211. $this->em->getManager()->flush();
  212. return $this->render('FicheAction/show.html.twig', [
  213. 'entity' => $entity,
  214. ]);
  215. }
  216. #[Route(path: '/document/{id}', name: 'get_document', methods: ['GET'])]
  217. public function getDocument($id): Response
  218. {
  219. $document = $this->em->getRepository(Document::class)->find($id);
  220. if (!$document) {
  221. throw $this->createNotFoundException('Unable to find Document.');
  222. }
  223. $fileName = $document->getWebPath();
  224. $response = new BinaryFileResponse($fileName);
  225. // Configuration des headers
  226. $response->headers->set('Cache-Control', 'private');
  227. // Pour l'affichage "inline" (dans le navigateur)
  228. $response->setContentDisposition(
  229. ResponseHeaderBag::DISPOSITION_INLINE,
  230. basename($fileName)
  231. );
  232. return $response;
  233. }
  234. /**
  235. * Afficher la fiche action pour impression.
  236. */
  237. #[Route(path: '/{id}/print', name: 'ficheAction_print', methods: ['GET'])]
  238. public function print($id): Response
  239. {
  240. // security
  241. if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  242. throw $this->createAccessDeniedException();
  243. }
  244. $entity = $this->em->getRepository(FicheAction::class)->find($id);
  245. return $this->render('FicheAction/print.html.twig', [
  246. 'entity' => $entity,
  247. ]);
  248. }
  249. /**
  250. * Afficher la fiche Action en PDF.
  251. */
  252. #[Route(path: '/{id}/printPdf', name: 'ficheAction_printPdf', methods: ['GET'])]
  253. public function printPdf($id): Response
  254. {
  255. // security
  256. if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  257. throw $this->createAccessDeniedException();
  258. }
  259. $entity = $this->em->getRepository(FicheAction::class)->find($id);
  260. $slugify = new Slugify();
  261. $ficheAction_slug = $slugify->slugify($entity);
  262. $path = $this->getParameter('kernel.project_dir').'/public/';
  263. $html = $this->renderView('FicheAction/print.html.twig', ['entity' => $entity, 'path' => $path]);
  264. $this->pdf->setTimeout(180);
  265. $this->pdf->setOption('enable-local-file-access', true);
  266. return new Response(
  267. $this->pdf->getOutputFromHtml(
  268. $html,
  269. [
  270. 'orientation' => 'Portrait',
  271. 'default-header' => false,
  272. 'lowquality' => true,
  273. ]
  274. ),
  275. Response::HTTP_OK,
  276. [
  277. 'Content-Type' => 'application/pdf',
  278. 'Content-Disposition' => 'attachment; filename="fiche_action-'.$ficheAction_slug.'.pdf"',
  279. ]
  280. );
  281. }
  282. }