/components/com_search/models/search.php

https://github.com/dextercowley/joomla-cms · PHP · 218 lines · 107 code · 27 blank · 84 comment · 10 complexity · d41c9e5a1b73b01b8f910bdf17e67a64 MD5 · raw file

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