Back to Menu class

Method linkOptions

public static string
linkOptions
(mixed $all = false, mixed $unassigned = false, mixed $clientId = 0)
Build the multiple select list for Menu Links/Pages
Parameters
  • bool $all True if all can be selected
  • bool $unassigned True if unassigned can be selected
  • int $clientId The client id
Returns
  • string
Since
  • 1.5
Class: Menu
Project: Joomla

Method linkOptions - Source code

/**
 * Build the multiple select list for Menu Links/Pages
 *
 * @param   boolean  $all         True if all can be selected
 * @param   boolean  $unassigned  True if unassigned can be selected
 * @param   int      $clientId    The client id
 *
 * @return  string
 *
 * @since   1.5
 */
public static function linkOptions($all = false, $unassigned = false, $clientId = 0)
{
    $db = Factory::getDbo();
    // Get a list of the menu items
    $query = $db->getQuery(true)->select([$db->quoteName('m.id'), $db->quoteName('m.parent_id'), $db->quoteName('m.title'), $db->quoteName('m.menutype'), $db->quoteName('m.client_id')])->from($db->quoteName('#__menu', 'm'))->where($db->quoteName('m.published') . ' = 1')->order([$db->quoteName('m.client_id'), $db->quoteName('m.menutype'), $db->quoteName('m.parent_id')]);
    if (isset($clientId)) {
        $clientId = (int) $clientId;
        $query->where($db->quoteName('m.client_id') . ' = :client')->bind(':client', $clientId, ParameterType::INTEGER);
    }
    $db->setQuery($query);
    $mitems = $db->loadObjectList();
    if (!$mitems) {
        $mitems = array();
    }
    // Establish the hierarchy of the menu
    $children = array();
    // First pass - collect children
    foreach ($mitems as $v) {
        $pt = $v->parent_id;
        $list = @$children[$pt] ? $children[$pt] : array();
        $list[] = $v;
        $children[$pt] = $list;
    }
    // Second pass - get an indent list of the items
    $list = static::treerecurse((int) $mitems[0]->parent_id, '', array(), $children, 9999, 0, 0);
    // Code that adds menu name to Display of Page(s)
    $mitems = array();
    if ($all | $unassigned) {
        $mitems[] = HTMLHelper::_('select.option', '<OPTGROUP>', Text::_('JOPTION_MENUS'));
        if ($all) {
            $mitems[] = HTMLHelper::_('select.option', 0, Text::_('JALL'));
        }
        if ($unassigned) {
            $mitems[] = HTMLHelper::_('select.option', -1, Text::_('JOPTION_UNASSIGNED'));
        }
        $mitems[] = HTMLHelper::_('select.option', '</OPTGROUP>');
    }
    $lastMenuType = null;
    $tmpMenuType = null;
    foreach ($list as $list_a) {
        if ($list_a->menutype != $lastMenuType) {
            if ($tmpMenuType) {
                $mitems[] = HTMLHelper::_('select.option', '</OPTGROUP>');
            }
            $mitems[] = HTMLHelper::_('select.option', '<OPTGROUP>', $list_a->menutype);
            $lastMenuType = $list_a->menutype;
            $tmpMenuType = $list_a->menutype;
        }
        $mitems[] = HTMLHelper::_('select.option', $list_a->id, $list_a->title);
    }
    if ($lastMenuType !== null) {
        $mitems[] = HTMLHelper::_('select.option', '</OPTGROUP>');
    }
    return $mitems;
}