<?php
namespace App\EventListener\SonataAdmin\Menu;
use App\Util\Helper\StringUtils;
use Knp\Menu\ItemInterface;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class MenuBuilderListener
{
/**
* Contains the value of the currently loaded environment.
*
* @var string
*/
private $environment;
/**
* MenuBuilderListener constructor.
*
* @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $authorizationChecker
* The authorization service manager
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
* The instance of the core kernel
*/
public function __construct(KernelInterface $kernel)
{
$this->environment = $kernel->getEnvironment();
}
/**
* Responsible for providing additional menu items to the sidebar navigation.
*
* @param \Sonata\AdminBundle\Event\ConfigureMenuEvent $event
* The instance of the dispatched event
*
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminProduct()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminSeries()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminProductCompany()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminProductPerson()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminAssetOffline()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminAssetOnlineInbox()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminTvSalesCustomer()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminTvSalesOfferMain()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminTvSalesOffer()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminVodOperator()
* @uses \App\EventListener\SonataAdmin\Menu\MenuBuilderListener::handleAdminVodPriceCampaignOperators()
*/
public function addMenuItems(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
$menu->removeChild("sonata_media");
$menu->getChild("sonata_classification")->removeChild("label_tags");
$menu->getChild("sonata_classification")->removeChild("label_collections");
foreach ($menu->getChildren() as $group) {
$order = [];
$doReorder = false;
foreach ($group->getChildren() as $menuItem) {
$order[] = $menuItem->getName();
if (!($admin = $menuItem->getExtra('admin'))) {
continue;
}
$methodName = StringUtils::camelize(sprintf(
'handle_%s',
str_replace('.', '_', $admin->getCode())
));
if (
method_exists($this, $methodName)
&& $admin->isGranted("CREATE")
&& ($items = $this->{$methodName}($group))
) {
$doReorder = true;
$order = array_merge($order, $items);
}
}
if ($doReorder) {
$group->reorderChildren($order);
}
}
}
/**
* Responsible for providing menu items to section "Products".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminProduct(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_product_create', [
'subclass' => 'productMovie',
], 'Add new Movie', 'add_product_movie');
$this->addLink($group, 'admin_app_product_create', [
'subclass' => 'productTVSeries',
], 'Add new TV Series', 'add_product_tv_series');
return [
'add_product_movie',
'add_product_tv_series',
];
}
/**
* Responsible for providing menu items to section "Series".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminSeries(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_series_create');
return ['admin_app_series_create'];
}
/**
* Responsible for providing menu items to section "Companies".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminProductCompany(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_company_create');
return ['admin_app_company_create'];
}
/**
* Responsible for providing menu items to section "Persons".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminProductPerson(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_person_create');
return ['admin_app_person_create'];
}
/**
* Responsible for providing menu items to section "Offline Assets".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminAssetOffline(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_assetoffline_create');
return ['admin_app_assetoffline_create'];
}
/**
* Responsible for providing menu items to section "Online Assets Inbox".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminAssetOnlineInbox(ItemInterface $group): array
{
$route = 'prod' === $this->environment
? 'admin_app_assetonlineinbox_upload'
: 'admin_app_sonatamediamedia_multi_upload';
$this->addLink(
$group,
$route,
['provider' => 'app.media.provider.asset_file'],
'Upload'
);
return [$route];
}
/**
* Responsible for providing menu items to section "TV Sales Customers".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminTvSalesCustomer(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_customer_create');
return ['admin_app_customer_create'];
}
/**
* Responsible for providing menu items to section "TV Sales Main Offers".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminTvSalesOfferMain(ItemInterface $group): array
{
$this->addLink(
$group,
'admin_app_offermain_create',
[],
'Add mainoffer'
);
return ['admin_app_offermain_create'];
}
/**
* Responsible for providing menu items to section "TV Sales Offers".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminTvSalesOffer(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_offer_create');
return ['admin_app_offer_create'];
}
/**
* Responsible for providing menu items to section "VOD Operators".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminVodOperator(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_vod_operator_create');
return ['admin_app_vod_operator_create'];
}
/**
* Responsible for providing menu items to section "VOD Price Campaigns Operators".
*
* @param \Knp\Menu\ItemInterface $group
* The instance of the menu item target group
*
* @return array|string[]
* The menu item names to include
*/
protected function handleAdminVodPriceCampaignOperators(ItemInterface $group): array
{
$this->addLink($group, 'admin_app_vod_pricecampaign_create');
return ['admin_app_vod_pricecampaign_create'];
}
private function addLink(
ItemInterface $menuItem,
string $route,
?array $routeParameters = [],
?string $label = 'Add new',
?string $name = null
) {
$menuItem
->addChild(
$name ?? $route,
['route' => $route, 'routeParameters' => $routeParameters]
)
->setLabel($label)
->setExtra('icon', '<i class="fas fa-angle-double-right ml-4" aria-hidden="true"></i>')
->getParent();
}
}