Ip\Menu\Helper::getMenuItems PHP Method

getMenuItems() public static method

Common usage: Get items of a first menu level (to display on top of the site), e.g., $result = \Ip\Menu\Helper::getMenuItems('menu1', 1, 1); Get 7 levels of children of selected page on top menu (to display on a side): $result = \Ip\Menu\Helper::getMenuItems('menu1', 2, 7); Pass the result to ipSlot('menu', array('items' => $result)); Please note, that items of a child level can only be returned if a selected page is in a breadcrumb parent page. In opposite case, the function returns an empty array.
public static getMenuItems ( string $menuName, integer $depthFrom = 1, integer $depthTo = 1000, string $orderBy = null ) : array
$menuName string eg menu1
$depthFrom integer
$depthTo integer
$orderBy string can be set to 'title' to change ordering
return array
    public static function getMenuItems($menuName, $depthFrom = 1, $depthTo = 1000, $orderBy = null)
    {
        if ($orderBy == 'title') {
            $order = '`title`';
        } else {
            $order = '`pageOrder`';
        }
        // variable check
        if ($depthFrom < 1) {
            $backtrace = debug_backtrace();
            if (isset($backtrace[0]['file']) && $backtrace[0]['line']) {
                throw new \Ip\Exception('$depthFrom can\'t be less than one. (Error source: ' . $backtrace[0]['file'] . ' line: ' . $backtrace[0]['line'] . ' ) ');
            } else {
                throw new \Ip\Exception('$depthFrom can\'t be less than one.');
            }
        }
        if ($depthTo < $depthFrom) {
            $backtrace = debug_backtrace();
            if (isset($backtrace[0]['file']) && $backtrace[0]['line']) {
                throw new \Ip\Exception('$depthTo can\'t be lower than $depthFrom. (Error source: ' . $backtrace[0]['file'] . ' line: ' . $backtrace[0]['line'] . ' ) ');
            } else {
                throw new \Ip\Exception('$depthTo can\'t be lower than $depthFrom.');
            }
        }
        // end variable check
        $breadcrumb = ipContent()->getBreadcrumb();
        $languageCode = ipContent()->getCurrentLanguage()->getCode();
        $menuRootId = ipDb()->selectValue('page', 'id', array('languageCode' => $languageCode, 'alias' => $menuName, 'isDeleted' => 0));
        if ($depthFrom == 1) {
            $elements = ipDb()->selectAll('page', '*', array('isVisible' => 1, 'isSecured' => 0, 'parentId' => $menuRootId, 'isDeleted' => 0), "ORDER BY {$order}");
            //get first level elements
        } elseif (isset($breadcrumb[$depthFrom - 2])) {
            // if we need a second level (2), we need to find a parent element at first level. And it is at position 0. This is where -2 comes from.
            if (!empty($breadcrumb[0])) {
                $rootPage = ipContent()->getPage($breadcrumb[0]->getParentId());
                if ($rootPage && $rootPage->getAlias() == $menuName) {
                    $parent = $breadcrumb[$depthFrom - 2];
                    $elements = ipDb()->selectAll('page', '*', array('isVisible' => 1, 'isSecured' => 0, 'parentId' => $parent->getId(), 'isDeleted' => 0), "ORDER BY {$order}");
                } else {
                    $elements = array();
                }
            } else {
                $elements = array();
            }
        }
        $items = array();
        if (!empty($elements)) {
            $items = self::arrayToMenuItem($elements, $depthTo, $depthFrom, $order);
        }
        return $items;
    }

Usage Example

Esempio n. 1
0
 /**
  * @desc Generate menu with custom ul ID and class
  * @author Allan Laal <*****@*****.**>
  * @param array $params
  * @return string
  */
 public static function menu_80($params)
 {
     $data = array('items' => null, 'depth' => 1, 'active' => 'active', 'crumb' => 'crumb', 'disabled' => 'disabled', 'parent' => 'parent', 'children' => 'children', 'view' => 'Ip/Internal/Config/view/menu.php');
     if (is_string($params)) {
         $params = array('items' => $params);
     }
     if (!empty($params[0]) && is_object($params[0]) && $params[0] instanceof \Ip\Menu\Item) {
         $params = array('items' => $params);
     }
     $data = array_merge($data, $params);
     // pass params to View along with other data
     if (isset($params['items']) && is_string($params['items'])) {
         $data['items'] = \Ip\Menu\Helper::getMenuItems($params['items']);
     }
     if (empty($data['attributes']) || !is_array($data['attributes'])) {
         $data['attributes'] = array();
     }
     //generate attributes str
     if (empty($data['attributes']['class'])) {
         $data['attributes']['class'] = '';
     }
     $data['attributes']['class'] = 'level' . $data['depth'] . ' ' . $data['attributes']['class'];
     $data['attributesStr'] = join(' ', array_map(function ($sKey) use($data) {
         if (is_bool($data['attributes'][$sKey])) {
             return $data['attributes'][$sKey] ? $sKey : '';
         }
         return $sKey . '="' . $data['attributes'][$sKey] . '"';
     }, array_keys($data['attributes'])));
     $view = ipView($data['view'], $data);
     return $view->render();
 }
All Usage Examples Of Ip\Menu\Helper::getMenuItems