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

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$template)
  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($template);
  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$template=null)
  70.     {
  71.         $content $this->contentTypeManager->getContent($request);
  72.         
  73.         if(!$content){
  74.             throw new NotFoundHttpException('Page not found');
  75.         }
  76.         $view $this->viewManager->load($template);
  77.         $event = new PageShowEvent($request$content);
  78.         $this->eventDispatcher->dispatch($eventEvents::PAGE_SHOW);
  79.         $view->content $content;
  80.         $view->pageTitle $content->getTitle();
  81.         $view->layout $this->themeManager->getLayout($routeDocument->getLayoutType());
  82.         return $view;
  83.     }
  84.     /**
  85.      * Get List of contents related to a category
  86.      *
  87.      * @param Request $request
  88.      * 
  89.      * @param [type] $routeDocument
  90.      * 
  91.      */
  92.     public function categoryAction(Request $requestRouteInterface $routeDocument$template)
  93.     {
  94.         $term $this->termRepository->findTermByRouteId($routeDocument->getId());
  95.         $pagination $this->contentRepository->findContentByTermId($term->getId());
  96.         
  97.         $view $this->viewManager->load($term->getTemplate() ? $term->getTemplate() : $template);
  98.         $view->pageTitle $routeDocument->getTitle();
  99.         $view->pagination $pagination;
  100.         $view->collection $pagination->getItems();
  101.         $view->term $term;
  102.         //$view->layout = $this->themeManager->getLayout($routeDocument->getLayoutType());
  103.         
  104.         return $view;
  105.     }
  106.     public function searchAction(Request $requestSerializerInterface $serializerstring $type)
  107.     {
  108.         $form $this->contentTypeManager->getSearchForm($type);
  109.         $page $request->get("page"1);
  110.         $form->handleRequest($request);
  111.         if($form->isSubmitted() && $form->isValid()){
  112.             $collection $this->contentTypeManager->filterResults($form->getData(), $page);
  113.             return new JsonResponse([
  114.                 "query" => $form->getData()["query"],
  115.                 "data" => $serializer->normalize($collection'json')
  116.             ]);
  117.         }
  118.         return new JsonResponse([
  119.             "status" => "error",
  120.             "message"=> "Something went wrong",
  121.         ]);
  122.     }
  123. }