<?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\PageShowEvent;
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, $template)
{
$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($template);
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, $template=null)
{
$content = $this->contentTypeManager->getContent($request);
if(!$content){
throw new NotFoundHttpException('Page not found');
}
$view = $this->viewManager->load($template);
$event = new PageShowEvent($request, $content);
$this->eventDispatcher->dispatch($event, Events::PAGE_SHOW);
$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, $template)
{
$term = $this->termRepository->findTermByRouteId($routeDocument->getId());
$pagination = $this->contentRepository->findContentByTermId($term->getId());
$view = $this->viewManager->load($term->getTemplate() ? $term->getTemplate() : $template);
$view->pageTitle = $routeDocument->getTitle();
$view->pagination = $pagination;
$view->collection = $pagination->getItems();
$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",
]);
}
}