Source for file contacts.php

Documentation is available at contacts.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Search.contacts
  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. /**
  13.  * Contacts Search plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Search.contacts
  17.  * @since       1.6
  18.  */
  19. class PlgSearchContacts extends JPlugin
  20. {
  21.     /**
  22.      * Load the language file on instantiation.
  23.      *
  24.      * @var    boolean 
  25.      * @since  3.1
  26.      */
  27.     protected $autoloadLanguage = true;
  28.  
  29.     /**
  30.      * @return array An array of search areas
  31.      */
  32.     public function onContentSearchAreas()
  33.     {
  34.         static $areas = array(
  35.             'contacts' => 'PLG_SEARCH_CONTACTS_CONTACTS'
  36.         );
  37.         return $areas;
  38.     }
  39.  
  40.     /**
  41.      * Contacts Search method
  42.      *
  43.      * The sql must return the following fields that are used in a common display
  44.      * routine: href, title, section, created, text, browsernav
  45.      *
  46.      * @param string Target search string
  47.      * @param string matching option, exact|any|all
  48.      * @param string ordering option, newest|oldest|popular|alpha|category
  49.      */
  50.     public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
  51.     {
  52.         $db = JFactory::getDbo();
  53.         $app = JFactory::getApplication();
  54.         $user = JFactory::getUser();
  55.         $groups = implode(',', $user->getAuthorisedViewLevels());
  56.  
  57.         if (is_array($areas))
  58.         {
  59.             if (!array_intersect($areas, array_keys($this->onContentSearchAreas())))
  60.             {
  61.                 return array();
  62.             }
  63.         }
  64.  
  65.         $sContent = $this->params->get('search_content', 1);
  66.         $sArchived = $this->params->get('search_archived', 1);
  67.         $limit = $this->params->def('search_limit', 50);
  68.         $state = array();
  69.         if ($sContent)
  70.         {
  71.             $state[] = 1;
  72.         }
  73.         if ($sArchived)
  74.         {
  75.             $state[] = 2;
  76.         }
  77.  
  78.         if (empty($state))
  79.         {
  80.             return array();
  81.         }
  82.  
  83.         $text = trim($text);
  84.         if ($text == '')
  85.         {
  86.             return array();
  87.         }
  88.  
  89.         $section = JText::_('PLG_SEARCH_CONTACTS_CONTACTS');
  90.  
  91.         switch ($ordering)
  92.         {
  93.             case 'alpha':
  94.                 $order = 'a.name ASC';
  95.                 break;
  96.  
  97.             case 'category':
  98.                 $order = 'c.title ASC, a.name ASC';
  99.                 break;
  100.  
  101.             case 'popular':
  102.             case 'newest':
  103.             case 'oldest':
  104.             default:
  105.                 $order = 'a.name DESC';
  106.         }
  107.  
  108.         $text = $db->quote('%' . $db->escape($text, true) . '%', false);
  109.  
  110.         $query = $db->getQuery(true);
  111.         //sqlsrv changes
  112.         $case_when = ' CASE WHEN ';
  113.         $case_when .= $query->charLength('a.alias', '!=', '0');
  114.         $case_when .= ' THEN ';
  115.         $a_id = $query->castAsChar('a.id');
  116.         $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
  117.         $case_when .= ' ELSE ';
  118.         $case_when .= $a_id . ' END as slug';
  119.  
  120.         $case_when1 = ' CASE WHEN ';
  121.         $case_when1 .= $query->charLength('c.alias', '!=', '0');
  122.         $case_when1 .= ' THEN ';
  123.         $c_id = $query->castAsChar('c.id');
  124.         $case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':');
  125.         $case_when1 .= ' ELSE ';
  126.         $case_when1 .= $c_id . ' END as catslug';
  127.  
  128.         $query->select(
  129.             'a.name AS title, \'\' AS created, a.con_position, a.misc, '
  130.                 . $case_when . ',' . $case_when1 . ', '
  131.                 . $query->concatenate(array("a.name", "a.con_position", "a.misc"), ",") . ' AS text,'
  132.                 . $query->concatenate(array($db->quote($section), "c.title"), " / ") . ' AS section,'
  133.                 . '\'2\' AS browsernav'
  134.         );
  135.         $query->from('#__contact_details AS a')
  136.             ->join('INNER', '#__categories AS c ON c.id = a.catid')
  137.             ->where(
  138.                 '(a.name LIKE ' . $text . ' OR a.misc LIKE ' . $text . ' OR a.con_position LIKE ' . $text
  139.                     . ' OR a.address LIKE ' . $text . ' OR a.suburb LIKE ' . $text . ' OR a.state LIKE ' . $text
  140.                     . ' OR a.country LIKE ' . $text . ' OR a.postcode LIKE ' . $text . ' OR a.telephone LIKE ' . $text
  141.                     . ' OR a.fax LIKE ' . $text . ') AND a.published IN (' . implode(',', $state) . ') AND c.published=1 '
  142.                     . ' AND a.access IN (' . $groups . ') AND c.access IN (' . $groups . ')'
  143.             )
  144.             ->group('a.id, a.con_position, a.misc, c.alias, c.id')
  145.             ->order($order);
  146.  
  147.         // Filter by language
  148.         if ($app->isSite() && JLanguageMultilang::isEnabled())
  149.         {
  150.             $tag = JFactory::getLanguage()->getTag();
  151.             $query->where('a.language in (' . $db->quote($tag) . ',' . $db->quote('*') . ')')
  152.                 ->where('c.language in (' . $db->quote($tag) . ',' . $db->quote('*') . ')');
  153.         }
  154.  
  155.         $db->setQuery($query, 0, $limit);
  156.         $rows = $db->loadObjectList();
  157.  
  158.         if ($rows)
  159.         {
  160.             foreach ($rows as $key => $row)
  161.             {
  162.                 $rows[$key]->href = 'index.php?option=com_contact&view=contact&id=' . $row->slug . '&catid=' . $row->catslug;
  163.                 $rows[$key]->text = $row->title;
  164.                 $rows[$key]->text .= ($row->con_position) ? ', ' . $row->con_position : '';
  165.                 $rows[$key]->text .= ($row->misc) ? ', ' . $row->misc : '';
  166.             }
  167.         }
  168.  
  169.         return $rows;
  170.     }
  171. }

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