Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

146 lines
4.7KB

  1. <?php
  2. namespace Lc\PietroBundle\Controller\IndividualData;
  3. use Doctrine\ORM\QueryBuilder;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  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\Controller\AbstractAdminController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Lc\SovBundle\Generator\CsvGenerator;
  18. abstract class IndividualDataAdminController extends AbstractAdminController
  19. {
  20. protected DreamStore $dreamStore;
  21. protected RevoltStore $revoltStore;
  22. protected ProjectBoostStore $projectBoostStore;
  23. protected ProjectInspiringStore $projectInspiringStore;
  24. public function __construct(
  25. DreamStore $dreamStore,
  26. RevoltStore $revoltStore,
  27. ProjectBoostStore $projectBoostStore,
  28. ProjectInspiringStore $projectInspiringStore
  29. ) {
  30. $this->dreamStore = $dreamStore;
  31. $this->revoltStore = $revoltStore;
  32. $this->projectBoostStore = $projectBoostStore;
  33. $this->projectInspiringStore = $projectInspiringStore;
  34. }
  35. public function createEntity(string $entityFqcn)
  36. {
  37. return $this->getIndividualDataContainer()->getFactory()->create();
  38. }
  39. public function configureFields(string $pageName): iterable
  40. {
  41. return $this->getIndividualDataContainer()
  42. ->getFieldDefinition()
  43. ->getFields($pageName);
  44. }
  45. public function createIndexQueryBuilder(
  46. SearchDto $searchDto,
  47. EntityDto $entityDto,
  48. FieldCollection $fields,
  49. FilterCollection $filters
  50. ): QueryBuilder {
  51. $queryBuilder = parent::createIndexQueryBuilder(
  52. $searchDto,
  53. $entityDto,
  54. $fields,
  55. $filters
  56. );
  57. $queryBuilder->andWhere('entity.status = ' . $searchDto->getRequest()->get('status'));
  58. return $queryBuilder;
  59. }
  60. public function configureActions(Actions $actions): Actions
  61. {
  62. parent::configureActions($actions);
  63. $export = Action::new('export', 'actions.export')
  64. ->setIcon('fa fa-download')
  65. ->linkToCrudAction('exportCsv')
  66. ->setCssClass('btn btn-primary export-csv')
  67. ->createAsGlobalAction();
  68. return $actions->add(Crud::PAGE_INDEX, $export);
  69. }
  70. public function exportCsv(Request $request)
  71. {
  72. $csv = new CsvGenerator();
  73. $csv->enableConvertEncoding('ISO-8859-1');
  74. $csv->setTitle('Export_Liste', true);
  75. $columns = [
  76. 'category' => 'Catégorie',
  77. 'thematic' => 'Thématique',
  78. 'subthematic' => 'Contribution',
  79. 'territory' => 'Lieu',
  80. 'description' => 'Description'
  81. ];
  82. $csv->setColumns($columns);
  83. $resultArray = $this->generateAllResultArray();
  84. $csv = $this->generateCsvData($csv, $resultArray);
  85. return $csv->getReponse();
  86. }
  87. private function generateAllResultArray(): array
  88. {
  89. $dreamArray = $this->dreamStore->get();
  90. $revoltArray = $this->revoltStore->get();
  91. $projectBoostArray = $this->projectBoostStore->get();
  92. $projectInspiringArray = $this->projectInspiringStore->get();
  93. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  94. }
  95. private function generateCsvData($csv, $resultArray)
  96. {
  97. foreach ($resultArray as $result) {
  98. $territory = $subthematic = $thematic = "";
  99. if ($result->getIndividualData()) {
  100. $territory = $result->getIndividualData()->getTerritory()->getName();
  101. if ($result->getSubthematic()) {
  102. $subthematic = $result->getSubthematic()->getName();
  103. }
  104. if ($result->getThematic()) {
  105. $thematic = $result->getThematic()->getName();
  106. }
  107. $data = [
  108. 'category' => $result->__toString(),
  109. 'thematic' => $thematic,
  110. 'subthematic' => $subthematic,
  111. 'territory' => $territory,
  112. 'description' => $result->getDescription()
  113. ];
  114. $csv->row($data);
  115. }
  116. }
  117. return $csv;
  118. }
  119. }