Back to Categories class

Method _load

protected void
_load
(mixed $id)
Load method
Parameters
  • int $id Id of category to load
Returns
  • void
Since
  • 1.6
Class: Categories
Project: Joomla

Method _load - Source code

/**
 * Load method
 *
 * @param   integer  $id  Id of category to load
 *
 * @return  void
 *
 * @since   1.6
 */
protected function _load($id)
{
    /** @var \Joomla\Database\DatabaseDriver */
    $db = Factory::getDbo();
    $app = Factory::getApplication();
    $user = Factory::getUser();
    $extension = $this->_extension;
    if ($id !== 'root') {
        $id = (int) $id;
        if ($id === 0) {
            $id = 'root';
        }
    }
    // Record that has this $id has been checked
    $this->_checkedCategories[$id] = true;
    $query = $db->getQuery(true)->select([$db->quoteName('c.id'), $db->quoteName('c.asset_id'), $db->quoteName('c.access'), $db->quoteName('c.alias'), $db->quoteName('c.checked_out'), $db->quoteName('c.checked_out_time'), $db->quoteName('c.created_time'), $db->quoteName('c.created_user_id'), $db->quoteName('c.description'), $db->quoteName('c.extension'), $db->quoteName('c.hits'), $db->quoteName('c.language'), $db->quoteName('c.level'), $db->quoteName('c.lft'), $db->quoteName('c.metadata'), $db->quoteName('c.metadesc'), $db->quoteName('c.metakey'), $db->quoteName('c.modified_time'), $db->quoteName('c.note'), $db->quoteName('c.params'), $db->quoteName('c.parent_id'), $db->quoteName('c.path'), $db->quoteName('c.published'), $db->quoteName('c.rgt'), $db->quoteName('c.title'), $db->quoteName('c.modified_user_id'), $db->quoteName('c.version')]);
    $case_when = ' CASE WHEN ';
    $case_when .= $query->charLength($db->quoteName('c.alias'), '!=', '0');
    $case_when .= ' THEN ';
    $c_id = $query->castAsChar($db->quoteName('c.id'));
    $case_when .= $query->concatenate(array($c_id, $db->quoteName('c.alias')), ':');
    $case_when .= ' ELSE ';
    $case_when .= $c_id . ' END as ' . $db->quoteName('slug');
    $query->select($case_when)->where('(' . $db->quoteName('c.extension') . ' = :extension OR ' . $db->quoteName('c.extension') . ' = ' . $db->quote('system') . ')')->bind(':extension', $extension);
    if ($this->_options['access']) {
        $groups = $user->getAuthorisedViewLevels();
        $query->whereIn($db->quoteName('c.access'), $groups);
    }
    if ($this->_options['published'] == 1) {
        $query->where($db->quoteName('c.published') . ' = 1');
    }
    $query->order($db->quoteName('c.lft'));
    // Note: s for selected id
    if ($id !== 'root') {
        // Get the selected category
        $query->from($db->quoteName('#__categories', 's'))->where($db->quoteName('s.id') . ' = :id')->bind(':id', $id, ParameterType::INTEGER);
        if ($app->isClient('site') && Multilanguage::isEnabled()) {
            // For the most part, we use c.lft column, which index is properly used instead of c.rgt
            $query->join('INNER', $db->quoteName('#__categories', 'c'), '(' . $db->quoteName('s.lft') . ' < ' . $db->quoteName('c.lft') . ' AND ' . $db->quoteName('c.lft') . ' < ' . $db->quoteName('s.rgt') . ' AND ' . $db->quoteName('c.language') . ' IN (' . implode(',', $query->bindArray([Factory::getLanguage()->getTag(), '*'], ParameterType::STRING)) . '))' . ' OR (' . $db->quoteName('c.lft') . ' <= ' . $db->quoteName('s.lft') . ' AND ' . $db->quoteName('s.rgt') . ' <= ' . $db->quoteName('c.rgt') . ')');
        } else {
            $query->join('INNER', $db->quoteName('#__categories', 'c'), '(' . $db->quoteName('s.lft') . ' <= ' . $db->quoteName('c.lft') . ' AND ' . $db->quoteName('c.lft') . ' < ' . $db->quoteName('s.rgt') . ')' . ' OR (' . $db->quoteName('c.lft') . ' < ' . $db->quoteName('s.lft') . ' AND ' . $db->quoteName('s.rgt') . ' < ' . $db->quoteName('c.rgt') . ')');
        }
    } else {
        $query->from($db->quoteName('#__categories', 'c'));
        if ($app->isClient('site') && Multilanguage::isEnabled()) {
            $query->whereIn($db->quoteName('c.language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
        }
    }
    // Note: i for item
    if ($this->_options['countItems'] == 1) {
        $subQuery = $db->getQuery(true)->select('COUNT(' . $db->quoteName($db->escape('i.' . $this->_key)) . ')')->from($db->quoteName($db->escape($this->_table), 'i'))->where($db->quoteName($db->escape('i.' . $this->_field)) . ' = ' . $db->quoteName('c.id'));
        if ($this->_options['published'] == 1) {
            $subQuery->where($db->quoteName($db->escape('i.' . $this->_statefield)) . ' = 1');
        }
        if ($this->_options['currentlang'] !== 0) {
            $subQuery->where($db->quoteName('i.language') . ' IN (' . implode(',', $query->bindArray([$this->_options['currentlang'], '*'], ParameterType::STRING)) . ')');
        }
        $query->select('(' . $subQuery . ') AS ' . $db->quoteName('numitems'));
    }
    // Get the results
    $db->setQuery($query);
    $results = $db->loadObjectList('id');
    $childrenLoaded = false;
    if (\count($results)) {
        // Foreach categories
        foreach ($results as $result) {
            // Deal with root category
            if ($result->id == 1) {
                $result->id = 'root';
            }
            // Deal with parent_id
            if ($result->parent_id == 1) {
                $result->parent_id = 'root';
            }
            // Create the node
            if (!isset($this->_nodes[$result->id])) {
                // Create the CategoryNode and add to _nodes
                $this->_nodes[$result->id] = new CategoryNode($result, $this);
                // If this is not root and if the current node's parent is in the list or the current node parent is 0
                if ($result->id !== 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id == 1)) {
                    // Compute relationship between node and its parent - set the parent in the _nodes field
                    $this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
                }
                // If the node's parent id is not in the _nodes list and the node is not root (doesn't have parent_id == 0),
                // then remove the node from the list
                if (!(isset($this->_nodes[$result->parent_id]) || $result->parent_id == 0)) {
                    unset($this->_nodes[$result->id]);
                    continue;
                }
                if ($result->id == $id || $childrenLoaded) {
                    $this->_nodes[$result->id]->setAllLoaded();
                    $childrenLoaded = true;
                }
            } elseif ($result->id == $id || $childrenLoaded) {
                // Create the CategoryNode
                $this->_nodes[$result->id] = new CategoryNode($result, $this);
                if ($result->id !== 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id)) {
                    // Compute relationship between node and its parent
                    $this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
                }
                // If the node's parent id is not in the _nodes list and the node is not root (doesn't have parent_id == 0),
                // then remove the node from the list
                if (!(isset($this->_nodes[$result->parent_id]) || $result->parent_id == 0)) {
                    unset($this->_nodes[$result->id]);
                    continue;
                }
                if ($result->id == $id || $childrenLoaded) {
                    $this->_nodes[$result->id]->setAllLoaded();
                    $childrenLoaded = true;
                }
            }
        }
    } else {
        $this->_nodes[$id] = null;
    }
}