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.

148 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. if ($searchDto->getRequest()->get('status')) {
  58. $queryBuilder->andWhere('entity.status = ' . $searchDto->getRequest()->get('status'));
  59. }
  60. return $queryBuilder;
  61. }
  62. public function configureActions(Actions $actions): Actions
  63. {
  64. parent::configureActions($actions);
  65. $export = Action::new('export', 'actions.export')
  66. ->setIcon('fa fa-download')
  67. ->linkToCrudAction('exportCsv')
  68. ->setCssClass('btn btn-primary export-csv')
  69. ->createAsGlobalAction();
  70. return $actions->add(Crud::PAGE_INDEX, $export);
  71. }
  72. public function exportCsv(Request $request)
  73. {
  74. $csv = new CsvGenerator();
  75. $csv->enableConvertEncoding('ISO-8859-1');
  76. $csv->setTitle('Export_Liste', true);
  77. $columns = [
  78. 'category' => 'Catégorie',
  79. 'thematic' => 'Thématique',
  80. 'subthematic' => 'Contribution',
  81. 'territory' => 'Lieu',
  82. 'description' => 'Description'
  83. ];
  84. $csv->setColumns($columns);
  85. $resultArray = $this->generateAllResultArray();
  86. $csv = $this->generateCsvData($csv, $resultArray);
  87. return $csv->getReponse();
  88. }
  89. private function generateAllResultArray(): array
  90. {
  91. $dreamArray = $this->dreamStore->get();
  92. $revoltArray = $this->revoltStore->get();
  93. $projectBoostArray = $this->projectBoostStore->get();
  94. $projectInspiringArray = $this->projectInspiringStore->get();
  95. return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
  96. }
  97. private function generateCsvData($csv, $resultArray)
  98. {
  99. foreach ($resultArray as $result) {
  100. $territory = $subthematic = $thematic = "";
  101. if ($result->getIndividualData()) {
  102. $territory = $result->getIndividualData()->getTerritory()->getName();
  103. if ($result->getSubthematic()) {
  104. $subthematic = $result->getSubthematic()->getName();
  105. }
  106. if ($result->getThematic()) {
  107. $thematic = $result->getThematic()->getName();
  108. }
  109. $data = [
  110. 'category' => $result->__toString(),
  111. 'thematic' => $thematic,
  112. 'subthematic' => $subthematic,
  113. 'territory' => $territory,
  114. 'description' => $result->getDescription()
  115. ];
  116. $csv->row($data);
  117. }
  118. }
  119. return $csv;
  120. }
  121. }