<?php
namespace App\Controller\Admin;
use App\Entity\FilmProjectProgressReport;
use App\String\Constant;
use App\Service\FilmApplicationService;
use App\Service\MailService;
use App\Utility\PdfExporter;
use App\Service\ProgressReportService;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\RequestStack;
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
use Knp\Snappy\Pdf;
use Twig\Environment;
class FilmProjectProgressReportCrudController extends AbstractCrudController
{
private EntityManagerInterface $em;
private AdminUrlGenerator $adminUrlGenerator;
private FilmApplicationService $filmApplicationService;
private $requestStack;
private ProgressReportService $progressReportService;
public function __construct(EntityManagerInterface $em, AdminUrlGenerator $adminUrlGenerator, FilmApplicationService $filmApplicationService, RequestStack $requestStack, ProgressReportService $progressReportService)
{
$this->em = $em;
$this->adminUrlGenerator = $adminUrlGenerator;
$this->filmApplicationService = $filmApplicationService;
$this->requestStack = $requestStack;
$this->progressReportService = $progressReportService;
}
public static function getEntityFqcn(): string
{
return FilmProjectProgressReport::class;
}
public function configureFields(string $pageName): iterable
{
return [
TextField::new('filmProject.title', 'Project Title')->setColumns(6)->hideOnForm(),
TextField::new('filmProject.productionStage', 'Project Stage')->hideOnForm()->setTemplatePath('admin/field/project_stage.html.twig'),
TextField::new('filmProject.status', 'Status')->hideOnForm()->setTemplatePath('admin/field/project_status.html.twig'),
TextField::new('filmProject.wordpressStatus', 'Wordpress Status')->hideOnForm()->setTemplatePath('admin/field/project_wordpress_status.html.twig'),
IdField::new('filmProject.wordpressId', 'WP ID')->hideOnForm()->setTemplatePath('admin/field/wordpress_id.html.twig')->setPermission(Constant::ADMIN_ROLE),
IdField::new('oldWordpressId', 'old WP ID')->hideOnForm()->setTemplatePath('admin/field/wordpress_id.html.twig')->setPermission(Constant::ADMIN_ROLE),
DateField::new('createdAt', 'Year')->setFormat('yyyy')->hideOnForm(),
TextField::new('status', 'Status')->hideOnForm()->setTemplatePath('admin/field/progress_report_status.html.twig'),
];
}
public function configureFilters(Filters $filters): Filters
{
// Get current user profile
$filters = $filters
->add(ChoiceFilter::new('status')->setChoices([
'Created' => 'Created',
'Completed' => 'Completed',
'Received' => 'Received',
]))
;
return $filters;
}
public function configureCrud(Crud $crud): Crud
{
$crud
->setSearchFields(['filmProject.title'])
->setPageTitle('index', 'Manage Progress Reports')
->overrideTemplate('crud/index', 'admin/index/index_alt.html.twig')
->overrideTemplate('flash_messages', 'admin/crud/flash_messages_alt.html.twig')
->setDefaultSort(['status' => 'ASC', 'modifiedAt' => 'DESC', ])
;
return $crud;
}
public function deleteEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
$entityInstance->setFilmProject(null); // set null
$entityManager->persist($entityInstance);
$entityManager->flush();
$entityManager->remove($entityInstance);
$entityManager->flush();
}
public function configureActions(Actions $actions): Actions
{
$viewProgressReport = Action::new('viewProgressReport', 'View Progress Report', '')
->linkToCrudAction('viewProgressReport')
->setHtmlAttributes(['target' => '_blank'])
;
$sendEmail = Action::new('sendEmail', 'Send Notification')
->linkToUrl('/progress-report/workflow/send-email')
->createAsGlobalAction()
->addCssClass('btn btn-primary d-block')
;
$sendBatchEmail = Action::new('sendBatchEmail', 'Send Notification')
->linkToCrudAction('sendBatchEmail')
;
$publishProjects = Action::new('publishProjects', 'Publish')
->linkToCrudAction('publishProjects')
->addCssClass('wordpress-action')
;
$unpublishProjects = Action::new('unpublishProjects', 'Unpublish')
->linkToCrudAction('unpublishProjects')
->addCssClass('wordpress-action')
;
$downloadPdf = Action::new('downloadPdf', 'Download Progress Report')
->linkToCrudAction('downloadPdf')
->displayIf(static function ($entity) {
$status = $entity->getStatus();
return $status == 'completed';
})
;
$downloadFeedbackPdf = Action::new('downloadFeedbackPdf', 'Download Feedback')
->linkToCrudAction('downloadFeedbackPdf')
->displayIf(static function ($entity) {
$status = $entity->getStatus();
return $status == 'completed';
})
;
$exportProgressReportList = Action::new('exportProgressReportList', 'Export Progress Report')
->linkToUrl('/admin/project/export-progress-report')
->createAsGlobalAction()
->addCssClass('btn btn-primary d-block')
// ->displayIf(static function ($entity) {
// $status = $entity->getStatus();
// return $status == 'approved';
// })
;
$exportProgressReportFeedbackList = Action::new('exportProgressReportFeedbackList', 'Export Feedback')
->linkToUrl('/admin/project/export-progress-report-feedback')
->createAsGlobalAction()
->addCssClass('btn btn-primary d-block')
// ->displayIf(static function ($entity) {
// $status = $entity->getStatus();
// return $status == 'approved';
// })
;
$toggleProgressReportOn = Action::new('toggleProgressReportOn', 'Turn On Progress Report')
->linkToUrl('/admin/progress-report/workflow/cron')
->createAsGlobalAction()
->addCssClass('btn btn-primary d-block')
;
$toggleProgressReportOff = Action::new('toggleProgressReportOff', 'Turn Off Progress Report')
->linkToUrl('/admin/progress-report/workflow/cron')
->createAsGlobalAction()
->addCssClass('btn btn-primary d-block')
;
$actions
->add(Crud::PAGE_INDEX, $viewProgressReport)
->add(Crud::PAGE_INDEX, $sendEmail)
->add(Crud::PAGE_INDEX, $downloadPdf)
// ->add(Crud::PAGE_INDEX, $downloadFeedbackPdf)
->add(Crud::PAGE_INDEX, $exportProgressReportList)
->add(Crud::PAGE_INDEX, $exportProgressReportFeedbackList)
->addBatchAction($publishProjects)
->addBatchAction($unpublishProjects)
->addBatchAction($sendBatchEmail)
->remove(Crud::PAGE_INDEX, Action::EDIT)
->remove(Crud::PAGE_INDEX, Action::NEW)
->setPermission($sendEmail, Constant::ADMIN_ROLE)
->setPermission($publishProjects, Constant::ADMIN_ROLE)
->setPermission($unpublishProjects, Constant::ADMIN_ROLE)
->setPermission($sendBatchEmail, Constant::ADMIN_ROLE)
->setPermission($exportProgressReportList, Constant::ADMIN_ROLE)
->setPermission($exportProgressReportFeedbackList, Constant::ADMIN_ROLE)
// ->setPermission($downloadFeedbackPdf, Constant::ADMIN_ROLE)
->reorder(Crud::PAGE_INDEX, ['viewProgressReport', 'downloadPdf', Action::DELETE])
;
if ($this->progressReportService->checkIfProgressReportStarts()) {
$actions
->add(Crud::PAGE_INDEX, $toggleProgressReportOff)
->setPermission($toggleProgressReportOff, Constant::ADMIN_ROLE)
;
} else {
$actions
->add(Crud::PAGE_INDEX, $toggleProgressReportOn)
->setPermission($toggleProgressReportOn, Constant::ADMIN_ROLE)
;
}
return $actions;
}
// Custom Actions
public function viewProgressReport(AdminContext $adminContext)
{
$entityInstance = $adminContext->getEntity()->getInstance();
$id = $entityInstance->getId();
$currentYear = (int) date('Y');
if ($entityInstance->getCreatedAt()) {
$currentYear = (int) ($entityInstance->getCreatedAt())->format('Y');
}
$filmProject = $entityInstance->getFilmProject();
$progressReportUrl = '/progress-report/'. $currentYear .'?projectId=' . $filmProject->getId();
return $this->redirect($progressReportUrl);
}
/**
* BATCH: Publish projects
*
* @param BatchActionDto $batchActionDto
* @return void
*/
public function publishProjects(BatchActionDto $batchActionDto)
{
$className = $batchActionDto->getEntityFqcn();
$entityManager = $this->container->get('doctrine')->getManagerForClass($className);
// Check if there is non-approved projects
foreach ($batchActionDto->getEntityIds() as $id) {
$progressReport = $entityManager->find($className, $id);
$entityInstance = $progressReport->getFilmProject();
if ($entityInstance->getStatus() != 'approved') {
$this->addFlash('notice', 'Project(s) have to be approved before being published.');
return $this->redirect($batchActionDto->getReferrerUrl());
}
}
foreach ($batchActionDto->getEntityIds() as $id) {
$progressReport = $entityManager->find($className, $id);
$entityInstance = $progressReport->getFilmProject();
$this->filmApplicationService->publishProject($entityInstance);
}
$this->addFlash('success', 'Project(s) have been published');
return $this->redirect($batchActionDto->getReferrerUrl());
}
/**
* BATCH: Unublish projects
*
* @param BatchActionDto $batchActionDto
* @return void
*/
public function unpublishProjects(BatchActionDto $batchActionDto)
{
$className = $batchActionDto->getEntityFqcn();
$entityManager = $this->container->get('doctrine')->getManagerForClass($className);
// Check if there is non-approved projects
foreach ($batchActionDto->getEntityIds() as $id) {
$progressReport = $entityManager->find($className, $id);
$entityInstance = $progressReport->getFilmProject();
if ($entityInstance->getStatus() != 'approved') {
$this->addFlash('notice', 'Project(s) have to be approved before being unpublished.');
return $this->redirect($batchActionDto->getReferrerUrl());
}
}
foreach ($batchActionDto->getEntityIds() as $id) {
$progressReport = $entityManager->find($className, $id);
$entityInstance = $progressReport->getFilmProject();
$this->filmApplicationService->unpublishProject($entityInstance);
}
$this->addFlash('success', 'Project(s) have been unpublished');
return $this->redirect($batchActionDto->getReferrerUrl());
}
public function downloadPdf(AdminContext $context, Environment $twig = null, Pdf $pdf)
{
$progressReport = $context->getEntity()->getInstance();
$pdfExporter = new PdfExporter($twig, $pdf);
$pdfExporter->convertProgressReportToPdf($progressReport);
}
public function downloadFeedbackPdf(AdminContext $context, Environment $twig = null, Pdf $pdf)
{
$progressReport = $context->getEntity()->getInstance();
$pdfExporter = new PdfExporter($twig, $pdf);
$pdfExporter->convertProgressReportFeedbackToPdf($progressReport);
}
/**
* BATCH: Send Batch Emails
*
* @param BatchActionDto $batchActionDto
* @return void
*/
public function sendBatchEmail(BatchActionDto $batchActionDto, MailService $mailService)
{
$className = $batchActionDto->getEntityFqcn();
$entityManager = $this->container->get('doctrine')->getManagerForClass($className);
// Check if there is non-approved projects
foreach ($batchActionDto->getEntityIds() as $id) {
$progressReport = $entityManager->find($className, $id);
$entityInstance = $progressReport->getFilmProject();
$mailService->sendDefaultProgressReportEmail($entityInstance);
if ($entityInstance->getStatus() != 'approved') {
$this->addFlash('notice', 'Project(s) have to be approved before being published.');
return $this->redirect($batchActionDto->getReferrerUrl());
}
}
$this->addFlash('success', 'Notification email(s) have been sent.');
return $this->redirect($batchActionDto->getReferrerUrl());
}
}