<?php
namespace App\Package\Openform\Front\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Package\Openform\Front\Controller\BaseController;
use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Intl\Locales;
use App\Package\Openform\Tools\FrontUtil\FrontUtil;
/**
* Description of SitemapController
*
* @author ostol
*/
class SitemapController extends BaseController {
protected $routeLocalizer;
private $em;
private $frontUtil;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em, FrontUtil $frontUtil) {
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->frontUtil = $frontUtil;
}
public function indexAction(Request $request) {
$locale = $request->getLocale();
$entity = $this->getPageByIdName('page_sitemap', $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/Sitemap/index.html.twig', [
'entity' => $entity,
'sitemap' => $this->getSitemap($locale, null, '▪'),
]);
}
private function getSitemap($lang, $parentId, $bullet) {
$pages = $this->getPagesByParentId($lang, $parentId);
$sitemap = [];
if (empty($pages)) {
return $sitemap;
}
foreach ($pages as $page) {
$pageTrans = $page->getTranslation()[$lang];
if ($page->getIdname()) {
$route = $this->removePagePrefix($page->getIdname());
$url = $this->frontUtil->pageUrlByIdName($route, $lang);
} else {
if (!$pageTrans->getSlug() || $pageTrans->getSlug() == '') {
continue;
}
$url = $this->routeLocalizer->generate('page', ['slug' => $pageTrans->getSlug()], $pageTrans->getLang());
}
$children = $this->getSitemap($lang, $page->getId(), '•');
$arr = [
'url' => $url,
'bullet' => $bullet,
'title' => $pageTrans->getTitle(),
'metaTitle' => $pageTrans->getMetaTitle(),
'children' => $children,
];
if ($page->getIdname()) {
if ($page->getIdname() == 'page_museums') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('mak_item', 'Museum', $lang)) ;
}
if ($page->getIdname() == 'page_archives') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('mak_item', 'Archive', $lang));
}
if ($page->getIdname() == 'page_private_collections') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('mak_item', 'PrivateCollection', $lang));
}
if ($page->getIdname() == 'page_exhibitions') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('exhibition_item', 'Exhibition', $lang));
}
if ($page->getIdname() == 'page_proposal') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('proposal_item', 'Proposal', $lang));
}
if ($page->getIdname() == 'page_news') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('news_item', 'News', $lang));
}
if ($page->getIdname() == 'page_mission') {
$arr['children'] = array_merge($arr['children'], $this->getChildren('mission_item', 'Mission', $lang));
}
}
$sitemap[] = $arr;
}
return $sitemap;
}
private function getChildren($route, $entityName, $lang)
{
$items = $this->em->getRepository('\App\Package\Openform\Entity\\' . $entityName)
->createQueryBuilder('m')
->select('m, t')
->leftJoin('m.Translation', 't')
->andWhere('m.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang')
->setParameter('lang', $lang)
->getQuery()
->getResult();
$childrenEntries = [];
foreach ($items as $item) {
$trans = $item->getTranslation()[$lang];
if (!$trans->getSlug() || $trans->getSlug() == '') {
continue;
}
$row = [
'bullet' => method_exists($item, 'getDepartments') || method_exists($item, 'getExhibitionDepartment') ? '▪' : '•',
'url' => $this->routeLocalizer->generate($route, ['slug' => $trans->getSlug(), 'makSlug' => isset($item->isMak) ? $item->getMakSlug($lang) : null, ], $lang),
'title' => $trans->getTitle(),
'metaTitle' => $trans->getMetaTitle(),
'children' => [],
];
if (method_exists($item, 'getMenuPage')) {
$row['children'] = array_merge($row['children'], $this->getMenuPages($lang, $item->getId(), null, $item->getMakSlug($lang), $entityName));
}
if (method_exists($item, 'getDepartments')) {
$row['children'] = array_merge($row['children'], $this->getDepartments($lang, $item->getId(), null, $item->getMakSlug($lang), $entityName));
}
if (method_exists($item, 'getExhibitionDepartment')) {
$row['children'] = array_merge($row['children'], $this->getDepartments($lang, $item->getId(), null, $item->getMakSlug($lang), $entityName));
}
$childrenEntries[] = $row;
}
return $childrenEntries;
}
private function getMenuPages($lang, $makId, $parentId, $makSlug, $entityName)
{
$qb = $this->em->getRepository('\App\Package\Openform\Entity\MenuPage')
->createQueryBuilder('e')
->select('e, t')
->leftJoin('e.Translation', 't')
->andWhere('e.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang')
->setParameter('lang', $lang);
if ($makId) {
$qb->andWhere('e.'.$entityName.' = :makId');
$qb->setParameter('makId', $makId);
} else if ($parentId) {
$qb->andWhere('e.Parent = :parentId');
$qb->setParameter('parentId', $parentId);
} else {
$qb->andWhere('e.Parent'.$entityName.'Department IS NULL');
}
$entities = $qb->getQuery()->getResult();
$result = [];
foreach ($entities as $entity) {
$entityTrans = $entity->getTranslation()[$lang];
$entry = [
'url' => $this->routeLocalizer->generate('menu_page', ['slug' => $entityTrans->getSlug(), 'makSlug' => $entity->getMakSlug($lang), 'rootParentSlug' => $entity->getRootParentSlug($lang)], $entityTrans->getLang()),
'bullet' => '•',
'title' => $entityTrans->getTitle(),
'metaTitle' => $entityTrans->getMetaTitle(),
'children' => [],
];
$entry['children'] = array_merge($entry['children'], $this->getMenuPages($lang, false, $entity->getId(), $makSlug, $entityName));
$result[] = $entry;
}
return $result;
}
private function getDepartments($lang, $makId, $parentId, $makSlug, $entityName)
{
$qb = $this->em->getRepository('\App\Package\Openform\Entity\\'.$entityName.'Department')
->createQueryBuilder('e')
->select('e, t')
->leftJoin('e.Translation', 't')
->andWhere('e.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang')
->setParameter('lang', $lang);
if ($makId) {
$qb->andWhere('e.'.$entityName.' = :makId');
$qb->setParameter('makId', $makId);
} else if ($parentId) {
$qb->andWhere('e.Parent'.$entityName.'Department = :parentId');
$qb->setParameter('parentId', $parentId);
} else {
$qb->andWhere('e.Parent'.$entityName.'Department IS NULL');
}
$entities = $qb->getQuery()->getResult();
$result = [];
foreach ($entities as $entity) {
$entityTrans = $entity->getTranslation()[$lang];
if ($entityName == 'Exhibition') {
$url = $this->routeLocalizer->generate('exhibition_department_item', ['slug' => $entityTrans->getSlug()], $entityTrans->getLang());
} else {
$url = $this->routeLocalizer->generate('mak_department_item', ['slug' => $entityTrans->getSlug(), 'makSlug' => $makSlug], $entityTrans->getLang());
}
$entry = [
'url' => $url,
'bullet' => '•',
'title' => $entityTrans->getTitle(),
'metaTitle' => $entityTrans->getMetaTitle(),
'children' => [],
];
$entry['children'] = array_merge($entry['children'], $this->getDepartments($lang, false, $entity->getId(), $makSlug, $entityName));
$entry['children'] = array_merge($entry['children'], $this->getAntiqueSuits($lang, $entity->getId(), $entityName));
$entry['children'] = array_merge($entry['children'], $this->getRelictPacks($lang, $entity->getId(), $entityName));
$result[] = $entry;
}
return $result;
}
private function getAntiqueSuits($lang, $parentId, $parentEntityName)
{
$parentEntityPath = '\App\Package\Openform\Entity\\'.$parentEntityName.'Department';
if (!method_exists($parentEntityPath, 'getAntiqueSuit')) {
return [];
}
$qb = $this->em->getRepository('\App\Package\Openform\Entity\\AntiqueSuit')
->createQueryBuilder('e')
->select('e, t, a, at')
->leftJoin('e.Translation', 't')
->leftJoin('e.Antique', 'a')
->leftJoin('a.Translation', 'at')
->andWhere('e.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang')
->andWhere('at.title IS NOT NULL AND at.title <> \'\' AND at.lang = :lang')
->andWhere('e.'.$parentEntityName.'Department = :parentId')
->setParameter('lang', $lang)
->setParameter('parentId', $parentId);
$antiqueSuits = $qb->getQuery()->getResult();
$antiqueSuitEntries = [];
foreach ($antiqueSuits as $antiqueSuit) {
$antiqueEntries = [];
foreach ($antiqueSuit->getAntique() as $antique) {
$antiqueTrans = $antique->getTranslation()[$lang];
$antiqueEntries[] = [
'url' => $this->routeLocalizer->generate('antique_item', ['slug' => $antiqueTrans->getSlug()], $antiqueTrans->getLang()),
'bullet' => '-',
'title' => $antiqueTrans->getTitle(),
'metaTitle' => $antiqueTrans->getMetaTitle(),
'children' => [],
];
}
$entityTrans = $antiqueSuit->getTranslation()[$lang];
$antiqueSuitEntries[] = [
'url' => $this->routeLocalizer->generate('antique_suit_item', ['slug' => $entityTrans->getSlug()], $entityTrans->getLang()),
'bullet' => '•',
'title' => $entityTrans->getTitle(),
'metaTitle' => $entityTrans->getMetaTitle(),
'children' => $antiqueEntries,
];
}
return $antiqueSuitEntries;
}
private function getRelictPacks($lang, $parentId, $parentEntityName)
{
$parentEntityPath = '\App\Package\Openform\Entity\\'.$parentEntityName.'Department';
if (!method_exists($parentEntityPath, 'getRelictPack')) {
return [];
}
$qb = $this->em->getRepository('\App\Package\Openform\Entity\\RelictPack')
->createQueryBuilder('e')
->select('e, t, a, at')
->leftJoin('e.Translation', 't')
->leftJoin('e.Relict', 'a')
->leftJoin('a.Translation', 'at')
->andWhere('e.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang')
->andWhere('at.title IS NOT NULL AND at.title <> \'\' AND at.lang = :lang')
->andWhere('e.Parent = :parentId')
->setParameter('lang', $lang)
->setParameter('parentId', $parentId);
$relictPacks = $qb->getQuery()->getResult();
$relictPackEntries = [];
foreach ($relictPacks as $relictPack) {
$relictEntries = [];
foreach ($relictPack->getRelict() as $relict) {
$relictTrans = $relict->getTranslation()[$lang];
$relictEntries[] = [
'url' => $this->routeLocalizer->generate('relict_item', ['slug' => $relictTrans->getSlug()], $relictTrans->getLang()),
'bullet' => '-',
'title' => $relictTrans->getTitle(),
'metaTitle' => $relictTrans->getMetaTitle(),
'children' => [],
];
}
$relictPackTrans = $relictPack->getTranslation()[$lang];
$relictPackEntries[] = [
'url' => $this->routeLocalizer->generate('relict_pack_item', ['slug' => $relictPackTrans->getSlug()], $relictPackTrans->getLang()),
'bullet' => '•',
'title' => $relictPackTrans->getTitle(),
'metaTitle' => $relictPackTrans->getMetaTitle(),
'children' => $relictEntries,
];
}
return $relictPackEntries;
}
private function getPagesByParentId($locale, $parentId = null)
{
$params = [
'lang' => $locale,
];
$qb = $this->em->getRepository(\App\Package\Openform\Entity\Page::class)->createQueryBuilder('e')
->select('e, t')
->leftJoin('e.Translation', 't')
->andWhere('e.stat = 1')
->andWhere('t.title IS NOT NULL AND t.title <> \'\' AND t.lang = :lang');
if ($parentId) {
$qb->andWhere('e.parentId = :parentId');
$params['parentId'] = $parentId;
} else {
$qb->andWhere('e.parentId IS NULL');
}
$qb->setParameters($params);
$qb->addOrderBy('e.ord', 'DESC');
return $qb->getQuery()->getResult();
}
function removePagePrefix($inputString)
{
if (substr($inputString, 0, 6) === 'page_') {
return substr($inputString, 6);
} else {
return $inputString;
}
}
}