[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @version        $Id: content.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package        Joomla
   5   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   6   * @license        GNU/GPL, see LICENSE.php
   7   * Joomla! is free software. This version may have been modified pursuant
   8   * to the GNU General Public License, and as distributed it includes or
   9   * is derivative of works licensed under the GNU General Public License or
  10   * other free or open source software licenses.
  11   * See COPYRIGHT.php for copyright notices and details.
  12   */
  13  
  14  // no direct access
  15  defined( '_JEXEC' ) or die( 'Restricted access' );
  16  
  17  $mainframe->registerEvent( 'onSearch', 'plgSearchContent' );
  18  $mainframe->registerEvent( 'onSearchAreas', 'plgSearchContentAreas' );
  19  
  20  JPlugin::loadLanguage( 'plg_search_content' );
  21  
  22  /**
  23   * @return array An array of search areas
  24   */
  25  function &plgSearchContentAreas()
  26  {
  27      static $areas = array(
  28          'content' => 'Articles'
  29      );
  30      return $areas;
  31  }
  32  
  33  /**
  34   * Content Search method
  35   * The sql must return the following fields that are used in a common display
  36   * routine: href, title, section, created, text, browsernav
  37   * @param string Target search string
  38   * @param string mathcing option, exact|any|all
  39   * @param string ordering option, newest|oldest|popular|alpha|category
  40   * @param mixed An array if the search it to be restricted to areas, null if search all
  41   */
  42  function plgSearchContent( $text, $phrase='', $ordering='', $areas=null )
  43  {
  44      global $mainframe;
  45  
  46      $db        =& JFactory::getDBO();
  47      $user    =& JFactory::getUser();
  48  
  49      require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
  50      require_once (JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_search'.DS.'helpers'.DS.'search.php');
  51  
  52      $searchText = $text;
  53      if (is_array( $areas )) {
  54          if (!array_intersect( $areas, array_keys( plgSearchContentAreas() ) )) {
  55              return array();
  56          }
  57      }
  58  
  59      // load plugin params info
  60       $plugin            =& JPluginHelper::getPlugin('search', 'content');
  61       $pluginParams    = new JParameter( $plugin->params );
  62  
  63      $sContent         = $pluginParams->get( 'search_content',         1 );
  64      $sUncategorised = $pluginParams->get( 'search_uncategorised',     1 );
  65      $sArchived         = $pluginParams->get( 'search_archived',         1 );
  66      $limit             = $pluginParams->def( 'search_limit',         50 );
  67  
  68      $nullDate         = $db->getNullDate();
  69      $date =& JFactory::getDate();
  70      $now = $date->toMySQL();
  71  
  72      $text = trim( $text );
  73      if ($text == '') {
  74          return array();
  75      }
  76  
  77      $wheres = array();
  78      switch ($phrase) {
  79          case 'exact':
  80              $text        = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', 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                  $word        = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
  97                  $wheres2     = array();
  98                  $wheres2[]     = 'a.title LIKE '.$word;
  99                  $wheres2[]     = 'a.introtext LIKE '.$word;
 100                  $wheres2[]     = 'a.fulltext LIKE '.$word;
 101                  $wheres2[]     = 'a.metakey LIKE '.$word;
 102                  $wheres2[]     = 'a.metadesc LIKE '.$word;
 103                  $wheres[]     = implode( ' OR ', $wheres2 );
 104              }
 105              $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
 106              break;
 107      }
 108  
 109      $morder = '';
 110      switch ($ordering) {
 111          case 'oldest':
 112              $order = 'a.created ASC';
 113              break;
 114  
 115          case 'popular':
 116              $order = 'a.hits DESC';
 117              break;
 118  
 119          case 'alpha':
 120              $order = 'a.title ASC';
 121              break;
 122  
 123          case 'category':
 124              $order = 'b.title ASC, a.title ASC';
 125              $morder = 'a.title ASC';
 126              break;
 127  
 128          case 'newest':
 129              default:
 130              $order = 'a.created DESC';
 131              break;
 132      }
 133  
 134      $rows = array();
 135  
 136      // search articles
 137      if ( $sContent && $limit > 0 )
 138      {
 139          $query = 'SELECT a.title AS title, a.metadesc, a.metakey,'
 140          . ' a.created AS created,'
 141          . ' CONCAT(a.introtext, a.fulltext) AS text,'
 142          . ' CONCAT_WS( "/", u.title, b.title ) AS section,'
 143          . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
 144          . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(":", b.id, b.alias) ELSE b.id END as catslug,'
 145          . ' u.id AS sectionid,'
 146          . ' "2" AS browsernav'
 147          . ' FROM #__content AS a'
 148          . ' INNER JOIN #__categories AS b ON b.id=a.catid'
 149          . ' INNER JOIN #__sections AS u ON u.id = a.sectionid'
 150          . ' WHERE ( '.$where.' )'
 151          . ' AND a.state = 1'
 152          . ' AND u.published = 1'
 153          . ' AND b.published = 1'
 154          . ' AND a.access <= '.(int) $user->get( 'aid' )
 155          . ' AND b.access <= '.(int) $user->get( 'aid' )
 156          . ' AND u.access <= '.(int) $user->get( 'aid' )
 157          . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
 158          . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
 159          . ' GROUP BY a.id'
 160          . ' ORDER BY '. $order
 161          ;
 162          $db->setQuery( $query, 0, $limit );
 163          $list = $db->loadObjectList();
 164          $limit -= count($list);
 165  
 166          if(isset($list))
 167          {
 168              foreach($list as $key => $item)
 169              {
 170                  $list[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid);
 171              }
 172          }
 173          $rows[] = $list;
 174      }
 175  
 176      // search uncategorised content
 177      if ( $sUncategorised && $limit > 0 )
 178      {
 179          $query = 'SELECT id, a.title AS title, a.created AS created, a.metadesc, a.metakey, '
 180          . ' CONCAT(a.introtext, a.fulltext) AS text,'
 181          . ' "2" as browsernav, "'. $db->getEscaped(JText::_('Uncategorised Content')) .'" AS section'
 182          . ' FROM #__content AS a'
 183          . ' WHERE ('.$where.')'
 184          . ' AND a.state = 1'
 185          . ' AND a.access <= '.(int) $user->get( 'aid' )
 186          . ' AND a.sectionid = 0'
 187          . ' AND a.catid = 0'
 188          . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
 189          . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
 190          . ' ORDER BY '. ($morder ? $morder : $order)
 191          ;
 192          $db->setQuery( $query, 0, $limit );
 193          $list2 = $db->loadObjectList();
 194          $limit -= count($list2);
 195  
 196          if(isset($list2))
 197          {
 198              foreach($list2 as $key => $item)
 199              {
 200                  $list2[$key]->href = ContentHelperRoute::getArticleRoute($item->id);
 201              }
 202          }
 203  
 204          $rows[] = $list2;
 205      }
 206  
 207      // search archived content
 208      if ( $sArchived && $limit > 0 )
 209      {
 210          $searchArchived = JText::_( 'Archived' );
 211  
 212          $query = 'SELECT a.title AS title, a.metadesc, a.metakey,'
 213          . ' a.created AS created,'
 214          . ' CONCAT(a.introtext, a.fulltext) AS text,'
 215          . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
 216          . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(":", b.id, b.alias) ELSE b.id END as catslug,'
 217          . ' u.id AS sectionid,'
 218          . ' CONCAT_WS( "/", u.title, b.title ) AS section,'
 219          . ' "2" AS browsernav'
 220          . ' FROM #__content AS a'
 221          . ' INNER JOIN #__categories AS b ON b.id=a.catid AND b.access <= ' .$user->get( 'gid' )
 222          . ' INNER JOIN #__sections AS u ON u.id = a.sectionid'
 223          . ' WHERE ( '.$where.' )'
 224          . ' AND a.state = -1'
 225          . ' AND u.published = 1'
 226          . ' AND b.published = 1'
 227          . ' AND a.access <= '.(int) $user->get( 'aid' )
 228          . ' AND b.access <= '.(int) $user->get( 'aid' )
 229          . ' AND u.access <= '.(int) $user->get( 'aid' )
 230          . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
 231          . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
 232          . ' ORDER BY '. $order
 233          ;
 234          $db->setQuery( $query, 0, $limit );
 235          $list3 = $db->loadObjectList();
 236  
 237          if(isset($list3))
 238          {
 239              foreach($list3 as $key => $item)
 240              {
 241                  $list3[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid);
 242              }
 243          }
 244  
 245          $rows[] = $list3;
 246      }
 247  
 248      $results = array();
 249      if(count($rows))
 250      {
 251          foreach($rows as $row)
 252          {
 253              $new_row = array();
 254              foreach($row AS $key => $article) {
 255                  if(searchHelper::checkNoHTML($article, $searchText, array('text', 'title', 'metadesc', 'metakey'))) {
 256                      $new_row[] = $article;
 257                  }
 258              }
 259              $results = array_merge($results, (array) $new_row);
 260          }
 261      }
 262  
 263      return $results;
 264  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1