/components/com_search/models/search.php

https://github.com/chalosalvador/GDS · PHP · 214 lines · 101 code · 28 blank · 85 comment · 10 complexity · 13146a0c3d11a223a2588c6c7598fdea MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: search.php 21504 2011-06-10 06:21:35Z infograf768 $
  4. * @package Joomla.Site
  5. * @subpackage com_search
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.model');
  12. /**
  13. * Search Component Search Model
  14. *
  15. * @package Joomla.Site
  16. * @subpackage com_search
  17. * @since 1.5
  18. */
  19. class SearchModelSearch extends JModel
  20. {
  21. /**
  22. * Sezrch data array
  23. *
  24. * @var array
  25. */
  26. var $_data = null;
  27. /**
  28. * Search total
  29. *
  30. * @var integer
  31. */
  32. var $_total = null;
  33. /**
  34. * Search areas
  35. *
  36. * @var integer
  37. */
  38. var $_areas = null;
  39. /**
  40. * Pagination object
  41. *
  42. * @var object
  43. */
  44. var $_pagination = null;
  45. /**
  46. * Constructor
  47. *
  48. * @since 1.5
  49. */
  50. function __construct()
  51. {
  52. parent::__construct();
  53. //Get configuration
  54. $app = JFactory::getApplication();
  55. $config = JFactory::getConfig();
  56. // Get the pagination request variables
  57. $this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'int'));
  58. $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
  59. // Set the search parameters
  60. $keyword = urldecode(JRequest::getString('searchword'));
  61. $match = JRequest::getWord('searchphrase', 'all');
  62. $ordering = JRequest::getWord('ordering', 'newest');
  63. $this->setSearch($keyword, $match, $ordering);
  64. //Set the search areas
  65. $areas = JRequest::getVar('areas');
  66. $this->setAreas($areas);
  67. }
  68. /**
  69. * Method to set the search parameters
  70. *
  71. * @access public
  72. * @param string search string
  73. * @param string mathcing option, exact|any|all
  74. * @param string ordering option, newest|oldest|popular|alpha|category
  75. */
  76. function setSearch($keyword, $match = 'all', $ordering = 'newest')
  77. {
  78. if (isset($keyword)) {
  79. $this->setState('origkeyword', $keyword);
  80. if($match !== 'exact') {
  81. $keyword = preg_replace('#\xE3\x80\x80#s', ' ', $keyword);
  82. }
  83. $this->setState('keyword', $keyword);
  84. }
  85. if (isset($match)) {
  86. $this->setState('match', $match);
  87. }
  88. if (isset($ordering)) {
  89. $this->setState('ordering', $ordering);
  90. }
  91. }
  92. /**
  93. * Method to set the search areas
  94. *
  95. * @access public
  96. * @param array Active areas
  97. * @param array Search areas
  98. */
  99. function setAreas($active = array(), $search = array())
  100. {
  101. $this->_areas['active'] = $active;
  102. $this->_areas['search'] = $search;
  103. }
  104. /**
  105. * Method to get weblink item data for the category
  106. *
  107. * @access public
  108. * @return array
  109. */
  110. function getData()
  111. {
  112. // Lets load the content if it doesn't already exist
  113. if (empty($this->_data))
  114. {
  115. $areas = $this->getAreas();
  116. JPluginHelper::importPlugin('search');
  117. $dispatcher = JDispatcher::getInstance();
  118. $results = $dispatcher->trigger('onContentSearch', array(
  119. $this->getState('keyword'),
  120. $this->getState('match'),
  121. $this->getState('ordering'),
  122. $areas['active'])
  123. );
  124. $rows = array();
  125. foreach ($results as $result) {
  126. $rows = array_merge((array) $rows, (array) $result);
  127. }
  128. $this->_total = count($rows);
  129. if ($this->getState('limit') > 0) {
  130. $this->_data = array_splice($rows, $this->getState('limitstart'), $this->getState('limit'));
  131. } else {
  132. $this->_data = $rows;
  133. }
  134. }
  135. return $this->_data;
  136. }
  137. /**
  138. * Method to get the total number of weblink items for the category
  139. *
  140. * @access public
  141. * @return integer
  142. */
  143. function getTotal()
  144. {
  145. return $this->_total;
  146. }
  147. /**
  148. * Method to get a pagination object of the weblink items for the category
  149. *
  150. * @access public
  151. * @return integer
  152. */
  153. function getPagination()
  154. {
  155. // Lets load the content if it doesn't already exist
  156. if (empty($this->_pagination))
  157. {
  158. jimport('joomla.html.pagination');
  159. $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
  160. }
  161. return $this->_pagination;
  162. }
  163. /**
  164. * Method to get the search areas
  165. *
  166. * @since 1.5
  167. */
  168. function getAreas()
  169. {
  170. // Load the Category data
  171. if (empty($this->_areas['search']))
  172. {
  173. $areas = array();
  174. JPluginHelper::importPlugin('search');
  175. $dispatcher = JDispatcher::getInstance();
  176. $searchareas = $dispatcher->trigger('onContentSearchAreas');
  177. foreach ($searchareas as $area) {
  178. if (is_array($area)) {
  179. $areas = array_merge($areas, $area);
  180. }
  181. }
  182. $this->_areas['search'] = $areas;
  183. }
  184. return $this->_areas;
  185. }
  186. }