Example #1
0
 /**
  * Create element
  * 
  * Create a navbar element
  * from an element array
  * configuration
  * 
  * @param string $name   - the element name
  * @param array  $array  - the element configuration
  * @param router $router - the application router
  * 
  * @throws \Exception
  * @return NavBar - the current instance
  */
 protected function createElement($name, $array, Router $router)
 {
     $element = new NavBarElement();
     $element->setName($name)->setLabel($name);
     foreach ($array as $key => $value) {
         switch ($key) {
             case 'label':
                 $element->setLabel($value);
                 break;
             case 'roles':
                 $element->setRoles($value);
                 break;
             case 'path':
                 $element->setPath($this->generateRoute($name, $value, $router));
                 break;
             case 'url':
                 $element->setPath($value);
                 break;
             case 'parent':
                 // nothing
                 break;
             case 'options':
                 $element->setOptions($value);
                 break;
             case 'position':
                 $element->setPosition($value);
                 break;
             default:
                 throw new \Exception(sprintf("The '%s' key for navbar element does not exist. Allowed : 'label', 'roles', 'path', 'parent', 'url', 'options', 'position'", $value), 500);
         }
     }
     return $element;
 }
Example #2
0
 /**
  * Check role
  * 
  * Verify if the user has
  * granted the needed roles
  * of the element and it's 
  * childs and remove it if 
  * not
  * 
  * @param NavBarElement $element  - the element to valid
  * @param array         $userRole - the user roles array
  * 
  * @return boolean
  */
 protected function checkRole(&$element, $userRole)
 {
     if ($element instanceof NavBarElement) {
         $elementRoles = $element->getRoles();
         if (!is_array($elementRoles)) {
             $elementRoles = array();
         }
         foreach ($elementRoles as $role) {
             if (!in_array($role, $userRole)) {
                 return false;
             }
         }
         $childs = $element->getChild();
         if (!empty($childs)) {
             foreach ($childs as $key => $child) {
                 if (!$this->checkRole($child, $userRole)) {
                     unset($childs[$key]);
                 }
             }
         }
         $element->setChild($childs);
     }
     return true;
 }