PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/system/Categories/lib/Categories/Controller/Ajax.php

https://github.com/ThiloWitt/core
PHP | 341 lines | 266 code | 50 blank | 25 comment | 25 complexity | 13f92c975bcb3fd26b412c115c4c738d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. /**
  15. * Categories_Controller_Ajax.
  16. */
  17. class Categories_Controller_Ajax extends Zikula_Controller_AbstractAjax
  18. {
  19. /**
  20. * Resequence categories
  21. */
  22. public function resequence()
  23. {
  24. $this->checkAjaxToken();
  25. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT));
  26. $data = json_decode($this->request->getPost()->get('data'), true);
  27. $cats = CategoryUtil::getSubCategories(1, true, true, true, true, true, '', 'id');
  28. foreach ($cats as $k => $cat) {
  29. $cid = $cat['id'];
  30. if (isset($data[$cid])) {
  31. $cats[$k]['sort_value'] = $data[$cid]['lineno'];
  32. $cats[$k]['parent_id'] = $data[$cid]['parent'];
  33. $obj = new Categories_DBObject_Category($cats[$k]);
  34. $obj->update();
  35. }
  36. }
  37. $result = array(
  38. 'response' => true
  39. );
  40. return new Zikula_Response_Ajax($result);
  41. }
  42. public function edit($args = array())
  43. {
  44. $this->checkAjaxToken();
  45. $mode = $this->request->getPost()->get('mode', 'new');
  46. $accessLevel = $mode == 'edit' ? ACCESS_EDIT : ACCESS_ADD;
  47. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', $accessLevel));
  48. $cid = isset($args['cid']) ? $args['cid'] : $this->request->getPost()->get('cid', 0);
  49. $parent = isset($args['parent']) ? $args['parent'] : $this->request->getPost()->get('parent', 1);
  50. $validationErrors = FormUtil::getValidationErrors();
  51. $editCat = '';
  52. $languages = ZLanguage::getInstalledLanguages();
  53. if ($validationErrors) {
  54. $category = new Categories_DBObject_Category(DBObject::GET_FROM_VALIDATION_FAILED); // need this for validation info
  55. $editCat = $category->get();
  56. $validationErrors = $validationErrors['category'];
  57. } else {
  58. // indicates that we're editing
  59. if ($mode == 'edit') {
  60. if (!$cid) {
  61. return new Zikula_Response_Ajax_BadData($this->__('Error! Cannot determine valid \'cid\' for edit mode in \'Categories_admin_edit\'.'));
  62. }
  63. $category = new Categories_DBObject_Category();
  64. $editCat = $category->select($cid);
  65. $this->throwNotFoundUnless($editCat, $this->__('Sorry! No such item found.'));
  66. } else {
  67. // someone just pressen 'new' -> populate defaults
  68. $category = new Categories_DBObject_Category(); // need this for validation info
  69. $editCat['sort_value'] = '0';
  70. $editCat['parent_id'] = $parent;
  71. }
  72. }
  73. $attributes = isset($editCat['__ATTRIBUTES__']) ? $editCat['__ATTRIBUTES__'] : array();
  74. Zikula_AbstractController::configureView();
  75. $this->view->setCaching(Zikula_View::CACHE_DISABLED);
  76. $this->view->assign('mode', $mode)
  77. ->assign('category', $editCat)
  78. ->assign('attributes', $attributes)
  79. ->assign('languages', $languages)
  80. ->assign('validation', $category->_objValidation);
  81. $result = array(
  82. 'action' => $mode == 'new' ? 'add' : 'edit',
  83. 'result' => $this->view->fetch('categories_adminajax_edit.tpl'),
  84. 'validationErrors' => $validationErrors
  85. );
  86. if ($validationErrors) {
  87. return new Zikula_Response_Ajax_BadData($validationErrors, $result);
  88. }
  89. return new Zikula_Response_Ajax($result);
  90. }
  91. public function copy()
  92. {
  93. $this->checkAjaxToken();
  94. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_ADD));
  95. $cid = $this->request->getPost()->get('cid');
  96. $parent = $this->request->getPost()->get('parent');
  97. $cat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cid);
  98. $cat->copy($parent);
  99. $copyParent = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cat->getDataField('parent_id'));
  100. $categories = CategoryUtil::getSubCategories($copyParent->getDataField('id'), true, true, true, true, true);
  101. $options = array(
  102. 'nullParent' => $copyParent->getDataField('parent_id'),
  103. 'withWraper' => false,
  104. );
  105. $node = CategoryUtil::getCategoryTreeJS((array)$categories, true, true, $options);
  106. $leafStatus = array(
  107. 'leaf' => array(),
  108. 'noleaf' => array()
  109. );
  110. foreach ($categories as $c) {
  111. if ($c['is_leaf']) {
  112. $leafStatus['leaf'][] = $c['id'];
  113. } else {
  114. $leafStatus['noleaf'][] = $c['id'];
  115. }
  116. }
  117. $result = array(
  118. 'action' => 'copy',
  119. 'cid' => $cid,
  120. 'copycid' => $copyParent->getDataField('id'),
  121. 'parent' => $copyParent->getDataField('parent_id'),
  122. 'node' => $node,
  123. 'leafstatus' => $leafStatus,
  124. 'result' => true
  125. );
  126. return new Zikula_Response_Ajax($result);
  127. }
  128. public function delete()
  129. {
  130. $this->checkAjaxToken();
  131. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_DELETE));
  132. $cid = $this->request->getPost()->get('cid');
  133. $cat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cid);
  134. $cat->delete(true);
  135. $result = array(
  136. 'action' => 'delete',
  137. 'cid' => $cid,
  138. 'result' => true
  139. );
  140. return new Zikula_Response_Ajax($result);
  141. }
  142. public function deleteandmovesubs()
  143. {
  144. $this->checkAjaxToken();
  145. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_DELETE));
  146. $cid = $this->request->getPost()->get('cid');
  147. $parent = $this->request->getPost()->get('parent');
  148. $cat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cid);
  149. $cat->deleteMoveSubcategories($parent);
  150. // need to re-render new parents node
  151. $newParent = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $parent);
  152. $categories = CategoryUtil::getSubCategories($newParent->getDataField('id'), true, true, true, true, true);
  153. $options = array(
  154. 'nullParent' => $newParent->getDataField('parent_id'),
  155. 'withWraper' => false,
  156. );
  157. $node = CategoryUtil::getCategoryTreeJS((array)$categories, true, true, $options);
  158. $leafStatus = array(
  159. 'leaf' => array(),
  160. 'noleaf' => array()
  161. );
  162. foreach ($categories as $c) {
  163. if ($c['is_leaf']) {
  164. $leafStatus['leaf'][] = $c['id'];
  165. } else {
  166. $leafStatus['noleaf'][] = $c['id'];
  167. }
  168. }
  169. $result = array(
  170. 'action' => 'deleteandmovesubs',
  171. 'cid' => $cid,
  172. 'parent' => $newParent->getDataField('id'),
  173. 'node' => $node,
  174. 'leafstatus' => $leafStatus,
  175. 'result' => true
  176. );
  177. return new Zikula_Response_Ajax($result);
  178. }
  179. public function deletedialog()
  180. {
  181. $this->checkAjaxToken();
  182. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_DELETE));
  183. $cid = $this->request->getPost()->get('cid');
  184. $allCats = CategoryUtil::getSubCategories(1, true, true, true, false, true, $cid);
  185. $selector = CategoryUtil::getSelector_Categories($allCats);
  186. Zikula_AbstractController::configureView();
  187. $this->view->setCaching(Zikula_View::CACHE_DISABLED);
  188. $this->view->assign('categorySelector', $selector);
  189. $result = array(
  190. 'result' => $this->view->fetch('categories_adminajax_delete.tpl'),
  191. );
  192. return new Zikula_Response_Ajax($result);
  193. }
  194. public function activate()
  195. {
  196. $this->checkAjaxToken();
  197. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT));
  198. $cid = $this->request->getPost()->get('cid');
  199. $cat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cid);
  200. $cat->setDataField('status', 'A');
  201. $cat->update();
  202. $result = array(
  203. 'action' => 'activate',
  204. 'cid' => $cid,
  205. 'result' => true
  206. );
  207. return new Zikula_Response_Ajax($result);
  208. }
  209. public function deactivate()
  210. {
  211. $this->checkAjaxToken();
  212. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT));
  213. $cid = $this->request->getPost()->get('cid');
  214. $cat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $cid);
  215. $cat->setDataField('status', 'I');
  216. $cat->update();
  217. $result = array(
  218. 'action' => 'deactivate',
  219. 'cid' => $cid,
  220. 'result' => true
  221. );
  222. return new Zikula_Response_Ajax($result);
  223. }
  224. public function save()
  225. {
  226. $this->checkAjaxToken();
  227. $mode = $this->request->getPost()->get('mode', 'new');
  228. $accessLevel = $mode == 'edit' ? ACCESS_EDIT : ACCESS_ADD;
  229. $this->throwForbiddenUnless(SecurityUtil::checkPermission('Categories::', '::', $accessLevel));
  230. $result = array();
  231. $cat = new Categories_DBObject_Category();
  232. $cat->getDataFromInput();
  233. if (!$cat->validate()) {
  234. $args = array(
  235. 'cid' => $cat->getDataField('id'),
  236. 'parent' => $cat->getDataField('parent_id'),
  237. 'mode' => $mode
  238. );
  239. return $this->edit($args);
  240. }
  241. $attributes = array();
  242. $values = $this->request->getPost()->get('attribute_value');
  243. foreach ($this->request->getPost()->get('attribute_name') as $index => $name) {
  244. if (!empty($name)) {
  245. $attributes[$name] = $values[$index];
  246. }
  247. }
  248. $cat->setDataField('__ATTRIBUTES__', $attributes);
  249. if ($mode == 'edit') {
  250. // retrieve old category from DB
  251. $category = $this->request->getPost()->get('category');
  252. $oldCat = new Categories_DBObject_Category(DBObject::GET_FROM_DB, $category['id']);
  253. // update new category data
  254. $cat->update();
  255. // since a name change will change the object path, we must rebuild it here
  256. if ($oldCat->getDataField('name') != $cat->getDataField('name')) {
  257. CategoryUtil::rebuildPaths('path', 'name', $cat->getDataField('id'));
  258. }
  259. } else {
  260. $cat->insert();
  261. // update new category data
  262. $cat->update();
  263. }
  264. $categories = CategoryUtil::getSubCategories($cat->getDataField('id'), true, true, true, true, true);
  265. $options = array(
  266. 'nullParent' => $cat->getDataField('parent_id'),
  267. 'withWraper' => false,
  268. );
  269. $node = CategoryUtil::getCategoryTreeJS((array)$categories, true, true, $options);
  270. $leafStatus = array(
  271. 'leaf' => array(),
  272. 'noleaf' => array()
  273. );
  274. foreach ($categories as $c) {
  275. if ($c['is_leaf']) {
  276. $leafStatus['leaf'][] = $c['id'];
  277. } else {
  278. $leafStatus['noleaf'][] = $c['id'];
  279. }
  280. }
  281. $result = array(
  282. 'action' => $mode == 'edit' ? 'edit' : 'add',
  283. 'cid' => $cat->getDataField('id'),
  284. 'parent' => $cat->getDataField('parent_id'),
  285. 'node' => $node,
  286. 'leafstatus' => $leafStatus,
  287. 'result' => true
  288. );
  289. return new Zikula_Response_Ajax($result);
  290. }
  291. }