src/App/Controller/Dashboard/Websites/AbstractContentController.php line 128

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Dashboard\Websites;
  3. use App\Controller\AbstractController;
  4. use App\Controller\PaginationTrait;
  5. use App\Doctrine\Repository\Content\LegacyProxySearch;
  6. use App\Doctrine\Repository\Content\ObjectRepository;
  7. use App\Doctrine\Repository\Content\ObjectSearch;
  8. use App\Entity\Content\AbstractObject;
  9. use App\Form\Forms\DummyForm;
  10. use App\Form\Forms\Searching\LegacyProxySearchForm;
  11. use App\Form\Forms\Searching\ObjectSearchForm;
  12. use App\Model\Content\ObjectInterface;
  13. use App\Service\Content\ContentManager;
  14. use Cms\ModuleBundle\Service\ContentManager as LegacyContentManager;
  15. use App\Service\Feed\FeedManager;
  16. use Cms\ContainerBundle\Entity\Containers\GenericContainer;
  17. use Cms\FrontendBundle\Service\ResolverManager;
  18. use Cms\ModuleBundle\Controller\ContentController;
  19. use Cms\ModuleBundle\Doctrine\ProxyRepository;
  20. use Cms\ModuleBundle\Entity\Proxy;
  21. use Cms\ModuleBundle\Service\ModuleManager;
  22. use Doctrine\Common\Util\ClassUtils;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. /**
  27.  *
  28.  * @Route(
  29.  *     "/websites/content/posts",
  30.  * )
  31.  */
  32. abstract class AbstractContentController extends AbstractController
  33. {
  34.     use PaginationTrait;
  35.     protected const ENTRY_TYPE null;
  36.     /**
  37.      *
  38.      */
  39.     protected function listLogic(string $classint $pagination 0)
  40.     {
  41.         // check that class is legit
  42.         if ($class !== AbstractObject::class && ! is_subclass_of($classAbstractObject::class)) {
  43.             throw new \LogicException();
  44.         }
  45.         // get sites
  46.         $sites $this->getEntityManager()->getRepository(GenericContainer::class)->findAllByWithDomain();
  47.         // search form logic
  48.         $this->handleSearch(
  49.             $form $this->createForm(
  50.                 ObjectSearchForm::class,
  51.                 $search = (new ObjectSearch())
  52.                     ->setVisibility(null)
  53.                     ->setAccount($this->getGlobalContext()->getEffectiveAccount())
  54.             )
  55.         );
  56.         // querying
  57.         $repo $this->getEntityManager()->getRepository($class);
  58.         if ( ! $repo instanceof ObjectRepository) {
  59.             throw new \LogicException();
  60.         }
  61.         $items $repo->findBySearch(
  62.             $search,
  63.             $this->getPageSize($search),
  64.             $this->getPageOffset($pagination$search)
  65.         );
  66.         // determine if we are out of bounds on the pagination
  67.         if ($this->isPageOutOfBounds($items$pagination$search)) {
  68.             return $this->handlePageOutOfBounds($items$search);
  69.         }
  70.         return $this->html([
  71.             'sites' => $sites,
  72.             'form' => $form->createView(),
  73.             'search' => $search,
  74.             'pagination' => $this->generatePagination(
  75.                 $items,
  76.                 $pagination,
  77.                 $search
  78.             ),
  79.             'items' => $items,
  80.         ]);
  81.     }
  82.     /**
  83.      *
  84.      */
  85.     protected function listLegacyLogic(string $classint $pagination 0)
  86.     {
  87.         // check that class is legit
  88.         if ( ! is_subclass_of($classProxy::class)) {
  89.             throw new \LogicException();
  90.         }
  91.         // get sites
  92.         $sites $this->getEntityManager()->getRepository(GenericContainer::class)->findAllByWithDomain();
  93.         // search form logic
  94.         $this->handleSearch(
  95.             $form $this->createForm(
  96.                 LegacyProxySearchForm::class,
  97.                 $search = (new LegacyProxySearch())
  98.                     ->setVisibility(null)
  99.                     ->setAccount($this->getGlobalContext()->getEffectiveAccount())
  100.             )
  101.         );
  102.         // querying
  103.         $repo $this->getEntityManager()->getRepository($class);
  104.         if ( ! $repo instanceof ProxyRepository) {
  105.             throw new \LogicException();
  106.         }
  107.         $items $repo->schoolnow(
  108.             $search,
  109.             $this->getPageSize($search),
  110.             $this->getPageOffset($pagination$search)
  111.         );
  112.         // determine if we are out of bounds on the pagination
  113.         if ($this->isPageOutOfBounds($items$pagination$search)) {
  114.             return $this->handlePageOutOfBounds($items$search);
  115.         }
  116.         return $this->html([
  117.             'sites' => $sites,
  118.             'form' => $form->createView(),
  119.             'search' => $search,
  120.             'pagination' => $this->generatePagination(
  121.                 $items,
  122.                 $pagination,
  123.                 $search
  124.             ),
  125.             'items' => $items,
  126.         ]);
  127.     }
  128.     /**
  129.      *
  130.      */
  131.     protected function createLogic(AbstractObject $objectstring $formstring $jump)
  132.     {
  133.         // AUDIT
  134.         $this->denyAccessUnlessGranted(
  135.             [
  136.                 sprintf('campussuite.cms.container.%s.manage'$object->getDepartment()->getType()),
  137.                 'campussuite.cms.module.manage',
  138.             ],
  139.             [$object$object->getDepartment()],
  140.         );
  141.         // load department from object
  142.         if ( ! $object->getDepartment()) {
  143.             throw new \LogicException();
  144.         }
  145.         // find the school that is at play here, if any
  146.         $school $this->getResolverManager()->getSchoolResolver()->resolveSchoolByDepartment(
  147.             $object->getDepartment()
  148.         );
  149.         // create the form
  150.         $form $this->createForm(
  151.             $form,
  152.             $object,
  153.             [
  154.                 'boosting' => ( ! empty($school)),
  155.                 'boosting_locked' => ($school && $school->getDepartment() === $object->getDepartment()),
  156.                 'pinning' => false,
  157.             ]
  158.         );
  159.         // set boosting default
  160.         $form->get('boosted')->setData(
  161.             ( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
  162.                 ? FeedManager::BOOSTING[ClassUtils::getClass($object)]
  163.                 : true)
  164.         );
  165.         // set pinning default
  166.         $form->get('pinned')->setData(
  167.             ( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
  168.                 ? FeedManager::PINNING[ClassUtils::getClass($object)]
  169.                 : false)
  170.         );
  171.         // handle submission
  172.         if ($this->handleForm($form)) {
  173.             // set object as draft and save it
  174.             $this->getContentManager()->createObject(
  175.                 $object->setVisibility(ObjectInterface::VISIBILITIES__UNPUBLISHED),
  176.                 $form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET FeedManager::FLAGS__BOOST__UNSET,
  177.                 $form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET FeedManager::FLAGS__PIN__UNSET,
  178.             );
  179.             // determine if we are publishing and handle it if so
  180.             if ($form->get('action')->getData() === 'publish') {
  181.                 $this->getContentManager()->publishObject(
  182.                     $object,
  183.                     $form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET FeedManager::FLAGS__BOOST__UNSET,
  184.                     $form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET FeedManager::FLAGS__PIN__UNSET,
  185.                 );
  186.             }
  187.             $this->getLoggingService()->createLog($object);
  188.             return $this->jumpOrRedirectToRoute($jump);
  189.         }
  190.         return $this->html([
  191.             'department' => $object->getDepartment(),
  192.             'school' => $school,
  193.             'object' => $object,
  194.             'form' => $form->createView(),
  195.         ]);
  196.     }
  197.     /**
  198.      *
  199.      */
  200.     protected function updateLogic(AbstractObject $objectstring $formstring $jump)
  201.     {
  202.         // AUDIT
  203.         $this->denyAccessUnlessGranted(
  204.             [
  205.                 sprintf('campussuite.cms.container.%s.manage'$object->getDepartment()->getType()),
  206.                 'campussuite.cms.module.manage',
  207.             ],
  208.             [$object$object->getDepartment()],
  209.         );
  210.         // find the school that is at play here, if any
  211.         $school $this->getResolverManager()->getSchoolResolver()->resolveSchoolByDepartment(
  212.             $object->getDepartment()
  213.         );
  214.         // locate entry if any
  215.         $entry $this->getFeedManager()->lookup($object);
  216.         if ( ! $entry) {
  217.             throw new \RuntimeException();
  218.         }
  219.         // create the form
  220.         $form $this->createForm(
  221.             $form,
  222.             $object,
  223.             [
  224.                 'boosting' => ( ! empty($school)),
  225.                 'boosting_locked' => ($school && $school->getDepartment() === $object->getDepartment()),
  226.                 'pinning' => false,
  227.             ]
  228.         );
  229.         // set boosting default
  230.         if ($entry) {
  231.             $form->get('boosted')->setData($entry->isBoosted());
  232.         } else {
  233.             $form->get('boosted')->setData(
  234.                 ( ! $school) ? false : (($school->getDepartment() === $object->getDepartment())
  235.                     ? FeedManager::BOOSTING[ClassUtils::getClass($object)]
  236.                     : false)
  237.             );
  238.         }
  239.         // set pinning default
  240.         if ($entry) {
  241.             $form->get('pinned')->setData($entry->isPinned());
  242.         } else {
  243.             $form->get('pinned')->setData(FeedManager::PINNING[ClassUtils::getClass($object)]);
  244.             //$form->get('pinned')->setData(false);
  245.         }
  246.         // handle submission
  247.         if ($this->handleForm($form)) {
  248.             // set object as draft and save it
  249.             $this->getContentManager()->updateObject(
  250.                 $object,
  251.                 $form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET FeedManager::FLAGS__BOOST__UNSET,
  252.                 $form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET FeedManager::FLAGS__PIN__UNSET,
  253.             );
  254.             // determine if we are publishing and handle it if so
  255.             if ($form->get('action')->getData() === 'publish') {
  256.                 $this->getContentManager()->publishObject(
  257.                     $object,
  258.                     $form->get('boosted')->getData() ? FeedManager::FLAGS__BOOST__SET FeedManager::FLAGS__BOOST__UNSET,
  259.                     $form->get('pinned')->getData() ? FeedManager::FLAGS__PIN__SET FeedManager::FLAGS__PIN__UNSET,
  260.                 );
  261.             }
  262.             $this->getLoggingService()->createLog($object);
  263.             return $this->jumpOrRedirectToRoute($jump);
  264.         }
  265.         return $this->html([
  266.             'department' => $object->getDepartment(),
  267.             'school' => $school,
  268.             'object' => $object,
  269.             'entry' => $entry,
  270.             'form' => $form->createView(),
  271.         ]);
  272.     }
  273.     /**
  274.      * @param Proxy $object
  275.      * @param string $jump
  276.      * @return RedirectResponse
  277.      */
  278.     protected function updateLegacyLogic(Proxy $objectstring $jump): RedirectResponse
  279.     {
  280.         return $this->redirectToRoute(
  281.             ContentController::ROUTES__MODIFY_CONFLICTS,
  282.             [
  283.                 'container' => $object->getContainer()->getId(),
  284.                 'module' => $this->getModuleManager()->getModuleConfigurationForEntity($object)->key(),
  285.                 'proxy' => $object->getId(),
  286.                 'redirectTo' => $jump,
  287.             ]
  288.         );
  289.     }
  290.     /**
  291.      *
  292.      */
  293.     protected function deleteLogic(AbstractObject $objectstring $jump)
  294.     {
  295.         // make sure it is ajax
  296.         if ( ! $this->getRequest()->isXmlHttpRequest()) {
  297.             throw new NotFoundHttpException();
  298.         }
  299.         // AUDIT
  300.         $this->denyAccessUnlessGranted(
  301.             [
  302.                 sprintf('campussuite.cms.container.%s.manage'$object->getDepartment()->getType()),
  303.                 'campussuite.cms.module.manage',
  304.             ],
  305.             [$object$object->getDepartment()],
  306.         );
  307.         // create the form
  308.         $form $this->createForm(DummyForm::class);
  309.         // handle submission
  310.         if ($this->handleForm($form)) {
  311.             // delete the object
  312.             $id $object->getId();
  313.             $this->getContentManager()->deleteObject(
  314.                 $object
  315.             );
  316.             $this->getLoggingService()->createLog($object$id);
  317.             // send back redirect
  318.             return $this->jsonView([
  319.                 'redirect' => true,
  320.             ]);
  321.         }
  322.         return $this->ajax([
  323.             'object' => $object,
  324.             'form' => $form->createView(),
  325.         ]);
  326.     }
  327.     /**
  328.      *
  329.      */
  330.     protected function deleteLegacyLogic(Proxy $objectstring $jump)
  331.     {
  332.         // make sure it is ajax
  333.         if ( ! $this->getRequest()->isXmlHttpRequest()) {
  334.             throw new NotFoundHttpException();
  335.         }
  336.         // create the form
  337.         $form $this->createForm(DummyForm::class);
  338.         // handle submission
  339.         if ($this->handleForm($form)) {
  340.             // delete the object
  341.             $id $object->getId();
  342.             $this->getLegacyContentManager()->proxyDelete(
  343.                 $object
  344.             );
  345.             $this->getLoggingService()->createLog($object$id);
  346.             // send back redirect
  347.             return $this->jsonView([
  348.                 'redirect' => true,
  349.             ]);
  350.         }
  351.         return $this->ajax([
  352.             'object' => $object,
  353.             'form' => $form->createView(),
  354.         ]);
  355.     }
  356.     /**
  357.      * @return ResolverManager|object
  358.      */
  359.     protected function getResolverManager(): ResolverManager
  360.     {
  361.         return $this->get(__METHOD__);
  362.     }
  363.     /**
  364.      * @return ContentManager|object
  365.      */
  366.     protected function getContentManager(): ContentManager
  367.     {
  368.         return $this->get(__METHOD__);
  369.     }
  370.     protected function getLegacyContentManager(): LegacyContentManager
  371.     {
  372.         return $this->get(LegacyContentManager::class);
  373.     }
  374.     /**
  375.      * @return FeedManager|object
  376.      */
  377.     protected function getFeedManager(): FeedManager
  378.     {
  379.         return $this->get(__METHOD__);
  380.     }
  381.     /**
  382.      * @return ModuleManager|object
  383.      */
  384.     protected function getModuleManager(): ModuleManager
  385.     {
  386.         return $this->get(__METHOD__);
  387.     }
  388.     public static function getSubscribedServices(): array
  389.     {
  390.         return array_merge(
  391.             parent::getSubscribedServices(),
  392.             [
  393.                 LegacyContentManager::class => '?'.LegacyContentManager::class,
  394.             ],
  395.         );
  396.     }
  397. }