Skip to content

Instantly share code, notes, and snippets.

@trunda
Last active December 13, 2015 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trunda/4959269 to your computer and use it in GitHub Desktop.
Save trunda/4959269 to your computer and use it in GitHub Desktop.
SmfMenu with SmfEvents
<?php
namespace Simpleshop\Modules\Admin\Presenters;
use Nette\Application\UI\Presenter;
use Simpleshop\Modules\Admin\Events\MenuEvent;
use Smf\Events\IEventDispatcher;
use Smf\Menu\Control\Factory;
abstract class BasePresenter extends Presenter
{
const EVENT_MENU_CREATED = "simpleshop.admin.menuCreated";
/** @var IEventDispatcher */
protected $eventDispatcher;
/** @var Factory */
protected $menuFactory;
public function injectEventDispatcher(IEventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
public function injectMenuFactory(Factory $factory)
{
$this->menuFactory = $factory;
}
protected function createComponentMenu()
{
$menu = $this->menuFactory->createControl();
$root = $menu->getRoot();
$root->addChild('catalog', array(
'label' => 'Katalog',
'icon' => 'list',
));
$this->eventDispatcher->dispatchEvent(self::EVENT_MENU_CREATED, new MenuEvent($menu));
return $menu;
}
}
<?php
namespace Simpleshop\Modules\Admin\Presenters;
/**
* Category presenter
*/
use Doctrine\ODM\MongoDB\DocumentRepository;
use Knp\Menu\MenuItem;
use Nette\Application\BadRequestException;
use Simpleshop\Model\Documents\Category;
use Simpleshop\Modules\Admin\Events\MenuEvent;
use Smf\Events\Event;
class CategoryPresenter extends BasePresenter
{
//...
/**
* @param \Simpleshop\Modules\Admin\Events\MenuEvent $event
* @eventListener(name=simpleshop.admin.menuCreated);
*/
public static function onMenuCreate(MenuEvent $event)
{
$menu = $event->getMenuControl();
$catalog = $menu->root['catalog'];
$catalog->addChild('categories', array(
'label' => 'Kategorie',
'icon' => 'folder-open',
'link' => 'Category:list',
))->addChild('new', array(
'label' => 'Nová',
'icon' => 'plus',
'link' => 'Category:new',
));
}
}
<?php
namespace Simpleshop\Modules\Admin\Events;
use Smf\Events\Event;
use Smf\Menu\Control\MenuControl;
class MenuEvent extends Event
{
/** @var \Smf\Menu\Control\MenuControl */
private $menuControl;
/**
* @param \Smf\Menu\Control\MenuControl $menu
*/
function __construct(MenuControl $menu)
{
$this->menuControl = $menu;
}
/**
* @return \Smf\Menu\Control\MenuControl
*/
public function getMenuControl()
{
return $this->menuControl;
}
}
<?php
/**
* Created by JetBrains PhpStorm.
* User: trunda
* Date: 05.02.13
* Time: 15:20
* To change this template use File | Settings | File Templates.
*/
namespace Simpleshop\Modules\Admin\Presenters;
use Simpleshop\Modules\Admin\Events\MenuEvent;
class ProductPresenter extends BasePresenter
{
// ...
/**
* @param \Simpleshop\Modules\Admin\Events\MenuEvent $event
* @eventListener(name=simpleshop.admin.menuCreated);
*/
public static function onMenuCreate(MenuEvent $event)
{
$menu = $event->getMenuControl();
$catalog = $menu->root['catalog'];
$catalog->addChild('products', array(
'label' => 'Produkty',
'icon' => 'cog',
'link' => 'Product:list',
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment