PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_finder/models/index.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 426 lines | 209 code | 63 blank | 154 comment | 17 complexity | f1d4c59657f361a292ba10d7d1dd1186 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_finder
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Index model class for Finder.
  12. *
  13. * @since 2.5
  14. */
  15. class FinderModelIndex extends JModelList
  16. {
  17. /**
  18. * The event to trigger after deleting the data.
  19. *
  20. * @var string
  21. * @since 2.5
  22. */
  23. protected $event_after_delete = 'onContentAfterDelete';
  24. /**
  25. * The event to trigger before deleting the data.
  26. *
  27. * @var string
  28. * @since 2.5
  29. */
  30. protected $event_before_delete = 'onContentBeforeDelete';
  31. /**
  32. * Constructor.
  33. *
  34. * @param array $config An associative array of configuration settings. [optional]
  35. *
  36. * @since 2.5
  37. * @see JController
  38. */
  39. public function __construct($config = array())
  40. {
  41. if (empty($config['filter_fields']))
  42. {
  43. $config['filter_fields'] = array(
  44. 'published', 'l.published',
  45. 'title', 'l.title',
  46. 'type_id', 'l.type_id',
  47. 'url', 'l.url',
  48. 'indexdate', 'l.indexdate',
  49. 'type'
  50. );
  51. }
  52. parent::__construct($config);
  53. }
  54. /**
  55. * Method to test whether a record can be deleted.
  56. *
  57. * @param object $record A record object.
  58. *
  59. * @return boolean True if allowed to delete the record. Defaults to the permission for the component.
  60. *
  61. * @since 2.5
  62. */
  63. protected function canDelete($record)
  64. {
  65. $user = JFactory::getUser();
  66. return $user->authorise('core.delete', $this->option);
  67. }
  68. /**
  69. * Method to test whether a record can be deleted.
  70. *
  71. * @param object $record A record object.
  72. *
  73. * @return boolean True if allowed to change the state of the record. Defaults to the permission for the component.
  74. *
  75. * @since 2.5
  76. */
  77. protected function canEditState($record)
  78. {
  79. $user = JFactory::getUser();
  80. return $user->authorise('core.edit.state', $this->option);
  81. }
  82. /**
  83. * Method to delete one or more records.
  84. *
  85. * @param array &$pks An array of record primary keys.
  86. *
  87. * @return boolean True if successful, false if an error occurs.
  88. *
  89. * @since 2.5
  90. */
  91. public function delete(&$pks)
  92. {
  93. $dispatcher = JEventDispatcher::getInstance();
  94. $pks = (array) $pks;
  95. $table = $this->getTable();
  96. // Include the content plugins for the on delete events.
  97. JPluginHelper::importPlugin('content');
  98. // Iterate the items to delete each one.
  99. foreach ($pks as $i => $pk)
  100. {
  101. if ($table->load($pk))
  102. {
  103. if ($this->canDelete($table))
  104. {
  105. $context = $this->option . '.' . $this->name;
  106. // Trigger the onContentBeforeDelete event.
  107. $result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
  108. if (in_array(false, $result, true))
  109. {
  110. $this->setError($table->getError());
  111. return false;
  112. }
  113. if (!$table->delete($pk))
  114. {
  115. $this->setError($table->getError());
  116. return false;
  117. }
  118. // Trigger the onContentAfterDelete event.
  119. $dispatcher->trigger($this->event_after_delete, array($context, $table));
  120. }
  121. else
  122. {
  123. // Prune items that you can't change.
  124. unset($pks[$i]);
  125. $error = $this->getError();
  126. if ($error)
  127. {
  128. $this->setError($error);
  129. }
  130. else
  131. {
  132. $this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
  133. }
  134. }
  135. }
  136. else
  137. {
  138. $this->setError($table->getError());
  139. return false;
  140. }
  141. }
  142. // Clear the component's cache
  143. $this->cleanCache();
  144. return true;
  145. }
  146. /**
  147. * Build an SQL query to load the list data.
  148. *
  149. * @return JDatabaseQuery A JDatabaseQuery object
  150. *
  151. * @since 2.5
  152. */
  153. protected function getListQuery()
  154. {
  155. $db = $this->getDbo();
  156. $query = $db->getQuery(true)
  157. ->select('l.*')
  158. ->select('t.title AS t_title')
  159. ->from($db->quoteName('#__finder_links') . ' AS l')
  160. ->join('INNER', $db->quoteName('#__finder_types') . ' AS t ON t.id = l.type_id');
  161. // Check the type filter.
  162. if ($this->getState('filter.type'))
  163. {
  164. $query->where('l.type_id = ' . (int) $this->getState('filter.type'));
  165. }
  166. // Check for state filter.
  167. if (is_numeric($this->getState('filter.state')))
  168. {
  169. $query->where('l.published = ' . (int) $this->getState('filter.state'));
  170. }
  171. // Check the search phrase.
  172. if ($this->getState('filter.search') != '')
  173. {
  174. $search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($this->getState('filter.search')), true) . '%'));
  175. // Do not filter by indexdate if $search contains non-ascii characters
  176. if (preg_match('/[^\x00-\x7F]/', $search))
  177. {
  178. $query->where('l.title LIKE ' . $search . ' OR l.url LIKE ' . $search);
  179. }
  180. else
  181. {
  182. $query->where('l.title LIKE ' . $search . ' OR l.url LIKE ' . $search . ' OR l.indexdate LIKE ' . $search);
  183. }
  184. }
  185. // Handle the list ordering.
  186. $ordering = $this->getState('list.ordering');
  187. $direction = $this->getState('list.direction');
  188. if (!empty($ordering))
  189. {
  190. $query->order($db->escape($ordering) . ' ' . $db->escape($direction));
  191. }
  192. return $query;
  193. }
  194. /**
  195. * Method to get the state of the Smart Search plug-ins.
  196. *
  197. * @return array Array of relevant plug-ins and whether they are enabled or not.
  198. *
  199. * @since 2.5
  200. */
  201. public function getPluginState()
  202. {
  203. $db = $this->getDbo();
  204. $query = $db->getQuery(true)
  205. ->select('name, enabled')
  206. ->from($db->quoteName('#__extensions'))
  207. ->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
  208. ->where($db->quoteName('folder') . ' IN(' . $db->quote('system') . ',' . $db->quote('content') . ')')
  209. ->where($db->quoteName('element') . ' = ' . $db->quote('finder'));
  210. $db->setQuery($query);
  211. $db->execute();
  212. $plugins = $db->loadObjectList('name');
  213. return $plugins;
  214. }
  215. /**
  216. * Method to get a store id based on model configuration state.
  217. *
  218. * This is necessary because the model is used by the component and
  219. * different modules that might need different sets of data or different
  220. * ordering requirements.
  221. *
  222. * @param string $id A prefix for the store id. [optional]
  223. *
  224. * @return string A store id.
  225. *
  226. * @since 2.5
  227. */
  228. protected function getStoreId($id = '')
  229. {
  230. // Compile the store id.
  231. $id .= ':' . $this->getState('filter.search');
  232. $id .= ':' . $this->getState('filter.state');
  233. $id .= ':' . $this->getState('filter.type');
  234. return parent::getStoreId($id);
  235. }
  236. /**
  237. * Returns a JTable object, always creating it.
  238. *
  239. * @param string $type The table type to instantiate. [optional]
  240. * @param string $prefix A prefix for the table class name. [optional]
  241. * @param array $config Configuration array for model. [optional]
  242. *
  243. * @return JTable A database object
  244. *
  245. * @since 2.5
  246. */
  247. public function getTable($type = 'Link', $prefix = 'FinderTable', $config = array())
  248. {
  249. return JTable::getInstance($type, $prefix, $config);
  250. }
  251. /**
  252. * Method to purge the index, deleting all links.
  253. *
  254. * @return boolean True on success, false on failure.
  255. *
  256. * @since 2.5
  257. * @throws Exception on database error
  258. */
  259. public function purge()
  260. {
  261. $db = $this->getDbo();
  262. // Truncate the links table.
  263. $db->truncateTable('#__finder_links');
  264. // Truncate the links terms tables.
  265. for ($i = 0; $i <= 15; $i++)
  266. {
  267. // Get the mapping table suffix.
  268. $suffix = dechex($i);
  269. $db->truncateTable('#__finder_links_terms' . $suffix);
  270. }
  271. // Truncate the terms table.
  272. $db->truncateTable('#__finder_terms');
  273. // Truncate the taxonomy map table.
  274. $db->truncateTable('#__finder_taxonomy_map');
  275. // Delete all the taxonomy nodes except the root.
  276. $query = $db->getQuery(true)
  277. ->delete($db->quoteName('#__finder_taxonomy'))
  278. ->where($db->quoteName('id') . ' > 1');
  279. $db->setQuery($query);
  280. $db->execute();
  281. // Truncate the tokens tables.
  282. $db->truncateTable('#__finder_tokens');
  283. // Truncate the tokens aggregate table.
  284. $db->truncateTable('#__finder_tokens_aggregate');
  285. return true;
  286. }
  287. /**
  288. * Method to auto-populate the model state. Calling getState in this method will result in recursion.
  289. *
  290. * @param string $ordering An optional ordering field. [optional]
  291. * @param string $direction An optional direction. [optional]
  292. *
  293. * @return void
  294. *
  295. * @since 2.5
  296. */
  297. protected function populateState($ordering = null, $direction = null)
  298. {
  299. // Load the filter state.
  300. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  301. $this->setState('filter.search', $search);
  302. $state = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_state', '', 'string');
  303. $this->setState('filter.state', $state);
  304. $type = $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string');
  305. $this->setState('filter.type', $type);
  306. // Load the parameters.
  307. $params = JComponentHelper::getParams('com_finder');
  308. $this->setState('params', $params);
  309. // List state information.
  310. parent::populateState('l.title', 'asc');
  311. }
  312. /**
  313. * Method to change the published state of one or more records.
  314. *
  315. * @param array &$pks A list of the primary keys to change.
  316. * @param integer $value The value of the published state. [optional]
  317. *
  318. * @return boolean True on success.
  319. *
  320. * @since 2.5
  321. */
  322. public function publish(&$pks, $value = 1)
  323. {
  324. $dispatcher = JEventDispatcher::getInstance();
  325. $user = JFactory::getUser();
  326. $table = $this->getTable();
  327. $pks = (array) $pks;
  328. // Include the content plugins for the change of state event.
  329. JPluginHelper::importPlugin('content');
  330. // Access checks.
  331. foreach ($pks as $i => $pk)
  332. {
  333. $table->reset();
  334. if ($table->load($pk))
  335. {
  336. if (!$this->canEditState($table))
  337. {
  338. // Prune items that you can't change.
  339. unset($pks[$i]);
  340. $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  341. return false;
  342. }
  343. }
  344. }
  345. // Attempt to change the state of the records.
  346. if (!$table->publish($pks, $value, $user->get('id')))
  347. {
  348. $this->setError($table->getError());
  349. return false;
  350. }
  351. $context = $this->option . '.' . $this->name;
  352. // Trigger the onContentChangeState event.
  353. $result = $dispatcher->trigger('onContentChangeState', array($context, $pks, $value));
  354. if (in_array(false, $result, true))
  355. {
  356. $this->setError($table->getError());
  357. return false;
  358. }
  359. // Clear the component's cache
  360. $this->cleanCache();
  361. return true;
  362. }
  363. }