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

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