PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_kunena/models/trash.php

https://gitlab.com/bilovus-andriy/voda_site
PHP | 345 lines | 225 code | 57 blank | 63 comment | 34 complexity | 78dd5c69d056c9049c2b66fefe10cc34 MD5 | raw file
  1. <?php
  2. /**
  3. * Kunena Component
  4. * @package Kunena.Administrator
  5. * @subpackage Models
  6. *
  7. * @copyright (C) 2008 - 2014 Kunena Team. All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  9. * @link http://www.kunena.org
  10. **/
  11. defined ( '_JEXEC' ) or die ();
  12. jimport ( 'joomla.application.component.model' );
  13. /**
  14. * Trash Model for Kunena
  15. *
  16. * @since 2.0
  17. */
  18. class KunenaAdminModelTrash extends KunenaModel {
  19. protected $__state_set = false;
  20. protected $_items = false;
  21. protected $_items_order = false;
  22. protected $_object = false;
  23. /**
  24. * Method to auto-populate the model state.
  25. */
  26. protected function populateState() {
  27. $this->context = 'com_kunena.admin.trash';
  28. $layout = $this->getUserStateFromRequest ( "com_kunena.admin.trash.layout", 'layout', 'messages', 'cmd' );
  29. // Set default view on messages
  30. if ($layout != 'messages') $layout='topics';
  31. $this->setState ( 'layout', $layout );
  32. if ($layout) {
  33. $this->context .= '.'.$layout;
  34. }
  35. // List state information
  36. $value = $this->getUserStateFromRequest ( "com_kunena.admin.trash.list.limit", 'limit', $this->app->getCfg ( 'list_limit' ), 'int' );
  37. $this->setState ( 'list.limit', $value );
  38. $value = $this->getUserStateFromRequest ( "com_kunena.admin.trash.list.start", 'limitstart', 0, 'int' );
  39. $this->setState ( 'list.start', $value );
  40. $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.ordering', 'filter_order', 'id', 'cmd' );
  41. $this->setState ( 'list.ordering', $value );
  42. $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.direction', 'filter_order_Dir', 'asc', 'word' );
  43. if ($value != 'asc')
  44. $value = 'desc';
  45. $this->setState ( 'list.direction', $value );
  46. $filter_active = '';
  47. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.search', 'filter_search', '', 'string' );
  48. $this->setState ( 'list.search', $value );
  49. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_title', 'filter_title', '', 'string' );
  50. $this->setState ( 'filter.title', $value );
  51. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_topic', 'filter_topic', '', 'string' );
  52. $this->setState ( 'filter.topic', $value );
  53. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_category', 'filter_category', '', 'string' );
  54. $this->setState ( 'filter.category', $value );
  55. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_ip', 'filter_ip', '', 'string' );
  56. $this->setState ( 'filter.ip', $value );
  57. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_author', 'filter_author', '', 'string' );
  58. $this->setState ( 'filter.author', $value );
  59. $filter_active .= $value = $this->getUserStateFromRequest ( 'com_kunena.admin.trash.list.filter_date', 'filter_time', '', 'string' );
  60. $this->setState ( 'filter.time', $value );
  61. $this->setState ( 'filter.active',!empty($filter_active));
  62. }
  63. /**
  64. * Method to get all deleted messages or topics in function of user selection.
  65. *
  66. * @return Array
  67. * @since 1.6
  68. */
  69. public function getTrashItems() {
  70. if ( $this->state->get( 'layout') == 'topics') {
  71. // Get topics
  72. return $this->_getTopics();
  73. }
  74. // Get messages
  75. return $this->_getMessages();
  76. }
  77. /**
  78. * Method to get all deleted messages.
  79. *
  80. * @return Array
  81. * @since 1.6
  82. */
  83. protected function _getMessages() {
  84. $db = JFactory::getDBO();
  85. $join = array();
  86. $query = $db->getQuery(true)->select('a.id')->from('#__kunena_messages AS a');
  87. $query->where('a.hold>=2');
  88. $filter = $this->getState('filter.title');
  89. if (!empty($filter)) {
  90. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  91. $query->where('(a.subject LIKE '.$like.')');
  92. }
  93. $filter = $this->getState('filter.topic');
  94. if (!empty($filter)) {
  95. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  96. $query->where('(tt.subject LIKE '.$like.')');
  97. $join['tt'] = true;
  98. }
  99. $filter = $this->getState('filter.category');
  100. if (!empty($filter)) {
  101. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  102. $query->where('(c.name LIKE '.$like.')');
  103. $join['c'] = true;
  104. }
  105. $filter = $this->getState('filter.ip');
  106. if (!empty($filter)) {
  107. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  108. $query->where('(a.ip LIKE '.$like.')');
  109. }
  110. $filter = $this->getState('filter.author');
  111. if (!empty($filter)) {
  112. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  113. $query->where('(a.name LIKE '.$like.')');
  114. }
  115. $filter = $this->getState('filter.time');
  116. if (!empty($filter)) {
  117. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  118. $query->where('(a.time LIKE '.$like.')');
  119. }
  120. $search = $this->getState('list.search');
  121. if (!empty($search)) {
  122. $like = $db->Quote('%'.$db->escape($search, true).'%');
  123. $query->where('( a.subject LIKE '.$like.' OR a.name LIKE '.$like.' OR a.id LIKE '.$like.' )');
  124. }
  125. // Add the list ordering clause.
  126. $direction = strtoupper($this->getState('list.direction'));
  127. switch ($this->getState('list.ordering')) {
  128. case 'title':
  129. $query->order('a.subject ' . $direction);
  130. break;
  131. case 'topic':
  132. $query->order('tt.subject ' . $direction);
  133. $join['tt'] = true;
  134. break;
  135. case 'category':
  136. $query->order('c.name ' . $direction);
  137. $join['c'] = true;
  138. break;
  139. case 'ip':
  140. $query->order('a.ip ' . $direction);
  141. break;
  142. case 'author':
  143. $query->order('a.name ' . $direction);
  144. break;
  145. case 'time':
  146. $query->order('a.time ' . $direction);
  147. break;
  148. default:
  149. $query->order('a.id ' . $direction);
  150. $this->setState('list.ordering', 'id');
  151. }
  152. if (isset($join['tt'])) $query->innerJoin('#__kunena_topics AS tt ON tt.id=a.thread');
  153. if (isset($join['c'])) $query->innerJoin('#__kunena_categories AS c ON c.id=a.catid');
  154. // TODO: add authorization.
  155. $cquery = clone $query;
  156. $cquery->clear('select')->clear('order')->select('COUNT(*)');
  157. $db->setQuery($cquery);
  158. $total = (int) $db->loadResult();
  159. $this->setState('list.total', $total);
  160. if (KunenaError::checkDatabaseError() || !$total) return array();
  161. // If out of range, use last page
  162. if ($this->getState('list.limit') && $total < $this->getState('list.start'))
  163. $this->setState('list.start', intval($total / $this->getState('list.limit')) * $this->getState('list.limit'));
  164. $db->setQuery($query, $this->getState('list.start'), $this->getState('list.limit'));
  165. $ids = $db->loadColumn();
  166. return KunenaForumMessageHelper::getMessages($ids, 'none');
  167. }
  168. /**
  169. * Method to get all deleted topics.
  170. *
  171. * @return Array
  172. * @since 1.6
  173. */
  174. protected function _getTopics() {
  175. $db = JFactory::getDBO();
  176. $join = array();
  177. $query = $db->getQuery(true)->select('a.id')->from('#__kunena_topics AS a');
  178. $query->where('a.hold>=2');
  179. $filter = $this->getState('filter.title');
  180. if (!empty($filter)) {
  181. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  182. $query->where('(a.subject LIKE '.$like.')');
  183. }
  184. $filter = $this->getState('filter.category');
  185. if (!empty($filter)) {
  186. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  187. $query->where('(c.name LIKE '.$like.')');
  188. $join['c'] = true;
  189. }
  190. $filter = $this->getState('filter.author');
  191. if (!empty($filter)) {
  192. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  193. $query->where('(m.name LIKE '.$like.')');
  194. $join['m'] = true;
  195. }
  196. $filter = $this->getState('filter.time');
  197. if (!empty($filter)) {
  198. $like = $db->Quote('%'.$db->escape($filter, true).'%');
  199. $query->where('(a.first_post_time LIKE '.$like.')');
  200. }
  201. $search = $this->getState('list.search');
  202. if (!empty($search)) {
  203. $like = $db->Quote('%'.$db->escape($search, true).'%');
  204. $query->where('( a.subject LIKE '.$like.' OR m.name LIKE '.$like.' OR a.id LIKE '.$like.' )');
  205. $join['m'] = true;
  206. }
  207. // Add the list ordering clause.
  208. $direction = strtoupper($this->getState('list.direction'));
  209. switch ($this->getState('list.ordering')) {
  210. case 'title':
  211. $query->order('a.subject ' . $direction);
  212. break;
  213. case 'category':
  214. $query->order('c.name ' . $direction);
  215. $join['c'] = true;
  216. break;
  217. case 'author':
  218. $query->order('m.name ' . $direction);
  219. $join['m'] = true;
  220. break;
  221. case 'time':
  222. $query->order('a.first_post_time ' . $direction);
  223. break;
  224. default:
  225. $query->order('a.id ' . $direction);
  226. $this->setState('list.ordering', 'id');
  227. }
  228. if (isset($join['c'])) $query->innerJoin('#__kunena_categories AS c ON c.id=a.category_id');
  229. if (isset($join['m'])) $query->innerJoin('#__kunena_messages AS m ON m.id=a.first_post_id');
  230. // TODO: add authorization.
  231. $cquery = clone $query;
  232. $cquery->clear('select')->clear('order')->select('COUNT(*)');
  233. $db->setQuery($cquery);
  234. $total = (int) $db->loadResult();
  235. $this->setState('list.total', $total);
  236. if (KunenaError::checkDatabaseError() || !$total) return array();
  237. // If out of range, use last page
  238. if ($this->getState('list.limit') && $total < $this->getState('list.start'))
  239. $this->setState('list.start', intval($total / $this->getState('list.limit')) * $this->getState('list.limit'));
  240. $db->setQuery($query, $this->getState('list.start'), $this->getState('list.limit'));
  241. $ids = $db->loadColumn();
  242. return KunenaForumTopicHelper::getTopics($ids, 'none');
  243. }
  244. /**
  245. * Method to get select options to choose between topics and messages.
  246. *
  247. * @return Array
  248. * @since 1.6
  249. */
  250. public function getViewOptions() {
  251. $view_options = array();
  252. $view_options[] = JHtml::_ ( 'select.option', 'topics',JText::_( 'COM_KUNENA_TRASH_TOPICS' ));
  253. $view_options[] = JHtml::_ ( 'select.option', 'messages',JText::_( 'COM_KUNENA_TRASH_MESSAGES'));
  254. return JHtml::_ ( 'select.genericlist', $view_options, 'layout', 'class="inputbox" size="1" onchange="this.form.submit()"', 'value', 'text', $this->getState('layout') );
  255. }
  256. /**
  257. * Method to get details on selected items.
  258. *
  259. * @return Array
  260. * @since 1.6
  261. */
  262. public function getPurgeItems() {
  263. $ids = (array) $this->app->getUserState('com_kunena.purge');
  264. $type = (string) $this->app->getUserState('com_kunena.type');
  265. $items = array();
  266. if ( $type=='topics' ) {
  267. $items = KunenaForumTopicHelper::getTopics($ids, 'none');
  268. } elseif ( $type=='messages' ) {
  269. $items = KunenaForumMessageHelper::getMessages($ids, 'none');
  270. }
  271. return $items;
  272. }
  273. /**
  274. * Method to hash datas.
  275. *
  276. * @return string Hashed value.
  277. * @since 1.6
  278. */
  279. public function getMd5() {
  280. $ids = (array) $this->app->getUserState ( 'com_kunena.purge' );
  281. return md5(serialize($ids));
  282. }
  283. public function getNavigation() {
  284. jimport ( 'joomla.html.pagination' );
  285. $navigation = new JPagination ($this->getState ( 'list.total'), $this->getState ( 'list.start'), $this->getState ( 'list.limit') );
  286. return $navigation;
  287. }
  288. }