src/Cms/ModuleBundle/Controller/Dashboard/ModalController.php line 122

Open in your IDE?
  1. <?php
  2. namespace Cms\ModuleBundle\Controller\Dashboard;
  3. use Cms\ContainerBundle\Entity\Container;
  4. use Cms\ContainerBundle\Entity\Containers\PersonalContainer;
  5. use Cms\CoreBundle\Model\Scenes\DashboardScenes\AjaxScene;
  6. use Cms\CoreBundle\Util\DateTimeUtils;
  7. use Cms\CoreBundle\Util\ModalsController;
  8. use Cms\ModuleBundle\Doctrine\ProxyRepository;
  9. use Cms\ModuleBundle\Entity\History;
  10. use Cms\ModuleBundle\Entity\Proxy;
  11. use Cms\ModuleBundle\Service\ModuleManager;
  12. use Cms\WorkflowsBundle\Service\WorkflowsManager;
  13. use Doctrine\Common\Util\ClassUtils;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. /**
  18.  * Class ModalController
  19.  * @package Cms\ModuleBundle\Controller\Dashboard
  20.  */
  21. final class ModalController extends ModalsController
  22. {
  23.     const ROUTES__START 'campussuite.cms.module.dashboard.modal.start';
  24.     const ROUTES__CONTAINER 'campussuite.cms.module.dashboard.modal.container';
  25.     const ROUTES__MODULE 'campussuite.cms.module.dashboard.modal.module';
  26.     const ROUTES__CREATE 'campussuite.cms.module.dashboard.modal.create';
  27.     const SESSION__LAST_LOCATION 'campussuite.cms.content.modal.last_location';
  28.     /**
  29.      * @return array|string[]
  30.      */
  31.     private function validModules()
  32.     {
  33.         return array(
  34.             'page' => 'Pages',
  35.         );
  36.     }
  37.     /**
  38.      * @return AjaxScene|RedirectResponse
  39.      * @throws \Exception
  40.      *
  41.      * @Route(
  42.      *  "/",
  43.      *  name = ModalController::ROUTES__START
  44.      * )
  45.      */
  46.     public function startAction()
  47.     {
  48.         // get params
  49.         $last null;
  50.         // attempt to get container
  51.         $container null;
  52.         if ( ! empty($this->getModalParam('containerId'))) {
  53.             $container $this->getEntityManager()
  54.                 ->getRepository(Container::class)
  55.                 ->find($this->getModalParam('containerId'));
  56.         }
  57.         // attempt to determine if there is a last location thing set
  58.         $lastId $this->getSession()->get(self::SESSION__LAST_LOCATION);
  59.         if ( ! empty($lastId)) {
  60.             $lastContainer $this->getEntityManager()->getRepository(Container::class)->find($lastId);
  61.             if ( ! empty($lastContainer)) {
  62.                 $container $lastContainer;
  63.             }
  64.         }
  65.         // generate redirect based on information
  66.         if ($last instanceof Container && ! empty($container) && $last->getRoot() === $container->getRoot()) {
  67.             // redirect to the root view of the container
  68.             return $this->redirectToRoute(
  69.                 self::ROUTES__CONTAINER,
  70.                 array(
  71.                     'containerId' => $last->getId(),
  72.                 )
  73.             );
  74.         } else if ( ! empty($container)) {
  75.             // redirect to the root view of the container
  76.             return $this->redirectToRoute(
  77.                 self::ROUTES__CONTAINER,
  78.                 array(
  79.                     'containerId' => $container->getId(),
  80.                 )
  81.             );
  82.         }
  83.         // get all the containers in a hierarchy
  84.         $containers $this->getEntityManager()->getRepository(Container::class)->findAllHierarchy();
  85.         // show view
  86.         return $this->viewAjax(
  87.             array(
  88.                 'containers' => $containers,
  89.             )
  90.         );
  91.     }
  92.     /**
  93.      * @param string $containerId
  94.      * @return RedirectResponse
  95.      * @throws \Exception
  96.      *
  97.      * @Route(
  98.      *  "/{containerId}",
  99.      *  name = ModalController::ROUTES__CONTAINER,
  100.      *  requirements = {
  101.      *      "containerId" = "[1-9]\d*"  
  102.      *  }
  103.      * )
  104.      */
  105.     public function containerAction($containerId)
  106.     {
  107.         // grab container
  108.         $container $this->getEntityManager()->getRepository(Container::class)->findExact($containerId);
  109.         // get modules
  110.         $modules $this->validModules();
  111.         // get default module to jump to
  112.         if (count($modules) === 1) {
  113.             reset($modules);
  114.             $moduleKey key($modules);
  115.         } else {
  116.             $moduleKey 'page';
  117.         }
  118.         // set the last location
  119.         $this->getSession()->set(self::SESSION__LAST_LOCATION$container->getId());
  120.         // get module info
  121.         $module $this->getModuleManager()->getModuleConfiguration($moduleKey);
  122.         
  123.         // done, redirect to page of content listing
  124.         return $this->redirectToRoute(
  125.             self::ROUTES__MODULE,
  126.             array(
  127.                 'containerId' => $container->getId(),
  128.                 'moduleKey' => $module->key(),
  129.                 'dataType' => $module->types()[0],
  130.             )
  131.         );
  132.     }
  133.     /**
  134.      * @param string $containerId
  135.      * @param string $moduleKey
  136.      * @param string $dataType
  137.      * @return AjaxScene
  138.      * @throws \Exception
  139.      *
  140.      * @Route(
  141.      *  "/{containerId}/{moduleKey}/{dataType}",
  142.      *  name = ModalController::ROUTES__MODULE,
  143.      *  requirements = {
  144.      *      "containerId" = "[1-9]\d*",
  145.      *      "moduleKey" = "[a-zA-Z]+",
  146.      *      "dataType" = "[a-zA-Z]+"
  147.      *  }
  148.      * )
  149.      */
  150.     public function moduleAction($containerId$moduleKey$dataType)
  151.     {
  152.         // get the container we are relative to
  153.         $relative $this->getEntityManager()->getRepository(Container::class)->findExact(
  154.             ( ! empty($this->getModalParam('containerId'))) ? $this->getModalParam('containerId') : $containerId
  155.         );
  156.         // get all the containers in a hierarchy
  157.         $containers $this->assembleDepartments($relative);
  158.         // grab container
  159.         $container $this->getEntityManager()->getRepository(Container::class)->findExact($containerId);
  160.         // make sure it is a valid module
  161.         if ( ! in_array($moduleKeyarray_keys($this->validModules()))) {
  162.             throw new \Exception();
  163.         }
  164.         
  165.         // get the module config for the given module
  166.         $module $this->getModuleManager()->getModuleConfiguration($moduleKey);
  167.         
  168.         // get the proxy type
  169.         $type $module->classesProxy($dataType);
  170.         
  171.         // use proxy class to get repository
  172.         /** @var ProxyRepository $repo */
  173.         $repo $this->getRepository($type);
  174.         
  175.         // get the items for our container
  176.         $items $repo->findAllForContainer($container);
  177.         // we should have all that we need
  178.         return $this->viewAjax(
  179.             array(
  180.                 'containers' => $containers,
  181.                 'ancestors' => $this->getEntityManager()->getRepository(Container::class)->findAncestors($container),
  182.                 'container' => $container,
  183.                 'relative' => $relative,
  184.                 'module' => $module,
  185.                 'items' => $items,
  186.                 'workflowSkipper' => (
  187.                         ( ! $this->getWorkflowsManager()->hasWorkflow($container))
  188.                     ||
  189.                         ( ! $this->getWorkflowsManager()->needsApproval(
  190.                             $this->getGlobalContext()->getEffectiveAccount(),
  191.                             $container
  192.                         ))
  193.                 ),
  194.             )
  195.         );
  196.     }
  197.     /**
  198.      * @param Request $request
  199.      * @param string $containerId
  200.      * @param string $moduleKey
  201.      * @param string $dataType
  202.      * @return AjaxScene|RedirectResponse
  203.      * @throws \Exception
  204.      *
  205.      * @Route(
  206.      *  "/{containerId}/{moduleKey}/{dataType}/create",
  207.      *  name = ModalController::ROUTES__CREATE,
  208.      *  requirements = {
  209.      *      "containerId" = "[1-9]\d*",
  210.      *      "moduleKey" = "[a-zA-Z]+",
  211.      *      "dataType" = "[a-zA-Z]+"
  212.      *  }
  213.      * )
  214.      */
  215.     public function createAction(Request $request$containerId$moduleKey$dataType)
  216.     {
  217.         // grab container
  218.         $container $this->getEntityManager()->getRepository(Container::class)->findExact($containerId);
  219.         $containers $this->assembleDepartments($container);
  220.         // make sure it is a valid module
  221.         if ( ! in_array($moduleKeyarray_keys($this->validModules()))) {
  222.             throw new \Exception();
  223.         }
  224.         // get the module config for the given module
  225.         $module $this->getModuleManager()->getModuleConfiguration($moduleKey);
  226.         // AUDIT
  227.         $this->denyAccessUnlessGranted(
  228.             array_filter(array(
  229.                 sprintf(
  230.                     'campussuite.cms.container.%s.manage',
  231.                     $container->getType()
  232.                 ),
  233.                 'campussuite.cms.module.manage',
  234.                 sprintf(
  235.                     'campussuite.cms.modules.%s.manage',
  236.                     $module->key()
  237.                 ),
  238.             )),
  239.             array(null$container)
  240.         );
  241.         // get the proxy type
  242.         $type $module->classesProxy($dataType);
  243.         // create an object of this type
  244.         /** @var Proxy $proxy */
  245.         $proxy = new $type();
  246.         $data $proxy->getData();
  247.         // generate form
  248.         $form $this->createForm($module->formClass($data), $data, array(
  249.             'minimal' => true,
  250.             'create' => true,
  251.             'container' => $container,
  252.         ));
  253.         // handle submission
  254.         if ($this->handleForm($form$request)) {
  255.             // generate the history item
  256.             $history $module->classesHistory($dataType);
  257.             /** @var History $history */
  258.             $history = new $history();
  259.             $history
  260.                 ->setData($proxy->getData())
  261.                 ->setProxy($proxy)
  262.                 ->setTimestamp(DateTimeUtils::now());
  263.             // set things on proxy that are needed
  264.             $proxy
  265.                 ->setContainer($container)
  266.                 ->setHistory($history);
  267.             // save them
  268.             $this->getEntityManager()->saveAll(array(
  269.                 $proxy,
  270.                 $history,
  271.             ));
  272.             // record log
  273.             $this->getActivityLogger()->createLog($proxy);
  274.             // done
  275.             return $this->redirectToRoute(
  276.                 self::ROUTES__MODULE,
  277.                 array(
  278.                     'containerId' => $container->getId(),
  279.                     'moduleKey' => $module->key(),
  280.                     'dataType' => $proxy->type(),
  281.                 )
  282.             );
  283.         }
  284.         // show form
  285.         return $this->viewAjax(
  286.             array(
  287.                 'containers' => $containers,
  288.                 'container' => $container,
  289.                 'ancestors' => $this->getEntityManager()->getRepository(Container::class)->findAncestors($container),
  290.                 'module' => $module,
  291.                 'form' => $form->createView(),
  292.             )
  293.         );
  294.     }
  295.     /**
  296.      * @param Container $current
  297.      * @return array|Container[]
  298.      */
  299.     private function assembleDepartments(Container $current)
  300.     {
  301.         // query based on class
  302.         // get only ones in same root
  303.         // and order by the left values
  304.         /** @var array|Container[] $containers */
  305.         $containers $this->getEntityManager()->getRepository(ClassUtils::getClass($current))->findBy(
  306.             array(
  307.                 'rt' => $current->getRoot(),
  308.             ),
  309.             array(
  310.                 'lft' => 'ASC',
  311.             )
  312.         );
  313.         // TODO: do we need to do the processing for hidden stuff???
  314.         return $containers;
  315.         /*
  316.         // loop over them to process whether they are hidden
  317.         $showable = [];
  318.         $root = null;
  319.         $left = null;
  320.         $right = null;
  321.         foreach ($containers as $container) {
  322.             // if we have tracked a hidden node, we need to do some other checks
  323.             if ($root !== null) {
  324.                 // if the current is not outside our tracked node, we need to clear tracking and re-check for hidden stuff
  325.                 // otherwise, we know this is a child node, can assume that this node is hidden, and can skip to the next entry
  326.                 if ($container->getRoot() !== $root || $container->getRight() < $left || $container->getLeft() > $right) {
  327.                     $root = null;
  328.                     $left = null;
  329.                     $right = null;
  330.                 } else {
  331.                     continue;
  332.                 }
  333.             }
  334.             // if we are here, we should not be tracking anything yet
  335.             // check whether the node is hidden
  336.             // if it is, start tracking and move to the next entry
  337.             // otherwise, we should have a visible node
  338.             if ($container->isHidden()) {
  339.                 $root = $container->getRoot();
  340.                 $left = $container->getLeft();
  341.                 $right = $container->getRight();
  342.             } else {
  343.                 $showable[] = $container;
  344.             }
  345.         }
  346.         // send back an array of things that we have determined should be visible
  347.         return $showable;
  348.         */
  349.     }
  350.     /**
  351.      * @return ModuleManager|object
  352.      */
  353.     private function getModuleManager(): ModuleManager
  354.     {
  355.         return $this->get(__METHOD__);
  356.     }
  357.     /**
  358.      * @return WorkflowsManager|object
  359.      */
  360.     private function getWorkflowsManager(): WorkflowsManager
  361.     {
  362.         return $this->get(__METHOD__);
  363.     }
  364. }