src/Application/Controller/ProjectsController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Article;
  4. use App\Admin\Document\Category;
  5. use App\Admin\Document\Page;
  6. use App\Admin\Document\Project;
  7. use App\Admin\Document\Tag;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use \Symfony\Component\Routing\Annotation\Route;
  12. class ProjectsController extends AbstractController
  13. {
  14.     const URL 'gallery';
  15.     /**
  16.      * @Route("/gallery", name="gallery")
  17.      * @param Request $request
  18.      * @return \Symfony\Component\HttpFoundation\Response
  19.      */
  20.     public function index(Request $request)
  21.     {
  22.         $page $this->setSeoByUrl(self::URL$request->getLocale());
  23.         $params $this->getItemsParams($request->get('tag'));
  24.         return $this->render('application/projects/index.html.twig'array_merge($params, [
  25.             'page' => $page,
  26.             'tags' => $this->getDocumentRepository(Tag::class)->findBy(['url' => ['$exists' => true]], [$request->getLocale() == 'en' 'enName' 'name' => 'asc']),
  27.         ]));
  28.     }
  29.     /**
  30.      * @Route("/gallery/{url}", name="gallery/item")
  31.      * @param string $blogUrl
  32.      * @return \Symfony\Component\HttpFoundation\Response
  33.      * @throws \Doctrine\ODM\MongoDB\MongoDBException
  34.      */
  35.     public function item(Request $request$url)
  36.     {
  37.         /**
  38.          * @var Project $item
  39.          */
  40.         $item $this->getDocumentRepository(Project::class)->findOneBy(['active' => true'url' => $url]);
  41.         if (!$item) {
  42.             throw $this->createNotFoundException("Project with url '{$url}' not found or not active");
  43.         }
  44.         $this->setSeo($item$request->getLocale());
  45.         return $this->render('application/projects/item.html.twig', [
  46.             'item' => $item,
  47.             'otherProject' => $this->getDocumentRepository(Project::class)->findBy(['active' => true'_id' => ['$ne' => $item->getId()]], ['date' => 'asc'], 6),
  48.         ]);
  49.     }
  50.     private function getItemsParams($tagUrl) {
  51.         $qb $this->getDocumentRepository(Project::class)->createQueryBuilder();
  52.         $qb->field('active')->equals(true);
  53.         $qb->sort('date''asc');
  54.         if ($tagUrl) {
  55.             $tag $this->getDocumentRepository(Tag::class)->findOneBy(['url' => $tagUrl]);
  56.             if ($tag) {
  57.                 $qb->field('tags')->equals($tag->getId());
  58.             }
  59.         } else {
  60.             $tag null;
  61.         }
  62.         $pagination $this->paginate($qb);
  63.         $items $pagination->getItems();
  64.         return [
  65.             'items' => $items,
  66.             'pagination' => $pagination->getPaginationData(),
  67.             'tag' => $tag,
  68.         ];
  69.     }
  70.     /**
  71.      * @Route("/gallery-show-more", name="gallery-show-more")
  72.      * @return Response
  73.      */
  74.     public function showMore(Request $request): Response
  75.     {
  76.         $params $this->getItemsParams($request->get('tag'));
  77.         return new JsonResponse([
  78.             'html' => $this->renderView('application/projects/project-list.html.twig'$params),
  79.             'paginationHtml' => $this->renderView('application/partial/pagination.html.twig'$params),
  80.         ]);
  81.     }
  82. }