Source for file taxonomy.php
Documentation is available at taxonomy.php
* @package Joomla.Administrator
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Stemmer base class for the Finder indexer package.
* @package Joomla.Administrator
* An internal cache of taxonomy branch data.
public static $branches =
array();
* An internal cache of taxonomy node data.
public static $nodes =
array();
* Method to add a branch to the taxonomy tree.
* @param string $title The title of the branch.
* @param integer $state The published state of the branch. [optional]
* @param integer $access The access state of the branch. [optional]
* @return integer The id of the branch.
* @throws Exception on database error.
public static function addBranch($title, $state =
1, $access =
1)
// Check to see if the branch is in the cache.
if (isset
(self::$branches[$title]))
return self::$branches[$title]->id;
// Check to see if the branch is in the table.
$db =
JFactory::getDbo();
$query =
$db->getQuery(true)
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') .
' = 1')
->where($db->quoteName('title') .
' = ' .
$db->quote($title));
$result =
$db->loadObject();
// Check if the database matches the input data.
if (!empty($result) &&
$result->state ==
$state &&
$result->access ==
$access)
// The data matches, add the item to the cache.
self::$branches[$title] =
$result;
return self::$branches[$title]->id;
* The database did not match the input. This could be because the
* state has changed or because the branch does not exist. Let's figure
* out which case is true and deal with it.
// Prepare the branch object.
$branch->state = (int)
$state;
$branch->access = (int)
$access;
// Prepare the branch object.
$branch->id = (int)
$result->id;
$branch->parent_id = (int)
$result->parent_id;
$branch->title =
$result->title;
$branch->state = (int)
$result->title;
$branch->access = (int)
$result->access;
$branch->ordering = (int)
$result->ordering;
self::storeNode($branch);
// Add the branch to the cache.
self::$branches[$title] =
$branch;
return self::$branches[$title]->id;
* Method to add a node to the taxonomy tree.
* @param string $branch The title of the branch to store the node in.
* @param string $title The title of the node.
* @param integer $state The published state of the node. [optional]
* @param integer $access The access state of the node. [optional]
* @return integer The id of the node.
* @throws Exception on database error.
public static function addNode($branch, $title, $state =
1, $access =
1)
// Check to see if the node is in the cache.
if (isset
(self::$nodes[$branch][$title]))
return self::$nodes[$branch][$title]->id;
// Get the branch id, insert it if it does not exist.
$branchId =
self::addBranch($branch);
// Check to see if the node is in the table.
$query =
$db->getQuery(true)
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') .
' = ' .
$db->quote($branchId))
->where($db->quoteName('title') .
' = ' .
$db->quote($title));
$result =
$db->loadObject();
// Check if the database matches the input data.
if (!empty($result) &&
$result->state ==
$state &&
$result->access ==
$access)
// The data matches, add the item to the cache.
self::$nodes[$branch][$title] =
$result;
return self::$nodes[$branch][$title]->id;
* The database did not match the input. This could be because the
* state has changed or because the node does not exist. Let's figure
* out which case is true and deal with it.
// Prepare the node object.
$node->parent_id = (int)
$branchId;
$node->state = (int)
$state;
$node->access = (int)
$access;
// Prepare the node object.
$node->id = (int)
$result->id;
$node->parent_id = (int)
$result->parent_id;
$node->title =
$result->title;
$node->state = (int)
$result->title;
$node->access = (int)
$result->access;
$node->ordering = (int)
$result->ordering;
// Add the node to the cache.
self::$nodes[$branch][$title] =
$node;
return self::$nodes[$branch][$title]->id;
* Method to add a map entry between a link and a taxonomy node.
* @param integer $linkId The link to map to.
* @param integer $nodeId The node to map to.
* @return boolean True on success.
* @throws Exception on database error.
public static function addMap($linkId, $nodeId)
$db =
JFactory::getDbo();
$query =
$db->getQuery(true)
->select($db->quoteName('link_id'))
->from($db->quoteName('#__finder_taxonomy_map'))
->where($db->quoteName('link_id') .
' = ' . (int)
$linkId)
->where($db->quoteName('node_id') .
' = ' . (int)
$nodeId);
$id = (int)
$db->loadResult();
$map->link_id = (int)
$linkId;
$map->node_id = (int)
$nodeId;
$db->updateObject('#__finder_taxonomy_map', $map, array('link_id', 'node_id'));
$db->insertObject('#__finder_taxonomy_map', $map);
* Method to get the title of all taxonomy branches.
* @return array An array of branch titles.
* @throws Exception on database error.
$groups =
implode(',', $user->getAuthorisedViewLevels());
// Create a query to get the taxonomy branch titles.
$query =
$db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') .
' = 1')
->where($db->quoteName('state') .
' = 1')
->where($db->quoteName('access') .
' IN (' .
$groups .
')');
// Get the branch titles.
$results =
$db->loadColumn();
* Method to find a taxonomy node in a branch.
* @param string $branch The branch to search.
* @param string $title The title of the node.
* @return mixed Integer id on success, null on no match.
* @throws Exception on database error.
$groups =
implode(',', $user->getAuthorisedViewLevels());
// Create a query to get the node.
$query =
$db->getQuery(true)
->from($db->quoteName('#__finder_taxonomy') .
' AS t1')
->join('INNER', $db->quoteName('#__finder_taxonomy') .
' AS t2 ON t2.id = t1.parent_id')
->where('t1.access IN (' .
$groups .
')')
->where('t1.title LIKE ' .
$db->quote($db->escape($title) .
'%'))
->where('t2.access IN (' .
$groups .
')')
->where('t2.title = ' .
$db->quote($branch));
$db->setQuery($query, 0, 1);
$result =
$db->loadObject();
* Method to remove map entries for a link.
* @param integer $linkId The link to remove.
* @return boolean True on success.
* @throws Exception on database error.
$query =
$db->getQuery(true)
->delete($db->quoteName('#__finder_taxonomy_map'))
->where($db->quoteName('link_id') .
' = ' . (int)
$linkId);
* Method to remove orphaned taxonomy nodes and branches.
* @return integer The number of deleted rows.
* @throws Exception on database error.
// Delete all orphaned nodes.
' FROM ' .
$db->quoteName('#__finder_taxonomy') .
' AS t' .
' LEFT JOIN ' .
$db->quoteName('#__finder_taxonomy_map') .
' AS m ON m.node_id = t.id' .
' WHERE t.parent_id > 1' .
' AND m.link_id IS NULL';
return $db->getAffectedRows();
* Method to store a node to the database. This method will accept either a branch or a node.
* @param object $item The item to store.
* @return boolean True on success.
* @throws Exception on database error.
// Check if we are updating or inserting the item.
$db->insertObject('#__finder_taxonomy', $item, 'id');
$db->updateObject('#__finder_taxonomy', $item, 'id');
Documentation generated on Tue, 19 Nov 2013 15:15:21 +0100 by phpDocumentor 1.4.3