Source for file taxonomy.php

Documentation is available at taxonomy.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_finder
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Stemmer base class for the Finder indexer package.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_finder
  17.  * @since       2.5
  18.  */
  19. {
  20.     /**
  21.      * An internal cache of taxonomy branch data.
  22.      *
  23.      * @var    array 
  24.      * @since  2.5
  25.      */
  26.     public static $branches array();
  27.  
  28.     /**
  29.      * An internal cache of taxonomy node data.
  30.      *
  31.      * @var    array 
  32.      * @since  2.5
  33.      */
  34.     public static $nodes array();
  35.  
  36.     /**
  37.      * Method to add a branch to the taxonomy tree.
  38.      *
  39.      * @param   string   $title   The title of the branch.
  40.      * @param   integer  $state   The published state of the branch. [optional]
  41.      * @param   integer  $access  The access state of the branch. [optional]
  42.      *
  43.      * @return  integer  The id of the branch.
  44.      *
  45.      * @since   2.5
  46.      * @throws  Exception on database error.
  47.      */
  48.     public static function addBranch($title$state 1$access 1)
  49.     {
  50.         // Check to see if the branch is in the cache.
  51.         if (isset(self::$branches[$title]))
  52.         {
  53.             return self::$branches[$title]->id;
  54.         }
  55.  
  56.         // Check to see if the branch is in the table.
  57.         $db JFactory::getDbo();
  58.         $query $db->getQuery(true)
  59.             ->select('*')
  60.             ->from($db->quoteName('#__finder_taxonomy'))
  61.             ->where($db->quoteName('parent_id'' = 1')
  62.             ->where($db->quoteName('title'' = ' $db->quote($title));
  63.         $db->setQuery($query);
  64.  
  65.         // Get the result.
  66.         $result $db->loadObject();
  67.  
  68.         // Check if the database matches the input data.
  69.         if (!empty($result&& $result->state == $state && $result->access == $access)
  70.         {
  71.             // The data matches, add the item to the cache.
  72.             self::$branches[$title$result;
  73.  
  74.             return self::$branches[$title]->id;
  75.         }
  76.  
  77.         /*
  78.          * The database did not match the input. This could be because the
  79.          * state has changed or because the branch does not exist. Let's figure
  80.          * out which case is true and deal with it.
  81.          */
  82.         $branch new JObject;
  83.         if (empty($result))
  84.         {
  85.             // Prepare the branch object.
  86.             $branch->parent_id 1;
  87.             $branch->title $title;
  88.             $branch->state = (int) $state;
  89.             $branch->access = (int) $access;
  90.         }
  91.         else
  92.         {
  93.             // Prepare the branch object.
  94.             $branch->id = (int) $result->id;
  95.             $branch->parent_id = (int) $result->parent_id;
  96.             $branch->title $result->title;
  97.             $branch->state = (int) $result->title;
  98.             $branch->access = (int) $result->access;
  99.             $branch->ordering = (int) $result->ordering;
  100.         }
  101.  
  102.         // Store the branch.
  103.         self::storeNode($branch);
  104.  
  105.         // Add the branch to the cache.
  106.         self::$branches[$title$branch;
  107.  
  108.         return self::$branches[$title]->id;
  109.     }
  110.  
  111.     /**
  112.      * Method to add a node to the taxonomy tree.
  113.      *
  114.      * @param   string   $branch  The title of the branch to store the node in.
  115.      * @param   string   $title   The title of the node.
  116.      * @param   integer  $state   The published state of the node. [optional]
  117.      * @param   integer  $access  The access state of the node. [optional]
  118.      *
  119.      * @return  integer  The id of the node.
  120.      *
  121.      * @since   2.5
  122.      * @throws  Exception on database error.
  123.      */
  124.     public static function addNode($branch$title$state 1$access 1)
  125.     {
  126.         // Check to see if the node is in the cache.
  127.         if (isset(self::$nodes[$branch][$title]))
  128.         {
  129.             return self::$nodes[$branch][$title]->id;
  130.         }
  131.  
  132.         // Get the branch id, insert it if it does not exist.
  133.         $branchId self::addBranch($branch);
  134.  
  135.         // Check to see if the node is in the table.
  136.         $db JFactory::getDbo();
  137.         $query $db->getQuery(true)
  138.             ->select('*')
  139.             ->from($db->quoteName('#__finder_taxonomy'))
  140.             ->where($db->quoteName('parent_id'' = ' $db->quote($branchId))
  141.             ->where($db->quoteName('title'' = ' $db->quote($title));
  142.         $db->setQuery($query);
  143.  
  144.         // Get the result.
  145.         $result $db->loadObject();
  146.  
  147.         // Check if the database matches the input data.
  148.         if (!empty($result&& $result->state == $state && $result->access == $access)
  149.         {
  150.             // The data matches, add the item to the cache.
  151.             self::$nodes[$branch][$title$result;
  152.  
  153.             return self::$nodes[$branch][$title]->id;
  154.         }
  155.  
  156.         /*
  157.          * The database did not match the input. This could be because the
  158.          * state has changed or because the node does not exist. Let's figure
  159.          * out which case is true and deal with it.
  160.          */
  161.         $node new JObject;
  162.         if (empty($result))
  163.         {
  164.             // Prepare the node object.
  165.             $node->parent_id = (int) $branchId;
  166.             $node->title $title;
  167.             $node->state = (int) $state;
  168.             $node->access = (int) $access;
  169.         }
  170.         else
  171.         {
  172.             // Prepare the node object.
  173.             $node->id = (int) $result->id;
  174.             $node->parent_id = (int) $result->parent_id;
  175.             $node->title $result->title;
  176.             $node->state = (int) $result->title;
  177.             $node->access = (int) $result->access;
  178.             $node->ordering = (int) $result->ordering;
  179.         }
  180.  
  181.         // Store the node.
  182.         self::storeNode($node);
  183.  
  184.         // Add the node to the cache.
  185.         self::$nodes[$branch][$title$node;
  186.  
  187.         return self::$nodes[$branch][$title]->id;
  188.     }
  189.  
  190.     /**
  191.      * Method to add a map entry between a link and a taxonomy node.
  192.      *
  193.      * @param   integer  $linkId  The link to map to.
  194.      * @param   integer  $nodeId  The node to map to.
  195.      *
  196.      * @return  boolean  True on success.
  197.      *
  198.      * @since   2.5
  199.      * @throws  Exception on database error.
  200.      */
  201.     public static function addMap($linkId$nodeId)
  202.     {
  203.         // Insert the map.
  204.         $db JFactory::getDbo();
  205.  
  206.         $query $db->getQuery(true)
  207.             ->select($db->quoteName('link_id'))
  208.             ->from($db->quoteName('#__finder_taxonomy_map'))
  209.             ->where($db->quoteName('link_id'' = ' . (int) $linkId)
  210.             ->where($db->quoteName('node_id'' = ' . (int) $nodeId);
  211.         $db->setQuery($query);
  212.         $db->execute();
  213.         $id = (int) $db->loadResult();
  214.  
  215.         $map new JObject;
  216.         $map->link_id = (int) $linkId;
  217.         $map->node_id = (int) $nodeId;
  218.  
  219.         if ($id)
  220.         {
  221.             $db->updateObject('#__finder_taxonomy_map'$maparray('link_id''node_id'));
  222.         }
  223.         else
  224.         {
  225.             $db->insertObject('#__finder_taxonomy_map'$map);
  226.         }
  227.  
  228.         return true;
  229.     }
  230.  
  231.     /**
  232.      * Method to get the title of all taxonomy branches.
  233.      *
  234.      * @return  array  An array of branch titles.
  235.      *
  236.      * @since   2.5
  237.      * @throws  Exception on database error.
  238.      */
  239.     public static function getBranchTitles()
  240.     {
  241.         $db JFactory::getDbo();
  242.  
  243.         // Set user variables
  244.         $user JFactory::getUser();
  245.         $groups implode(','$user->getAuthorisedViewLevels());
  246.  
  247.         // Create a query to get the taxonomy branch titles.
  248.         $query $db->getQuery(true)
  249.             ->select($db->quoteName('title'))
  250.             ->from($db->quoteName('#__finder_taxonomy'))
  251.             ->where($db->quoteName('parent_id'' = 1')
  252.             ->where($db->quoteName('state'' = 1')
  253.             ->where($db->quoteName('access'' IN (' $groups ')');
  254.  
  255.         // Get the branch titles.
  256.         $db->setQuery($query);
  257.         $results $db->loadColumn();
  258.  
  259.         return $results;
  260.     }
  261.  
  262.     /**
  263.      * Method to find a taxonomy node in a branch.
  264.      *
  265.      * @param   string  $branch  The branch to search.
  266.      * @param   string  $title   The title of the node.
  267.      *
  268.      * @return  mixed  Integer id on success, null on no match.
  269.      *
  270.      * @since   2.5
  271.      * @throws  Exception on database error.
  272.      */
  273.     public static function getNodeByTitle($branch$title)
  274.     {
  275.         $db JFactory::getDbo();
  276.  
  277.         // Set user variables
  278.         $user JFactory::getUser();
  279.         $groups implode(','$user->getAuthorisedViewLevels());
  280.  
  281.         // Create a query to get the node.
  282.         $query $db->getQuery(true)
  283.             ->select('t1.*')
  284.             ->from($db->quoteName('#__finder_taxonomy'' AS t1')
  285.             ->join('INNER'$db->quoteName('#__finder_taxonomy'' AS t2 ON t2.id = t1.parent_id')
  286.             ->where('t1.access IN (' $groups ')')
  287.             ->where('t1.state = 1')
  288.             ->where('t1.title LIKE ' $db->quote($db->escape($title'%'))
  289.             ->where('t2.access IN (' $groups ')')
  290.             ->where('t2.state = 1')
  291.             ->where('t2.title = ' $db->quote($branch));
  292.  
  293.         // Get the node.
  294.         $db->setQuery($query01);
  295.         $result $db->loadObject();
  296.  
  297.         return $result;
  298.     }
  299.  
  300.     /**
  301.      * Method to remove map entries for a link.
  302.      *
  303.      * @param   integer  $linkId  The link to remove.
  304.      *
  305.      * @return  boolean  True on success.
  306.      *
  307.      * @since   2.5
  308.      * @throws  Exception on database error.
  309.      */
  310.     public static function removeMaps($linkId)
  311.     {
  312.         // Delete the maps.
  313.         $db JFactory::getDbo();
  314.         $query $db->getQuery(true)
  315.             ->delete($db->quoteName('#__finder_taxonomy_map'))
  316.             ->where($db->quoteName('link_id'' = ' . (int) $linkId);
  317.         $db->setQuery($query);
  318.         $db->execute();
  319.  
  320.         return true;
  321.     }
  322.  
  323.     /**
  324.      * Method to remove orphaned taxonomy nodes and branches.
  325.      *
  326.      * @return  integer  The number of deleted rows.
  327.      *
  328.      * @since   2.5
  329.      * @throws  Exception on database error.
  330.      */
  331.     public static function removeOrphanNodes()
  332.     {
  333.         // Delete all orphaned nodes.
  334.         $db JFactory::getDbo();
  335.         $query 'DELETE t' .
  336.             ' FROM ' $db->quoteName('#__finder_taxonomy'' AS t' .
  337.             ' LEFT JOIN ' $db->quoteName('#__finder_taxonomy_map'' AS m ON m.node_id = t.id' .
  338.             ' WHERE t.parent_id > 1' .
  339.             ' AND m.link_id IS NULL';
  340.         $db->setQuery($query);
  341.         $db->execute();
  342.  
  343.         return $db->getAffectedRows();
  344.     }
  345.  
  346.     /**
  347.      * Method to store a node to the database.  This method will accept either a branch or a node.
  348.      *
  349.      * @param   object  $item  The item to store.
  350.      *
  351.      * @return  boolean  True on success.
  352.      *
  353.      * @since   2.5
  354.      * @throws  Exception on database error.
  355.      */
  356.     protected static function storeNode($item)
  357.     {
  358.         $db JFactory::getDbo();
  359.  
  360.         // Check if we are updating or inserting the item.
  361.         if (empty($item->id))
  362.         {
  363.             // Insert the item.
  364.             $db->insertObject('#__finder_taxonomy'$item'id');
  365.         }
  366.         else
  367.         {
  368.             // Update the item.
  369.             $db->updateObject('#__finder_taxonomy'$item'id');
  370.         }
  371.  
  372.         return true;
  373.     }
  374. }

Documentation generated on Tue, 19 Nov 2013 15:15:21 +0100 by phpDocumentor 1.4.3