Back to StandardRules class

Method build

public void
build
(mixed &$query, mixed &$segments)
Build a standard URL
Parameters
  • array & $query The vars that should be converted
  • array & $segments The URL segments to create
Returns
  • void
Since
  • 3.4
Class: StandardRules
Project: Joomla

Method build - Source code

/**
 * Build a standard URL
 *
 * @param   array  &$query     The vars that should be converted
 * @param   array  &$segments  The URL segments to create
 *
 * @return  void
 *
 * @since   3.4
 */
public function build(&$query, &$segments)
{
    if (!isset($query['Itemid'], $query['view'])) {
        return;
    }
    // Get the menu item belonging to the Itemid that has been found
    $item = $this->router->menu->getItem($query['Itemid']);
    if ($item === null || $item->component !== 'com_' . $this->router->getName() || !isset($item->query['view'])) {
        return;
    }
    // Get menu item layout
    $mLayout = isset($item->query['layout']) ? $item->query['layout'] : null;
    // Get all views for this component
    $views = $this->router->getViews();
    // Return directly when the URL of the Itemid is identical with the URL to build
    if ($item->query['view'] === $query['view']) {
        $view = $views[$query['view']];
        if (!$view->key) {
            unset($query['view']);
            if (isset($query['layout']) && $mLayout === $query['layout']) {
                unset($query['layout']);
            }
            return;
        }
        if (isset($query[$view->key]) && $item->query[$view->key] == (int) $query[$view->key]) {
            unset($query[$view->key]);
            while ($view) {
                unset($query[$view->parent_key]);
                $view = $view->parent;
            }
            unset($query['view']);
            if (isset($query['layout']) && $mLayout === $query['layout']) {
                unset($query['layout']);
            }
            return;
        }
    }
    // Get the path from the view of the current URL and parse it to the menu item
    $path = array_reverse($this->router->getPath($query), true);
    $found = false;
    foreach ($path as $element => $ids) {
        $view = $views[$element];
        if ($found === false && $item->query['view'] === $element) {
            if ($view->nestable) {
                $found = true;
            } elseif ($view->children) {
                $found = true;
                continue;
            }
        }
        if ($found === false) {
            // Jump to the next view
            continue;
        }
        if ($ids) {
            if ($view->nestable) {
                $found2 = false;
                foreach (array_reverse($ids, true) as $id => $segment) {
                    if ($found2) {
                        $segments[] = str_replace(':', '-', $segment);
                    } elseif ((int) $item->query[$view->key] === (int) $id) {
                        $found2 = true;
                    }
                }
            } elseif ($ids === true) {
                $segments[] = $element;
            } else {
                $segments[] = str_replace(':', '-', current($ids));
            }
        }
        if ($view->parent_key) {
            // Remove parent key from query
            unset($query[$view->parent_key]);
        }
    }
    if ($found) {
        unset($query[$views[$query['view']]->key], $query['view']);
        if (isset($query['layout']) && $mLayout === $query['layout']) {
            unset($query['layout']);
        }
    }
}