From 1265840371983f54a95adb511c9f999e7dc1aca3 Mon Sep 17 00:00:00 2001 From: Raynald Date: Sun, 12 Nov 2017 16:57:09 +0100 Subject: [PATCH 1/6] Add icon & user restricted functionality to menu --- Admin/MenuAdmin.php | 6 ++ Entity/Menu.php | 59 ++++++++++++- MenuBuilder/MainMenuBuilder.php | 39 +++++---- Resources/config/doctrine/Menu.orm.yml | 5 ++ Resources/config/services.xml | 1 + Resources/views/Menu/knp_menu.html.twig | 107 ++++++++++++++++++++++++ 6 files changed, 201 insertions(+), 16 deletions(-) create mode 100644 Resources/views/Menu/knp_menu.html.twig diff --git a/Admin/MenuAdmin.php b/Admin/MenuAdmin.php index bf9afff..2e873a0 100644 --- a/Admin/MenuAdmin.php +++ b/Admin/MenuAdmin.php @@ -170,6 +170,8 @@ protected function configureFormFields(FormMapper $formMapper) $formMapper ->add('name', 'text') ->add('page', 'sonata_page_selector', array( + ->add('icon', 'text', array('required' => false)) + ->add('page', PageSelectorType::class, array( 'site' => $this->siteInstance, 'model_manager' => $this->getModelManager(), 'class' => 'Application\Sonata\PageBundle\Entity\Page', @@ -181,6 +183,8 @@ protected function configureFormFields(FormMapper $formMapper) ) )) ->add('parent','sonata_type_model', array('required' => false)) + ->add('parent', 'sonata_type_model', array('required' => false)) + ->add('userRestricted', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ->add('active', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ->add('clickable', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ; @@ -198,8 +202,10 @@ protected function configureListFields(ListMapper $listMapper) $listMapper ->addIdentifier('id') ->add('name', 'string', array('template' => 'SkillbertoSonataPageMenuBundle:Admin:base_list_field.html.twig')) + ->add('icon', 'string', array('template' => 'SkillbertoSonataPageMenuBundle:Admin:base_list_field.html.twig')) ->add('page') ->add('parent') + ->add('userRestricted') ->add('active') ->add('clickable') ->add('_action', 'actions', array( diff --git a/Entity/Menu.php b/Entity/Menu.php index cf9db32..13432dc 100644 --- a/Entity/Menu.php +++ b/Entity/Menu.php @@ -19,6 +19,11 @@ class Menu */ protected $name; + /** + * @var string + */ + protected $icon; + /** * @var boolean */ @@ -74,6 +79,11 @@ class Menu */ protected $active; + /** + * @var boolean + */ + protected $userRestricted; + /** * @var \DateTime */ @@ -130,6 +140,29 @@ public function getName() return $this->name; } + /** + * Set icon + * + * @param string $icon + * @return Menu + */ + public function setIcon($icon) + { + $this->icon = $icon; + + return $this; + } + + /** + * Get icon + * + * @return string + */ + public function getIcon() + { + return $this->icon; + } + /** * Set clickable * @@ -374,7 +407,7 @@ public function getAttribute() * Set active * * @param boolean $active - * @return Menu // Add your code here + * @return Menu */ public function setActive($active) @@ -394,6 +427,30 @@ public function getActive() return $this->active; } + /** + * Set user restricted + * + * @param boolean $userRestricted + * @return Menu + + */ + public function setUserRestricted($userRestricted) + { + $this->userRestricted = $userRestricted; + + return $this; + } + + /** + * Get user restricted + * + * @return boolean + */ + public function getUserRestricted() + { + return $this->userRestricted; + } + /** * Set createdAt * diff --git a/MenuBuilder/MainMenuBuilder.php b/MenuBuilder/MainMenuBuilder.php index 322c5ae..b32225d 100644 --- a/MenuBuilder/MainMenuBuilder.php +++ b/MenuBuilder/MainMenuBuilder.php @@ -11,28 +11,30 @@ use Symfony\Cmf\Component\Routing\ChainedRouterInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; class MainMenuBuilder implements MenuBuilderInterface { - protected - $menuEntity, - $factoryInterface, - $managerRegistry, - $routerInterface, - $siteSelectorInterface, - $currentMenuName = null, - $rendered = false, - $mainMenu, - $request; - - public function __construct($menuEntity, FactoryInterface $factoryInterface, ManagerRegistry $managerRegistry, RequestStack $requestStack, ChainedRouterInterface $routerInterface, SiteSelectorInterface $siteSelectorInterface) + protected $menuEntity; + protected $factoryInterface; + protected $managerRegistry; + protected $routerInterface; + protected $siteSelectorInterface; + protected $currentMenuName = null; + protected $rendered = false; + protected $mainMenu; + protected $request; + protected $authorizationChecker; + + public function __construct($menuEntity, FactoryInterface $factoryInterface, ManagerRegistry $managerRegistry, RequestStack $requestStack, ChainedRouterInterface $routerInterface, SiteSelectorInterface $siteSelectorInterface, AuthorizationCheckerInterface $authorizationChecker) { $this->menuEntity = $menuEntity; $this->factoryInterface = $factoryInterface; $this->managerRegistry = $managerRegistry; - $this->request = $requestStack->getCurrentRequest();; + $this->request = $requestStack->getCurrentRequest(); $this->routerInterface = $routerInterface; $this->siteSelectorInterface = $siteSelectorInterface; + $this->authorizationChecker = $authorizationChecker; } public function getMenu() @@ -75,6 +77,10 @@ protected function createMenuStructure($menus, ItemInterface $root = null) protected function createMenu(Menu $menu, ItemInterface $root = null) { + if(null !== $menu->getParent() && $menu->getParent()->getUserRestricted() && !$this->authorizationChecker->isGranted('ROLE_USER')) { + return false; + } + $currentItem = $this->createMenuItem($menu); $level = $menu->getLvl(); @@ -82,7 +88,10 @@ protected function createMenu(Menu $menu, ItemInterface $root = null) if ($level == 0) { $this->mainMenu = $currentItem; } else { - $root->addChild($currentItem); + $currentMenu = $root->addChild($currentItem); + if (null !== $menu->getIcon()) { + $currentMenu->setExtra('icon', $menu->getIcon()); + } } if (count($menu->getChildren()) > 0 && ($menu->getActive() or $level == 0)) { @@ -128,4 +137,4 @@ protected function putChildAttributes(ItemInterface $itemInterface) { $itemInterface->setAttribute("class", "sub-menu"); } -} \ No newline at end of file +} diff --git a/Resources/config/doctrine/Menu.orm.yml b/Resources/config/doctrine/Menu.orm.yml index 820d405..85139fc 100644 --- a/Resources/config/doctrine/Menu.orm.yml +++ b/Resources/config/doctrine/Menu.orm.yml @@ -15,6 +15,9 @@ Skillberto\SonataPageMenuBundle\Entity\Menu: fields: name: type: string + icon: + type: string + nullable: true clickable: type: boolean lft: @@ -42,6 +45,8 @@ Skillberto\SonataPageMenuBundle\Entity\Menu: # - sortablePosition active: type: boolean + userRestricted: + type: boolean createdAt: type: datetime updatedAt: diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 23c0b92..89aee0b 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -18,6 +18,7 @@ + diff --git a/Resources/views/Menu/knp_menu.html.twig b/Resources/views/Menu/knp_menu.html.twig new file mode 100644 index 0000000..5f6570c --- /dev/null +++ b/Resources/views/Menu/knp_menu.html.twig @@ -0,0 +1,107 @@ +{% extends 'knp_menu.html.twig' %} +{% import 'knp_menu.html.twig' as macros %} + +{% block item %} + {% if item.displayed %} + {%- set attributes = item.attributes %} + {%- set is_dropdown = item.getExtra('dropdown', false) %} + {%- set icon = item.getExtra('icon') %} + + {%- if item.getExtra('divider_prepend') %} + {{ block('dividerElement') }} + {%- endif %} + + {# building the class of the item #} + {%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %} + {%- if matcher.isCurrent(item) %} + {%- set classes = classes|merge([options.currentClass]) %} + {%- elseif matcher.isAncestor(item, options.depth) %} + {%- set classes = classes|merge([options.ancestorClass]) %} + {%- endif %} + {%- if item.actsLikeFirst %} + {%- set classes = classes|merge([options.firstClass]) %} + {%- endif %} + {%- if item.actsLikeLast %} + {%- set classes = classes|merge([options.lastClass]) %} + {%- endif %} + + {# building the class of the children #} + {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %} + {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %} + + {# adding classes for dropdown #} + {%- if is_dropdown %} + {%- set classes = classes|merge(['dropdown']) %} + {%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %} + {%- endif %} + + {# putting classes together #} + {%- if classes is not empty %} + {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} + {%- endif %} + {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %} + + {# displaying the item #} + + {%- if is_dropdown %} + {{ block('dropdownElement') }} + {%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %} + {{ block('linkElement') }} + {%- else %} + {{ block('spanElement') }} + {%- endif %} + {# render the list of children#} + {{ block('list') }} + + + {%- if item.getExtra('divider_append') %} + {{ block('dividerElement') }} + {%- endif %} + {% endif %} +{% endblock %} + +{% block dividerElement %} + {% if item.level == 1 %} +
  • + {% else %} +
  • + {% endif %} +{% endblock %} + +{% block linkElement %} + + {% if icon is not empty %} + + {% endif %} + {{ block('label') }} + +{% endblock %} + +{% block spanElement %} + + {% if icon is not empty %} + + {% endif %} + {{ block('label') }} + +{% endblock %} + +{% block dropdownElement %} + {%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %} + {%- set classes = classes|merge(['dropdown-toggle']) %} + {%- set attributes = item.linkAttributes %} + {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} + {%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %} + + {% if icon is not empty %} + + {% endif %} + {{ block('label') }} + + +{% endblock %} + +{% block label %}{{ item.label|trans( + item.getExtra('translation_params', {}), + item.getExtra('translation_domain') +) }}{% endblock %} \ No newline at end of file From c8cf3989143188daa4f9aa8bc09468d4d6c04b47 Mon Sep 17 00:00:00 2001 From: Raynald Date: Sun, 12 Nov 2017 16:57:35 +0100 Subject: [PATCH 2/6] Update sonata template according to the last version --- .../views/Admin/base_list_field.html.twig | 46 ++++++++++++++++--- .../Admin/list__action_activate.html.twig | 13 ++++++ .../views/Admin/list__action_sort.html.twig | 2 +- 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 Resources/views/Admin/list__action_activate.html.twig diff --git a/Resources/views/Admin/base_list_field.html.twig b/Resources/views/Admin/base_list_field.html.twig index 0b69115..46e5080 100755 --- a/Resources/views/Admin/base_list_field.html.twig +++ b/Resources/views/Admin/base_list_field.html.twig @@ -9,7 +9,7 @@ file that was distributed with this source code. #} - + {% set route = field_description.options.route.name|default(null) %} {% set action = route == 'show' ? 'VIEW' : route|upper %} {% set spaces = "" %} @@ -28,18 +28,52 @@ file that was distributed with this source code. and route and action and admin.hasRoute(route) - and admin.isGranted(action, action in ['VIEW', 'EDIT'] ? object : null) + and admin.hasAccess(route, route in ['show', 'edit'] ? object : null) %} - {%- block field %}{{ value }}{% endblock -%} + {%- block field %} + {% spaceless %} + {% if field_description.options.collapse is defined %} + {% set collapse = field_description.options.collapse %} +
    {{ value }}
    + {% else %} + {{ value }} + {% endif %} + {% endspaceless %} + {% endblock -%}
    {% else %} - {% set isEditable = field_description.options.editable is defined and field_description.options.editable and admin.isGranted('EDIT', object) %} + {% set isEditable = field_description.options.editable is defined and field_description.options.editable and admin.hasAccess('edit', object) %} {% set xEditableType = field_description.type|sonata_xeditable_type %} {% if isEditable and xEditableType %} - {% set url = path('sonata_admin_set_object_field_value', { 'context': 'list', 'field': field_description.name, 'objectId': admin.id(object), 'code': admin.code(object) }) %} - + {% set url = path( + 'sonata_admin_set_object_field_value', + admin.getPersistentParameters|default([])|merge({ + 'context': 'list', + 'field': field_description.name, + 'objectId': admin.id(object), + 'code': admin.code(object) + }) + ) %} + + {% if field_description.type == 'date' and value is not empty %} + {% set data_value = value.format('Y-m-d') %} + {% elseif field_description.type == 'boolean' and value is empty %} + {% set data_value = 0 %} + {% else %} + {% set data_value = value %} + {% endif %} + + {{ block('field') }} {% else %} diff --git a/Resources/views/Admin/list__action_activate.html.twig b/Resources/views/Admin/list__action_activate.html.twig new file mode 100644 index 0000000..ea49648 --- /dev/null +++ b/Resources/views/Admin/list__action_activate.html.twig @@ -0,0 +1,13 @@ +{% if admin.hasAccess('edit', object) and admin.hasRoute('activate') %} + {% if object.active %} + {% set action = 'action_inactivate' %} + {% set icon = 'fa-circle-o' %} + {% else %} + {% set action = 'action_activate' %} + {% set icon = 'fa-circle' %} + {% endif %} + + + {{ action|trans }} + +{% endif %} diff --git a/Resources/views/Admin/list__action_sort.html.twig b/Resources/views/Admin/list__action_sort.html.twig index 1840e80..9bff170 100644 --- a/Resources/views/Admin/list__action_sort.html.twig +++ b/Resources/views/Admin/list__action_sort.html.twig @@ -1,4 +1,4 @@ -{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %} +{% if admin.hasAccess('edit', object) and admin.hasRoute('edit') %} {% set parent = 0 %} {% if object.getParent() is defined and object.getParent() is not null %} From 3ae23ede34014861af7099076186658d5ed18e3f Mon Sep 17 00:00:00 2001 From: Raynald Date: Sun, 12 Nov 2017 19:07:49 +0100 Subject: [PATCH 3/6] Display menu icons in admin list --- Resources/views/Admin/base_list_field.html.twig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Resources/views/Admin/base_list_field.html.twig b/Resources/views/Admin/base_list_field.html.twig index 46e5080..bab750e 100755 --- a/Resources/views/Admin/base_list_field.html.twig +++ b/Resources/views/Admin/base_list_field.html.twig @@ -14,7 +14,7 @@ file that was distributed with this source code. {% set action = route == 'show' ? 'VIEW' : route|upper %} {% set spaces = "" %} - {% if object.getLvl() is defined and object.getLvl > 0%} + {% if object.getLvl() is defined and object.getLvl > 0 and field_description.name != 'icon' %} {% set level = object.getLvl() %} {% for i in 1..level %} @@ -77,7 +77,11 @@ file that was distributed with this source code. {{ block('field') }} {% else %} - {{ block('field') }} + {% if field_description.name == 'icon' %} + + {% else %} + {{ block('field') }} + {% endif %} {% endif %} {% endif %} From 1b8eb6b56fc16eaa6ed0af464c74a125b2c54976 Mon Sep 17 00:00:00 2001 From: Raynald Date: Mon, 13 Nov 2017 21:51:40 +0100 Subject: [PATCH 4/6] Delete unused file --- Resources/views/Menu/knp_menu.html.twig | 107 ------------------------ 1 file changed, 107 deletions(-) delete mode 100644 Resources/views/Menu/knp_menu.html.twig diff --git a/Resources/views/Menu/knp_menu.html.twig b/Resources/views/Menu/knp_menu.html.twig deleted file mode 100644 index 5f6570c..0000000 --- a/Resources/views/Menu/knp_menu.html.twig +++ /dev/null @@ -1,107 +0,0 @@ -{% extends 'knp_menu.html.twig' %} -{% import 'knp_menu.html.twig' as macros %} - -{% block item %} - {% if item.displayed %} - {%- set attributes = item.attributes %} - {%- set is_dropdown = item.getExtra('dropdown', false) %} - {%- set icon = item.getExtra('icon') %} - - {%- if item.getExtra('divider_prepend') %} - {{ block('dividerElement') }} - {%- endif %} - - {# building the class of the item #} - {%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %} - {%- if matcher.isCurrent(item) %} - {%- set classes = classes|merge([options.currentClass]) %} - {%- elseif matcher.isAncestor(item, options.depth) %} - {%- set classes = classes|merge([options.ancestorClass]) %} - {%- endif %} - {%- if item.actsLikeFirst %} - {%- set classes = classes|merge([options.firstClass]) %} - {%- endif %} - {%- if item.actsLikeLast %} - {%- set classes = classes|merge([options.lastClass]) %} - {%- endif %} - - {# building the class of the children #} - {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %} - {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %} - - {# adding classes for dropdown #} - {%- if is_dropdown %} - {%- set classes = classes|merge(['dropdown']) %} - {%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %} - {%- endif %} - - {# putting classes together #} - {%- if classes is not empty %} - {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} - {%- endif %} - {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %} - - {# displaying the item #} - - {%- if is_dropdown %} - {{ block('dropdownElement') }} - {%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %} - {{ block('linkElement') }} - {%- else %} - {{ block('spanElement') }} - {%- endif %} - {# render the list of children#} - {{ block('list') }} - - - {%- if item.getExtra('divider_append') %} - {{ block('dividerElement') }} - {%- endif %} - {% endif %} -{% endblock %} - -{% block dividerElement %} - {% if item.level == 1 %} -
  • - {% else %} -
  • - {% endif %} -{% endblock %} - -{% block linkElement %} - - {% if icon is not empty %} - - {% endif %} - {{ block('label') }} - -{% endblock %} - -{% block spanElement %} - - {% if icon is not empty %} - - {% endif %} - {{ block('label') }} - -{% endblock %} - -{% block dropdownElement %} - {%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %} - {%- set classes = classes|merge(['dropdown-toggle']) %} - {%- set attributes = item.linkAttributes %} - {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} - {%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %} - - {% if icon is not empty %} - - {% endif %} - {{ block('label') }} - - -{% endblock %} - -{% block label %}{{ item.label|trans( - item.getExtra('translation_params', {}), - item.getExtra('translation_domain') -) }}{% endblock %} \ No newline at end of file From 3daa6c8e47fd84764a2463412fa77499edb129c1 Mon Sep 17 00:00:00 2001 From: Raynald Date: Tue, 14 Nov 2017 15:06:06 +0100 Subject: [PATCH 5/6] Fix duplicated lines --- Admin/MenuAdmin.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Admin/MenuAdmin.php b/Admin/MenuAdmin.php index 2e873a0..9241a12 100644 --- a/Admin/MenuAdmin.php +++ b/Admin/MenuAdmin.php @@ -16,6 +16,7 @@ use Sonata\AdminBundle\Route\RouteCollection; use Sonata\PageBundle\Model\PageManagerInterface; use Symfony\Component\Validator\Constraints as Assert; +use Sonata\PageBundle\Form\Type\PageSelectorType; class MenuAdmin extends Admin { @@ -169,7 +170,6 @@ protected function configureFormFields(FormMapper $formMapper) $formMapper ->add('name', 'text') - ->add('page', 'sonata_page_selector', array( ->add('icon', 'text', array('required' => false)) ->add('page', PageSelectorType::class, array( 'site' => $this->siteInstance, @@ -182,7 +182,6 @@ protected function configureFormFields(FormMapper $formMapper) 'siteId' => $this->getSubject() ? $this->getSubject()->getSite()->getId() : null ) )) - ->add('parent','sonata_type_model', array('required' => false)) ->add('parent', 'sonata_type_model', array('required' => false)) ->add('userRestricted', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ->add('active', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) From df78d06db0cab4649cbcdbfc34fcbea4daaa85ca Mon Sep 17 00:00:00 2001 From: Raynald Date: Tue, 14 Nov 2017 19:00:54 +0100 Subject: [PATCH 6/6] Add 'Hide When User Connected' option to a menu item --- Admin/MenuAdmin.php | 8 +- Entity/Menu.php | 195 ++++++++++++++----------- MenuBuilder/MainMenuBuilder.php | 7 +- Resources/config/doctrine/Menu.orm.yml | 2 + 4 files changed, 125 insertions(+), 87 deletions(-) diff --git a/Admin/MenuAdmin.php b/Admin/MenuAdmin.php index 9241a12..b2e683c 100644 --- a/Admin/MenuAdmin.php +++ b/Admin/MenuAdmin.php @@ -183,10 +183,11 @@ protected function configureFormFields(FormMapper $formMapper) ) )) ->add('parent', 'sonata_type_model', array('required' => false)) - ->add('userRestricted', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ->add('active', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) ->add('clickable', 'checkbox', array('required' => false, 'attr' => $this->formAttribute)) - ; + ->add('userRestricted', 'checkbox', array('required' => false)) + ->add('hideWhenUserConnected', 'checkbox', array('required' => false)) + ; } /** @@ -204,9 +205,10 @@ protected function configureListFields(ListMapper $listMapper) ->add('icon', 'string', array('template' => 'SkillbertoSonataPageMenuBundle:Admin:base_list_field.html.twig')) ->add('page') ->add('parent') - ->add('userRestricted') ->add('active') ->add('clickable') + ->add('userRestricted') + ->add('hideWhenUserConnected') ->add('_action', 'actions', array( 'actions' => array( 'edit' => array(), diff --git a/Entity/Menu.php b/Entity/Menu.php index 13432dc..98abfd8 100644 --- a/Entity/Menu.php +++ b/Entity/Menu.php @@ -13,87 +13,92 @@ class Menu * @var integer */ protected $id; - + /** * @var string */ protected $name; - + /** * @var string */ protected $icon; - + /** * @var boolean */ protected $clickable; - + /** * @var integer */ protected $lft; - + /** * @var integer */ protected $rgt; - + /** * @var integer */ protected $root; - + /** * @var integer */ protected $lvl; - + /** * @var \Doctrine\Common\Collections\Collection */ protected $children; - + /** * @var \Application\Sonata\PageBundle\Entity\Page */ protected $page; - + /** * @var \Application\Sonata\PageBundle\Entity\Site */ protected $site; - + /** * @var \Skillberto\SonataPageMenuBundle\Entity\Menu */ protected $parent; - + /** * @var array */ protected $attribute; - + /** * @var boolean */ protected $active; - + /** * @var boolean */ protected $userRestricted; + /** + * @var boolean + */ + protected $hideWhenUserConnected; + /** * @var \DateTime */ protected $createdAt; - + /** * @var \DateTime */ protected $updatedAt; - + /** * Constructor */ @@ -101,22 +106,22 @@ public function __construct() { $this->children = new \Doctrine\Common\Collections\ArrayCollection(); } - + public function __toString() { - return $this->getName(); + return (string) $this->getName(); } - + /** * Get id * - * @return integer + * @return integer */ public function getId() { return $this->id; } - + /** * Set name * @@ -126,20 +131,20 @@ public function getId() public function setName($name) { $this->name = $name; - + return $this; } - + /** * Get name * - * @return string + * @return string */ public function getName() { return $this->name; } - + /** * Set icon * @@ -162,7 +167,7 @@ public function getIcon() { return $this->icon; } - + /** * Set clickable * @@ -172,20 +177,20 @@ public function getIcon() public function setClickable($clickable) { $this->clickable = $clickable; - + return $this; } - + /** * Get clickable * - * @return boolean + * @return boolean */ public function getClickable() { return $this->clickable; } - + /** * Set lft * @@ -195,20 +200,20 @@ public function getClickable() public function setLft($lft) { $this->lft = $lft; - + return $this; } - + /** * Get lft * - * @return integer + * @return integer */ public function getLft() { return $this->lft; } - + /** * Set rgt * @@ -218,20 +223,20 @@ public function getLft() public function setRgt($rgt) { $this->rgt = $rgt; - + return $this; } - + /** * Get rgt * - * @return integer + * @return integer */ public function getRgt() { return $this->rgt; } - + /** * Set root * @@ -241,20 +246,20 @@ public function getRgt() public function setRoot($root) { $this->root = $root; - + return $this; } - + /** * Get root * - * @return integer + * @return integer */ public function getRoot() { return $this->root; } - + /** * Set lvl * @@ -264,20 +269,20 @@ public function getRoot() public function setLvl($lvl) { $this->lvl = $lvl; - + return $this; } - + /** * Get lvl * - * @return integer + * @return integer */ public function getLvl() { return $this->lvl; } - + /** * Add children * @@ -287,10 +292,10 @@ public function getLvl() public function addChild(\Skillberto\SonataPageMenuBundle\Entity\Menu $children) { $this->children[] = $children; - + return $this; } - + /** * Remove children * @@ -300,17 +305,17 @@ public function removeChild(\Skillberto\SonataPageMenuBundle\Entity\Menu $childr { $this->children->removeElement($children); } - + /** * Get children * - * @return \Doctrine\Common\Collections\Collection + * @return \Doctrine\Common\Collections\Collection */ public function getChildren() { return $this->children; } - + /** * Set page * @@ -320,20 +325,20 @@ public function getChildren() public function setPage(\Application\Sonata\PageBundle\Entity\Page $page = null) { $this->page = $page; - + return $this; } - + /** * Get page * - * @return \Application\Sonata\PageBundle\Entity\Page + * @return \Application\Sonata\PageBundle\Entity\Page */ public function getPage() { return $this->page; } - + /** * Set site * @@ -343,20 +348,20 @@ public function getPage() public function setSite(\Application\Sonata\PageBundle\Entity\Site $site = null) { $this->site = $site; - + return $this; } - + /** * Get site * - * @return \Application\Sonata\PageBundle\Entity\Site + * @return \Application\Sonata\PageBundle\Entity\Site */ public function getSite() { return $this->site; } - + /** * Set parent * @@ -366,20 +371,20 @@ public function getSite() public function setParent(\Skillberto\SonataPageMenuBundle\Entity\Menu $parent = null) { $this->parent = $parent; - + return $this; } - + /** * Get parent * - * @return \Skillberto\SonataPageMenuBundle\Entity\Menu + * @return \Skillberto\SonataPageMenuBundle\Entity\Menu */ public function getParent() { return $this->parent; } - + /** * Set attribute * @@ -389,44 +394,44 @@ public function getParent() public function setAttribute($attribute) { $this->attribute = $attribute; - + return $this; } - + /** * Get attribute * - * @return array + * @return array */ public function getAttribute() { return $this->attribute; } - + /** * Set active * * @param boolean $active * @return Menu - + */ public function setActive($active) { $this->active = $active; - + return $this; } - + /** * Get active * - * @return boolean + * @return boolean */ public function getActive() { return $this->active; } - + /** * Set user restricted * @@ -450,7 +455,31 @@ public function getUserRestricted() { return $this->userRestricted; } - + + /** + * Set hide when user connected + * + * @param boolean $hideWhenUserConnected + * @return Menu + + */ + public function setHideWhenUserConnected($hideWhenUserConnected) + { + $this->hideWhenUserConnected = $hideWhenUserConnected; + + return $this; + } + + /** + * Get hide when user connected + * + * @return boolean + */ + public function getHideWhenUserConnected() + { + return $this->hideWhenUserConnected; + } + /** * Set createdAt * @@ -460,20 +489,20 @@ public function getUserRestricted() public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; - + return $this; } - + /** * Get createdAt * - * @return \DateTime + * @return \DateTime */ public function getCreatedAt() { return $this->createdAt; } - + /** * Set updatedAt * @@ -483,21 +512,21 @@ public function getCreatedAt() public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; - + return $this; } - + /** * Get updatedAt * - * @return \DateTime + * @return \DateTime */ public function getUpdatedAt() { return $this->updatedAt; } - - + + /** * @ORM\PrePersist */ @@ -506,7 +535,7 @@ public function createdAt() $this->setCreatedAt( new \DateTime("now") ); $this->setUpdatedAt( new \DateTime("now") ); } - + /** * @ORM\PostPersist */ diff --git a/MenuBuilder/MainMenuBuilder.php b/MenuBuilder/MainMenuBuilder.php index b32225d..d36e040 100644 --- a/MenuBuilder/MainMenuBuilder.php +++ b/MenuBuilder/MainMenuBuilder.php @@ -77,7 +77,12 @@ protected function createMenuStructure($menus, ItemInterface $root = null) protected function createMenu(Menu $menu, ItemInterface $root = null) { - if(null !== $menu->getParent() && $menu->getParent()->getUserRestricted() && !$this->authorizationChecker->isGranted('ROLE_USER')) { + if((null !== $menu->getParent() && $menu->getParent()->getUserRestricted() || $menu->getUserRestricted()) && + !$this->authorizationChecker->isGranted('ROLE_USER')) { + return false; + } + + if($menu->getHideWhenUserConnected() && $this->authorizationChecker->isGranted('ROLE_USER')) { return false; } diff --git a/Resources/config/doctrine/Menu.orm.yml b/Resources/config/doctrine/Menu.orm.yml index 85139fc..907d610 100644 --- a/Resources/config/doctrine/Menu.orm.yml +++ b/Resources/config/doctrine/Menu.orm.yml @@ -47,6 +47,8 @@ Skillberto\SonataPageMenuBundle\Entity\Menu: type: boolean userRestricted: type: boolean + hideWhenUserConnected: + type: boolean createdAt: type: datetime updatedAt: