vendor/wart/cms-core-bundle/Controller/Front/UniconController.php line 44

Open in your IDE?
  1. <?php
  2. namespace Wart\CmsBundle\Controller\Front;
  3. use Doctrine\ORM\QueryBuilder;
  4. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Wart\CmsBundle\Controller\DefaultFrontController;
  12. use Wart\CmsBundle\Entity\Category;
  13. use Wart\CmsBundle\Entity\ContentType;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  15. use Wart\CmsBundle\Entity\FilterField;
  16. use Wart\CmsBundle\Entity\UserComment;
  17. use Wart\CmsBundle\Form\UserCommentType;
  18. use Wart\CmsBundle\Repository\UniconBaseRepository;
  19. /**
  20.  * Class UniconController
  21.  * @package Wart\CmsBundle\Controller\Front
  22.  * @Route("/")
  23.  */
  24. class UniconController extends DefaultFrontController{
  25.     /**
  26.      * @param Category $category, $material
  27.      * @ParamConverter("category", options={"mapping": {"catalias": "alias"}})
  28.      *
  29.      * @return Response
  30.      */
  31.     public function materialAction(Request $requestCategory $category$material)
  32.     {
  33.         $this->ctype $category->getCtype();
  34.         $this->category $category;
  35.         $em $this->getDoctrine()->getManager();
  36.         $WUIDService $this->get('wart_cms.service.wuid');
  37.         $repo $em->getRepository($this->ctype->getEntityName());
  38.         $material $repo->findOneByAlias($material);
  39.         if(!$material || $material->getCategory() !== $category){
  40.             throw new NotFoundHttpException();
  41.         }
  42.         $this->material $material;
  43.         $this->pages[] = 'material';
  44.         $this->pages[] = 'material-' $this->ctype->getAlias();
  45.         $catRepo $em->getRepository('WartCmsBundle:Category');
  46.         $path $catRepo->getPath($category);
  47.         $commentForm $this->createForm(UserCommentType::class, new UserComment(), [
  48.             'action' => $this->generateUrl('wart_cms_front_usercomment_index', [
  49.                 'wuid' => $WUIDService->getId($material),
  50.             ])
  51.         ]);
  52.         return $this->render('item', [
  53.             'ctype' => $this->ctype,
  54.             'category' => $category,
  55.             'path' => $path,
  56.             'material' => $material,
  57.             'commentForm' => $commentForm->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * @param Category $category
  62.      * @param Integer $page
  63.      * @param Request $request
  64.      *
  65.      * @ParamConverter("category", options={"mapping": {"catalias": "alias"}})
  66.      * @return Response
  67.      */
  68.     public function categoryAction(Category $category$pageRequest $request)
  69.     {
  70.         $this->ctype $category->getCtype();
  71.         $this->category $category;
  72.         $this->pages[] = 'category';
  73.         $this->pages[] = 'category-' $this->ctype->getAlias();
  74.         $em $this->getDoctrine()->getManager();
  75.         /** @var UniconBaseRepository $repo */
  76.         $repo $em->getRepository($this->ctype->getEntityName());
  77.         // Set material order from category
  78.         $orderField sprintf('mat.%s'$category->getOrderField());
  79.         $orderDirection $category->getOrderDirection();
  80.         $order = [
  81.             $orderField => $orderDirection,
  82.         ];
  83.         if ($filterName $request->query->getAlnum('filterName')){
  84.             $filterService $this->get('wart_cms.service.unicon_filter');
  85.             $filterService->setCategory($category);
  86.             $fields $filterService->setFilterByAlias($filterName)->getFilter(true);
  87.             if($fields){
  88.                 $activeFilters = [];
  89.                 /** @var FilterField $filter */
  90.                 foreach ($fields as $filter){
  91.                     if ($filter->isActive()){
  92.                         $activeFilters[] = $filter;
  93.                     }
  94.                 }
  95.                 //Получим мега-запрос из генератора и попробуем получить по нему данные.
  96.                 $materialsQuery $filterService->getFilterQuery($this->ctype$activeFilters);
  97.             }
  98.         }
  99.         else {
  100.             if ($this->ctype->isCatalog()){
  101.                 $materialsQuery $repo->findAllTreeQuery($category$order);
  102.             }
  103.             else {
  104.                 $materialsQuery $repo->findByCategoryQuery($category$order);
  105.             }
  106.         }
  107.         $categoryRepo $em->getRepository('WartCmsBundle:Category');
  108.         $path $categoryRepo->getCategoryParentTree($category);
  109.         $routepath $categoryRepo->getCategoryPath($categoryfalse);
  110.         preg_match('~page-(\d+)~'$page$pageRes);
  111.         $page $pageRes[1];
  112.         $limit $this->getLimit($request);
  113.         $paginator  $this->get('knp_paginator');
  114.         /** @var SlidingPagination $pagination */
  115.         $pagination $paginator->paginate(
  116.             $materialsQuery,
  117.             $page,
  118.             $limit
  119.         );
  120.         if ($category->getLvl() > 0) {
  121.             $pagination->setUsedRoute('wart_cms_front_unicon_category_pagination');
  122.             $pagination->setParam('catpath'$routepath);
  123.         }
  124.         else {
  125.             $this->pages[] = 'rootcat';
  126.             $this->pages[] = 'rootcat-' $this->ctype->getAlias();
  127.             $pagination->setUsedRoute('wart_cms_front_unicon_rootcat_pagination');
  128.         }
  129.         return $this->render('category', array(
  130.             'ctype' => $this->ctype,
  131.             'category' => $category,
  132.             'path' => $path,
  133.             'materials' => $pagination
  134.         ));
  135.     }
  136.     
  137.     /**
  138.      * @Route("/search")
  139.      */
  140.     public function searchAction(Request $request){
  141.         $needle $request->request->get('qs');
  142.         $router $this->get('wart_cms.routing.service');
  143.         $serializer $this->get('serializer');
  144.         
  145.         if (empty($needle)){
  146.             return new Response('Parameters is empty');
  147.         }
  148.         $em $this->getDoctrine()->getManager();
  149.         $ctypeRepo $em->getRepository('WartCmsBundle:ContentType');
  150.         /** @var ContentType $ctype */
  151.         $ctypes $ctypeRepo->findBy([
  152.             'sellable' => true,
  153.         ]);
  154.         $items = [];
  155.         foreach ($ctypes as  $ctype){
  156.             /** @var UniconBaseRepository $repository */
  157.             $repository $em->getRepository($ctype->getEntityName());
  158.             /** @var QueryBuilder $qb */
  159.             $qb $repository->createQueryBuilder('m')
  160.                 ->join('m.offers''offer')
  161.                 ->addSelect('offer')
  162.                 ->leftJoin('m.category''category')
  163.                 ->addSelect('category')
  164.                 ->where('m.name like :needle')
  165.                 ->orWhere('offer.article like :needle')
  166.                 ->orWhere('offer.name like :needle')
  167.                 ->andWhere('m.status = 1')
  168.                 ->setParameter('needle'sprintf('%%%s%%'$needle))
  169.                 ->setMaxResults(20)
  170.             ;
  171.             
  172.             $query $qb->getQuery();
  173.             $items array_merge($items,$query->getResult());
  174.         }
  175.         if (!empty($items)){
  176.             $response $serializer->serialize($items'json');
  177.         }
  178.         else {
  179.             $response json_encode(['Nothing found']);
  180.         }
  181.         
  182.         return new Response($response200, [
  183.             'Content-Type' => 'application/json',
  184.         ]);
  185.         
  186.     }
  187.     /**
  188.      * Возвращает количество материалов на странице
  189.      * @param Request $request
  190.      * Return items per page limit
  191.      * @return int|mixed
  192.      */
  193.     protected function getLimit($request)
  194.     {
  195.         if ($this->ctype->getPaginated()){
  196.             $session $request->getSession();
  197.             if ($inpage $request->query->getInt('inpage')){
  198.                 $limit $inpage $inpage $this->ctype->getInpage();
  199.                 $session->set('inpage'$limit);
  200.             }
  201.             elseif ($inpage $session->get('inpage')){
  202.                 $limit $inpage $inpage $this->ctype->getInpage();
  203.             }
  204.             else {
  205.                 $limit $this->ctype->getInpage();
  206.             }
  207.         }
  208.         else {
  209.             $limit 999;
  210.         }
  211.         return $limit;
  212.     }
  213. }