Back to Menu class

Method menuItems

public static array
(mixed $config = array())
Returns an array of menu items grouped by menu.
Parameters
  • array $config An array of configuration options [published, checkacl, clientid].
Returns
  • array
Since
  • 1.6
Class: Menu
Project: Joomla

Method menuItems - Source code

/**
 * Returns an array of menu items grouped by menu.
 *
 * @param   array  $config  An array of configuration options [published, checkacl, clientid].
 *
 * @return  array
 *
 * @since   1.6
 */
public static function menuItems($config = array())
{
    $key = serialize($config);
    if (empty(static::$items[$key])) {
        // B/C - not passed  = 0, null can be passed for both clients
        $clientId = array_key_exists('clientid', $config) ? $config['clientid'] : 0;
        $menus = static::menus($clientId);
        $db = Factory::getDbo();
        $query = $db->getQuery(true)->select([$db->quoteName('a.id', 'value'), $db->quoteName('a.title', 'text'), $db->quoteName('a.level'), $db->quoteName('a.menutype'), $db->quoteName('a.client_id')])->from($db->quoteName('#__menu', 'a'))->where($db->quoteName('a.parent_id') . ' > 0');
        // Filter on the client id
        if (isset($clientId)) {
            $query->where($db->quoteName('a.client_id') . ' = :client')->bind(':client', $clientId, ParameterType::INTEGER);
        }
        // Filter on the published state
        if (isset($config['published'])) {
            if (is_numeric($config['published'])) {
                $query->where($db->quoteName('a.published') . ' = :published')->bind(':published', $config['published'], ParameterType::INTEGER);
            } elseif ($config['published'] === '') {
                $query->where($db->quoteName('a.published') . ' IN (0,1)');
            }
        }
        $query->order($db->quoteName('a.lft'));
        $db->setQuery($query);
        $items = $db->loadObjectList();
        // Collate menu items based on menutype
        $lookup = array();
        foreach ($items as &$item) {
            if (!isset($lookup[$item->menutype])) {
                $lookup[$item->menutype] = array();
            }
            $lookup[$item->menutype][] =& $item;
            // Translate the menu item title when client is administrator
            if ($clientId === 1) {
                $item->text = Text::_($item->text);
            }
            $item->text = str_repeat('- ', $item->level) . $item->text;
        }
        static::$items[$key] = array();
        $user = Factory::getUser();
        $aclcheck = !empty($config['checkacl']) ? (int) $config['checkacl'] : 0;
        foreach ($menus as &$menu) {
            if ($aclcheck) {
                $action = $aclcheck == $menu->id ? 'edit' : 'create';
                if (!$user->authorise('core.' . $action, 'com_menus.menu.' . $menu->id)) {
                    continue;
                }
            }
            // Start group:
            $optGroup = new \stdClass();
            $optGroup->value = '<OPTGROUP>';
            $optGroup->text = $menu->text;
            static::$items[$key][] = $optGroup;
            // Special "Add to this Menu" option:
            static::$items[$key][] = HTMLHelper::_('select.option', $menu->value . '.1', Text::_('JLIB_HTML_ADD_TO_THIS_MENU'));
            // Menu items:
            if (isset($lookup[$menu->value])) {
                foreach ($lookup[$menu->value] as &$item) {
                    static::$items[$key][] = HTMLHelper::_('select.option', $menu->value . '.' . $item->value, $item->text);
                }
            }
            // Finish group:
            $closeOptGroup = new \stdClass();
            $closeOptGroup->value = '</OPTGROUP>';
            $closeOptGroup->text = $menu->text;
            static::$items[$key][] = $closeOptGroup;
        }
    }
    return static::$items[$key];
}