PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/apps/user/controllers/admin/CategoryController.php

https://github.com/Doap/xoops
PHP | 238 lines | 198 code | 20 blank | 20 comment | 11 complexity | f50fbf10c5429bfe937a367f310597f8 MD5 | raw file
  1. <?php
  2. /**
  3. * User profile meta category admin controller
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright Xoops Engine http://www.xoopsengine.org
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @category Xoops_Module
  17. * @package User
  18. * @version $Id$
  19. */
  20. class User_CategoryController extends Xoops_Zend_Controller_Action
  21. {
  22. public function indexAction()
  23. {
  24. $form = $this->getForm();
  25. $this->renderForm($form);
  26. }
  27. public function saveAction()
  28. {
  29. $module = $this->getRequest()->getModuleName();
  30. if (!$this->getRequest()->isPost()) {
  31. return $this->_helper->redirector('index');
  32. }
  33. $params = $this->getRequest()->getPost();
  34. $form = $this->getForm();
  35. if (!$form->isValid($params)) {
  36. return $this->renderForm($form);
  37. }
  38. $categoryList = $form->getValues();
  39. $modelCategory = Xoops::service('module')->getModel("category", "user");
  40. $rowset = $modelCategory->fetchAll(array());
  41. $message = array();
  42. $existList = array();
  43. foreach ($rowset as $row) {
  44. $key = $row->key;
  45. $existList[$key] = 1;
  46. if (!empty($categoryList[$key]["delete"])) {
  47. if (!$status = $row->delete()) {
  48. $message[] = XOOPS::_("Category '{$row->key}' was not deleted.");
  49. } else {
  50. $form->removeSubForm($key);
  51. unset($existList[$key]);
  52. }
  53. } elseif (!$row->setFromArray($categoryList[$key])->save()) {
  54. $message[] = XOOPS::_("Category '{$key}' was not saved.");
  55. }
  56. }
  57. XOOPS::service("registry")->handler("meta", $module)->flush();
  58. $addList = array();
  59. if (empty($message) && !empty($categoryList["add"])) {
  60. $list = explode("\n", $categoryList["add"]);
  61. foreach ($list as $item) {
  62. list($key, $order, $title) = explode(" ", $item, 3);
  63. if (isset($existList[$key])) {
  64. $message[] = XOOPS::_("Category key '{$key}' already taken.");
  65. continue;
  66. }
  67. $addList[$key] = array(
  68. "key" => $key,
  69. "title" => $title,
  70. "order" => $order,
  71. );
  72. }
  73. }
  74. if (!empty($message)) {
  75. $form->addErrors($message);
  76. $form->addDecorators(array(
  77. 'Errors',
  78. ));
  79. return $this->renderForm($form);
  80. }
  81. $message = XOOPS::_("Profile meta data saved.");
  82. $failedList = array();
  83. foreach ($addList as $key => $data) {
  84. $status = $modelCategory->insert($data);
  85. if (!$status) {
  86. $failedList[] = $key;
  87. }
  88. }
  89. if (!empty($failedList)) {
  90. $message = XOOPS::_("Profile meta data saved. But following meta were not added: ") . implode(" ", $failedList);
  91. }
  92. $urlOptions = array("action" => "index", "controller" => "category", "route" => "admin");
  93. $options = array("time" => 3, "message" => $message);
  94. $this->redirect($urlOptions, $options);
  95. }
  96. protected function renderForm($form)
  97. {
  98. $module = $this->getRequest()->getModuleName();
  99. $this->setTemplate("profile_category.html");
  100. $form->assign($this->view);
  101. $title = XOOPS::_("Profile meta category edit");
  102. $this->template->assign("title", $title);
  103. // Category list
  104. $modelCategory = Xoops::service('module')->getModel("category", "user");
  105. $select = $modelCategory->select()->order(array("order ASC", "id ASC"));
  106. $rowset = $modelCategory->fetchAll($select);
  107. $links_category = array();
  108. foreach ($rowset as $row) {
  109. $links_category[] = array(
  110. "link" => $this->view->url(array(
  111. "action" => "sort",
  112. "controller" => "category",
  113. "module" => $module,
  114. "category" => $row->key,
  115. ),
  116. "admin"
  117. ),
  118. "title" => $row->title,
  119. );
  120. }
  121. $links_category[] = array(
  122. "link" => $this->view->url(array(
  123. "action" => "sort",
  124. "controller" => "category",
  125. "module" => $module,
  126. "category" => "-",
  127. ),
  128. "admin"
  129. ),
  130. "title" => XOOPS::_("Hidden category"),
  131. );
  132. $this->template->assign("links_category", $links_category);
  133. }
  134. public function getForm()
  135. {
  136. $module = $this->getRequest()->getModuleName();
  137. // Category list
  138. $modelCategory = Xoops::service('module')->getModel("category", "user");
  139. $select = $modelCategory->select()->order(array("order ASC", "id ASC"));
  140. $rowset = $modelCategory->fetchAll($select);
  141. $this->view->headLink(array(
  142. "href" => "form.css",
  143. "rel" => "stylesheet",
  144. "type" => "text/css"
  145. ));
  146. $action = $this->view->url(array(
  147. "action" => "save",
  148. "controller" => "category",
  149. "module" => $module
  150. ),
  151. "admin"
  152. );
  153. $options = array(
  154. "name" => "xoopsProfile",
  155. "action" => $action,
  156. "method" => "post",
  157. );
  158. $form = new Xoops_Zend_Form($options);
  159. $i = 0;
  160. foreach ($rowset as $row) {
  161. $options = array(
  162. "legend" => $row->title . " (" . $row->key . ")",
  163. );
  164. $subform = new Xoops_Zend_Form_SubForm($options);
  165. $options = array(
  166. "label" => "Title",
  167. "value" => $row->title,
  168. "required" => true,
  169. "filters" => array(
  170. "trim" => array(
  171. "filter" => "StringTrim",
  172. ),
  173. ),
  174. );
  175. $subform->addElement("text", "title", $options);
  176. $options = array(
  177. "label" => "Order",
  178. "value" => ($i++) * 10,
  179. "required" => true,
  180. "filters" => array(
  181. "trim" => array(
  182. "filter" => "StringTrim",
  183. ),
  184. ),
  185. );
  186. $subform->addElement("text", "order", $options);
  187. $options = array(
  188. "label" => "Delete",
  189. "multioptions" => array(
  190. "1" => "",
  191. ),
  192. );
  193. $subform->addElement("MultiCheckbox", "delete", $options);
  194. $form->addSubform($subform, $row->key);
  195. }
  196. $options = array(
  197. "label" => "Add categories",
  198. "description" => "One category per line (delimited by space): key order title",
  199. "cols" => 50,
  200. "rows" => 5,
  201. );
  202. $form->addElement("Textarea", "add", $options);
  203. $options = array(
  204. "label" => "Save",
  205. "required" => false,
  206. "ignore" => true,
  207. );
  208. $form->addElement("submit", "save", $options);
  209. $form->setDescription("Edit profile meta categories");
  210. $form->setDecorators(array(
  211. 'FormElements',
  212. array('HtmlTag', array('tag' => 'dl', 'class' => 'xoops-form')),
  213. array('Description', array('placement' => 'prepend')),
  214. 'Form'
  215. ));
  216. return $form;
  217. }
  218. }