src/Application/Controller/BuildController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Article;
  4. use App\Admin\Document\Build;
  5. use App\Admin\Document\Category;
  6. use App\Admin\Document\Page;
  7. use App\Admin\Document\Tag;
  8. use App\Application\Twig\GlobalLoader;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use \Symfony\Component\Routing\Annotation\Route;
  13. class BuildController extends AbstractController
  14. {
  15.     const URL 'montazh';
  16.     /**
  17.      * @return int
  18.      */
  19.     protected function getDefaultItemCountPerPage(){
  20.         return 9;
  21.     }
  22.     /**
  23.      * @Route("/montazh", name="montazh")
  24.      * @param Request $request
  25.      * @return \Symfony\Component\HttpFoundation\Response
  26.      */
  27.     public function index(Request $request)
  28.     {
  29.         $page $this->setSeoByUrl(self::URL$request->getLocale());
  30.         $categoryList = [];
  31.         /**
  32.          * @var Category $category
  33.          */
  34.         foreach ($this->getGlobal()->getMenu() as $category) {
  35.             $hasChildBuild false;
  36.             foreach ($category->getChildren() as $child) {
  37.                 $child['hasBuild'] = $this->hasBuild($child);
  38.                 if ($child['hasBuild']) {
  39.                     $hasChildBuild true;
  40.                 }
  41.             }
  42.             $category['hasBuild'] = $this->hasBuild($category);
  43.             $category['hasChildBuild'] = $hasChildBuild;
  44.             if ($category['hasBuild'] || $hasChildBuild) {
  45.                 $categoryList[] = $category;
  46.             }
  47.         }
  48.         return $this->render('application/montazh/index.html.twig', [
  49.             'page' => $page,
  50.             'categoryList' => $categoryList
  51.         ]);
  52.     }
  53.     private function hasBuild(Category $category) {
  54.         return $this->getDocumentRepository(Build::class)->findOneBy(['active' => true'categories' => $category->getId()]);
  55.     }
  56.     /**
  57.      * @Route("/montazh/{categoryUrl}", name="montazh/category")
  58.      * @return \Symfony\Component\HttpFoundation\Response
  59.      * @throws \Doctrine\ODM\MongoDB\MongoDBException
  60.      */
  61.     public function category(Request $request$categoryUrl)
  62.     {
  63.         $page $this->setSeoByUrl(self::URL$request->getLocale());
  64.         $params $this->getItemsParams($categoryUrl);
  65.         /**
  66.          * @var Category $category
  67.          */
  68.         $category $params['category'];
  69.         $categoryList = [];
  70.         if ($category->getParent()) {
  71.             foreach ($category->getParent()->getChildren() as $child) {
  72.                 if ($this->hasBuild($child)) {
  73.                     $categoryList[] = $child;
  74.                 }
  75.             }
  76.         }
  77.         return $this->render('application/montazh/category.html.twig'array_merge($params, [
  78.             'page' => $page,
  79.             'categoryList' => $categoryList
  80.         ]));
  81.     }
  82.     private function getItemsParams($categoryUrl) {
  83.         /**
  84.          * @var Category $category
  85.          */
  86.         $category $this->getDocumentRepository(Category::class)->findOneBy(['active' => true'url' => $categoryUrl]);
  87.         if (!$category) {
  88.             throw $this->createNotFoundException('Category not found or not active: ' $categoryUrl);
  89.         }
  90.         $qb $this->getDocumentRepository(Build::class)->createQueryBuilder();
  91.         $qb->field('active')->equals(true);
  92.         $qb->field('categories')->equals($category->getId());
  93.         $qb->sort('date''desc');
  94.         $pagination $this->paginate($qb);
  95.         $items $pagination->getItems();
  96.         return [
  97.             'items' => $items,
  98.             'pagination' => $pagination->getPaginationData(),
  99.             'category' => $category,
  100.         ];
  101.     }
  102.     /**
  103.      * @Route("/montazh-show-more/{categoryUrl}", name="montazh/category-show-more")
  104.      * @return Response
  105.      */
  106.     public function showMore(Request $request$categoryUrl): Response
  107.     {
  108.         $params $this->getItemsParams($categoryUrl);
  109.         return new JsonResponse([
  110.             'html' => $this->renderView('application/montazh/list.html.twig'$params),
  111.             'paginationHtml' => $this->renderView('application/partial/pagination.html.twig'$params),
  112.         ]);
  113.     }
  114.     /**
  115.      * @Route("/montazh/{categoryUrl}/{url}", name="montazh/item")
  116.      * @param string $url
  117.      * @return \Symfony\Component\HttpFoundation\Response
  118.      * @throws \Doctrine\ODM\MongoDB\MongoDBException
  119.      */
  120.     public function item(Request $request$categoryUrl$url)
  121.     {
  122.         $category $this->getDocumentRepository(Category::class)->findOneBy(['active' => true'url' => $categoryUrl]);
  123.         if (!$category) {
  124.             throw $this->createNotFoundException('Category not found or not active: ' $categoryUrl);
  125.         }
  126.         /**
  127.          * @var Build $item
  128.          */
  129.         $item $this->getDocumentRepository(Build::class)->findOneBy(['active' => true'url' => $url'categories' => $category->getId()]);
  130.         if (!$item) {
  131.             throw $this->createNotFoundException("Build with url '{$url}' and category '{$categoryUrl}' not found or not active");
  132.         }
  133.         $this->setSeo($item$request->getLocale());
  134.         return $this->render('application/montazh/item.html.twig', [
  135.             'item' => $item,
  136.             'category' => $item->getCategory()
  137.         ]);
  138.     }
  139. }