[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/plugins/search/content/ -> content.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1