Source for file content.php

Documentation is available at content.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Search.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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. require_once JPATH_SITE '/components/com_content/router.php';
  13.  
  14. /**
  15.  * Content Search plugin
  16.  *
  17.  * @package     Joomla.Plugin
  18.  * @subpackage  Search.content
  19.  * @since       1.6
  20.  */
  21. class PlgSearchContent extends JPlugin
  22. {
  23.     /**
  24.      * @return array An array of search areas
  25.      */
  26.     public function onContentSearchAreas()
  27.     {
  28.         static $areas array(
  29.             'content' => 'JGLOBAL_ARTICLES'
  30.         );
  31.         return $areas;
  32.     }
  33.  
  34.     /**
  35.      * Content Search method
  36.      * The sql must return the following fields that are used in a common display
  37.      * routine: href, title, section, created, text, browsernav
  38.      * @param string Target search string
  39.      * @param string mathcing option, exact|any|all
  40.      * @param string ordering option, newest|oldest|popular|alpha|category
  41.      * @param mixed  An array if the search it to be restricted to areas, null if search all
  42.      */
  43.     public function onContentSearch($text$phrase ''$ordering ''$areas null)
  44.     {
  45.         $db JFactory::getDbo();
  46.         $app JFactory::getApplication();
  47.         $user JFactory::getUser();
  48.         $groups implode(','$user->getAuthorisedViewLevels());
  49.         $tag JFactory::getLanguage()->getTag();
  50.  
  51.         require_once JPATH_SITE '/components/com_content/helpers/route.php';
  52.         require_once JPATH_ADMINISTRATOR '/components/com_search/helpers/search.php';
  53.  
  54.         $searchText $text;
  55.         if (is_array($areas))
  56.         {
  57.             if (!array_intersect($areasarray_keys($this->onContentSearchAreas())))
  58.             {
  59.                 return array();
  60.             }
  61.         }
  62.  
  63.         $sContent $this->params->get('search_content'1);
  64.         $sArchived $this->params->get('search_archived'1);
  65.         $limit $this->params->def('search_limit'50);
  66.  
  67.         $nullDate $db->getNullDate();
  68.         $date JFactory::getDate();
  69.         $now $date->toSql();
  70.  
  71.         $text trim($text);
  72.         if ($text == '')
  73.         {
  74.             return array();
  75.         }
  76.  
  77.         switch ($phrase)
  78.         {
  79.             case 'exact':
  80.                 $text $db->quote('%' $db->escape($texttrue'%'false);
  81.                 $wheres2 array();
  82.                 $wheres2['a.title LIKE ' $text;
  83.                 $wheres2['a.introtext LIKE ' $text;
  84.                 $wheres2['a.fulltext LIKE ' $text;
  85.                 $wheres2['a.metakey LIKE ' $text;
  86.                 $wheres2['a.metadesc LIKE ' $text;
  87.                 $where '(' implode(') OR ('$wheres2')';
  88.                 break;
  89.  
  90.             case 'all':
  91.             case 'any':
  92.             default:
  93.                 $words explode(' '$text);
  94.                 $wheres array();
  95.                 foreach ($words as $word)
  96.                 {
  97.                     $word $db->quote('%' $db->escape($wordtrue'%'false);
  98.                     $wheres2 array();
  99.                     $wheres2['a.title LIKE ' $word;
  100.                     $wheres2['a.introtext LIKE ' $word;
  101.                     $wheres2['a.fulltext LIKE ' $word;
  102.                     $wheres2['a.metakey LIKE ' $word;
  103.                     $wheres2['a.metadesc LIKE ' $word;
  104.                     $wheres[implode(' OR '$wheres2);
  105.                 }
  106.                 $where '(' implode(($phrase == 'all' ') AND (' ') OR (')$wheres')';
  107.                 break;
  108.         }
  109.  
  110.         switch ($ordering)
  111.         {
  112.             case 'oldest':
  113.                 $order 'a.created ASC';
  114.                 break;
  115.  
  116.             case 'popular':
  117.                 $order 'a.hits DESC';
  118.                 break;
  119.  
  120.             case 'alpha':
  121.                 $order 'a.title ASC';
  122.                 break;
  123.  
  124.             case 'category':
  125.                 $order 'c.title ASC, a.title ASC';
  126.                 break;
  127.  
  128.             case 'newest':
  129.             default:
  130.                 $order 'a.created DESC';
  131.                 break;
  132.         }
  133.  
  134.         $rows array();
  135.         $query $db->getQuery(true);
  136.  
  137.         // search articles
  138.         if ($sContent && $limit 0)
  139.         {
  140.             $query->clear();
  141.             //sqlsrv changes
  142.             $case_when ' CASE WHEN ';
  143.             $case_when .= $query->charLength('a.alias''!=''0');
  144.             $case_when .= ' THEN ';
  145.             $a_id $query->castAsChar('a.id');
  146.             $case_when .= $query->concatenate(array($a_id'a.alias')':');
  147.             $case_when .= ' ELSE ';
  148.             $case_when .= $a_id ' END as slug';
  149.  
  150.             $case_when1 ' CASE WHEN ';
  151.             $case_when1 .= $query->charLength('c.alias''!=''0');
  152.             $case_when1 .= ' THEN ';
  153.             $c_id $query->castAsChar('c.id');
  154.             $case_when1 .= $query->concatenate(array($c_id'c.alias')':');
  155.             $case_when1 .= ' ELSE ';
  156.             $case_when1 .= $c_id ' END as catslug';
  157.  
  158.             $query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created')
  159.                 ->select($query->concatenate(array('a.introtext''a.fulltext')) ' AS text')
  160.                 ->select('c.title AS section, ' $case_when ',' $case_when1 ', ' '\'2\' AS browsernav')
  161.  
  162.                 ->from('#__content AS a')
  163.                 ->join('INNER''#__categories AS c ON c.id=a.catid')
  164.                 ->where(
  165.                     '(' $where ') AND a.state=1 AND c.published = 1 AND a.access IN (' $groups ') '
  166.                         . 'AND c.access IN (' $groups ') '
  167.                         . 'AND (a.publish_up = ' $db->quote($nullDate' OR a.publish_up <= ' $db->quote($now') '
  168.                         . 'AND (a.publish_down = ' $db->quote($nullDate' OR a.publish_down >= ' $db->quote($now')'
  169.                 )
  170.                 ->group('a.id, a.title, a.metadesc, a.metakey, a.created, a.introtext, a.fulltext, c.title, a.alias, c.alias, c.id')
  171.                 ->order($order);
  172.  
  173.             // Filter by language
  174.             if ($app->isSite(&& JLanguageMultilang::isEnabled())
  175.             {
  176.                 $query->where('a.language in (' $db->quote($tag',' $db->quote('*'')')
  177.                     ->where('c.language in (' $db->quote($tag',' $db->quote('*'')');
  178.             }
  179.  
  180.             $db->setQuery($query0$limit);
  181.             $list $db->loadObjectList();
  182.             $limit -= count($list);
  183.  
  184.             if (isset($list))
  185.             {
  186.                 foreach ($list as $key => $item)
  187.                 {
  188.                     $list[$key]->href ContentHelperRoute::getArticleRoute($item->slug$item->catslug);
  189.                 }
  190.             }
  191.             $rows[$list;
  192.         }
  193.  
  194.         // search archived content
  195.         if ($sArchived && $limit 0)
  196.         {
  197.             $query->clear();
  198.             //sqlsrv changes
  199.             $case_when ' CASE WHEN ';
  200.             $case_when .= $query->charLength('a.alias''!=''0');
  201.             $case_when .= ' THEN ';
  202.             $a_id $query->castAsChar('a.id');
  203.             $case_when .= $query->concatenate(array($a_id'a.alias')':');
  204.             $case_when .= ' ELSE ';
  205.             $case_when .= $a_id ' END as slug';
  206.  
  207.             $case_when1 ' CASE WHEN ';
  208.             $case_when1 .= $query->charLength('c.alias''!=''0');
  209.             $case_when1 .= ' THEN ';
  210.             $c_id $query->castAsChar('c.id');
  211.             $case_when1 .= $query->concatenate(array($c_id'c.alias')':');
  212.             $case_when1 .= ' ELSE ';
  213.             $case_when1 .= $c_id ' END as catslug';
  214.  
  215.             $query->select(
  216.                 'a.title AS title, a.metadesc, a.metakey, a.created AS created, '
  217.                     . $query->concatenate(array("a.introtext""a.fulltext")) ' AS text,'
  218.                     . $case_when ',' $case_when1 ', '
  219.                     . 'c.title AS section, \'2\' AS browsernav'
  220.             );
  221.             //.'CONCAT_WS("/", c.title) AS section, \'2\' AS browsernav' );
  222.             $query->from('#__content AS a')
  223.                 ->join('INNER''#__categories AS c ON c.id=a.catid AND c.access IN (' $groups ')')
  224.                 ->where(
  225.                     '(' $where ') AND a.state = 2 AND c.published = 1 AND a.access IN (' $groups
  226.                         . ') AND c.access IN (' $groups ') '
  227.                         . 'AND (a.publish_up = ' $db->quote($nullDate' OR a.publish_up <= ' $db->quote($now') '
  228.                         . 'AND (a.publish_down = ' $db->quote($nullDate' OR a.publish_down >= ' $db->quote($now')'
  229.                 )
  230.                 ->order($order);
  231.  
  232.             // Filter by language
  233.             if ($app->isSite(&& JLanguageMultilang::isEnabled())
  234.             {
  235.                 $query->where('a.language in (' $db->quote($tag',' $db->quote('*'')')
  236.                     ->where('c.language in (' $db->quote($tag',' $db->quote('*'')');
  237.             }
  238.  
  239.             $db->setQuery($query0$limit);
  240.             $list3 $db->loadObjectList();
  241.  
  242.             // find an itemid for archived to use if there isn't another one
  243.             $item $app->getMenu()->getItems('link''index.php?option=com_content&view=archive'true);
  244.             $itemid = isset($item->id'&Itemid=' $item->id '';
  245.  
  246.             if (isset($list3))
  247.             {
  248.                 foreach ($list3 as $key => $item)
  249.                 {
  250.                     $date JFactory::getDate($item->created);
  251.  
  252.                     $created_month $date->format("n");
  253.                     $created_year $date->format("Y");
  254.  
  255.                     $list3[$key]->href JRoute::_('index.php?option=com_content&view=archive&year=' $created_year '&month=' $created_month $itemid);
  256.                 }
  257.             }
  258.  
  259.             $rows[$list3;
  260.         }
  261.  
  262.         $results array();
  263.         if (count($rows))
  264.         {
  265.             foreach ($rows as $row)
  266.             {
  267.                 $new_row array();
  268.                 foreach ($row as $article)
  269.                 {
  270.                     if (SearchHelper::checkNoHTML($article$searchTextarray('text''title''metadesc''metakey')))
  271.                     {
  272.                         $new_row[$article;
  273.                     }
  274.                 }
  275.                 $results array_merge($results(array) $new_row);
  276.             }
  277.         }
  278.  
  279.         return $results;
  280.     }
  281. }

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