src/Cms/WorkflowsBundle/Controller/Dashboard/DraftsController.php line 176

Open in your IDE?
  1. <?php
  2. namespace Cms\WorkflowsBundle\Controller\Dashboard;
  3. use Cms\CoreBundle\Model\Scenes\DashboardScenes\AjaxScene;
  4. use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
  5. use Cms\CoreBundle\Model\Search\Search;
  6. use Cms\ModuleBundle\Controller\ContentController;
  7. use Cms\ModuleBundle\Entity\Draft;
  8. use Cms\ModuleBundle\Form\Type\Actions\PublishActionType;
  9. use Cms\ModuleBundle\Service\ModuleManager;
  10. use Cms\WorkflowsBundle\Controller\AbstractWorkflowsController;
  11. use Cms\WorkflowsBundle\Entity\Publication\ScheduledPublication;
  12. use Cms\WorkflowsBundle\Entity\Workflow;
  13. use Cms\WorkflowsBundle\Entity\WorkflowSubmission;
  14. use Cms\WorkflowsBundle\Form\Type\PublicationCancelType;
  15. use Cms\WorkflowsBundle\Service\Search\DraftsSearcher;
  16. use Cms\WorkflowsBundle\Service\Search\ScheduledPublicationSearcher;
  17. use Cms\WorkflowsBundle\Service\Search\WorkflowSubmissionSearcher;
  18. use Doctrine\ORM\QueryBuilder;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Form\FormError;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. /**
  24.  * Class DraftsController
  25.  * @package Cms\WorkflowsBundle\Controller\Dashboard
  26.  *
  27.  * @Route(
  28.  *     "/drafts"
  29.  * )
  30.  */
  31. final class DraftsController extends AbstractWorkflowsController
  32. {
  33.     const ROUTES__INDEX 'cms.workflows.dashboard.drafts.index';
  34.     const ROUTES__DRAFTS 'cms.workflows.dashboard.drafts.drafts';
  35.     const ROUTES__DRAFTS_LAZY 'cms.workflows.dashboard.drafts.drafts_lazy';
  36.     const ROUTES__MY_DRAFTS 'cms.workflows.dashboard.drafts.my_drafts';
  37.     const ROUTES__MY_DRAFTS_LAZY 'cms.workflows.dashboard.drafts.my_drafts_lazy';
  38.     const ROUTES__SUBMISSIONS 'cms.workflows.dashboard.drafts.submissions';
  39.     const ROUTES__SUBMISSIONS_LAZY 'cms.workflows.dashboard.drafts.submissions_lazy';
  40.     const ROUTES__MY_SUBMISSIONS 'cms.workflows.dashboard.drafts.my_submissions';
  41.     const ROUTES__MY_SUBMISSIONS_LAZY 'cms.workflows.dashboard.drafts.my_submissions_lazy';
  42.     const ROUTES__PUBLICATIONS 'cms.workflows.dashboard.drafts.publications';
  43.     const ROUTES__PUBLICATIONS_LAZY 'cms.workflows.dashboard.drafts.publications_lazy';
  44.     const ROUTES__PUBLICATION_CANCEL 'cms.workflows.dashboard.drafts.publication_cancel';
  45.     const ROUTES__PUBLICATION_RESCHEDULE 'cms.workflows.dashboard.drafts.publication_reschedule';
  46.     /**
  47.      * @return DraftsSearcher|object
  48.      */
  49.     private function getDraftsSearcher(): DraftsSearcher
  50.     {
  51.         return $this->get(__METHOD__);
  52.     }
  53.     /**
  54.      * @return ModuleManager|object
  55.      */
  56.     private function getModuleManager(): ModuleManager
  57.     {
  58.         return $this->get(__METHOD__);
  59.     }
  60.     /**
  61.      * @return WorkflowSubmissionSearcher|object
  62.      */
  63.     private function getWorkflowSubmissionSearcher(): WorkflowSubmissionSearcher
  64.     {
  65.         return $this->get(__METHOD__);
  66.     }
  67.     /**
  68.      * @return ScheduledPublicationSearcher|object
  69.      */
  70.     private function getScheduledPublicationSearcher(): ScheduledPublicationSearcher
  71.     {
  72.         return $this->get(__METHOD__);
  73.     }
  74.     /**
  75.      * @return RedirectResponse
  76.      *
  77.      * @Route(
  78.      *     "",
  79.      *     name = DraftsController::ROUTES__INDEX
  80.      * )
  81.      */
  82.     public function indexAction()
  83.     {
  84.         return $this->redirectToRoute(self::ROUTES__MY_DRAFTS);
  85.     }
  86.     /**
  87.      * @param Request $request
  88.      * @return AjaxScene|DocumentScene
  89.      * @throws \Exception
  90.      *
  91.      * @Route(
  92.      *     "/drafts",
  93.      *     name = DraftsController::ROUTES__DRAFTS
  94.      * )
  95.      * @Route(
  96.      *     "/drafts/lazyload",
  97.      *     name = DraftsController::ROUTES__DRAFTS_LAZY
  98.      * )
  99.      */
  100.     public function draftsAction(Request $request)
  101.     {
  102.         // do search logic to find applicable workflows
  103.         $search $this->getDraftsSearcher()->handleRequest($request);
  104.         if ($search instanceof RedirectResponse) {
  105.             return $search;
  106.         }
  107.         // query for drafts
  108.         $drafts $this->getDraftsSearcher()->search(
  109.             $search,
  110.             null
  111.         );
  112.         // function for linking the edit buttons
  113.         $editCallback = function($draft) {
  114.             switch (true) {
  115.                 case $draft instanceof Draft:
  116.                     return $this->generateUrl(
  117.                         ContentController::ROUTES__MODIFY_CONFLICTS,
  118.                         array(
  119.                             'container' => $draft->getProxy()->getContainer()->getId(),
  120.                             'module' => $this->getModuleManager()->getModuleConfigurationForEntity($draft)->key(),
  121.                             'proxy' => $draft->getProxy()->getId(),
  122.                             //'draft' => $draft->getId(),
  123.                             'redirectTo' => $this->getRequest()->getRequestUri(),
  124.                         )
  125.                     );
  126.             }
  127.             throw new \Exception();
  128.         };
  129.         // handle lazyloading
  130.         if ($request->isXmlHttpRequest()) {
  131.             return $this->viewAjax(
  132.                 '@CmsWorkflows/Dashboard/Drafts/includes/drafts.html.twig',
  133.                 array(
  134.                     'search' => $search,
  135.                     'drafts' => $drafts,
  136.                     'editCallback' => $editCallback,
  137.                 )
  138.             );
  139.         }
  140.         return $this->view(
  141.             array(
  142.                 'search' => $search,
  143.                 'drafts' => $drafts,
  144.                 'editCallback' => $editCallback,
  145.             )
  146.         );
  147.     }
  148.     /**
  149.      * @param Request $request
  150.      * @return AjaxScene|DocumentScene
  151.      * @throws \Exception
  152.      *
  153.      * @Route(
  154.      *     "/my-drafts",
  155.      *     name = DraftsController::ROUTES__MY_DRAFTS
  156.      * )
  157.      * @Route(
  158.      *     "/my-drafts/lazyload",
  159.      *     name = DraftsController::ROUTES__MY_DRAFTS_LAZY
  160.      * )
  161.      */
  162.     public function myDraftsAction(Request $request)
  163.     {
  164.         // do search logic to find applicable workflows
  165.         $search $this->getDraftsSearcher()->handleRequest($request);
  166.         if ($search instanceof RedirectResponse) {
  167.             return $search;
  168.         }
  169.         // query for drafts
  170.         $drafts $this->getDraftsSearcher()->search(
  171.             $search,
  172.             $this->getGlobalContext()->getEffectiveAccount()
  173.         );
  174.         // function for linking the edit buttons
  175.         $editCallback = function($draft) {
  176.             switch (true) {
  177.                 case $draft instanceof Draft:
  178.                     return $this->generateUrl(
  179.                         ContentController::ROUTES__MODIFY_CONFLICTS,
  180.                         array(
  181.                             'container' => $draft->getProxy()->getContainer()->getId(),
  182.                             'module' => $this->getModuleManager()->getModuleConfigurationForEntity($draft)->key(),
  183.                             'proxy' => $draft->getProxy()->getId(),
  184.                             //'draft' => $draft->getId(),
  185.                             'redirectTo' => $this->getRequest()->getRequestUri(),
  186.                         )
  187.                     );
  188.             }
  189.             throw new \Exception();
  190.         };
  191.         // handle lazyloading
  192.         if ($request->isXmlHttpRequest()) {
  193.             return $this->viewAjax(
  194.                 '@CmsWorkflows/Dashboard/Drafts/includes/drafts.html.twig',
  195.                 array(
  196.                     'search' => $search,
  197.                     'drafts' => $drafts,
  198.                     'editCallback' => $editCallback,
  199.                 )
  200.             );
  201.         }
  202.         return $this->view(
  203.             '@CmsWorkflows/Dashboard/Drafts/drafts.html.twig',
  204.             array(
  205.                 'search' => $search,
  206.                 'drafts' => $drafts,
  207.                 'editCallback' => $editCallback,
  208.             )
  209.         );
  210.     }
  211.     /**
  212.      * @param Request $request
  213.      * @return AjaxScene|DocumentScene
  214.      * @throws \Exception
  215.      *
  216.      * @Route(
  217.      *     "/submissions",
  218.      *     name = DraftsController::ROUTES__SUBMISSIONS
  219.      * )
  220.      * @Route(
  221.      *     "/submissions/lazyload",
  222.      *     name = DraftsController::ROUTES__SUBMISSIONS_LAZY
  223.      * )
  224.      */
  225.     public function submissionsAction(Request $request)
  226.     {
  227.         // do search logic to find applicable workflows
  228.         $search $this->getWorkflowSubmissionSearcher()->handleRequest($request);
  229.         if ($search instanceof RedirectResponse) {
  230.             return $search;
  231.         }
  232.         // use groups for user to limit the search space to the ones tied to a workflow they are a reviewer for
  233.         $search->registerPreFilter(
  234.             function (QueryBuilder $qbSearch $search)
  235.             {
  236.                 // get the workflows that have these groups set as reviewers
  237.                 $workflows $this->getEntityManager()->getRepository(Workflow::class)
  238.                     ->findByReviewedByAccount(
  239.                         $this->getGlobalContext()->getEffectiveAccount()
  240.                     );
  241.                 // add a restriction on the query builder where the submission is tied to a workflow they are a reviewer of
  242.                 $qb
  243.                     ->andWhere(sprintf(
  244.                         '%s.workflow IN (:workflows)',
  245.                         $qb->getRootAliases()[0]
  246.                     ))
  247.                     ->setParameter('workflows'$workflows);
  248.             }
  249.         );
  250.         // get the submissions
  251.         $submissions $this->getEntityManager()->getRepository(WorkflowSubmission::class)->search($search);
  252.         // handle lazyloading
  253.         if ($request->isXmlHttpRequest()) {
  254.             return $this->viewAjax(
  255.                 '@CmsWorkflows/Dashboard/Drafts/includes/submissions.html.twig',
  256.                 array(
  257.                     'search' => $search,
  258.                     'submissions' => $submissions,
  259.                 )
  260.             );
  261.         }
  262.         return $this->view(
  263.             array(
  264.                 'search' => $search,
  265.                 'submissions' => $submissions,
  266.             )
  267.         );
  268.     }
  269.     /**
  270.      * @param Request $request
  271.      * @return AjaxScene|DocumentScene
  272.      * @throws \Exception
  273.      *
  274.      * @Route(
  275.      *     "/my-submissions",
  276.      *     name = DraftsController::ROUTES__MY_SUBMISSIONS
  277.      * )
  278.      * @Route(
  279.      *     "/my-submissions/lazyload",
  280.      *     name = DraftsController::ROUTES__MY_SUBMISSIONS_LAZY
  281.      * )
  282.      */
  283.     public function mySubmissionsAction(Request $request)
  284.     {
  285.         // do search logic to find applicable workflows
  286.         $search $this->getWorkflowSubmissionSearcher()->handleRequest($request);
  287.         if ($search instanceof RedirectResponse) {
  288.             return $search;
  289.         }
  290.         // only get the submissions created by us
  291.         $search->registerPreFilter(
  292.             function (QueryBuilder $qbSearch $search)
  293.             {
  294.                 // add a restriction on the query builder where the submission is tied to a workflow they are a reviewer of
  295.                 $qb
  296.                     ->andWhere(sprintf(
  297.                         '%s.createdBy = :account',
  298.                         $qb->getRootAliases()[0]
  299.                     ))
  300.                     ->setParameter(
  301.                         'account',
  302.                         $this->getGlobalContext()->getEffectiveAccount()
  303.                     );
  304.             }
  305.         );
  306.         // get the submissions
  307.         $submissions $this->getEntityManager()->getRepository(WorkflowSubmission::class)->search($search);
  308.         // handle lazyloading
  309.         if ($request->isXmlHttpRequest()) {
  310.             return $this->viewAjax(
  311.                 '@CmsWorkflows/Dashboard/Drafts/includes/submissions.html.twig',
  312.                 array(
  313.                     'search' => $search,
  314.                     'submissions' => $submissions,
  315.                 )
  316.             );
  317.         }
  318.         return $this->view(
  319.             '@CmsWorkflows/Dashboard/Drafts/submissions.html.twig',
  320.             array(
  321.                 'search' => $search,
  322.                 'submissions' => $submissions,
  323.             )
  324.         );
  325.     }
  326.     /**
  327.      * @param Request $request
  328.      * @return AjaxScene|DocumentScene
  329.      * @throws \Exception
  330.      *
  331.      * @Route(
  332.      *     "/publications",
  333.      *     name = DraftsController::ROUTES__PUBLICATIONS
  334.      * )
  335.      * @Route(
  336.      *     "/publications/lazyload",
  337.      *     name = DraftsController::ROUTES__PUBLICATIONS_LAZY
  338.      * )
  339.      */
  340.     public function publicationsAction(Request $request)
  341.     {
  342.         // do search logic to find applicable workflows
  343.         $search $this->getScheduledPublicationSearcher()->handleRequest($request);
  344.         if ($search instanceof RedirectResponse) {
  345.             return $search;
  346.         }
  347.         // get the scheduled publications
  348.         $publications $this->getEntityManager()->getRepository(ScheduledPublication::class)->search($search);
  349.         // handle lazyloading
  350.         if ($request->isXmlHttpRequest()) {
  351.             return $this->viewAjax(
  352.                 '@CmsWorkflows/Dashboard/Drafts/includes/publications.html.twig',
  353.                 array(
  354.                     'search' => $search,
  355.                     'publications' => $publications,
  356.                 )
  357.             );
  358.         }
  359.         return $this->view(
  360.             array(
  361.                 'search' => $search,
  362.                 'publications' => $publications,
  363.             )
  364.         );
  365.     }
  366.     /**
  367.      * @param string $id
  368.      * @return DocumentScene|RedirectResponse
  369.      * @throws \Exception
  370.      *
  371.      * @Route(
  372.      *     "/publication/{id}/cancel",
  373.      *     name = DraftsController::ROUTES__PUBLICATION_CANCEL,
  374.      *     requirements = {
  375.      *         "id" = "[1-9]\d*"
  376.      *     }
  377.      * )
  378.      */
  379.     public function publicationCancelAction($id)
  380.     {
  381.         // TODO: AUDIT
  382.         //$this->denyAccessUnlessGranted('campussuite.cms.workflows.publication_manage');
  383.         // load up the publication
  384.         $publication $this->getEntityManager()->getRepository(ScheduledPublication::class)->findExact($id);
  385.         // generate the form
  386.         $form $this->createForm(PublicationCancelType::class, [], []);
  387.         // process the form
  388.         if ($this->handleForm($form)) {
  389.             try {
  390.                 $this->getPublisher()->cancel($publication);
  391.                 // go back to the main listing page
  392.                 return $this->redirectToRoute(self::ROUTES__PUBLICATIONS);
  393.             } catch (\Exception $e) {
  394.                 $form->addError(new FormError($e->getMessage()));
  395.             }
  396.         }
  397.         return $this->view(array(
  398.             'publication' => $publication,
  399.             'form' => $form->createView(),
  400.         ));
  401.     }
  402.     /**
  403.      * @param string $id
  404.      * @return DocumentScene|RedirectResponse
  405.      * @throws \Exception
  406.      *
  407.      * @Route(
  408.      *     "/publication/{id}/reschedule",
  409.      *     name = DraftsController::ROUTES__PUBLICATION_RESCHEDULE,
  410.      *     requirements = {
  411.      *         "id" = "[1-9]\d*"
  412.      *     }
  413.      * )
  414.      */
  415.     public function publicationRescheduleAction($id)
  416.     {
  417.         // load up the publication
  418.         $publication $this->getEntityManager()->getRepository(ScheduledPublication::class)->findExact($id);
  419.         // generate the form
  420.         $form $this->createForm(
  421.             PublishActionType::class,
  422.             $publication
  423.         );
  424.         // process the form
  425.         if ($this->handleForm($form)) {
  426.             try {
  427.                 $this->getPublisher()->reschedule(
  428.                     $publication,
  429.                     $form->getData()->getPublishAt()
  430.                 );
  431.                 // go back to the main listing page
  432.                 return $this->redirectToRoute(self::ROUTES__PUBLICATIONS);
  433.             } catch (\Exception $e) {
  434.                 $form->addError(new FormError($e->getMessage()));
  435.             }
  436.         }
  437.         return $this->view(array(
  438.             'publication' => $publication,
  439.             'form' => $form->createView(),
  440.         ));
  441.     }
  442. }