src/Controller/Admin/FilmProjectProgressReportCrudController.php line 151

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\FilmProjectProgressReport;
  4. use App\String\Constant;
  5. use App\Service\FilmApplicationService;
  6. use App\Service\MailService;
  7. use App\Utility\PdfExporter;
  8. use App\Service\ProgressReportService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  13. use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
  14. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  15. use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
  16. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  17. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  18. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  19. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  20. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  21. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  22. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  23. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  24. use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
  25. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  26. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  27. use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
  28. use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
  29. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  30. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  31. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  32. use Symfony\Component\HttpFoundation\RequestStack;
  33. use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
  34. use Knp\Snappy\Pdf;
  35. use Twig\Environment;
  36. use App\Filter\YearFilter;
  37. class FilmProjectProgressReportCrudController extends AbstractCrudController
  38. {
  39.     private EntityManagerInterface $em;
  40.     private AdminUrlGenerator $adminUrlGenerator;
  41.     private FilmApplicationService $filmApplicationService;
  42.     private $requestStack;
  43.     private ProgressReportService $progressReportService;
  44.     public function __construct(EntityManagerInterface $emAdminUrlGenerator $adminUrlGeneratorFilmApplicationService $filmApplicationServiceRequestStack $requestStackProgressReportService $progressReportService)
  45.     {
  46.         $this->em $em;
  47.         $this->adminUrlGenerator $adminUrlGenerator;
  48.         $this->filmApplicationService $filmApplicationService;
  49.         $this->requestStack $requestStack;
  50.         $this->progressReportService $progressReportService;
  51.     }
  52.     public static function getEntityFqcn(): string
  53.     {
  54.         return FilmProjectProgressReport::class;
  55.     }
  56.     public function configureFields(string $pageName): iterable
  57.     {
  58.         return [
  59.             TextField::new('filmProject.title''Project Title')->setColumns(6)->hideOnForm(),
  60.             TextField::new('filmProject.productionStage''Project Stage')->hideOnForm()->setTemplatePath('admin/field/project_stage.html.twig'),
  61.             TextField::new('filmProject.status''Status')->hideOnForm()->setTemplatePath('admin/field/project_status.html.twig'),
  62.             TextField::new('filmProject.wordpressStatus''Wordpress Status')->hideOnForm()->setTemplatePath('admin/field/project_wordpress_status.html.twig'),
  63.             IdField::new('filmProject.wordpressId''WP ID')->hideOnForm()->setTemplatePath('admin/field/wordpress_id.html.twig')->setPermission(Constant::ADMIN_ROLE),
  64.             IdField::new('oldWordpressId''old WP ID')->hideOnForm()->setTemplatePath('admin/field/wordpress_id.html.twig')->setPermission(Constant::ADMIN_ROLE),
  65.             DateField::new('createdAt''Year')->setFormat('yyyy')->hideOnForm(),
  66.             TextField::new('status''Status')->hideOnForm()->setTemplatePath('admin/field/progress_report_status.html.twig'),
  67.         ];
  68.     }
  69.     public function configureFilters(Filters $filters): Filters
  70.     {
  71.         $filters $filters
  72.             ->add(YearFilter::new('createdAt''Year'));
  73.         // Get current user profile
  74.         $filters $filters
  75.             ->add(ChoiceFilter::new('status')->setChoices([
  76.                 'Created' => 'Created',
  77.                 'Completed' => 'Completed',
  78.                 'Received' => 'Received',
  79.             ]))
  80.         ;
  81.         return $filters;
  82.     }
  83.     public function configureCrud(Crud $crud): Crud
  84.     {
  85.         $request $this->requestStack->getCurrentRequest();
  86.         $perPage $request->query->getInt('perPage', default: 10);
  87.         $crud
  88.             ->setSearchFields(['filmProject.title'])
  89.             ->setPageTitle('index''Manage Progress Reports')
  90.             ->overrideTemplate('crud/index''admin/index/index_alt.html.twig')
  91.             ->overrideTemplate('flash_messages''admin/crud/flash_messages_alt.html.twig')
  92.             ->setDefaultSort(['status' => 'ASC''modifiedAt' => 'DESC', ])
  93.             ->setPaginatorPageSize($perPage)
  94.         ;
  95.         return $crud;
  96.     }
  97.     public function deleteEntity(EntityManagerInterface $entityManager$entityInstance): void
  98.     {
  99.         $entityInstance->setFilmProject(null); // set null
  100.         $entityManager->persist($entityInstance);
  101.         $entityManager->flush();
  102.         $entityManager->remove($entityInstance);
  103.         $entityManager->flush();
  104.     }
  105.     public function configureActions(Actions $actions): Actions
  106.     {
  107.         $viewProgressReport Action::new('viewProgressReport''View Progress Report''')
  108.             ->linkToCrudAction('viewProgressReport')
  109.             ->setHtmlAttributes(['target' => '_blank'])
  110.         ;
  111.         $sendEmail Action::new('sendEmail''Send Notification')
  112.             ->linkToUrl('/progress-report/workflow/send-email')
  113.             ->createAsGlobalAction()
  114.             ->addCssClass('btn btn-primary d-block')
  115.         ;
  116.         $sendBatchEmail Action::new('sendBatchEmail''Send Notification')
  117.             ->linkToCrudAction('sendBatchEmail')
  118.         ;
  119.         $publishProjects Action::new('publishProjects''Publish')
  120.             ->linkToCrudAction('publishProjects')
  121.             ->addCssClass('wordpress-action')
  122.         ;
  123.         $unpublishProjects Action::new('unpublishProjects''Unpublish')
  124.             ->linkToCrudAction('unpublishProjects')
  125.             ->addCssClass('wordpress-action')
  126.         ;
  127.         $downloadPdf Action::new('downloadPdf''Download Progress Report')
  128.             ->linkToCrudAction('downloadPdf')
  129.             ->displayIf(static function ($entity) {
  130.                 $status $entity->getStatus();
  131.                 return $status == 'completed';
  132.             })
  133.         ;
  134.         $downloadFeedbackPdf Action::new('downloadFeedbackPdf''Download Feedback')
  135.             ->linkToCrudAction('downloadFeedbackPdf')
  136.             ->displayIf(static function ($entity) {
  137.                 $status $entity->getStatus();
  138.                 return $status == 'completed';
  139.             })
  140.         ;
  141.         // $exportProgressReportList = Action::new('exportProgressReportList', 'Export Progress Report')
  142.         //     ->linkToUrl('/admin/project/export-progress-report')
  143.         //     ->createAsGlobalAction()
  144.         //     ->addCssClass('btn btn-primary d-block')
  145.         //     // ->displayIf(static function ($entity) {
  146.         //     //     $status = $entity->getStatus();
  147.         //     //     return $status == 'approved';
  148.         //     // })
  149.         // ;
  150.         $exportProgressReportList Action::new('exportProgressReportList''Export Progress Report')
  151.             ->linkToUrl(function () {
  152.                 $request $this->requestStack->getCurrentRequest();
  153.                 $filters $request->query->all('filters');
  154.                 return '/admin/project/export-progress-report' 
  155.                     . (!empty($filters) ? '?' http_build_query(['filters' => $filters]) : '');
  156.             })
  157.             ->createAsGlobalAction()
  158.             ->addCssClass('btn btn-primary d-block')
  159.         ;
  160.         $exportProgressReportFeedbackList Action::new('exportProgressReportFeedbackList''Export Feedback')
  161.             ->linkToUrl('/admin/project/export-progress-report-feedback')
  162.             ->createAsGlobalAction()
  163.             ->addCssClass('btn btn-primary d-block')
  164.             // ->displayIf(static function ($entity) {
  165.             //     $status = $entity->getStatus();
  166.             //     return $status == 'approved';
  167.             // })
  168.         ;
  169.         $toggleProgressReportOn Action::new('toggleProgressReportOn''Turn On Progress Report')
  170.             ->linkToUrl('/admin/progress-report/workflow/cron')
  171.             ->createAsGlobalAction()
  172.             ->addCssClass('btn btn-primary d-block')
  173.         ;
  174.         $toggleProgressReportOff Action::new('toggleProgressReportOff''Turn Off Progress Report')
  175.             ->linkToUrl('/admin/progress-report/workflow/cron')
  176.             ->createAsGlobalAction()
  177.             ->addCssClass('btn btn-primary d-block')
  178.         ;
  179.         $actions
  180.             ->add(Crud::PAGE_INDEX$viewProgressReport)
  181.             ->add(Crud::PAGE_INDEX$sendEmail)
  182.             ->add(Crud::PAGE_INDEX$downloadPdf)
  183.             // ->add(Crud::PAGE_INDEX, $downloadFeedbackPdf)
  184.             ->add(Crud::PAGE_INDEX$exportProgressReportList)
  185.             ->add(Crud::PAGE_INDEX$exportProgressReportFeedbackList)
  186.             
  187.             ->addBatchAction($publishProjects)
  188.             ->addBatchAction($unpublishProjects)
  189.             ->addBatchAction($sendBatchEmail)
  190.             ->remove(Crud::PAGE_INDEXAction::EDIT)
  191.             ->remove(Crud::PAGE_INDEXAction::NEW)
  192.             ->setPermission($sendEmailConstant::ADMIN_ROLE)
  193.             ->setPermission($publishProjectsConstant::ADMIN_ROLE)
  194.             ->setPermission($unpublishProjectsConstant::ADMIN_ROLE)
  195.             ->setPermission($sendBatchEmailConstant::ADMIN_ROLE)
  196.             ->setPermission($exportProgressReportListConstant::ADMIN_ROLE)
  197.             ->setPermission($exportProgressReportFeedbackListConstant::ADMIN_ROLE)
  198.             // ->setPermission($downloadFeedbackPdf, Constant::ADMIN_ROLE)
  199.             ->reorder(Crud::PAGE_INDEX, ['viewProgressReport''downloadPdf'Action::DELETE])
  200.         ;
  201.         if ($this->progressReportService->checkIfProgressReportStarts()) {
  202.             $actions
  203.                 ->add(Crud::PAGE_INDEX$toggleProgressReportOff)
  204.                 ->setPermission($toggleProgressReportOffConstant::ADMIN_ROLE)
  205.             ;
  206.         } else {
  207.             $actions
  208.                 ->add(Crud::PAGE_INDEX$toggleProgressReportOn)
  209.                 ->setPermission($toggleProgressReportOnConstant::ADMIN_ROLE)
  210.             ;
  211.         }
  212.         return $actions;
  213.     }
  214.     // Custom Actions
  215.     public function viewProgressReport(AdminContext $adminContext)
  216.     {
  217.         $entityInstance $adminContext->getEntity()->getInstance();
  218.         $id $entityInstance->getId();
  219.         $currentYear = (int) date('Y');
  220.         if ($entityInstance->getCreatedAt()) {
  221.             $currentYear = (int) ($entityInstance->getCreatedAt())->format('Y');
  222.         }
  223.         $filmProject $entityInstance->getFilmProject();
  224.         $progressReportUrl '/progress-report/'$currentYear .'?projectId=' $filmProject->getId();
  225.         return $this->redirect($progressReportUrl);
  226.     }
  227.     /**
  228.      * BATCH: Publish projects
  229.      *
  230.      * @param BatchActionDto $batchActionDto
  231.      * @return void
  232.      */
  233.     public function publishProjects(BatchActionDto $batchActionDto)
  234.     {
  235.         $className $batchActionDto->getEntityFqcn();
  236.         $entityManager $this->container->get('doctrine')->getManagerForClass($className);
  237.         // Check if there is non-approved projects
  238.         foreach ($batchActionDto->getEntityIds() as $id) {
  239.             $progressReport $entityManager->find($className$id);
  240.             $entityInstance $progressReport->getFilmProject();
  241.             if ($entityInstance->getStatus() != 'approved') {
  242.                 $this->addFlash('notice''Project(s) have to be approved before being published.');
  243.                 return $this->redirect($batchActionDto->getReferrerUrl());
  244.             }
  245.         }
  246.         foreach ($batchActionDto->getEntityIds() as $id) {
  247.             $progressReport $entityManager->find($className$id);
  248.             $entityInstance $progressReport->getFilmProject();
  249.             $this->filmApplicationService->publishProject($entityInstance);
  250.         }
  251.         $this->addFlash('success''Project(s) have been published');
  252.         return $this->redirect($batchActionDto->getReferrerUrl());
  253.     }
  254.     /**
  255.      * BATCH: Unublish projects
  256.      *
  257.      * @param BatchActionDto $batchActionDto
  258.      * @return void
  259.      */
  260.     public function unpublishProjects(BatchActionDto $batchActionDto)
  261.     {
  262.         $className $batchActionDto->getEntityFqcn();
  263.         $entityManager $this->container->get('doctrine')->getManagerForClass($className);
  264.         // Check if there is non-approved projects
  265.         foreach ($batchActionDto->getEntityIds() as $id) {
  266.             $progressReport $entityManager->find($className$id);
  267.             $entityInstance $progressReport->getFilmProject();
  268.             
  269.             if ($entityInstance->getStatus() != 'approved') {
  270.                 $this->addFlash('notice''Project(s) have to be approved before being unpublished.');
  271.                 return $this->redirect($batchActionDto->getReferrerUrl());
  272.             }
  273.         }
  274.         foreach ($batchActionDto->getEntityIds() as $id) {
  275.             $progressReport $entityManager->find($className$id);
  276.             $entityInstance $progressReport->getFilmProject();
  277.             $this->filmApplicationService->unpublishProject($entityInstance);
  278.         }
  279.         $this->addFlash('success''Project(s) have been unpublished');
  280.         return $this->redirect($batchActionDto->getReferrerUrl());
  281.     }
  282.     public function downloadPdf(AdminContext $contextEnvironment $twig nullPdf $pdf)
  283.     {
  284.         $progressReport $context->getEntity()->getInstance();
  285.         $pdfExporter = new PdfExporter($twig$pdf);
  286.         $pdfExporter->convertProgressReportToPdf($progressReport);
  287.     }
  288.     public function downloadFeedbackPdf(AdminContext $contextEnvironment $twig nullPdf $pdf)
  289.     {
  290.         $progressReport $context->getEntity()->getInstance();
  291.         $pdfExporter = new PdfExporter($twig$pdf);
  292.         $pdfExporter->convertProgressReportFeedbackToPdf($progressReport);
  293.     }
  294.     /**
  295.      * BATCH: Send Batch Emails
  296.      *
  297.      * @param BatchActionDto $batchActionDto
  298.      * @return void
  299.      */
  300.     public function sendBatchEmail(BatchActionDto $batchActionDtoMailService $mailService)
  301.     {
  302.         $className $batchActionDto->getEntityFqcn();
  303.         $entityManager $this->container->get('doctrine')->getManagerForClass($className);
  304.         // Check if there is non-approved projects
  305.         foreach ($batchActionDto->getEntityIds() as $id) {
  306.             $progressReport $entityManager->find($className$id);
  307.             $entityInstance $progressReport->getFilmProject();
  308.             $mailService->sendDefaultProgressReportEmail($entityInstance);
  309.             
  310.             if ($entityInstance->getStatus() != 'approved') {
  311.                 $this->addFlash('notice''Project(s) have to be approved before being published.');
  312.                 return $this->redirect($batchActionDto->getReferrerUrl());
  313.             }
  314.         }
  315.         $this->addFlash('success''Notification email(s) have been sent.');
  316.         return $this->redirect($batchActionDto->getReferrerUrl());
  317.     }
  318. }