You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
12KB

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\AbstractData;
  4. use App\Form\SearchListForm;
  5. use App\Repository\BlockStore;
  6. use App\Repository\DreamStore;
  7. use App\Repository\ProjectBoostStore;
  8. use App\Repository\ProjectInspiringStore;
  9. use App\Repository\RevoltStore;
  10. use App\Repository\TerritoryStore;
  11. use App\Repository\ThematicStore;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Dompdf\Dompdf;
  15. use Dompdf\Options;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. use Lc\SovBundle\Generator\CsvGenerator;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Twig\Environment;
  22. use ReflectionClass;
  23. class CartoController extends DefaultController
  24. {
  25. protected EntityManagerInterface $em;
  26. protected DreamStore $dreamStore;
  27. protected RevoltStore $revoltStore;
  28. protected ProjectBoostStore $projectBoostStore;
  29. protected ProjectInspiringStore $projectInspiringStore;
  30. protected TerritoryStore $territoryStore;
  31. protected ThematicStore $thematicStore;
  32. protected BlockStore $blockStore;
  33. protected PaginatorInterface $paginator;
  34. protected Environment $templating;
  35. protected ParameterBagInterface $parameterBag;
  36. public function __construct(
  37. EntityManagerInterface $em,
  38. DreamStore $dreamStore,
  39. RevoltStore $revoltStore,
  40. ProjectBoostStore $projectBoostStore,
  41. ProjectInspiringStore $projectInspiringStore,
  42. TerritoryStore $territoryStore,
  43. ThematicStore $thematicStore,
  44. BlockStore $blockStore,
  45. PaginatorInterface $paginator,
  46. Environment $templating,
  47. ParameterBagInterface $parameterBag
  48. ) {
  49. $this->em = $em;
  50. $this->dreamStore = $dreamStore;
  51. $this->revoltStore = $revoltStore;
  52. $this->projectBoostStore = $projectBoostStore;
  53. $this->projectInspiringStore = $projectInspiringStore;
  54. $this->territoryStore = $territoryStore;
  55. $this->thematicStore = $thematicStore;
  56. $this->blockStore = $blockStore;
  57. $this->paginator = $paginator;
  58. $this->templating = $templating;
  59. $this->parameterBag = $parameterBag;
  60. }
  61. public function cartoInteractive()
  62. {
  63. $blockCartoInt = $this->blockStore->getOneOnlineByDevAlias('carto-int');
  64. return $this->render(
  65. 'frontend/carto-int.html.twig',
  66. [
  67. 'blockCartoInt' => $blockCartoInt,
  68. 'nbContrib' => $this->countContrib()
  69. ]
  70. );
  71. }
  72. public function cartoListe(Request $request)
  73. {
  74. $blockCartoInt = $this->blockStore->getOneOnlineByDevAlias('carto-int');
  75. $resultArrayPagination = array();
  76. $form = $this->createForm(SearchListForm::class, null, [
  77. 'method' => 'GET',
  78. ]);
  79. $form->handleRequest($request);
  80. if ($form->isSubmitted() && $form->isValid()) {
  81. $data = $form->getData();
  82. $resultArray = $this->generateResultArray($data);
  83. if ($form->get('export_excel')->isClicked()) {
  84. return $this->exportCsv($resultArray);
  85. } elseif ($form->get('export_pdf')->isClicked()) {
  86. return $this->exportPdf($resultArray);
  87. } else {
  88. $page = $data['page'];
  89. $resultArrayPagination = $this->paginator->paginate(
  90. $resultArray,
  91. $page,
  92. 10
  93. );
  94. }
  95. }
  96. return $this->render(
  97. 'frontend/carto-liste.html.twig',
  98. [
  99. 'form' => $form->createView(),
  100. 'nbContrib' => $this->countContrib(),
  101. 'resultArray' => $resultArrayPagination,
  102. 'blockCartoInt' => $blockCartoInt,
  103. ]
  104. );
  105. }
  106. public function cartoCarte()
  107. {
  108. $blockCartoInt = $this->blockStore->getOneOnlineByDevAlias('carto-int');
  109. $resultArray = $this->generateAllResultArray();
  110. $resultSortArray = array();
  111. $territoryArray = $this->territoryStore->get();
  112. foreach ($territoryArray as $territory) {
  113. $resultSortArray[$territory->getDevAlias()] =
  114. [
  115. AbstractData::TERRITORY => "",
  116. AbstractData::CATEGORY_REVOLT => "0",
  117. AbstractData::CATEGORY_DREAM => "0",
  118. AbstractData::CATEGORY_PROJECTBOOST => "0",
  119. AbstractData::CATEGORY_PROJECTINSPIRING => "0",
  120. ];
  121. }
  122. foreach ($resultArray as $result) {
  123. $className = (new ReflectionClass($result))->getShortName();
  124. if ($result->getIndividualData()) {
  125. $devAliasTerritory = $result->getIndividualData()->getTerritory()->getDevAlias();
  126. $idTerritory = $result->getIndividualData()->getTerritory()->getId();
  127. } elseif ($result->getCollectifData()->getTerritory()) {
  128. $devAliasTerritory = $result->getCollectifData()->getTerritory()->getDevAlias();
  129. $idTerritory = $result->getCollectifData()->getTerritory()->getId();
  130. }
  131. if (empty($resultSortArray[$devAliasTerritory][AbstractData::TERRITORY])) {
  132. $resultSortArray[$devAliasTerritory][AbstractData::TERRITORY] = $idTerritory;
  133. }
  134. $resultSortArray[$devAliasTerritory][$className] = $resultSortArray[$devAliasTerritory][$className] + 1;
  135. }
  136. return $this->render(
  137. 'frontend/carto-carte.html.twig',
  138. [
  139. 'resultSortArray' => $resultSortArray,
  140. 'nbContrib' => $this->countContrib(),
  141. 'blockCartoInt' => $blockCartoInt,
  142. ]
  143. );
  144. }
  145. public function cartoJson(Request $request)
  146. {
  147. $thematicArray = $this->thematicStore->get();
  148. $data = array();
  149. $key = 0;
  150. foreach ($thematicArray as $thematic) {
  151. $revoltArray = $this->revoltStore->getByThematic($thematic);
  152. $dreamArray = $this->dreamStore->getByThematic($thematic);
  153. $projectBoostArray = $this->projectBoostStore->getByThematic($thematic);
  154. $projectInspArray = $this->projectInspiringStore->getByThematic($thematic);
  155. $contribArray = array_merge($revoltArray, $dreamArray, $projectBoostArray, $projectInspArray);
  156. foreach ($contribArray as $contrib) {
  157. $keyAlreadyHere = array_search($contrib->__toString(), array_column($data, 'name'));
  158. // si "Nos révoltes" ou "Nos Reves" etc, n'existe pas encore on le créer
  159. if ($keyAlreadyHere === false) {
  160. $data[$key] = [
  161. 'name' => $contrib->__toString(),
  162. 'children' => []
  163. ];
  164. // on insere le premier thème de la contribution
  165. $data[$key]['children'][] = [
  166. 'name' => $contrib->getThematic()->getName(),
  167. 'nb' => 1,
  168. ];
  169. $key++;
  170. } else {
  171. $keyTheme = array_search(
  172. $contrib->getThematic()->getName(),
  173. array_column($data[$keyAlreadyHere]['children'], 'name')
  174. );
  175. // si le thème de la contribution n'existe pas encore on le créer
  176. if ($keyTheme === false) {
  177. $data[$keyAlreadyHere]['children'][] = [
  178. 'name' => $contrib->getThematic()->getName(),
  179. 'nb' => 1,
  180. ];
  181. } else {
  182. $data[$keyAlreadyHere]['children'][$keyTheme]['nb']++;
  183. }
  184. }
  185. }
  186. }
  187. // dump($data);
  188. // die;
  189. return new JsonResponse($data);
  190. }
  191. private function generateResultArray($data): array
  192. {
  193. $dreamArray = $revoltArray = $projectBoostArray = $projectInspiringArray = array();
  194. $subthematic = $data['search'];
  195. $categoryArray = $data['category'];
  196. $territoryArray = $data['territory'];
  197. $thematicArray = $data['thematic'];
  198. if (in_array(AbstractData::CATEGORY_DREAM, $categoryArray) || empty($categoryArray)) {
  199. $dreamArray = $this->dreamStore->filterSearch($subthematic, $territoryArray, $thematicArray);
  200. }
  201. if (in_array(AbstractData::CATEGORY_REVOLT, $categoryArray) || empty($categoryArray)) {
  202. $revoltArray = $this->revoltStore->filterSearch($subthematic, $territoryArray, $thematicArray);
  203. }
  204. if (in_array(AbstractData::CATEGORY_PROJECTBOOST, $categoryArray) || empty($categoryArray)) {
  205. $projectBoostArray = $this->projectBoostStore->filterSearch(
  206. $subthematic,
  207. $territoryArray,
  208. $thematicArray
  209. );
  210. }
  211. if (in_array(AbstractData::CATEGORY_PROJECTINSPIRING, $categoryArray) || empty($categoryArray)) {
  212. $projectInspiringArray = $this->projectInspiringStore->filterSearch(
  213. $subthematic,
  214. $territoryArray,
  215. $thematicArray
  216. );
  217. }
  218. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  219. }
  220. public function exportPdf($resultArray = array())
  221. {
  222. // Configure Dompdf according to your needs
  223. $pdfOptions = new Options();
  224. $pdfOptions->set('defaultFont', 'Arial');
  225. // Instantiate Dompdf with our options
  226. $dompdf = new Dompdf($pdfOptions);
  227. if (empty($resultArray)) {
  228. $resultArray = $this->generateAllResultArray();
  229. }
  230. // Retrieve the HTML generated in our twig
  231. $html = $this->templating->render('frontend/pdf.html.twig', [
  232. 'resultArray' => $resultArray,
  233. 'css' => file_get_contents(
  234. $this->parameterBag->get('app.assets_directory') . 'css/pdf.css'
  235. )
  236. ]);
  237. // Load HTML to Dompdf
  238. $dompdf->loadHtml($html);
  239. // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  240. $dompdf->setPaper('A4', 'portrait');
  241. // Render the HTML as PDF
  242. $dompdf->render();
  243. $dompdf->stream('Export-pdf.pdf', [
  244. "Attachment" => true
  245. ]);
  246. }
  247. public function exportCsv($resultArray = array())
  248. {
  249. $csv = new CsvGenerator();
  250. $csv->enableConvertEncoding('ISO-8859-1');
  251. $csv->setTitle('Export_Liste', true);
  252. $columns = [
  253. 'category' => 'Catégorie',
  254. 'thematic' => 'Thématique',
  255. 'subthematic' => 'Contribution',
  256. 'territory' => 'Lieu'
  257. ];
  258. $csv->setColumns($columns);
  259. if (empty($resultArray)) {
  260. $resultArray = $this->generateAllResultArray();
  261. }
  262. $csv = $this->generateCsvData($csv, $resultArray);
  263. return $csv->getReponse();
  264. }
  265. private function generateAllResultArray(): array
  266. {
  267. $dreamArray = $this->dreamStore->get();
  268. $revoltArray = $this->revoltStore->get();
  269. $projectBoostArray = $this->projectBoostStore->get();
  270. $projectInspiringArray = $this->projectInspiringStore->get();
  271. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  272. }
  273. private function generateCsvData($csv, $resultArray)
  274. {
  275. foreach ($resultArray as $result) {
  276. $territory = $subthematic = $thematic = "";
  277. if ($result->getIndividualData()) {
  278. $territory = $result->getIndividualData()->getTerritory()->getName();
  279. } elseif ($result->getCollectifData()->getTerritory()) {
  280. $territory = $result->getCollectifData()->getTerritory()->getName();
  281. }
  282. if ($result->getSubthematic()) {
  283. $subthematic = $result->getSubthematic()->getName();
  284. }
  285. if ($result->getThematic()) {
  286. $thematic = $result->getThematic()->getName();
  287. }
  288. $data = [
  289. 'category' => $result->__toString(),
  290. 'thematic' => $thematic,
  291. 'subthematic' => $subthematic,
  292. 'territory' => $territory
  293. ];
  294. $csv->row($data);
  295. }
  296. return $csv;
  297. }
  298. }