PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/googlemini/code/trunk/components/com_artofgm/models/artofgm.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 404 lines | 202 code | 63 blank | 139 comment | 25 complexity | 04912718b3ae5f89dfb2a3f1822698bc MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: artofgm.php 518 2011-01-07 06:36:43Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_artofgm
  6. * @copyright Copyright 2011 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. juimport('joomla.application.component.model16');
  12. /**
  13. * ArtofGM model.
  14. *
  15. * Based on JModelList.
  16. *
  17. * @package NewLifeInIT
  18. * @subpackage com_artofgm
  19. * @since 1.0
  20. */
  21. class ArtofGMModelArtofGM extends JModel16
  22. {
  23. /**
  24. * Internal memory based cache array of data.
  25. *
  26. * @var array
  27. * @since 1.6
  28. */
  29. protected $cache = array();
  30. /**
  31. * Context string for the model type. This is used to handle uniqueness
  32. * when dealing with the getStoreId() method and caching data structures.
  33. *
  34. * @var string
  35. * @since 1.6
  36. */
  37. protected $context = null;
  38. /**
  39. * An internal cache for the last query used.
  40. *
  41. * @var JDatabaseQuery
  42. * @since 1.6
  43. */
  44. protected $query = array();
  45. /**
  46. * Constructor.
  47. *
  48. * @param array An optional associative array of configuration settings.
  49. * @see JController
  50. */
  51. public function __construct($config = array())
  52. {
  53. parent::__construct($config);
  54. // Guess the context as Option.ModelName.
  55. if (empty($this->context)) {
  56. $this->context = strtolower($this->option.'.'.$this->getName());
  57. }
  58. }
  59. /**
  60. * Method to get an array of data items.
  61. *
  62. * @return mixed An array of data items on success, false on failure.
  63. * @since 1.6
  64. */
  65. public function getItems()
  66. {
  67. // Get a storage key.
  68. $store = $this->getStoreId();
  69. // Try to load the data from internal storage.
  70. if (!empty($this->cache[$store])) {
  71. return $this->cache[$store];
  72. }
  73. require_once JPATH_SITE.'/components/com_artofgm/helpers/http.php';
  74. $config = JComponentHelper::getParams('com_artofgm');
  75. $server = $config->get('server');
  76. $listStart = (int) $this->getState('list.start');
  77. $listLimit = (int) $this->getState('list.limit');
  78. if (empty($server)) {
  79. throw new Exception(JText::_('COM_ARTOFGM_SERVER_NOT_SET'));
  80. }
  81. // Optional variables.
  82. $q = urlencode($this->getState('list.q'));
  83. $as_q = urlencode($this->getState('list.as_q'));
  84. $as_epq = urlencode($this->getState('list.as_epq'));
  85. $as_oq = urlencode($this->getState('list.as_oq'));
  86. $as_eq = urlencode($this->getState('list.as_eq'));
  87. $as_ft = urlencode($this->getState('list.as_ft'));
  88. $as_filetype = urlencode($this->getState('list.as_filetype'));
  89. $as_occt = urlencode($this->getState('list.as_occt'));
  90. $as_dt = urlencode($this->getState('list.as_dt'));
  91. $as_sitesearch = urlencode($this->getState('list.as_sitesearch'));
  92. $sort = urlencode($this->getState('list.sort'));
  93. $filter = urlencode($this->getState('list.filter'));
  94. $lr = urlencode($this->getState('list.lr'));
  95. // Check if this is an advanced search.
  96. $adv = ($as_q
  97. || $as_epq
  98. || $as_oq
  99. || $as_eq
  100. || ($as_ft == 'e')
  101. || $as_filetype
  102. || $as_occt
  103. // || ($as_dt == 'e')
  104. || $as_sitesearch
  105. );
  106. $url = $server.'/search?' .
  107. ($adv
  108. ? (
  109. ($as_q ? '&as_q='.$as_q : '')
  110. .($as_epq ? '&as_epq='.$as_epq : '')
  111. .($as_oq ? '&as_oq='.$as_oq : '')
  112. .($as_eq ? '&as_eq='.$as_eq : '')
  113. .($as_ft == 'e' || ($as_ft == 'i' && $as_filetype) ? '&as_ft='.$as_ft.'&as_filetype='.$as_filetype : '')
  114. .($as_occt ? '&as_occt='.$as_occt : '')
  115. .($as_sitesearch ? '&as_dt='.$as_dt.'&as_sitesearch='.$as_sitesearch : '')
  116. )
  117. : '&q='.$q)
  118. .'&output='.$this->getState('list.output', 'xml')
  119. .'&site='.$this->getState('list.site', 'default_collection')
  120. .'&client='.$this->getState('list.client', 'default_frontend')
  121. .'&start='.$listStart
  122. .'&num='.$this->getState('list.limit')
  123. .'&oe=UTF8'
  124. .($sort ? '&sort='.$sort : '')
  125. .($filter !== null ? '&filter='.$filter : '')
  126. .($lr ? '&lr='.$lr : '')
  127. ;
  128. // Get the content from the server.
  129. $content = ArtofGMHelperHttp::getUrl($url);
  130. if ($this->getState('debug')) {
  131. $this->setState('server.url', $url);
  132. $this->setState('server.response', $content);
  133. }
  134. // Parse the XML.
  135. try
  136. {
  137. $xml = new SimpleXMLElement($content);
  138. }
  139. catch (Exception $e)
  140. {
  141. JError::raiseError(
  142. 500,
  143. $e->getMessage().
  144. '<br />'.$url.
  145. '<br /><textarea cols="100" rows="10">'.htmlspecialchars($content).'</textarea>'
  146. );
  147. }
  148. // Get the 'q', could come from advanced search.
  149. $this->setState('list.q', $xml->Q);
  150. // Count the <FI/> to check if results are filtered.
  151. $filtered = $xml->xpath('//FI');
  152. $this->setState('list.filtered', (boolean) count($filtered));
  153. // Get the NU next link
  154. $hasNext = $xml->xpath('//NB/NU');
  155. $this->setState('list.hasnext', (boolean) count($hasNext));
  156. // The estimated total number of results for the search.
  157. $magnitude = (int) $xml->RES->M;
  158. $this->setState('list.magnitude', $magnitude);
  159. // The index (1-based) of the first search result returned in this result set.
  160. $startNum = (int) $xml->RES['SN'];
  161. $this->setState('list.startnum', $startNum);
  162. // The index (1-based) of the last search result returned in this result set.
  163. $endNum = (int) $xml->RES['EN'];
  164. $this->setState('list.endnum', $endNum);
  165. if (!$hasNext) {
  166. $this->setTotal($endNum);
  167. }
  168. else {
  169. // The appliance returns no more than 1,000 results total for a single query.
  170. $this->setTotal(min(1000, $magnitude));
  171. }
  172. if ($xml->Spelling) {
  173. $this->setState('list.spelling', (string) $xml->Spelling->Suggestion['q']);
  174. }
  175. else {
  176. $this->setState('list.spelling', null);
  177. }
  178. // Search the results records
  179. $items = array();
  180. $results = $xml->xpath('//R');
  181. foreach ($results as $result)
  182. {
  183. $item = new stdClass;
  184. // The index of the search result.
  185. $item->n = (int) $result['N'];
  186. // The recommended indentation level of the results.
  187. $item->indent = (int) $result['L'];
  188. // The index of the search result.
  189. $item->mime = (string) $result['MIME'];
  190. // The URL of the search result.
  191. $item->url = (string) $result->U;
  192. //The URL encoded version of the URL that is in the U parameter
  193. $item->urle = (string) $result->UE;
  194. // The title of the search result.
  195. $item->title = (string) $result->T;
  196. // Provides a general rating of the relevance of the search result.
  197. $item->relevance = (string) $result->RK;
  198. // The snippet for the search result.
  199. $item->snippet = (string) $result->S;
  200. // The snippet for the search result.
  201. $item->date = (string) $result->CRAWLDATE;
  202. // The snippet for the search result.
  203. $item->size = (string) $result->HAS->C['SZ'];
  204. $items[] = $item;
  205. }
  206. // Add the items to the internal cache.
  207. $this->cache[$store] = $items;
  208. return $this->cache[$store];
  209. }
  210. /**
  211. * Method to get a JPagination object for the data set.
  212. *
  213. * @return object A JPagination object for the data set.
  214. * @since 1.6
  215. */
  216. public function getPagination()
  217. {
  218. // Get a storage key.
  219. $store = $this->getStoreId('getPagination');
  220. // Try to load the data from internal storage.
  221. if (!empty($this->cache[$store])) {
  222. return $this->cache[$store];
  223. }
  224. // Create the pagination object.
  225. jimport('joomla.html.pagination');
  226. $limit = (int) $this->getState('list.limit') - (int) $this->getState('list.links');
  227. $page = new JPagination($this->getTotal(), (int) $this->getState('list.start'), $limit);
  228. // Add the object to the internal cache.
  229. $this->cache[$store] = $page;
  230. return $this->cache[$store];
  231. }
  232. /**
  233. * Method to get the params for this model.
  234. *
  235. * @return JRegistry
  236. * @since 1.0
  237. */
  238. public function getParams()
  239. {
  240. // Initialise variables.
  241. $params = JFactory::getApplication()->getParams();
  242. return $params;
  243. }
  244. /**
  245. * Method to get a store id based on the model configuration state.
  246. *
  247. * This is necessary because the model is used by the component and
  248. * different modules that might need different sets of data or different
  249. * ordering requirements.
  250. *
  251. * @param string An identifier string to generate the store id.
  252. * @return string A store id.
  253. * @since 1.6
  254. */
  255. protected function getStoreId($id = '')
  256. {
  257. // Add the list state to the store id.
  258. $id .= ':'.$this->getState('list.start');
  259. $id .= ':'.$this->getState('list.limit');
  260. $id .= ':'.$this->getState('list.ordering');
  261. $id .= ':'.$this->getState('list.direction');
  262. return md5($this->context.':'.$id);
  263. }
  264. /**
  265. * Method to get the total number of items for the data set.
  266. *
  267. * @return integer The total number of items available in the data set.
  268. * @since 1.6
  269. */
  270. public function getTotal()
  271. {
  272. // Get a storage key.
  273. $store = $this->getStoreId('getTotal');
  274. // Try to load the data from internal storage.
  275. if (!empty($this->cache[$store])) {
  276. return $this->cache[$store];
  277. }
  278. return 0;
  279. }
  280. /**
  281. * Method to auto-populate the model state.
  282. *
  283. * This method should only be called once per instantiation and is designed
  284. * to be called on the first call to the getState() method unless the model
  285. * configuration flag to ignore the request is set.
  286. *
  287. * Note. Calling getState in this method will result in recursion.
  288. *
  289. * @param string An optional ordering field.
  290. * @param string An optional direction (asc|desc).
  291. * @since 1.6
  292. */
  293. protected function populateState()
  294. {
  295. // If the context is set, assume that stateful lists are used.
  296. if ($this->context) {
  297. $app = JFactory::getApplication();
  298. $this->setState('list.q', JRequest::getVar('q'));
  299. $this->setState('list.as_q', JRequest::getVar('as_q'));
  300. $this->setState('list.as_epq', JRequest::getVar('as_epq'));
  301. $this->setState('list.as_oq', JRequest::getVar('as_oq'));
  302. $this->setState('list.as_eq', JRequest::getVar('as_eq'));
  303. $this->setState('list.as_ft', JRequest::getVar('as_ft'));
  304. $this->setState('list.as_filetype', JRequest::getVar('as_filetype'));
  305. $this->setState('list.as_occt', JRequest::getVar('as_occt'));
  306. $this->setState('list.as_lq', JRequest::getVar('as_lq'));
  307. $this->setState('list.as_dt', JRequest::getVar('as_dt'));
  308. $this->setState('list.as_sitesearch', JRequest::getVar('as_sitesearch'));
  309. $this->setState('list.sort', JRequest::getVar('sort'));
  310. $this->setState('list.filter', JRequest::getVar('filter', 1));
  311. // Pagination values.
  312. $value = JRequest::getInt('limit', $app->getCfg('list_limit'));
  313. $limit = $value;
  314. $this->setState('list.limit', $limit);
  315. $value = JRequest::getInt('limitstart', 0);
  316. $limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0);
  317. $this->setState('list.start', $limitstart);
  318. // $value = $app->getUserStateFromRequest($this->context.'.ordercol', 'filter_order');
  319. // $this->setState('list.ordering', $value);
  320. //
  321. // $value = $app->getUserStateFromRequest($this->context.'.orderdirn', 'filter_order_Dir');
  322. // $this->setState('list.direction', $value);
  323. }
  324. else {
  325. $this->setState('list.start', 0);
  326. $this->state->set('list.limit', 0);
  327. }
  328. }
  329. /**
  330. * Method to set the total number of items for the data set.
  331. *
  332. * @return integer The total number of items available in the data set.
  333. * @since 1.6
  334. */
  335. public function setTotal($total)
  336. {
  337. // Get a storage key.
  338. $store = $this->getStoreId('getTotal');
  339. $this->cache[$store] = $total;
  340. }
  341. }