Source for file categories.php

Documentation is available at categories.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Finder.Categories
  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('JPATH_BASE'or die;
  11.  
  12. require_once JPATH_ADMINISTRATOR '/components/com_finder/helpers/indexer/adapter.php';
  13.  
  14. /**
  15.  * Finder adapter for Joomla Categories.
  16.  *
  17.  * @package     Joomla.Plugin
  18.  * @subpackage  Finder.Categories
  19.  * @since       2.5
  20.  */
  21. {
  22.     /**
  23.      * The plugin identifier.
  24.      *
  25.      * @var    string 
  26.      * @since  2.5
  27.      */
  28.     protected $context = 'Categories';
  29.  
  30.     /**
  31.      * The extension name.
  32.      *
  33.      * @var    string 
  34.      * @since  2.5
  35.      */
  36.     protected $extension = 'com_categories';
  37.  
  38.     /**
  39.      * The sublayout to use when rendering the results.
  40.      *
  41.      * @var    string 
  42.      * @since  2.5
  43.      */
  44.     protected $layout = 'category';
  45.  
  46.     /**
  47.      * The type of content that the adapter indexes.
  48.      *
  49.      * @var    string 
  50.      * @since  2.5
  51.      */
  52.     protected $type_title = 'Category';
  53.  
  54.     /**
  55.      * The table name.
  56.      *
  57.      * @var    string 
  58.      * @since  2.5
  59.      */
  60.     protected $table = '#__categories';
  61.  
  62.     /**
  63.      * The field the published state is stored in.
  64.      *
  65.      * @var    string 
  66.      * @since  2.5
  67.      */
  68.     protected $state_field = 'published';
  69.  
  70.     /**
  71.      * Load the language file on instantiation.
  72.      *
  73.      * @var    boolean 
  74.      * @since  3.1
  75.      */
  76.     protected $autoloadLanguage = true;
  77.  
  78.     /**
  79.      * Method to remove the link information for items that have been deleted.
  80.      *
  81.      * @param   string  $context  The context of the action being performed.
  82.      * @param   JTable  $table    A JTable object containing the record to be deleted
  83.      *
  84.      * @return  boolean  True on success.
  85.      *
  86.      * @since   2.5
  87.      * @throws  Exception on database error.
  88.      */
  89.     public function onFinderDelete($context$table)
  90.     {
  91.         if ($context == 'com_categories.category')
  92.         {
  93.             $id $table->id;
  94.         }
  95.         elseif ($context == 'com_finder.index')
  96.         {
  97.             $id $table->link_id;
  98.         }
  99.         else
  100.         {
  101.             return true;
  102.         }
  103.         // Remove the items.
  104.         return $this->remove($id);
  105.     }
  106.  
  107.     /**
  108.      * Method to determine if the access level of an item changed.
  109.      *
  110.      * @param   string   $context  The context of the content passed to the plugin.
  111.      * @param   JTable   $row      A JTable object
  112.      * @param   boolean  $isNew    If the content has just been created
  113.      *
  114.      * @return  boolean  True on success.
  115.      *
  116.      * @since   2.5
  117.      * @throws  Exception on database error.
  118.      */
  119.     public function onFinderAfterSave($context$row$isNew)
  120.     {
  121.         // We only want to handle categories here
  122.         if ($context == 'com_categories.category')
  123.         {
  124.             // Check if the access levels are different
  125.             if (!$isNew && $this->old_access != $row->access)
  126.             {
  127.                 // Process the change.
  128.                 $this->itemAccessChange($row);
  129.  
  130.                 // Reindex the item
  131.                 $this->reindex($row->id);
  132.             }
  133.  
  134.             // Check if the parent access level is different
  135.             if (!$isNew && $this->old_cataccess != $row->access)
  136.             {
  137.                 $this->categoryAccessChange($row);
  138.             }
  139.  
  140.         }
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * Method to reindex the link information for an item that has been saved.
  146.      * This event is fired before the data is actually saved so we are going
  147.      * to queue the item to be indexed later.
  148.      *
  149.      * @param   string   $context  The context of the content passed to the plugin.
  150.      * @param   JTable   $row     A JTable object
  151.      * @param   boolean  $isNew    If the content is just about to be created
  152.      *
  153.      * @return  boolean  True on success.
  154.      *
  155.      * @since   2.5
  156.      * @throws  Exception on database error.
  157.      */
  158.     public function onFinderBeforeSave($context$row$isNew)
  159.     {
  160.         // We only want to handle categories here
  161.         if ($context == 'com_categories.category')
  162.         {
  163.             // Query the database for the old access level and the parent if the item isn't new
  164.             if (!$isNew)
  165.             {
  166.                 $this->checkItemAccess($row);
  167.                 $this->checkCategoryAccess($row);
  168.             }
  169.         }
  170.  
  171.         return true;
  172.     }
  173.  
  174.     /**
  175.      * Method to update the link information for items that have been changed
  176.      * from outside the edit screen. This is fired when the item is published,
  177.      * unpublished, archived, or unarchived from the list view.
  178.      *
  179.      * @param   string   $context  The context for the content passed to the plugin.
  180.      * @param   array    $pks      A list of primary key ids of the content that has changed state.
  181.      * @param   integer  $value    The value of the state that the content has been changed to.
  182.      *
  183.      * @return  void 
  184.      *
  185.      * @since   2.5
  186.      */
  187.     public function onFinderChangeState($context$pks$value)
  188.     {
  189.         // We only want to handle categories here
  190.         if ($context == 'com_categories.category')
  191.         {
  192.             // The category published state is tied to the parent category
  193.             // published state so we need to look up all published states
  194.             // before we change anything.
  195.             foreach ($pks as $pk)
  196.             {
  197.                 /* TODO: The $item variable does not seem to be used at all
  198.                 $query = clone($this->getStateQuery());
  199.                 $query->where('a.id = ' . (int) $pk);
  200.  
  201.                 // Get the published states.
  202.                 $this->db->setQuery($query);
  203.                 $item = $this->db->loadObject();
  204.                 */
  205.  
  206.                 // Translate the state.
  207.                 $state null;
  208.  
  209.                 if ($item->parent_id != 1)
  210.                 {
  211.                     $state $item->cat_state;
  212.                 }
  213.  
  214.                 $temp $this->translateState($value$state);
  215.  
  216.                 // Update the item.
  217.                 $this->change($pk'state'$temp);
  218.  
  219.                 // Reindex the item
  220.                 $this->reindex($pk);
  221.             }
  222.         }
  223.  
  224.         // Handle when the plugin is disabled
  225.         if ($context == 'com_plugins.plugin' && $value === 0)
  226.         {
  227.             $this->pluginDisable($pks);
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Method to index an item. The item must be a FinderIndexerResult object.
  233.      *
  234.      * @param   FinderIndexerResult  $item    The item to index as an FinderIndexerResult object.
  235.      * @param   string               $format  The item format
  236.      *
  237.      * @return  void 
  238.      *
  239.      * @since   2.5
  240.      * @throws  Exception on database error.
  241.      */
  242.     protected function index(FinderIndexerResult $item$format 'html')
  243.     {
  244.         // Check if the extension is enabled
  245.         if (JComponentHelper::isEnabled($this->extension== false)
  246.         {
  247.             return;
  248.         }
  249.  
  250.         $item->setLanguage();
  251.  
  252.         // Need to import component route helpers dynamically, hence the reason it's handled here
  253.         $path JPATH_SITE '/components/' $item->extension '/helpers/route.php';
  254.         if (is_file($path))
  255.         {
  256.             include_once $path;
  257.         }
  258.  
  259.         $extension ucfirst(substr($item->extension4));
  260.  
  261.         // Initialize the item parameters.
  262.         $registry new JRegistry;
  263.         $registry->loadString($item->params);
  264.         $item->params $registry;
  265.  
  266.         $registry new JRegistry;
  267.         $registry->loadString($item->metadata);
  268.         $item->metadata $registry;
  269.  
  270.         /* Add the meta-data processing instructions based on the categories
  271.          * configuration parameters.
  272.          */
  273.         // Add the meta-author.
  274.         $item->metaauthor $item->metadata->get('author');
  275.  
  276.         // Handle the link to the meta-data.
  277.         $item->addInstruction(FinderIndexer::META_CONTEXT'link');
  278.         $item->addInstruction(FinderIndexer::META_CONTEXT'metakey');
  279.         $item->addInstruction(FinderIndexer::META_CONTEXT'metadesc');
  280.         $item->addInstruction(FinderIndexer::META_CONTEXT'metaauthor');
  281.         $item->addInstruction(FinderIndexer::META_CONTEXT'author');
  282.         //$item->addInstruction(FinderIndexer::META_CONTEXT, 'created_by_alias');
  283.  
  284.         // Trigger the onContentPrepare event.
  285.         $item->summary FinderIndexerHelper::prepareContent($item->summary$item->params);
  286.  
  287.         // Build the necessary route and path information.
  288.         $item->url $this->getURL($item->id$item->extension$this->layout);
  289.  
  290.         $class $extension 'HelperRoute';
  291.         if (class_exists($class&& method_exists($class'getCategoryRoute'))
  292.         {
  293.             $item->route $class::getCategoryRoute($item->id);
  294.         }
  295.         else
  296.         {
  297.             $item->route ContentHelperRoute::getCategoryRoute($item->slug$item->catid);
  298.         }
  299.         $item->path FinderIndexerHelper::getContentPath($item->route);
  300.  
  301.         // Get the menu title if it exists.
  302.         $title $this->getItemMenuTitle($item->url);
  303.  
  304.         // Adjust the title if necessary.
  305.         if (!empty($title&& $this->params->get('use_menu_title'true))
  306.         {
  307.             $item->title $title;
  308.         }
  309.  
  310.         // Translate the state. Categories should only be published if the parent category is published.
  311.         $item->state $this->translateState($item->state);
  312.  
  313.         // Add the type taxonomy data.
  314.         $item->addTaxonomy('Type''Category');
  315.  
  316.         // Add the language taxonomy data.
  317.         $item->addTaxonomy('Language'$item->language);
  318.  
  319.         // Get content extras.
  320.         FinderIndexerHelper::getContentExtras($item);
  321.  
  322.         // Index the item.
  323.         $this->indexer->index($item);
  324.     }
  325.  
  326.     /**
  327.      * Method to setup the indexer to be run.
  328.      *
  329.      * @return  boolean  True on success.
  330.      *
  331.      * @since   2.5
  332.      */
  333.     protected function setup()
  334.     {
  335.         // Load com_content route helper as it is the fallback for routing in the indexer in this instance.
  336.         include_once JPATH_SITE '/components/com_content/helpers/route.php';
  337.  
  338.         return true;
  339.     }
  340.  
  341.     /**
  342.      * Method to get the SQL query used to retrieve the list of content items.
  343.      *
  344.      * @param   mixed  $query  A JDatabaseQuery object or null.
  345.      *
  346.      * @return  JDatabaseQuery  A database object.
  347.      *
  348.      * @since   2.5
  349.      */
  350.     protected function getListQuery($query null)
  351.     {
  352.         $db JFactory::getDbo();
  353.         // Check if we can use the supplied SQL query.
  354.         $query $query instanceof JDatabaseQuery $query $db->getQuery(true)
  355.             ->select('a.id, a.title, a.alias, a.description AS summary, a.extension')
  356.             ->select('a.created_user_id AS created_by, a.modified_time AS modified, a.modified_user_id AS modified_by')
  357.             ->select('a.metakey, a.metadesc, a.metadata, a.language, a.lft, a.parent_id, a.level')
  358.             ->select('a.created_time AS start_date, a.published AS state, a.access, a.params');
  359.  
  360.         // Handle the alias CASE WHEN portion of the query
  361.         $case_when_item_alias ' CASE WHEN ';
  362.         $case_when_item_alias .= $query->charLength('a.alias''!=''0');
  363.         $case_when_item_alias .= ' THEN ';
  364.         $a_id $query->castAsChar('a.id');
  365.         $case_when_item_alias .= $query->concatenate(array($a_id'a.alias')':');
  366.         $case_when_item_alias .= ' ELSE ';
  367.         $case_when_item_alias .= $a_id.' END as slug';
  368.         $query->select($case_when_item_alias)
  369.             ->from('#__categories AS a')
  370.             ->where($db->quoteName('a.id'' > 1');
  371.  
  372.         return $query;
  373.     }
  374.  
  375.     /**
  376.      * Method to get a SQL query to load the published and access states for
  377.      * an article and category.
  378.      *
  379.      * @return  JDatabaseQuery  A database object.
  380.      *
  381.      * @since   2.5
  382.      */
  383.     protected function getStateQuery()
  384.     {
  385.         $query $this->db->getQuery(true)
  386.             ->select($this->db->quoteName('a.id'))
  387.             ->select('a.' $this->state_field . ' AS state, c.published AS cat_state')
  388.             ->select('a.access, c.access AS cat_access')
  389.             ->from($this->db->quoteName('#__categories'' AS a')
  390.             ->join('LEFT''#__categories AS c ON c.id = a.parent_id');
  391.  
  392.         return $query;
  393.     }
  394. }

Documentation generated on Tue, 19 Nov 2013 14:55:11 +0100 by phpDocumentor 1.4.3