|
- <?php
-
- namespace Lc\PietroBundle\Controller\IndividualData;
-
- use Doctrine\ORM\QueryBuilder;
- use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
- use Lc\PietroBundle\Repository\Dream\DreamStore;
- use Lc\PietroBundle\Repository\ProjectBoost\ProjectBoostStore;
- use Lc\PietroBundle\Repository\ProjectInspiring\ProjectInspiringStore;
- use Lc\PietroBundle\Repository\Revolt\RevoltStore;
- use Lc\PietroBundle\Controller\AbstractAdminController;
- use Lc\SovBundle\Repository\RepositoryQueryInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Lc\SovBundle\Generator\CsvGenerator;
-
- abstract class IndividualDataAdminController extends AbstractAdminController
- {
- protected DreamStore $dreamStore;
- protected RevoltStore $revoltStore;
- protected ProjectBoostStore $projectBoostStore;
- protected ProjectInspiringStore $projectInspiringStore;
-
- public function __construct(
- DreamStore $dreamStore,
- RevoltStore $revoltStore,
- ProjectBoostStore $projectBoostStore,
- ProjectInspiringStore $projectInspiringStore
- ) {
- $this->dreamStore = $dreamStore;
- $this->revoltStore = $revoltStore;
- $this->projectBoostStore = $projectBoostStore;
- $this->projectInspiringStore = $projectInspiringStore;
- }
-
- public function createEntity(string $entityFqcn)
- {
- return $this->getIndividualDataContainer()->getFactory()->create();
- }
-
- public function configureFields(string $pageName): iterable
- {
- return $this->getIndividualDataContainer()
- ->getFieldDefinition()
- ->getFields($pageName);
- }
-
- public function getRepositoryQuery(): RepositoryQueryInterface
- {
- return $this->getIndividualDataContainer()->getRepositoryQuery();
- }
-
- public function createIndexRepositoryQuery(
- SearchDto $searchDto,
- EntityDto $entityDto,
- FieldCollection $fields,
- FilterCollection $filters
- ): RepositoryQueryInterface {
- $repositoryQuery = parent::createIndexRepositoryQuery($searchDto, $entityDto, $fields, $filters);
- $status = $searchDto->getRequest()->get('status');
- if ($status || $status === 0) {
- $repositoryQuery->filterByStatus($searchDto->getRequest()->get('status'));
- }
- return $repositoryQuery;
- }
-
- public function configureActions(Actions $actions): Actions
- {
- parent::configureActions($actions);
-
- $export = Action::new('export', 'actions.export')
- ->setIcon('fa fa-download')
- ->linkToCrudAction('exportCsv')
- ->setCssClass('btn btn-sm btn-primary export-csv')
- ->createAsGlobalAction();
-
- return $actions->add(Crud::PAGE_INDEX, $export);
- }
-
- public function exportCsv(Request $request)
- {
- $csv = new CsvGenerator();
- $csv->enableConvertEncoding('ISO-8859-1');
- $csv->setTitle('Export_Liste_Individuel', true);
-
- $columns = [
- 'category' => 'Catégorie',
- 'thematic' => 'Thématique',
- 'subthematic' => 'Contribution',
- 'territory' => 'Lieu',
- 'description' => 'Description'
- ];
- $csv->setColumns($columns);
-
- $resultArray = $this->generateAllResultArray();
-
- $csv = $this->generateCsvData($csv, $resultArray);
-
- return $csv->getReponse();
- }
-
- private function generateAllResultArray(): array
- {
- $dreamArray = $this->dreamStore->get();
- $revoltArray = $this->revoltStore->get();
- $projectBoostArray = $this->projectBoostStore->get();
- $projectInspiringArray = $this->projectInspiringStore->get();
-
- return array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);
- }
-
- private function generateCsvData($csv, $resultArray)
- {
- foreach ($resultArray as $result) {
- $territory = $subthematic = $thematic = "";
- if ($result->getIndividualData()) {
- $territory = $result->getIndividualData()->getTerritory()->getName();
-
-
- if ($result->getSubthematic()) {
- $subthematic = $result->getSubthematic()->getName();
- }
-
- if ($result->getThematic()) {
- $thematic = $result->getThematic()->getName();
- }
-
- $data = [
- 'category' => $result->__toString(),
- 'thematic' => $thematic,
- 'subthematic' => $subthematic,
- 'territory' => $territory,
- 'description' => $result->getDescription()
- ];
-
- $csv->row($data);
- }
- }
-
- return $csv;
- }
- }
|