<?php
namespace App\Application\Controller;
use App\Admin\Document\Article;
use App\Admin\Document\Build;
use App\Admin\Document\Category;
use App\Admin\Document\Page;
use App\Admin\Document\Tag;
use App\Application\Twig\GlobalLoader;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use \Symfony\Component\Routing\Annotation\Route;
class BuildController extends AbstractController
{
const URL = 'montazh';
/**
* @return int
*/
protected function getDefaultItemCountPerPage(){
return 9;
}
/**
* @Route("/montazh", name="montazh")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Request $request)
{
$page = $this->setSeoByUrl(self::URL, $request->getLocale());
$categoryList = [];
/**
* @var Category $category
*/
foreach ($this->getGlobal()->getMenu() as $category) {
$hasChildBuild = false;
foreach ($category->getChildren() as $child) {
$child['hasBuild'] = $this->hasBuild($child);
if ($child['hasBuild']) {
$hasChildBuild = true;
}
}
$category['hasBuild'] = $this->hasBuild($category);
$category['hasChildBuild'] = $hasChildBuild;
if ($category['hasBuild'] || $hasChildBuild) {
$categoryList[] = $category;
}
}
return $this->render('application/montazh/index.html.twig', [
'page' => $page,
'categoryList' => $categoryList
]);
}
private function hasBuild(Category $category) {
return $this->getDocumentRepository(Build::class)->findOneBy(['active' => true, 'categories' => $category->getId()]);
}
/**
* @Route("/montazh/{categoryUrl}", name="montazh/category")
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ODM\MongoDB\MongoDBException
*/
public function category(Request $request, $categoryUrl)
{
$page = $this->setSeoByUrl(self::URL, $request->getLocale());
$params = $this->getItemsParams($categoryUrl);
/**
* @var Category $category
*/
$category = $params['category'];
$categoryList = [];
if ($category->getParent()) {
foreach ($category->getParent()->getChildren() as $child) {
if ($this->hasBuild($child)) {
$categoryList[] = $child;
}
}
}
return $this->render('application/montazh/category.html.twig', array_merge($params, [
'page' => $page,
'categoryList' => $categoryList
]));
}
private function getItemsParams($categoryUrl) {
/**
* @var Category $category
*/
$category = $this->getDocumentRepository(Category::class)->findOneBy(['active' => true, 'url' => $categoryUrl]);
if (!$category) {
throw $this->createNotFoundException('Category not found or not active: ' . $categoryUrl);
}
$qb = $this->getDocumentRepository(Build::class)->createQueryBuilder();
$qb->field('active')->equals(true);
$qb->field('categories')->equals($category->getId());
$qb->sort('date', 'desc');
$pagination = $this->paginate($qb);
$items = $pagination->getItems();
return [
'items' => $items,
'pagination' => $pagination->getPaginationData(),
'category' => $category,
];
}
/**
* @Route("/montazh-show-more/{categoryUrl}", name="montazh/category-show-more")
* @return Response
*/
public function showMore(Request $request, $categoryUrl): Response
{
$params = $this->getItemsParams($categoryUrl);
return new JsonResponse([
'html' => $this->renderView('application/montazh/list.html.twig', $params),
'paginationHtml' => $this->renderView('application/partial/pagination.html.twig', $params),
]);
}
/**
* @Route("/montazh/{categoryUrl}/{url}", name="montazh/item")
* @param string $url
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ODM\MongoDB\MongoDBException
*/
public function item(Request $request, $categoryUrl, $url)
{
$category = $this->getDocumentRepository(Category::class)->findOneBy(['active' => true, 'url' => $categoryUrl]);
if (!$category) {
throw $this->createNotFoundException('Category not found or not active: ' . $categoryUrl);
}
/**
* @var Build $item
*/
$item = $this->getDocumentRepository(Build::class)->findOneBy(['active' => true, 'url' => $url, 'categories' => $category->getId()]);
if (!$item) {
throw $this->createNotFoundException("Build with url '{$url}' and category '{$categoryUrl}' not found or not active");
}
$this->setSeo($item, $request->getLocale());
return $this->render('application/montazh/item.html.twig', [
'item' => $item,
'category' => $item->getCategory()
]);
}
}