Source for file weblinks.php

Documentation is available at weblinks.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Search.weblinks
  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_weblinks/helpers/route.php';
  13.  
  14. /**
  15.  * Weblinks Search plugin
  16.  *
  17.  * @package     Joomla.Plugin
  18.  * @subpackage  Search.weblinks
  19.  * @since       1.6
  20.  */
  21. class PlgSearchWeblinks extends JPlugin
  22. {
  23.     /**
  24.      * Load the language file on instantiation.
  25.      *
  26.      * @var    boolean 
  27.      * @since  3.1
  28.      */
  29.     protected $autoloadLanguage = true;
  30.  
  31.     /**
  32.      * @return array An array of search areas
  33.      */
  34.     public function onContentSearchAreas()
  35.     {
  36.         static $areas array(
  37.             'weblinks' => 'PLG_SEARCH_WEBLINKS_WEBLINKS'
  38.         );
  39.         return $areas;
  40.     }
  41.  
  42.     /**
  43.      * Weblink Search method
  44.      *
  45.      * The sql must return the following fields that are used in a common display
  46.      * routine: href, title, section, created, text, browsernav
  47.      *
  48.      * @param string Target search string
  49.      * @param string mathcing option, exact|any|all
  50.      * @param string ordering option, newest|oldest|popular|alpha|category
  51.      * @param mixed  An array if the search it to be restricted to areas, null if search all
  52.      */
  53.     public function onContentSearch($text$phrase ''$ordering ''$areas null)
  54.     {
  55.         $db JFactory::getDbo();
  56.         $app JFactory::getApplication();
  57.         $user JFactory::getUser();
  58.         $groups implode(','$user->getAuthorisedViewLevels());
  59.  
  60.         $searchText $text;
  61.  
  62.         if (is_array($areas))
  63.         {
  64.             if (!array_intersect($areasarray_keys($this->onContentSearchAreas())))
  65.             {
  66.                 return array();
  67.             }
  68.         }
  69.  
  70.         $sContent $this->params->get('search_content'1);
  71.         $sArchived $this->params->get('search_archived'1);
  72.         $limit $this->params->def('search_limit'50);
  73.         $state array();
  74.         if ($sContent)
  75.         {
  76.             $state[1;
  77.         }
  78.         if ($sArchived)
  79.         {
  80.             $state[2;
  81.         }
  82.  
  83.         if (empty($state))
  84.         {
  85.             return array();
  86.         }
  87.  
  88.         $text trim($text);
  89.         if ($text == '')
  90.         {
  91.             return array();
  92.         }
  93.         $searchWeblinks JText::_('PLG_SEARCH_WEBLINKS');
  94.  
  95.         switch ($phrase)
  96.         {
  97.             case 'exact':
  98.                 $text $db->quote('%' $db->escape($texttrue'%'false);
  99.                 $wheres2 array();
  100.                 $wheres2['a.url LIKE ' $text;
  101.                 $wheres2['a.description LIKE ' $text;
  102.                 $wheres2['a.title LIKE ' $text;
  103.                 $where '(' implode(') OR ('$wheres2')';
  104.                 break;
  105.  
  106.             case 'all':
  107.             case 'any':
  108.             default:
  109.                 $words explode(' '$text);
  110.                 $wheres array();
  111.                 foreach ($words as $word)
  112.                 {
  113.                     $word $db->quote('%' $db->escape($wordtrue'%'false);
  114.                     $wheres2 array();
  115.                     $wheres2['a.url LIKE ' $word;
  116.                     $wheres2['a.description LIKE ' $word;
  117.                     $wheres2['a.title LIKE ' $word;
  118.                     $wheres[implode(' OR '$wheres2);
  119.                 }
  120.                 $where '(' implode(($phrase == 'all' ') AND (' ') OR (')$wheres')';
  121.                 break;
  122.         }
  123.  
  124.         switch ($ordering)
  125.         {
  126.             case 'oldest':
  127.                 $order 'a.created ASC';
  128.                 break;
  129.  
  130.             case 'popular':
  131.                 $order 'a.hits DESC';
  132.                 break;
  133.  
  134.             case 'alpha':
  135.                 $order 'a.title ASC';
  136.                 break;
  137.  
  138.             case 'category':
  139.                 $order 'c.title ASC, a.title ASC';
  140.                 break;
  141.  
  142.             case 'newest':
  143.             default:
  144.                 $order 'a.created DESC';
  145.         }
  146.  
  147.         $query $db->getQuery(true);
  148.         //sqlsrv changes
  149.         $case_when ' CASE WHEN ';
  150.         $case_when .= $query->charLength('a.alias''!=''0');
  151.         $case_when .= ' THEN ';
  152.         $a_id $query->castAsChar('a.id');
  153.         $case_when .= $query->concatenate(array($a_id'a.alias')':');
  154.         $case_when .= ' ELSE ';
  155.         $case_when .= $a_id ' END as slug';
  156.  
  157.         $case_when1 ' CASE WHEN ';
  158.         $case_when1 .= $query->charLength('c.alias''!=''0');
  159.         $case_when1 .= ' THEN ';
  160.         $c_id $query->castAsChar('c.id');
  161.         $case_when1 .= $query->concatenate(array($c_id'c.alias')':');
  162.         $case_when1 .= ' ELSE ';
  163.         $case_when1 .= $c_id ' END as catslug';
  164.  
  165.         $query->select('a.title AS title, \'\' AS created, a.url, a.description AS text, ' $case_when "," $case_when1)
  166.         ->select($query->concatenate(array($db->quote($searchWeblinks)'c.title')" / "' AS section')
  167.         ->select('\'1\' AS browsernav')
  168.         ->from('#__weblinks AS a')
  169.         ->join('INNER''#__categories as c ON c.id = a.catid')
  170.         ->where('(' $where ') AND a.state IN (' implode(','$state') AND c.published = 1 AND c.access IN (' $groups ')')
  171.         ->order($order);
  172.  
  173.         // Filter by language
  174.         if ($app->isSite(&& JLanguageMultilang::isEnabled())
  175.         {
  176.             $tag JFactory::getLanguage()->getTag();
  177.             $query->where('a.language in (' $db->quote($tag',' $db->quote('*'')')
  178.                 ->where('c.language in (' $db->quote($tag',' $db->quote('*'')');
  179.         }
  180.  
  181.         $db->setQuery($query0$limit);
  182.         $rows $db->loadObjectList();
  183.  
  184.         $return array();
  185.         if ($rows)
  186.         {
  187.             foreach ($rows as $key => $row)
  188.             {
  189.                 $rows[$key]->href WeblinksHelperRoute::getWeblinkRoute($row->slug$row->catslug);
  190.             }
  191.  
  192.             foreach ($rows as $weblink)
  193.             {
  194.                 if (searchHelper::checkNoHTML($weblink$searchTextarray('url''text''title')))
  195.                 {
  196.                     $return[$weblink;
  197.                 }
  198.             }
  199.         }
  200.  
  201.         return $return;
  202.     }
  203. }

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