{
$this->_checkedCategories[$id] = true;
$query = $db->getQuery(true);
$query->select('c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version');
$case_when = ' CASE WHEN ';
$case_when .= $query->charLength('c.alias', '!=', '0');
$case_when .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$case_when .= $query->concatenate(array($c_id, 'c.alias'), ':');
$case_when .= ' ELSE ';
$case_when .= $c_id . ' END as slug';
$query->select($case_when)
->from('#__categories as c')
->where('(c.extension=' . $db->quote($extension) . ' OR c.extension=' . $db->quote('system') . ')');
if ($this->_options['access'])
{
$query->where('c.access IN (' . implode(',', $user->getAuthorisedViewLevels()) . ')');
}
if ($this->_options['published'] == 1)
{
$query->where('c.published = 1');
}
$query->order('c.lft');
if ($id != 'root')
{
$query->join('LEFT', '#__categories AS s ON (s.lft <= c.lft AND s.rgt >= c.rgt) OR (s.lft > c.lft AND s.rgt < c.rgt)')
->where('s.id=' . (int) $id);
}
$subQuery = ' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ' .
'ON cat.lft BETWEEN parent.lft AND parent.rgt WHERE parent.extension = ' . $db->quote($extension) .
' AND parent.published != 1 GROUP BY cat.id) ';
$query->join('LEFT', $subQuery . 'AS badcats ON badcats.id = c.id')
->where('badcats.id is null');
if (isset($this->_options['countItems']) && $this->_options['countItems'] == 1)
{
if ($this->_options['published'] == 1)
{
$query->join(
'LEFT',
$db->quoteName($this->_table) . ' AS i ON i.' . $db->quoteName($this->_field) . ' = c.id AND i.' . $this->_statefield . ' = 1'
);
}
else
{
$query->join('LEFT', $db->quoteName($this->_table) . ' AS i ON i.' . $db->quoteName($this->_field) . ' = c.id');
}
$query->select('COUNT(i.' . $db->quoteName($this->_key) . ') AS numitems');
}
$query->group(
'c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version'
);
$db->setQuery($query);
$results = $db->loadObjectList('id');
$childrenLoaded = false;
if (count($results))
{
foreach ($results as $result)
{
if ($result->id == 1)
{
$result->id = 'root';
}
if ($result->parent_id == 1)
{
$result->parent_id = 'root';
}
if (!isset($this->_nodes[$result->id]))
{
if ($result->id != 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id == 1))
{
$this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
}
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)
{
if ($result->id != 'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id))
{
$this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
}
if (!isset($this->_nodes[$result->parent_id]))
{
unset($this->_nodes[$result->id]);
continue;
}
if ($result->id == $id || $childrenLoaded)
{
$this->_nodes[$result->id]->setAllLoaded();
$childrenLoaded = true;
}
}
}
}
else
{
$this->_nodes[$id] = null;
}
}