Beispiel #1
0
 public static function buildMenu($menus, $parse_hooks = true)
 {
     $items = array();
     $user = cmsUser::getInstance();
     // перебираем все вернувшиеся пункты меню
     foreach ($menus as $item) {
         $is_root_added = false;
         if ($item['groups_view'] && !$user->isInGroups($item['groups_view'])) {
             continue;
         }
         if ($item['groups_hide'] && $user->isInGroups($item['groups_hide'])) {
             continue;
         }
         $hook_result = array('items' => false);
         if ($parse_hooks) {
             // если URL пункта меню содержит свойство пользователя
             if (strpos($item['url'], '{user.') !== false) {
                 $item['url'] = string_replace_user_properties($item['url']);
             } else {
                 // если URL пункта меню содержит шаблон {controller:action}
                 if (preg_match('/^{([a-z0-9]+):*([a-z0-9_]*)}$/i', $item['url'], $matches)) {
                     // то вызываем хук menu указанного контроллера
                     $controller = $matches[1];
                     $action = $matches[2];
                     $hook_result = cmsEventsManager::hook("menu_{$controller}", array('action' => $action, 'menu_item_id' => $item['id'], 'menu_item_url' => $item['url']));
                     // если хук вернул результат
                     if ($hook_result) {
                         // получаем новый URL пункта меню
                         $item['url'] = isset($hook_result['url']) ? $hook_result['url'] : '';
                         if (isset($hook_result['counter'])) {
                             $item['counter'] = $hook_result['counter'];
                         }
                         if (isset($hook_result['items']) && is_array($hook_result['items'])) {
                             $item['childs_count'] = sizeof($hook_result['items']);
                         }
                         $is_root_added = true;
                     } else {
                         continue;
                     }
                 }
             }
             $is_external = mb_strpos($item['url'], '://') !== false;
             if (!$is_root_added && !$is_external) {
                 $item['url'] = href_to($item['url']);
             }
         }
         // добавляем обработанную строку в результирующий массив
         $items[$item['id']] = $item;
         // получаем дополнительные пункты меню
         if (isset($hook_result['items']) && is_array($hook_result['items'])) {
             foreach ($hook_result['items'] as $i) {
                 $i['menu_id'] = $item['menu_id'];
                 $items[$i['id']] = $i;
             }
         }
     }
     $tree = array();
     cmsModel::buildTreeRecursive($items, $tree);
     // возвращаем дерево
     return $tree;
 }