Source for file view.html.php

Documentation is available at view.html.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_search
  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.  * HTML View class for the search component
  14.  *
  15.  * @package     Joomla.Site
  16.  * @subpackage  com_search
  17.  * @since       1.0
  18.  */
  19. class SearchViewSearch extends JViewLegacy
  20. {
  21.     /**
  22.      * Execute and display a template script.
  23.      *
  24.      * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  25.      *
  26.      * @return  mixed  A string if successful, otherwise a Error object.
  27.      */
  28.     public function display($tpl = null)
  29.     {
  30.         require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/search.php';
  31.  
  32.         $app     = JFactory::getApplication();
  33.         $uri     = JUri::getInstance();
  34.  
  35.         $error   = null;
  36.         $rows    = null;
  37.         $results = null;
  38.         $total   = 0;
  39.  
  40.         // Get some data from the model
  41.         $areas      = $this->get('areas');
  42.         $state      = $this->get('state');
  43.         $searchword = $state->get('keyword');
  44.         $params     = $app->getParams();
  45.  
  46.         $menus = $app->getMenu();
  47.         $menu  = $menus->getActive();
  48.  
  49.         // Because the application sets a default page title, we need to get it right from the menu item itself
  50.         if (is_object($menu))
  51.         {
  52.             $menu_params = new JRegistry;
  53.             $menu_params->loadString($menu->params);
  54.  
  55.             if (!$menu_params->get('page_title'))
  56.             {
  57.                 $params->set('page_title', JText::_('COM_SEARCH_SEARCH'));
  58.             }
  59.         }
  60.         else
  61.         {
  62.             $params->set('page_title', JText::_('COM_SEARCH_SEARCH'));
  63.         }
  64.  
  65.         $title = $params->get('page_title');
  66.  
  67.         if ($app->getCfg('sitename_pagetitles', 0) == 1)
  68.         {
  69.             $title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title);
  70.         }
  71.         elseif ($app->getCfg('sitename_pagetitles', 0) == 2)
  72.         {
  73.             $title = JText::sprintf('JPAGETITLE', $title, $app->getCfg('sitename'));
  74.         }
  75.  
  76.         $this->document->setTitle($title);
  77.  
  78.         if ($params->get('menu-meta_description'))
  79.         {
  80.             $this->document->setDescription($params->get('menu-meta_description'));
  81.         }
  82.  
  83.         if ($params->get('menu-meta_keywords'))
  84.         {
  85.             $this->document->setMetadata('keywords', $params->get('menu-meta_keywords'));
  86.         }
  87.  
  88.         if ($params->get('robots'))
  89.         {
  90.             $this->document->setMetadata('robots', $params->get('robots'));
  91.         }
  92.  
  93.         // Built select lists
  94.         $orders   = array();
  95.         $orders[] = JHtml::_('select.option', 'newest', JText::_('COM_SEARCH_NEWEST_FIRST'));
  96.         $orders[] = JHtml::_('select.option', 'oldest', JText::_('COM_SEARCH_OLDEST_FIRST'));
  97.         $orders[] = JHtml::_('select.option', 'popular', JText::_('COM_SEARCH_MOST_POPULAR'));
  98.         $orders[] = JHtml::_('select.option', 'alpha', JText::_('COM_SEARCH_ALPHABETICAL'));
  99.         $orders[] = JHtml::_('select.option', 'category', JText::_('JCATEGORY'));
  100.  
  101.         $lists             = array();
  102.         $lists['ordering'] = JHtml::_('select.genericlist', $orders, 'ordering', 'class="inputbox"', 'value', 'text', $state->get('ordering'));
  103.  
  104.         $searchphrases         = array();
  105.         $searchphrases[]       = JHtml::_('select.option', 'all', JText::_('COM_SEARCH_ALL_WORDS'));
  106.         $searchphrases[]       = JHtml::_('select.option', 'any', JText::_('COM_SEARCH_ANY_WORDS'));
  107.         $searchphrases[]       = JHtml::_('select.option', 'exact', JText::_('COM_SEARCH_EXACT_PHRASE'));
  108.         $lists['searchphrase'] = JHtml::_('select.radiolist', $searchphrases, 'searchphrase', '', 'value', 'text', $state->get('match'));
  109.  
  110.         // Log the search
  111.         JSearchHelper::logSearch($searchword, 'com_search');
  112.  
  113.         // Limit searchword
  114.         $lang        = JFactory::getLanguage();
  115.         $upper_limit = $lang->getUpperLimitSearchWord();
  116.         $lower_limit = $lang->getLowerLimitSearchWord();
  117.  
  118.         if (SearchHelper::limitSearchWord($searchword))
  119.         {
  120.             $error = JText::sprintf('COM_SEARCH_ERROR_SEARCH_MESSAGE', $lower_limit, $upper_limit);
  121.         }
  122.  
  123.         // Sanitise searchword
  124.         if (SearchHelper::santiseSearchWord($searchword, $state->get('match')))
  125.         {
  126.             $error = JText::_('COM_SEARCH_ERROR_IGNOREKEYWORD');
  127.         }
  128.  
  129.         if (!$searchword && !empty($this->input) && count($this->input->post))
  130.         {
  131.             // $error = JText::_('COM_SEARCH_ERROR_ENTERKEYWORD');
  132.         }
  133.  
  134.         // Put the filtered results back into the model
  135.         // for next release, the checks should be done in the model perhaps...
  136.         $state->set('keyword', $searchword);
  137.  
  138.         if ($error == null)
  139.         {
  140.             $results    = $this->get('data');
  141.             $total      = $this->get('total');
  142.             $pagination = $this->get('pagination');
  143.  
  144.             require_once JPATH_SITE . '/components/com_content/helpers/route.php';
  145.  
  146.             for ($i = 0, $count = count($results); $i < $count; $i++)
  147.             {
  148.                 $row = & $results[$i]->text;
  149.  
  150.                 if ($state->get('match') == 'exact')
  151.                 {
  152.                     $searchwords = array($searchword);
  153.                     $needle      = $searchword;
  154.                 }
  155.                 else
  156.                 {
  157.                     $searchworda = preg_replace('#\xE3\x80\x80#s', ' ', $searchword);
  158.                     $searchwords = preg_split("/\s+/u", $searchworda);
  159.                     $needle      = $searchwords[0];
  160.                 }
  161.  
  162.                 $row         = SearchHelper::prepareSearchContent($row, $needle);
  163.                 $searchwords = array_values(array_unique($searchwords));
  164.                 $srow        = strtolower(SearchHelper::remove_accents($row));
  165.                 $hl1         = '<span class="highlight">';
  166.                 $hl2         = '</span>';
  167.                 $cnt         = 0;
  168.  
  169.                 foreach ($searchwords as $hlword)
  170.                 {
  171.                     if (extension_loaded('mbstring'))
  172.                     {
  173.                         if (($pos = mb_strpos($srow, strtolower(SearchHelper::remove_accents($hlword)))) !== false)
  174.                         {
  175.                             $pos += $cnt++ * mb_strlen($hl1 . $hl2);
  176.  
  177.                             // iconv transliterates '€' to 'EUR'
  178.                             // TODO: add other expanding translations?
  179.                             $eur_compensation = $pos > 0 ? substr_count($row, "\xE2\x82\xAC", 0, $pos) * 2 : 0;
  180.                             $pos -= $eur_compensation;
  181.                             $row = mb_substr($row, 0, $pos) . $hl1 . mb_substr($row, $pos, mb_strlen($hlword)) . $hl2 . mb_substr($row, $pos + mb_strlen($hlword));
  182.                         }
  183.                     }
  184.                     else
  185.                     {
  186.                         if (($pos = JString::strpos($srow, strtolower(SearchHelper::remove_accents($hlword)))) !== false)
  187.                         {
  188.                             $pos += $cnt++ * JString::strlen($hl1 . $hl2);
  189.  
  190.                             // iconv transliterates '€' to 'EUR'
  191.                             // TODO: add other expanding translations?
  192.                             $eur_compensation = $pos > 0 ? substr_count($row, "\xE2\x82\xAC", 0, $pos) * 2 : 0;
  193.                             $pos -= $eur_compensation;
  194.                             $row = JString::substr($row, 0, $pos) . $hl1 . JString::substr($row, $pos, JString::strlen($hlword)) . $hl2 . JString::substr($row, $pos + JString::strlen($hlword));
  195.                         }
  196.                     }
  197.                 }
  198.  
  199.                 $result = & $results[$i];
  200.  
  201.                 if ($result->created)
  202.                 {
  203.                     $created = JHtml::_('date', $result->created, JText::_('DATE_FORMAT_LC3'));
  204.                 }
  205.                 else
  206.                 {
  207.                     $created = '';
  208.                 }
  209.  
  210.                 $result->text    = JHtml::_('content.prepare', $result->text, '', 'com_search.search');
  211.                 $result->created = $created;
  212.                 $result->count   = $i + 1;
  213.             }
  214.         }
  215.  
  216.         // Check for layout override
  217.         $active = JFactory::getApplication()->getMenu()->getActive();
  218.  
  219.         if (isset($active->query['layout']))
  220.         {
  221.             $this->setLayout($active->query['layout']);
  222.         }
  223.  
  224.         // Escape strings for HTML output
  225.         $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx'));
  226.         $this->pagination    = &$pagination;
  227.         $this->results       = &$results;
  228.         $this->lists         = &$lists;
  229.         $this->params        = &$params;
  230.         $this->ordering      = $state->get('ordering');
  231.         $this->searchword    = $searchword;
  232.         $this->origkeyword   = $state->get('origkeyword');
  233.         $this->searchphrase  = $state->get('match');
  234.         $this->searchareas   = $areas;
  235.         $this->total         = $total;
  236.         $this->error         = $error;
  237.         $this->action        = $uri;
  238.  
  239.         parent::display($tpl);
  240.     }
  241. }

Documentation generated on Tue, 19 Nov 2013 15:17:22 +0100 by phpDocumentor 1.4.3