Source for file categories.php
Documentation is available at categories.php
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Array to hold the object instances
public static $instances =
array();
* Array of category nodes
* Array of checked categories -- used to save values when _nodes are null
* Name of the extension the categories belong to
* Name of the linked content table to get category content count
* Name of the category field
* Name of the items state field
* @param array $options Array of options
$this->_table =
$options['table'];
$this->_field =
(isset
($options['field']) &&
$options['field']) ?
$options['field'] :
'catid';
$this->_key =
(isset
($options['key']) &&
$options['key']) ?
$options['key'] :
'id';
$this->_statefield =
(isset
($options['statefield'])) ?
$options['statefield'] :
'state';
$options['access'] =
(isset
($options['access'])) ?
$options['access'] :
'true';
$options['published'] =
(isset
($options['published'])) ?
$options['published'] :
1;
* Returns a reference to a JCategories object
* @param string $extension Name of the categories extension
* @param array $options An array of options
* @return JCategories JCategories object
public static function getInstance($extension, $options =
array())
if (isset
(self::$instances[$hash]))
return self::$instances[$hash];
$parts =
explode('.', $extension);
$section =
count($parts) >
1 ?
$parts[1] :
'';
$path =
JPATH_SITE .
'/components/' .
$component .
'/helpers/category.php';
self::$instances[$hash] =
new $classname($options);
return self::$instances[$hash];
* Loads a specific category and all its children in a JCategoryNode object
* @param mixed $id an optional id integer or equal to 'root'
* @param boolean $forceload True to force the _load method to execute
* @return mixed JCategoryNode object or null if $id is not valid
public function get($id =
'root', $forceload =
false)
// If this $id has not been processed yet, execute the _load method
// If we already have a value in _nodes for this $id, then use it.
if (isset
($this->_nodes[$id]))
// If we processed this $id already and it was not valid, then return null.
* @param integer $id Id of category to load
protected function _load($id)
// Record that has this $id has been checked
$query =
$db->getQuery(true);
// Right join with c for category
$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');
$c_id =
$query->castAsChar('c.id');
$case_when .=
$query->concatenate(array($c_id, 'c.alias'), ':');
$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') .
')');
$query->where('c.access IN (' .
implode(',', $user->getAuthorisedViewLevels()) .
')');
$query->where('c.published = 1');
// Note: s for selected id
// Get the selected category
$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');
$db->quoteName($this->_table) .
' AS i ON i.' .
$db->quoteName($this->_field) .
' = c.id AND i.' .
$this->_statefield .
' = 1'
$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');
'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'
$results =
$db->loadObjectList('id');
foreach ($results as $result)
// Deal with root category
if ($result->parent_id ==
1)
$result->parent_id =
'root';
if (!isset
($this->_nodes[$result->id]))
// Create the JCategoryNode and add to _nodes
// 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]);
if ($result->id ==
$id ||
$childrenLoaded)
$this->_nodes[$result->id]->setAllLoaded();
elseif ($result->id ==
$id ||
$childrenLoaded)
// Create the JCategoryNode
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 (!isset
($this->_nodes[$result->parent_id]))
unset
($this->_nodes[$result->id]);
if ($result->id ==
$id ||
$childrenLoaded)
$this->_nodes[$result->id]->setAllLoaded();
* Helper class to load Categorytree
* The id of the category in the asset table
* The id of the parent of category in the asset table, 0 for category root
* The lft value for this category in the category tree
* The rgt value for this category in the category tree
* The depth of this category's position in the category tree
* The extension this category is associated with
* The menu title for the category (a short name)
* The the alias for the category
* Description of the category.
* The publication status of the category
* Whether the category is or is not checked out
* The time at which the category was checked out
* Access level for the category
* JSON string of parameters
* Key words for meta data
* JSON string of other meta data
* The ID of the user who created the category
* The time at which the category was created
* The ID of the user who last modified the category
* The time at which the category was modified
* Nmber of times the category has been viewed
* The language for the category in xx-XX format
* Number of items in this category or descendants of this category
* Number of children items
* Slug fo the category (used in URL)
* Path from root to this category
* Category left of this one
* Category right of this one
* true if all children have been loaded
* Constructor of this tree
* @param array $category The category data.
* @param JCategoryNode $constructor The tree constructor.
public function __construct($category =
null, $constructor =
null)
* Set the parent of this category
* If the category already has a parent, the link is unset
* @param mixed $parent JCategoryNode for the parent to be set or null
unset
($this->_parent->_children[$key]);
$parent->_children[] =
& $this;
$this->_path =
$parent->getPath();
if (count($parent->_children) >
1)
* If the child already has a parent, the link is unset
* @param JCategoryNode $child The child to be added.
$child->setParent($this);
* Remove a specific child
* @param integer $id ID of a category
unset
($this->_parent->_children[$key]);
* Get the children of this node
* @param boolean $recursive False by default
* @return array The children
$items =
array_merge($items, $child->getChildren(true));
* Get the parent of this node
* @return mixed JCategoryNode or null
* Test if this node has children
* @return boolean True if there is a child
* Test if this node has a parent
* @return boolean True if there is a parent
* Function to set the left or right sibling of a category
* @param JCategoryNode $sibling JCategoryNode object for the sibling
* @param boolean $right If set to false, the sibling is the left one
public function setSibling($sibling, $right =
true)
* Returns the right or left sibling of a category
* @param boolean $right If set to false, returns the left sibling
* @return mixed JCategoryNode object with the sibling information or
* NULL if there is no sibling on that side.
* Returns the category parameters
$temp->loadString($this->params);
* Returns the category metadata
* @return JRegistry A JRegistry object containing the metadata
* Returns the category path to the root category
* Returns the user that created the category
* @param boolean $modified_user Returns the modified_user when set to true
* @return JUser A JUser object containing a userid
public function getAuthor($modified_user =
false)
* Set to load all children
* Returns the number of items.
* @param boolean $recursive If false number of children, if true number of descendants
* @return integer Number of children or descendants
$count =
$count +
$child->getNumItems(true);
Documentation generated on Tue, 19 Nov 2013 14:55:09 +0100 by phpDocumentor 1.4.3