lib/boab/cms-bundle/src/Controller/ContentController.php line 81

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Boab\CmsBundle\Controller\BaseController;
  6. use Boab\CmsBundle\Manager\ContentTypeManagerInterface;
  7. use Boab\CmsBundle\Event\PageShowEvent;
  8. use Boab\CmsBundle\View\Annotation\Template;
  9. use Boab\CmsBundle\Repository\ContentRepositoryInterface;
  10. use Boab\CmsBundle\Repository\TermRepositoryInterface;
  11. use Boab\CmsBundle\Events;
  12. use Boab\CmsBundle\Entity\RouteInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. /**
  16.  * @template(theme="kantua")
  17.  */
  18. class ContentController extends BaseController
  19. {
  20.     private $contentRepository;
  21.     private $contentTypeManager;
  22.     private $termRepository;
  23.     public function __construct
  24.         ContentRepositoryInterface $contentRepository
  25.         ContentTypeManagerInterface $contentTypeManager
  26.         TermRepositoryInterface $termRepository
  27.     )
  28.     {
  29.         $this->contentRepository $contentRepository;
  30.         $this->contentTypeManager $contentTypeManager;
  31.         $this->termRepository $termRepository;
  32.     }
  33.     /**
  34.      * Get List of contents
  35.      *
  36.      * @param Request $request
  37.      * 
  38.      * @param [type] $routeDocument
  39.      * 
  40.      * @return void
  41.      */
  42.     public function listAction(Request $requestRouteInterface $routeDocument$contentTemplate)
  43.     {  
  44.         $pageNumber $request->get('page'1);
  45.         $form $this->contentTypeManager->getSearchForm($routeDocument->getContentType(),[
  46.             "action"=>$this->router->generate("app.search_content", ["type"=>$routeDocument->getContentType()])
  47.         ]);
  48.         $view $this->viewManager->load($contentTemplate);
  49.         if($form){
  50.             $view->form $form->createView();
  51.         }
  52.         $pagination $this->contentTypeManager->getCollection($request$pageNumber); 
  53.         $view->pagination $pagination;
  54.         $view->collection $pagination->getItems();
  55.         $view->routeDocument $routeDocument;
  56.         $view->pageTitle $routeDocument->getTitle();
  57.         $view->layout $this->themeManager->getLayout($routeDocument->getLayoutType());
  58.         return $view;
  59.     }
  60.     /**
  61.      * Get List of contents
  62.      *
  63.      * @param Request $request
  64.      * 
  65.      * @param [type] $routeDocument
  66.      * 
  67.      * @return void
  68.      */
  69.     public function showAction(Request $requestRouteInterface $routeDocument$contentTemplate)
  70.     {
  71.         $content $this->contentTypeManager->getContent($request);
  72.         if(!$content){
  73.             throw new NotFoundHttpException('Page not found');
  74.         }
  75.         $view $this->viewManager->load($contentTemplate);
  76.         $event = new PageShowEvent($request$content);
  77.         $this->eventDispatcher->dispatch($eventEvents::PAGE_SHOW);
  78.         $view->content $content;
  79.         $view->pageTitle $content->getTitle();
  80.         $view->layout $this->themeManager->getLayout($routeDocument->getLayoutType());
  81.         return $view;
  82.     }
  83.     /**
  84.      * Get List of contents related to a category
  85.      *
  86.      * @param Request $request
  87.      * 
  88.      * @param [type] $routeDocument
  89.      * 
  90.      */
  91.     public function categoryAction(Request $requestRouteInterface $routeDocument$contentTemplate)
  92.     {
  93.         $term $this->termRepository->findTermByRouteId($routeDocument->getId());
  94.         $pagination $this->contentRepository->findContentByTermId($term->getId());
  95.         
  96.         $view $this->viewManager->load($term->getTemplate() ? $term->getTemplate() : $contentTemplate);
  97.         $view->pageTitle $routeDocument->getTitle();
  98.         $view->pagination $pagination;
  99.         $view->collection $pagination->getItems();
  100.         $view->term $term;
  101.         //$view->layout = $this->themeManager->getLayout($routeDocument->getLayoutType());
  102.         
  103.         return $view;
  104.     }
  105.     public function searchAction(Request $requestSerializerInterface $serializerstring $type)
  106.     {
  107.         $form $this->contentTypeManager->getSearchForm($type);
  108.         $page $request->get("page"1);
  109.         $form->handleRequest($request);
  110.         if($form->isSubmitted() && $form->isValid()){
  111.             $collection $this->contentTypeManager->filterResults($form->getData(), $page);
  112.             return new JsonResponse([
  113.                 "query" => $form->getData()["query"],
  114.                 "data" => $serializer->normalize($collection'json')
  115.             ]);
  116.         }
  117.         return new JsonResponse([
  118.             "status" => "error",
  119.             "message"=> "Something went wrong",
  120.         ]);
  121.     }
  122. }