diff --git a/Admin/MenuAdmin.php b/Admin/MenuAdmin.php index cac7b43..798e6c3 100644 --- a/Admin/MenuAdmin.php +++ b/Admin/MenuAdmin.php @@ -167,6 +167,7 @@ protected function configureFormFields(FormMapper $formMapper) $formMapper ->add('name', 'text') + ->add('icon', 'text', array('required' => false)) ->add('page', PageSelectorType::class, array( 'site' => $this->siteInstance, 'model_manager' => $this->getModelManager(), @@ -181,7 +182,9 @@ protected function configureFormFields(FormMapper $formMapper) ->add('parent', 'sonata_type_model', array('required' => false)) ->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)) + ; } /** @@ -196,14 +199,14 @@ 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('active') ->add('clickable') - ->add( - '_action', - 'actions', - array( + ->add('userRestricted') + ->add('hideWhenUserConnected') + ->add('_action', 'actions', array( 'actions' => array( 'edit' => array(), 'delete' => array(), diff --git a/Entity/Menu.php b/Entity/Menu.php index 00a3257..98abfd8 100644 --- a/Entity/Menu.php +++ b/Entity/Menu.php @@ -13,77 +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 */ @@ -91,22 +106,22 @@ public function __construct() { $this->children = new \Doctrine\Common\Collections\ArrayCollection(); } - + public function __toString() { return (string) $this->getName(); } - + /** * Get id * - * @return integer + * @return integer */ public function getId() { return $this->id; } - + /** * Set name * @@ -116,20 +131,43 @@ 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 + * + * @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 * @@ -139,20 +177,20 @@ public function getName() public function setClickable($clickable) { $this->clickable = $clickable; - + return $this; } - + /** * Get clickable * - * @return boolean + * @return boolean */ public function getClickable() { return $this->clickable; } - + /** * Set lft * @@ -162,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 * @@ -185,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 * @@ -208,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 * @@ -231,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 * @@ -254,10 +292,10 @@ public function getLvl() public function addChild(\Skillberto\SonataPageMenuBundle\Entity\Menu $children) { $this->children[] = $children; - + return $this; } - + /** * Remove children * @@ -267,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 * @@ -287,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 * @@ -310,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 * @@ -333,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 * @@ -356,44 +394,92 @@ 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 // Add your code here - + * @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 + * + * @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 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 * @@ -403,20 +489,20 @@ public function getActive() public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; - + return $this; } - + /** * Get createdAt * - * @return \DateTime + * @return \DateTime */ public function getCreatedAt() { return $this->createdAt; } - + /** * Set updatedAt * @@ -426,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 */ @@ -449,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 3bc5dac..e435cb3 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,15 @@ protected function createMenuStructure($menus, ItemInterface $root = null) protected function createMenu(Menu $menu, ItemInterface $root = null) { + 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; + } + $currentItem = $this->createMenuItem($menu); $level = $menu->getLvl(); @@ -82,14 +93,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 ($level == 1 && $menu->getChildren()->count() > 0) { - $currentMenu->setExtra('dropdown', true); - } } if (count($menu->getChildren()) > 0 && ($menu->getActive() or $level == 0)) { @@ -135,4 +142,4 @@ protected function putChildAttributes(ItemInterface $itemInterface) { $itemInterface->setAttribute("class", ""); } -} \ No newline at end of file +} diff --git a/Resources/config/doctrine/Menu.orm.yml b/Resources/config/doctrine/Menu.orm.yml index 820d405..907d610 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,10 @@ Skillberto\SonataPageMenuBundle\Entity\Menu: # - sortablePosition active: type: boolean + userRestricted: + type: boolean + hideWhenUserConnected: + type: boolean createdAt: type: datetime updatedAt: diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 51b7c2a..9d09a7e 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -18,6 +18,7 @@ + diff --git a/Resources/views/Admin/base_list_field.html.twig b/Resources/views/Admin/base_list_field.html.twig index 0b69115..bab750e 100755 --- a/Resources/views/Admin/base_list_field.html.twig +++ b/Resources/views/Admin/base_list_field.html.twig @@ -9,12 +9,12 @@ 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 = "" %} - {% 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 %} @@ -28,22 +28,60 @@ 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 %} - {{ block('field') }} + {% if field_description.name == 'icon' %} + + {% else %} + {{ block('field') }} + {% endif %} {% endif %} {% endif %} diff --git a/Resources/views/Admin/list__action_activate.html.twig b/Resources/views/Admin/list__action_activate.html.twig index fa4ce90..ea49648 100644 --- a/Resources/views/Admin/list__action_activate.html.twig +++ b/Resources/views/Admin/list__action_activate.html.twig @@ -1,4 +1,4 @@ -{% if admin.isGranted('EDIT', object) and admin.hasRoute('activate') %} +{% if admin.hasAccess('edit', object) and admin.hasRoute('activate') %} {% if object.active %} {% set action = 'action_inactivate' %} {% set icon = 'fa-circle-o' %} 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 %}