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.

355 lines
13KB

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