src/Controller/FicheActionController.php line 61

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