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.

IndividualDataAdminController.php 4.9KB

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