<?php
namespace Boab\CmsBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Boab\CmsBundle\Controller\BaseController;
use Boab\CmsBundle\Manager\ContentTypeManagerInterface;
use Boab\CmsBundle\Event\PageRenderEvent;
use Boab\CmsBundle\View\Annotation\Template;
use Boab\CmsBundle\Repository\ContentRepositoryInterface;
use Boab\CmsBundle\Repository\TermRepositoryInterface;
use Boab\CmsBundle\Events;
use Boab\CmsBundle\Entity\RouteInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @template(theme="kantua")
*/
class ContentController extends BaseController
{
private $contentRepository;
private $contentTypeManager;
private $termRepository;
public function __construct(
ContentRepositoryInterface $contentRepository,
ContentTypeManagerInterface $contentTypeManager,
TermRepositoryInterface $termRepository
)
{
$this->contentRepository = $contentRepository;
$this->contentTypeManager = $contentTypeManager;
$this->termRepository = $termRepository;
}
/**
* Get List of contents
*
* @param Request $request
*
* @param [type] $routeDocument
*
* @return void
*/
public function listAction(Request $request, RouteInterface $routeDocument, $contentTemplate)
{
$pageNumber = $request->get('page', 1);
$form = $this->contentTypeManager->getSearchForm($routeDocument->getContentType(),[
"action"=>$this->router->generate("app.search_content", ["type"=>$routeDocument->getContentType()])
]);
$view = $this->viewManager->load($contentTemplate);
if($form){
$view->form = $form->createView();
}
$pagination = $this->contentTypeManager->getCollection($request, $pageNumber);
$view->pagination = $pagination;
$view->collection = $pagination->getItems();
$view->routeDocument = $routeDocument;
$view->pageTitle = $routeDocument->getTitle();
$view->layout = $this->themeManager->getLayout($routeDocument->getLayoutType());
return $view;
}
/**
* Get List of contents
*
* @param Request $request
*
* @param [type] $routeDocument
*
* @return void
*/
public function showAction(Request $request, RouteInterface $routeDocument, $contentTemplate)
{
$content = $this->contentTypeManager->getContent($request);
if(!$content){
throw new NotFoundHttpException('Page not found');
}
$view = $this->viewManager->load($contentTemplate);
$event = new PageRenderEvent($request, $content);
$this->eventDispatcher->dispatch($event, Events::PAGE_RENDER);
$view->content = $content;
$view->pageTitle = $content->getTitle();
$view->layout = $this->themeManager->getLayout($routeDocument->getLayoutType());
return $view;
}
/**
* Get List of contents related to a category
*
* @param Request $request
*
* @param [type] $routeDocument
*
*/
public function categoryAction(Request $request, RouteInterface $routeDocument, $contentTemplate)
{
$term = $this->termRepository->findTermByRouteId($routeDocument->getId());
$collection = $this->contentRepository->findContentByTermId($term->getId());
$view = $this->viewManager->load($contentTemplate);
$view->pageTitle = $routeDocument->getTitle();
$view->collection = $collection;
$view->term = $term;
$view->layout = $this->themeManager->getLayout($routeDocument->getLayoutType());
return $view;
}
public function searchAction(Request $request, SerializerInterface $serializer, string $type)
{
$form = $this->contentTypeManager->getSearchForm($type);
$page = $request->get("page", 1);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$collection = $this->contentTypeManager->filterResults($form->getData(), $page);
return new JsonResponse([
"query" => $form->getData()["query"],
"data" => $serializer->normalize($collection, 'json')
]);
}
return new JsonResponse([
"status" => "error",
"message"=> "Something went wrong",
]);
}
}