Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

354 lines
13KB

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Form\SearchListForm;
  4. use App\Repository\BlockStore;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Dompdf\Dompdf;
  8. use Dompdf\Options;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Lc\PietroBundle\Model\AbstractData;
  11. use Lc\PietroBundle\Repository\Dream\DreamStore;
  12. use Lc\PietroBundle\Repository\ProjectBoost\ProjectBoostStore;
  13. use Lc\PietroBundle\Repository\ProjectInspiring\ProjectInspiringStore;
  14. use Lc\PietroBundle\Repository\Revolt\RevoltStore;
  15. use Lc\PietroBundle\Repository\Territory\TerritoryStore;
  16. use Lc\PietroBundle\Repository\Thematic\ThematicStore;
  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. } else {
  96. $resultArray = $this->generateResultArray();
  97. $resultArrayPagination = $this->paginator->paginate(
  98. $resultArray,
  99. 1,
  100. 10
  101. );
  102. }
  103. return $this->render(
  104. 'frontend/carto-liste.html.twig',
  105. [
  106. 'form' => $form->createView(),
  107. 'nbContrib' => $this->countContrib(),
  108. 'resultArray' => $resultArrayPagination,
  109. 'blockCartoInt' => $blockCartoInt,
  110. ]
  111. );
  112. }
  113. public function cartoCarte()
  114. {
  115. $blockCartoInt = $this->blockStore->getOneOnlineByDevAlias('carto-int');
  116. $resultArray = $this->generateAllResultArray();
  117. $resultSortArray = array();
  118. $territoryArray = $this->territoryStore->get();
  119. foreach ($territoryArray as $territory) {
  120. $resultSortArray[$territory->getDevAlias()] =
  121. [
  122. AbstractData::TERRITORY => $territory->getId(),
  123. AbstractData::CATEGORY_REVOLT => "0",
  124. AbstractData::CATEGORY_DREAM => "0",
  125. AbstractData::CATEGORY_PROJECTBOOST => "0",
  126. AbstractData::CATEGORY_PROJECTINSPIRING => "0",
  127. ];
  128. }
  129. foreach ($resultArray as $result) {
  130. $className = (new ReflectionClass($result))->getShortName();
  131. if ($result->getIndividualData()) {
  132. $devAliasTerritory = $result->getIndividualData()->getTerritory()->getDevAlias();
  133. } elseif ($result->getCollectifData()->getTerritory()) {
  134. $devAliasTerritory = $result->getCollectifData()->getTerritory()->getDevAlias();
  135. }
  136. $resultSortArray[$devAliasTerritory][$className] = $resultSortArray[$devAliasTerritory][$className] + 1;
  137. }
  138. return $this->render(
  139. 'frontend/carto-carte.html.twig',
  140. [
  141. 'resultSortArray' => $resultSortArray,
  142. 'nbContrib' => $this->countContrib(),
  143. 'blockCartoInt' => $blockCartoInt,
  144. ]
  145. );
  146. }
  147. public function cartoJson(Request $request)
  148. {
  149. $thematicArray = $this->thematicStore->get();
  150. $data = array();
  151. $key = 0;
  152. foreach ($thematicArray as $thematic) {
  153. $revoltArray = $this->revoltStore->getByThematic($thematic);
  154. $dreamArray = $this->dreamStore->getByThematic($thematic);
  155. $projectBoostArray = $this->projectBoostStore->getByThematic($thematic);
  156. $projectInspArray = $this->projectInspiringStore->getByThematic($thematic);
  157. $contribArray = array_merge($revoltArray, $dreamArray, $projectBoostArray, $projectInspArray);
  158. foreach ($contribArray as $contrib) {
  159. $keyAlreadyHere = array_search($contrib->__toString(), array_column($data, 'name'));
  160. // si "Nos révoltes" ou "Nos Reves" etc, n'existe pas encore on le créer
  161. if ($keyAlreadyHere === false) {
  162. $data[$key] = [
  163. 'name' => $contrib->__toString(),
  164. 'children' => []
  165. ];
  166. // on insere le premier thème de la contribution
  167. $data[$key]['children'][] = [
  168. 'name' => $thematic->getName(),
  169. 'id_thematic' => $thematic->getId(),
  170. 'id_category' => AbstractData::getCategoryByLabel($contrib->__toString()),
  171. 'nb' => 1,
  172. ];
  173. $key++;
  174. } else {
  175. $keyTheme = array_search(
  176. $thematic->getName(),
  177. array_column($data[$keyAlreadyHere]['children'], 'name')
  178. );
  179. // si le thème de la contribution n'existe pas encore on le créer
  180. if ($keyTheme === false) {
  181. $data[$keyAlreadyHere]['children'][] = [
  182. 'name' => $thematic->getName(),
  183. 'id_thematic' => $thematic->getId(),
  184. 'id_category' => AbstractData::getCategoryByLabel($contrib->__toString()),
  185. 'nb' => 1,
  186. ];
  187. } else {
  188. $data[$keyAlreadyHere]['children'][$keyTheme]['nb']++;
  189. }
  190. }
  191. }
  192. }
  193. return new JsonResponse($data);
  194. }
  195. private function generateResultArray(
  196. $data = array(
  197. 'search' => '',
  198. 'category' => array()
  199. )
  200. ): array {
  201. $dreamArray = $revoltArray = $projectBoostArray = $projectInspiringArray = array();
  202. $subthematic = $data['search'];
  203. $categoryArray = $data['category'];
  204. $territoryArray = isset($data['territory']) ? $data['territory'] : new ArrayCollection();
  205. $thematicArray = isset($data['thematic']) ? $data['thematic'] : new ArrayCollection();
  206. if (in_array(AbstractData::CATEGORY_DREAM, $categoryArray) || empty($categoryArray)) {
  207. $dreamArray = $this->dreamStore->filterSearch($subthematic, $territoryArray, $thematicArray);
  208. }
  209. if (in_array(AbstractData::CATEGORY_REVOLT, $categoryArray) || empty($categoryArray)) {
  210. $revoltArray = $this->revoltStore->filterSearch($subthematic, $territoryArray, $thematicArray);
  211. }
  212. if (in_array(AbstractData::CATEGORY_PROJECTBOOST, $categoryArray) || empty($categoryArray)) {
  213. $projectBoostArray = $this->projectBoostStore->filterSearch(
  214. $subthematic,
  215. $territoryArray,
  216. $thematicArray
  217. );
  218. }
  219. if (in_array(AbstractData::CATEGORY_PROJECTINSPIRING, $categoryArray) || empty($categoryArray)) {
  220. $projectInspiringArray = $this->projectInspiringStore->filterSearch(
  221. $subthematic,
  222. $territoryArray,
  223. $thematicArray
  224. );
  225. }
  226. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  227. }
  228. public function exportPdf($resultArray = array())
  229. {
  230. // Configure Dompdf according to your needs
  231. $pdfOptions = new Options();
  232. $pdfOptions->set('defaultFont', 'Arial');
  233. // Instantiate Dompdf with our options
  234. $dompdf = new Dompdf($pdfOptions);
  235. if (empty($resultArray)) {
  236. $resultArray = $this->generateAllResultArray();
  237. }
  238. // Retrieve the HTML generated in our twig
  239. $html = $this->templating->render('frontend/pdf.html.twig', [
  240. 'resultArray' => $resultArray,
  241. 'css' => file_get_contents(
  242. $this->parameterBag->get('app.assets_directory') . 'css/pdf.css'
  243. )
  244. ]);
  245. // Load HTML to Dompdf
  246. $dompdf->loadHtml($html);
  247. // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  248. $dompdf->setPaper('A4', 'portrait');
  249. // Render the HTML as PDF
  250. $dompdf->render();
  251. $dompdf->stream('Export-pdf.pdf', [
  252. "Attachment" => true
  253. ]);
  254. }
  255. public function exportCsv($resultArray = array())
  256. {
  257. $csv = new CsvGenerator();
  258. $csv->enableConvertEncoding('ISO-8859-1');
  259. $csv->setTitle('Export_Liste', true);
  260. $columns = [
  261. 'category' => 'Catégorie',
  262. 'thematic' => 'Thématique',
  263. 'subthematic' => 'Contribution',
  264. 'territory' => 'Lieu'
  265. ];
  266. $csv->setColumns($columns);
  267. if (empty($resultArray)) {
  268. $resultArray = $this->generateAllResultArray();
  269. }
  270. $csv = $this->generateCsvData($csv, $resultArray);
  271. return $csv->getReponse();
  272. }
  273. private function generateAllResultArray(): array
  274. {
  275. $dreamArray = $this->dreamStore->get();
  276. $revoltArray = $this->revoltStore->get();
  277. $projectBoostArray = $this->projectBoostStore->get();
  278. $projectInspiringArray = $this->projectInspiringStore->get();
  279. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  280. }
  281. private function generateCsvData($csv, $resultArray)
  282. {
  283. foreach ($resultArray as $result) {
  284. $territory = $subthematic = $thematic = "";
  285. if ($result->getIndividualData()) {
  286. $territory = $result->getIndividualData()->getTerritory()->getName();
  287. } elseif ($result->getCollectifData()->getTerritory()) {
  288. $territory = $result->getCollectifData()->getTerritory()->getName();
  289. }
  290. if ($result->getSubthematic()) {
  291. $subthematic = $result->getSubthematic()->getName();
  292. }
  293. if ($result->getThematic()) {
  294. $thematic = $result->getThematic()->getName();
  295. }
  296. $data = [
  297. 'category' => $result->__toString(),
  298. 'thematic' => $thematic,
  299. 'subthematic' => $subthematic,
  300. 'territory' => $territory
  301. ];
  302. $csv->row($data);
  303. }
  304. return $csv;
  305. }
  306. }