PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_kunena/models/categories.php

https://gitlab.com/bilovus-andriy/voda_site
PHP | 348 lines | 242 code | 58 blank | 48 comment | 34 complexity | da829eb845cd59f91ef75eb20a5f3d94 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. jimport( 'joomla.html.pagination' );
  14. /**
  15. * Categories Model for Kunena
  16. *
  17. * @since 2.0
  18. */
  19. class KunenaAdminModelCategories extends KunenaModel {
  20. public $context;
  21. /**
  22. * @var KunenaForumCategory[]
  23. */
  24. protected $_admincategories = false;
  25. /**
  26. * @var KunenaForumCategory
  27. */
  28. protected $_admincategory = false;
  29. /**
  30. * Method to auto-populate the model state.
  31. */
  32. protected function populateState() {
  33. $this->context = 'com_kunena.admin.categories';
  34. $app = JFactory::getApplication();
  35. // Adjust the context to support modal layouts.
  36. $layout = $app->input->get('layout');
  37. if ($layout) {
  38. $this->context .= '.'.$layout;
  39. }
  40. // List state information.
  41. $value = $this->getUserStateFromRequest ( $this->context.'.list.start', 'limitstart', 0, 'int' );
  42. $this->setState ( 'list.start', $value );
  43. $value = $this->getUserStateFromRequest ( $this->context.'.list.limit', 'limit', $this->app->getCfg ( 'list_limit' ), 'int' );
  44. $this->setState ( 'list.limit', $value );
  45. $value = $this->getUserStateFromRequest ( $this->context.'.list.ordering', 'filter_order', 'ordering', 'cmd' );
  46. $this->setState ( 'list.ordering', $value );
  47. $value = $this->getUserStateFromRequest ( $this->context.'.list.direction', 'filter_order_Dir', 'asc', 'word' );
  48. if ($value != 'asc')
  49. $value = 'desc';
  50. $this->setState ( 'list.direction', $value );
  51. $filter_active = '';
  52. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.search', 'filter_search', '', 'string' );
  53. $this->setState ( 'filter.search', $value );
  54. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.published', 'filter_published', '', 'string' );
  55. $this->setState ( 'filter.published', $value !== '' ? (int) $value : null );
  56. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.title', 'filter_title', '', 'string' );
  57. $this->setState ( 'filter.title', $value !== '' ? $value : null );
  58. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.type', 'filter_type', '', 'string' );
  59. $this->setState ( 'filter.type', $value !== '' ? $value : null );
  60. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.access', 'filter_access', '', 'string' );
  61. $this->setState ( 'filter.access', $value !== '' ? (int) $value : null );
  62. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.locked', 'filter_locked', '', 'string' );
  63. $this->setState ( 'filter.locked', $value !== '' ? (int) $value : null );
  64. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.allow_polls', 'filter_allow_polls', '', 'string' );
  65. $this->setState ( 'filter.allow_polls', $value !== '' ? (int) $value : null );
  66. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.review', 'filter_review', '', 'string' );
  67. $this->setState ( 'filter.review', $value !== '' ? (int) $value : null );
  68. $filter_active .= $value = $this->getUserStateFromRequest ( $this->context.'.filter.anonymous', 'filter_anonymous', '', 'string' );
  69. $this->setState ( 'filter.anonymous', $value !== '' ? (int) $value : null );
  70. $this->setState ( 'filter.active',!empty($filter_active));
  71. // TODO: implement
  72. $value = $this->getUserStateFromRequest ( $this->context.".filter.levels", 'levellimit', 10, 'int' );
  73. $this->setState ( 'filter.levels', $value );
  74. $catid = $this->getInt ( 'catid', 0 );
  75. $layout = $this->getWord ( 'layout', 'edit' );
  76. $parent_id = 0;
  77. if ($layout == 'create') {
  78. $parent_id = $catid;
  79. $catid = 0;
  80. }
  81. $this->setState ( 'item.id', $catid );
  82. $this->setState ( 'item.parent_id', $parent_id );
  83. }
  84. public function getAdminCategories() {
  85. if ( $this->_admincategories === false ) {
  86. $params = array (
  87. 'ordering'=>$this->getState ( 'list.ordering' ),
  88. 'direction'=>$this->getState ( 'list.direction' ) == 'asc' ? 1 : -1,
  89. 'search'=>$this->getState ( 'filter.search' ),
  90. 'unpublished'=>1,
  91. 'published'=>$this->getState ( 'filter.published'),
  92. 'filter_title'=>$this->getState ( 'filter.title'),
  93. 'filter_type'=>$this->getState ( 'filter.type'),
  94. 'filter_access'=>$this->getState ( 'filter.access'),
  95. 'filter_locked'=>$this->getState ( 'filter.locked'),
  96. 'filter_allow_polls'=>$this->getState ( 'filter.allow_polls'),
  97. 'filter_review'=>$this->getState ( 'filter.review'),
  98. 'filter_anonymous'=>$this->getState ( 'filter.anonymous'),
  99. 'action'=>'admin');
  100. $catid = $this->getState ( 'item.id', 0 );
  101. $categories = array();
  102. $orphans = array();
  103. if ($catid) {
  104. $categories = KunenaForumCategoryHelper::getParents($catid, $this->getState ( 'filter.levels' ), array('unpublished'=>1, 'action'=>'none'));
  105. $categories[] = KunenaForumCategoryHelper::get($catid);
  106. } else {
  107. $orphans = KunenaForumCategoryHelper::getOrphaned($this->getState ( 'filter.levels' ), $params);
  108. }
  109. $categories = array_merge($categories, KunenaForumCategoryHelper::getChildren($catid, $this->getState ( 'filter.levels' ), $params));
  110. $categories = array_merge($orphans, $categories);
  111. $categories = KunenaForumCategoryHelper::getIndentation($categories);
  112. $this->setState ( 'list.total', count($categories) );
  113. if ($this->getState ( 'list.limit' )) $this->_admincategories = array_slice ( $categories, $this->getState ( 'list.start' ), $this->getState ( 'list.limit' ) );
  114. else $this->_admincategories = $categories;
  115. $admin = 0;
  116. $acl = KunenaAccess::getInstance();
  117. /** @var KunenaForumCategory $category */
  118. foreach ($this->_admincategories as $category) {
  119. // TODO: Following is needed for J!2.5 only:
  120. $parent = $category->getParent();
  121. $siblings = array_keys(KunenaForumCategoryHelper::getCategoryTree($category->parent_id));
  122. $category->up = $this->me->isAdmin($parent) && reset($siblings) != $category->id;
  123. $category->down = $this->me->isAdmin($parent) && end($siblings) != $category->id;
  124. $category->reorder = $this->me->isAdmin($parent);
  125. // Get ACL groups for the category.
  126. $access = $acl->getCategoryAccess($category);
  127. $category->accessname = array();
  128. foreach ($access as $item) {
  129. $category->accessname[] = $item['title'];
  130. }
  131. $category->accessname = implode(' / ', $category->accessname);
  132. // Checkout?
  133. if ($this->me->isAdmin($category) && $category->isCheckedOut(0)) {
  134. $category->editor = KunenaFactory::getUser($category->checked_out)->getName();
  135. } else {
  136. $category->checked_out = 0;
  137. $category->editor = '';
  138. }
  139. $admin += $this->me->isAdmin($category);
  140. }
  141. $this->setState ( 'list.count.admin', $admin );
  142. }
  143. if (!empty($orphans)) {
  144. $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_CATEGORY_ORPHAN_DESC' ), 'notice' );
  145. }
  146. return $this->_admincategories;
  147. }
  148. public function getAdminNavigation() {
  149. $navigation = new JPagination ($this->getState ( 'list.total'), $this->getState ( 'list.start'), $this->getState ( 'list.limit') );
  150. return $navigation;
  151. }
  152. public function getAdminCategory() {
  153. $category = KunenaForumCategoryHelper::get ( $this->getState ( 'item.id' ) );
  154. if (!$this->me->isAdmin($category)) {
  155. return false;
  156. }
  157. if ($this->_admincategory === false) {
  158. if ($category->exists ()) {
  159. if (!$category->isCheckedOut ( $this->me->userid ))
  160. $category->checkout ( $this->me->userid );
  161. } else {
  162. // New category is by default child of the first section -- this will help new users to do it right
  163. $db = JFactory::getDBO ();
  164. $db->setQuery ( "SELECT a.id, a.name FROM #__kunena_categories AS a WHERE parent_id='0' AND id!='$category->id' ORDER BY ordering" );
  165. $sections = $db->loadObjectList ();
  166. KunenaError::checkDatabaseError ();
  167. $category->parent_id = $this->getState ( 'item.parent_id' );
  168. $category->published = 0;
  169. $category->ordering = 9999;
  170. $category->pub_recurse = 1;
  171. $category->admin_recurse = 1;
  172. $category->accesstype = 'joomla.level';
  173. $category->access = 1;
  174. $category->pub_access = 1;
  175. $category->admin_access = 8;
  176. }
  177. $this->_admincategory = $category;
  178. }
  179. return $this->_admincategory;
  180. }
  181. public function getAdminOptions() {
  182. $category = $this->getAdminCategory();
  183. if (!$category) return false;
  184. $catList = array ();
  185. $catList [] = JHtml::_ ( 'select.option', 0, JText::_ ( 'COM_KUNENA_TOPLEVEL' ) );
  186. // make a standard yes/no list
  187. $published = array ();
  188. $published [] = JHtml::_ ( 'select.option', 1, JText::_ ( 'COM_KUNENA_PUBLISHED' ) );
  189. $published [] = JHtml::_ ( 'select.option', 0, JText::_ ( 'COM_KUNENA_UNPUBLISHED' ) );
  190. // make a standard yes/no list
  191. $yesno = array ();
  192. $yesno [] = JHtml::_ ( 'select.option', 0, JText::_ ( 'COM_KUNENA_NO' ) );
  193. $yesno [] = JHtml::_ ( 'select.option', 1, JText::_ ( 'COM_KUNENA_YES' ) );
  194. // Anonymous posts default
  195. $post_anonymous = array ();
  196. $post_anonymous [] = JHtml::_ ( 'select.option', '0', JText::_ ( 'COM_KUNENA_CATEGORY_ANONYMOUS_X_REG' ) );
  197. $post_anonymous [] = JHtml::_ ( 'select.option', '1', JText::_ ( 'COM_KUNENA_CATEGORY_ANONYMOUS_X_ANO' ) );
  198. $cat_params = array ();
  199. $cat_params['ordering'] = 'ordering';
  200. $cat_params['toplevel'] = JText::_('COM_KUNENA_TOPLEVEL');
  201. $cat_params['sections'] = 1;
  202. $cat_params['unpublished'] = 1;
  203. $cat_params['catid'] = $category->id;
  204. $cat_params['action'] = 'admin';
  205. $channels_params = array();
  206. $channels_params['catid'] = $category->id;
  207. $channels_params['action'] = 'admin';
  208. $channels_options = array();
  209. $channels_options [] = JHtml::_ ( 'select.option', 'THIS', JText::_ ( 'COM_KUNENA_CATEGORY_CHANNELS_OPTION_THIS' ) );
  210. $channels_options [] = JHtml::_ ( 'select.option', 'CHILDREN', JText::_ ( 'COM_KUNENA_CATEGORY_CHANNELS_OPTION_CHILDREN' ) );
  211. if (empty($category->channels)) $category->channels = 'THIS';
  212. $topic_ordering_options = array();
  213. $topic_ordering_options[] = JHtml::_ ( 'select.option', 'lastpost', JText::_ ( 'COM_KUNENA_CATEGORY_TOPIC_ORDERING_OPTION_LASTPOST' ) );
  214. $topic_ordering_options[] = JHtml::_ ( 'select.option', 'creation', JText::_ ( 'COM_KUNENA_CATEGORY_TOPIC_ORDERING_OPTION_CREATION' ) );
  215. $topic_ordering_options[] = JHtml::_ ( 'select.option', 'alpha', JText::_ ( 'COM_KUNENA_CATEGORY_TOPIC_ORDERING_OPTION_ALPHA' ) );
  216. $aliases = array_keys($category->getAliases());
  217. $lists = array ();
  218. $lists ['accesstypes'] = KunenaAccess::getInstance()->getAccessTypesList($category);
  219. $lists ['accesslists'] = KunenaAccess::getInstance()->getAccessOptions($category);
  220. $lists ['categories'] = JHtml::_('kunenaforum.categorylist', 'parent_id', 0, null, $cat_params, 'class="inputbox"', 'value', 'text', $category->parent_id);
  221. $lists ['channels'] = JHtml::_('kunenaforum.categorylist', 'channels[]', 0, $channels_options, $channels_params, 'class="inputbox" multiple="multiple"', 'value', 'text', explode(',', $category->channels));
  222. $lists ['aliases'] = $aliases ? JHtml::_ ( 'kunenaforum.checklist', 'aliases', $aliases, true) : null;
  223. $lists ['published'] = JHtml::_ ( 'select.genericlist', $published, 'published', 'class="inputbox"', 'value', 'text', $category->published );
  224. $lists ['forumLocked'] = JHtml::_ ( 'select.genericlist', $yesno, 'locked', 'class="inputbox" size="1"', 'value', 'text', $category->locked );
  225. $lists ['forumReview'] = JHtml::_ ( 'select.genericlist', $yesno, 'review', 'class="inputbox" size="1"', 'value', 'text', $category->review );
  226. $lists ['allow_polls'] = JHtml::_ ( 'select.genericlist', $yesno, 'allow_polls', 'class="inputbox" size="1"', 'value', 'text', $category->allow_polls );
  227. $lists ['allow_anonymous'] = JHtml::_ ( 'select.genericlist', $yesno, 'allow_anonymous', 'class="inputbox" size="1"', 'value', 'text', $category->allow_anonymous );
  228. $lists ['post_anonymous'] = JHtml::_ ( 'select.genericlist', $post_anonymous, 'post_anonymous', 'class="inputbox" size="1"', 'value', 'text', $category->post_anonymous );
  229. $lists ['topic_ordering'] = JHtml::_ ( 'select.genericlist', $topic_ordering_options, 'topic_ordering', 'class="inputbox" size="1"', 'value', 'text', $category->topic_ordering );
  230. // TODO:
  231. /*
  232. $topicicons = array ();
  233. jimport( 'joomla.filesystem.folder' );
  234. $topiciconslist = JFolder::folders(JPATH_ROOT.'/media/kunena/topicicons');
  235. foreach( $topiciconslist as $icon ) {
  236. $topicicons[] = JHtml::_ ( 'select.option', $icon, $icon );
  237. }
  238. $lists ['category_iconset'] = JHtml::_ ( 'select.genericlist', $topicicons, 'iconset', 'class="inputbox" size="1"', 'value', 'text', $category->iconset );
  239. */
  240. return $lists;
  241. }
  242. function getAdminModerators() {
  243. $category = $this->getAdminCategory();
  244. if (!$category) return false;
  245. $moderators = $category->getModerators(false);
  246. return $moderators;
  247. }
  248. protected function getReorderConditions($table) {
  249. $condition = array();
  250. $condition[] = 'parent_id = '.(int) $table->parent_id;
  251. return $condition;
  252. }
  253. public function saveorder($pks = null, $order = null) {
  254. $table = JTable::getInstance('KunenaCategories', 'Table');
  255. $conditions = array();
  256. if (empty($pks)) return false;
  257. // Update ordering values
  258. foreach ($pks as $i => $pk) {
  259. $table->load((int) $pk);
  260. if ($table->ordering != $order[$i]) {
  261. $table->ordering = $order[$i];
  262. if (!$table->store()) {
  263. $this->setError($table->getError());
  264. return false;
  265. }
  266. // Remember to reorder within position and client_id
  267. $condition = $this->getReorderConditions($table);
  268. $found = false;
  269. foreach ($conditions as $cond) {
  270. if ($cond[1] == $condition) {
  271. $found = true;
  272. break;
  273. }
  274. }
  275. if (!$found) {
  276. $key = $table->getKeyName();
  277. $conditions[] = array($table->$key, $condition);
  278. }
  279. }
  280. }
  281. // Execute reorder for each category.
  282. foreach ($conditions as $cond) {
  283. $table->load($cond[0]);
  284. $table->reorder($cond[1]);
  285. }
  286. // Clear the component's cache
  287. $this->cleanCache();
  288. return true;
  289. }
  290. }