<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MenuDefaultController extends Controller
{
public function fetchMenuAction(Request $request, $identifier, $currentUrl, $renderUL = true)
{
$locale = $request->getLocale();
$em = $this->getDoctrine()->getManager();
$menu = $em->getRepository('App:Menu')->findOneBy(['identifier' => $identifier]);
if (null === $menu) {
return new Response('');
// throw $this->createNotFoundException('PCGC: The menu \'' . $indentifier . '\' does not exist');
}
return $this->render('@theme/common/menu.html.twig', [
'menu' => $this->getMenuStructure($locale, $menu),
'currentUrl' => $currentUrl,
]);
}
public function hasMenuChildren($menuData, $id)
{
return array_filter($menuData, fn ($var) => $var['menu_parent_id'] == $id);
}
private function getMenuStructure($locale, $menu)
{
$menuData = $menu->getMenuItems();
$items = [];
foreach ($menuData as $menuItem) {
$page = $menuItem->getPageId();
if (null != $page) {
$navTitle = $page->getNavTitle();
$slug = $page->getSlug();
} else {
$navTitle = $menuItem->getNameOverride();
$slug = $menuItem->getUrlOverride();
}
//no navtitle override for translations
if ('en' == $locale && '' != $menuItem->getNameOverride()) {
$navTitle = $menuItem->getNameOverride();
}
$items[] = [
'menu_item_id' => $menuItem->getMenuItemId(),
'navtitle' => $navTitle,
'slug' => $this->fixSlug($slug),
'isdynamic' => $menuItem->getIsdynamic(),
'url_override' => $menuItem->getUrlOverride() ? $this->fixSlug($menuItem->getUrlOverride()) : null,
'dynamicentity' => $menuItem->getDynamicentity(),
'new_window' => $menuItem->getNewwindow(),
'menu_parent_id' => $menuItem->getMenuParentId(),
];
}
foreach ($items as $key => $item) {
if (!$item['isdynamic']) {
continue;
}
$items[$key]['dynamic_items'] = $this->getDynamicItems($item);
}
$menu = $this->hasMenuChildren($items, 0);
foreach ($menu as $key => $item) {
$menu[$key]['children'] = $this->hasMenuChildren($items, $item['menu_item_id']);
foreach ($menu[$key]['children'] as $key2 => $item2) {
$menu[$key]['children'][$key2]['children'] = $this->hasMenuChildren($items, $item2['menu_item_id']);
foreach ($menu[$key]['children'][$key2]['children'] as $key3 => $item3) {
$menu[$key]['children'][$key2]['children'][$key3]['children'] = $this->hasMenuChildren($items, $item3['menu_item_id']);
}
}
}
return $menu;
}
private function getDynamicItems($item)
{
$dynamicItems = [];
$em = $this->getDoctrine()->getManager();
$bundleEntity = str_replace('/', '', $item['dynamicentity']);
$dyn_manu_items = $em->getRepository($bundleEntity)->findBy(['active' => 1, 'deleted' => 0]);
$possibleTitles = ['getTitle', 'getName', 'getHeadline', 'getHeading', 'getMetaTitle'];
foreach ($dyn_manu_items as $dyn_manu_item) {
// check to ensure that the get<Whatever> fucntion exists on the entity
foreach ($possibleTitles as $title) {
if (method_exists($dyn_manu_item, $title)) {
$dyn_title = call_user_func([$dyn_manu_item, $title]);
break;
}
}
$url = $item['url_override'].'/'.$dyn_manu_item->getSlug();
$dynamicItems[] = [
'navtitle' => $dyn_title,
'slug' => $url,
];
}
return $dynamicItems;
}
private function fixSlug($slug)
{
// some slugs will be prefixed with '/' while other may not (i.e db or custom) - so add them if they need them
if ('http' === substr($slug, 0, 4)) {
return $slug;
}
if ('/' == substr($slug, 0, 1)) {
$slug = ltrim($slug, '/');
}
$slug = ltrim($slug, '/');
return '/'.$slug;
}
}