<?php
namespace Wart\CmsBundle\Controller\Front;
use Doctrine\ORM\QueryBuilder;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Wart\CmsBundle\Controller\DefaultFrontController;
use Wart\CmsBundle\Entity\Category;
use Wart\CmsBundle\Entity\ContentType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Wart\CmsBundle\Entity\FilterField;
use Wart\CmsBundle\Entity\UserComment;
use Wart\CmsBundle\Form\UserCommentType;
use Wart\CmsBundle\Repository\UniconBaseRepository;
/**
* Class UniconController
* @package Wart\CmsBundle\Controller\Front
* @Route("/")
*/
class UniconController extends DefaultFrontController{
/**
* @param Category $category, $material
* @ParamConverter("category", options={"mapping": {"catalias": "alias"}})
*
* @return Response
*/
public function materialAction(Request $request, Category $category, $material)
{
$this->ctype = $category->getCtype();
$this->category = $category;
$em = $this->getDoctrine()->getManager();
$WUIDService = $this->get('wart_cms.service.wuid');
$repo = $em->getRepository($this->ctype->getEntityName());
$material = $repo->findOneByAlias($material);
if(!$material || $material->getCategory() !== $category){
throw new NotFoundHttpException();
}
$this->material = $material;
$this->pages[] = 'material';
$this->pages[] = 'material-' . $this->ctype->getAlias();
$catRepo = $em->getRepository('WartCmsBundle:Category');
$path = $catRepo->getPath($category);
$commentForm = $this->createForm(UserCommentType::class, new UserComment(), [
'action' => $this->generateUrl('wart_cms_front_usercomment_index', [
'wuid' => $WUIDService->getId($material),
])
]);
return $this->render('item', [
'ctype' => $this->ctype,
'category' => $category,
'path' => $path,
'material' => $material,
'commentForm' => $commentForm->createView(),
]);
}
/**
* @param Category $category
* @param Integer $page
* @param Request $request
*
* @ParamConverter("category", options={"mapping": {"catalias": "alias"}})
* @return Response
*/
public function categoryAction(Category $category, $page, Request $request)
{
$this->ctype = $category->getCtype();
$this->category = $category;
$this->pages[] = 'category';
$this->pages[] = 'category-' . $this->ctype->getAlias();
$em = $this->getDoctrine()->getManager();
/** @var UniconBaseRepository $repo */
$repo = $em->getRepository($this->ctype->getEntityName());
// Set material order from category
$orderField = sprintf('mat.%s', $category->getOrderField());
$orderDirection = $category->getOrderDirection();
$order = [
$orderField => $orderDirection,
];
if ($filterName = $request->query->getAlnum('filterName')){
$filterService = $this->get('wart_cms.service.unicon_filter');
$filterService->setCategory($category);
$fields = $filterService->setFilterByAlias($filterName)->getFilter(true);
if($fields){
$activeFilters = [];
/** @var FilterField $filter */
foreach ($fields as $filter){
if ($filter->isActive()){
$activeFilters[] = $filter;
}
}
//Получим мега-запрос из генератора и попробуем получить по нему данные.
$materialsQuery = $filterService->getFilterQuery($this->ctype, $activeFilters);
}
}
else {
if ($this->ctype->isCatalog()){
$materialsQuery = $repo->findAllTreeQuery($category, $order);
}
else {
$materialsQuery = $repo->findByCategoryQuery($category, $order);
}
}
$categoryRepo = $em->getRepository('WartCmsBundle:Category');
$path = $categoryRepo->getCategoryParentTree($category);
$routepath = $categoryRepo->getCategoryPath($category, false);
preg_match('~page-(\d+)~', $page, $pageRes);
$page = $pageRes[1];
$limit = $this->getLimit($request);
$paginator = $this->get('knp_paginator');
/** @var SlidingPagination $pagination */
$pagination = $paginator->paginate(
$materialsQuery,
$page,
$limit
);
if ($category->getLvl() > 0) {
$pagination->setUsedRoute('wart_cms_front_unicon_category_pagination');
$pagination->setParam('catpath', $routepath);
}
else {
$this->pages[] = 'rootcat';
$this->pages[] = 'rootcat-' . $this->ctype->getAlias();
$pagination->setUsedRoute('wart_cms_front_unicon_rootcat_pagination');
}
return $this->render('category', array(
'ctype' => $this->ctype,
'category' => $category,
'path' => $path,
'materials' => $pagination
));
}
/**
* @Route("/search")
*/
public function searchAction(Request $request){
$needle = $request->request->get('qs');
$router = $this->get('wart_cms.routing.service');
$serializer = $this->get('serializer');
if (empty($needle)){
return new Response('Parameters is empty');
}
$em = $this->getDoctrine()->getManager();
$ctypeRepo = $em->getRepository('WartCmsBundle:ContentType');
/** @var ContentType $ctype */
$ctypes = $ctypeRepo->findBy([
'sellable' => true,
]);
$items = [];
foreach ($ctypes as $ctype){
/** @var UniconBaseRepository $repository */
$repository = $em->getRepository($ctype->getEntityName());
/** @var QueryBuilder $qb */
$qb = $repository->createQueryBuilder('m')
->join('m.offers', 'offer')
->addSelect('offer')
->leftJoin('m.category', 'category')
->addSelect('category')
->where('m.name like :needle')
->orWhere('offer.article like :needle')
->orWhere('offer.name like :needle')
->andWhere('m.status = 1')
->setParameter('needle', sprintf('%%%s%%', $needle))
->setMaxResults(20)
;
$query = $qb->getQuery();
$items = array_merge($items,$query->getResult());
}
if (!empty($items)){
$response = $serializer->serialize($items, 'json');
}
else {
$response = json_encode(['Nothing found']);
}
return new Response($response, 200, [
'Content-Type' => 'application/json',
]);
}
/**
* Возвращает количество материалов на странице
* @param Request $request
* Return items per page limit
* @return int|mixed
*/
protected function getLimit($request)
{
if ($this->ctype->getPaginated()){
$session = $request->getSession();
if ($inpage = $request->query->getInt('inpage')){
$limit = $inpage > 0 ? $inpage : $this->ctype->getInpage();
$session->set('inpage', $limit);
}
elseif ($inpage = $session->get('inpage')){
$limit = $inpage > 0 ? $inpage : $this->ctype->getInpage();
}
else {
$limit = $this->ctype->getInpage();
}
}
else {
$limit = 999;
}
return $limit;
}
}