Source for file content.php

Documentation is available at content.php

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

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