src/Controller/Admin/FilmProjectProgressReportCrudController.php line 143

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