/administrator/components/com_zoo/controllers/submission.php

https://gitlab.com/vnsoftdev/amms · PHP · 380 lines · 209 code · 105 blank · 66 comment · 15 complexity · 7d92d6315c27f890c875053bd0ff2468 MD5 · raw file

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: SubmissionController
  10. The controller class for submission
  11. */
  12. class SubmissionController extends AppController {
  13. public $application;
  14. public function __construct($default = array()) {
  15. parent::__construct($default);
  16. // set table
  17. $this->table = $this->app->table->submission;
  18. // get application
  19. $this->application = $this->app->zoo->getApplication();
  20. // check ACL
  21. if (!$this->application->isAdmin()) {
  22. throw new ConfigurationControllerException("Invalid Access Permissions!", 1);
  23. }
  24. // set base url
  25. $this->baseurl = $this->app->link(array('controller' => $this->controller), false);
  26. // register tasks
  27. $this->registerTask('add', 'edit');
  28. $this->registerTask('apply', 'save');
  29. $this->registerTask('save2new', 'save' );
  30. }
  31. public function display($cachable = false, $urlparams = false) {
  32. jimport('joomla.html.pagination');
  33. // set toolbar items
  34. $this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Items'));
  35. $this->app->toolbar->addNew();
  36. $this->app->toolbar->editList();
  37. $this->app->toolbar->custom('docopy', 'copy.png', 'copy_f2.png', 'Copy');
  38. $this->app->toolbar->deleteList();
  39. $this->app->zoo->toolbarHelp();
  40. $this->app->html->_('behavior.tooltip');
  41. $state_prefix = $this->option.'_'.$this->application->id.'.submission';
  42. $filter_order = $this->app->system->application->getUserStateFromRequest($state_prefix.'filter_order', 'filter_order', 'name', 'cmd');
  43. $filter_order_Dir = $this->app->system->application->getUserStateFromRequest($state_prefix.'filter_order_Dir', 'filter_order_Dir', 'desc', 'word');
  44. // get data from the table
  45. $where = array();
  46. // application filter
  47. $where[] = 'application_id = ' . (int) $this->application->id;
  48. $options = array(
  49. 'conditions' => array(implode(' AND ', $where)),
  50. 'order' => $filter_order.' '.$filter_order_Dir);
  51. $this->submissions = $this->table->all($options);
  52. $this->submissions = array_merge($this->submissions);
  53. // table ordering and search filter
  54. $this->lists['order_Dir'] = $filter_order_Dir;
  55. $this->lists['order'] = $filter_order;
  56. // display view
  57. $this->getView()->display();
  58. }
  59. public function edit() {
  60. // disable menu
  61. $this->app->request->setVar('hidemainmenu', 1);
  62. // get request vars
  63. $cid = $this->app->request->get('cid.0', 'int');
  64. $edit = $cid > 0;
  65. // get item
  66. if ($edit) {
  67. $this->submission = $this->table->get($cid);
  68. } else {
  69. $this->submission = $this->app->object->create('Submission');
  70. $this->submission->application_id = $this->application->id;
  71. $this->submission->access = 1;
  72. }
  73. // set toolbar items
  74. $this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Submission').': '.$this->submission->name.' <small><small>[ '.($edit ? JText::_('Edit') : JText::_('New')).' ]</small></small>');
  75. $this->app->toolbar->apply();
  76. $this->app->toolbar->save();
  77. $this->app->toolbar->save2new();
  78. $this->app->toolbar->cancel('cancel', $edit ? 'Close' : 'Cancel');
  79. $this->app->zoo->toolbarHelp();
  80. // published select
  81. $this->lists['select_published'] = $this->app->html->_('select.booleanlist', 'state', null, $this->submission->state);
  82. // access select
  83. $this->lists['select_access'] = $this->app->html->_('zoo.accesslevel', array(), 'access', 'class="inputbox"', 'value', 'text',$this->submission->access);
  84. // tooltip select
  85. $this->lists['select_tooltip'] = $this->app->html->_('select.booleanlist', 'params[show_tooltip]', null, $this->submission->showTooltip());
  86. // item captcha select
  87. $options = array($this->app->html->_('select.option', '', '- '.JText::_('Select Plugin').' -'));
  88. $this->lists['select_item_captcha'] = $this->app->html->_('zoo.pluginlist', $options, 'params[captcha]', '', 'value', 'text', $this->submission->getParams()->get('captcha', null), null, true, 'captcha');
  89. // type select
  90. $this->types = array();
  91. foreach ($this->application->getTypes() as $type) {
  92. // list types with submission layouts only
  93. if (count($this->app->type->layouts($type, 'submission')) > 0) {
  94. $form = $this->submission->getForm($type->id);
  95. $this->types[$type->id]['name'] = $type->name;
  96. $options = array($this->app->html->_('select.option', '', '- '.JText::_('not submittable').' -'));
  97. $this->types[$type->id]['select_layouts'] = $this->app->html->_('zoo.layoutlist', $type, 'submission', $options, 'params[form]['.$type->id.'][layout]', '', 'value', 'text', $form->get('layout'));
  98. $options = array($this->app->html->_('select.option', '', '- '.JText::_('uncategorized').' -'));
  99. $this->types[$type->id]['select_categories'] = $this->app->html->_('zoo.categorylist', $this->application, $options, 'params[form]['.$type->id.'][category]', 'size="1"', 'value', 'text', $form->get('category'));
  100. }
  101. }
  102. // display view
  103. $this->getView()->setLayout('edit')->display();
  104. }
  105. public function save() {
  106. // check for request forgeries
  107. $this->app->session->checkToken() or jexit('Invalid Token');
  108. // init vars
  109. $post = $this->app->request->get('post:', 'array', array());
  110. $cid = $this->app->request->get('cid.0', 'int');
  111. try {
  112. // get item
  113. if ($cid) {
  114. $submission = $this->table->get($cid);
  115. } else {
  116. $submission = $this->app->object->create('Submission');
  117. $submission->application_id = $this->application->id;
  118. }
  119. // bind submission data
  120. self::bind($submission, $post, array('params'));
  121. // Force alias to be set
  122. if (!strlen(trim($submission->alias))) {
  123. $submission->alias = $this->app->string->sluggify($submission->name);
  124. }
  125. // generate unique slug
  126. $submission->alias = $this->app->alias->submission->getUniqueAlias($submission->id, $this->app->string->sluggify($submission->alias));
  127. // set params
  128. $submission->getParams()
  129. ->set('form.', @$post['params']['form'])
  130. ->set('trusted_mode', @$post['params']['trusted_mode'])
  131. ->set('show_tooltip', @$post['params']['show_tooltip'])
  132. ->set('max_submissions', @$post['params']['max_submissions'])
  133. ->set('captcha', @$post['params']['captcha'])
  134. ->set('captcha_guest_only', @$post['params']['captcha_guest_only'])
  135. ->set('email_notification', @$post['params']['email_notification'])
  136. ->set('config.', @$post['params']['config'])
  137. ->set('content.', @$post['params']['content']);
  138. // save submission
  139. $this->table->save($submission);
  140. // set redirect message
  141. $msg = JText::_('Submission Saved');
  142. } catch (AppException $e) {
  143. // raise notice on exception
  144. $this->app->error->raiseNotice(0, JText::_('Error Saving Submission').' ('.$e.')');
  145. $this->_task = 'apply';
  146. $msg = null;
  147. }
  148. $link = $this->baseurl;
  149. switch ($this->getTask()) {
  150. case 'apply' :
  151. $link .= '&task=edit&cid[]='.$submission->id;
  152. break;
  153. case 'save2new' :
  154. $link .= '&task=add';
  155. break;
  156. }
  157. $this->setRedirect($link, $msg);
  158. }
  159. public function remove() {
  160. // check for request forgeries
  161. $this->app->session->checkToken() or jexit('Invalid Token');
  162. // init vars
  163. $cid = $this->app->request->get('cid', 'array', array());
  164. if (count($cid) < 1) {
  165. $this->app->error->raiseError(500, JText::_('Select a submission to delete'));
  166. }
  167. try {
  168. // delete items
  169. foreach ($cid as $id) {
  170. $this->table->delete($this->table->get($id));
  171. }
  172. // set redirect message
  173. $msg = JText::_('Submission Deleted');
  174. } catch (AppException $e) {
  175. // raise notice on exception
  176. $this->app->error->raiseWarning(0, JText::_('Error Deleting Submission').' ('.$e.')');
  177. $msg = null;
  178. }
  179. $this->setRedirect($this->baseurl, $msg);
  180. }
  181. public function docopy() {
  182. // check for request forgeries
  183. $this->app->session->checkToken() or jexit('Invalid Token');
  184. // init vars
  185. $cid = $this->app->request->get('cid', 'array', array());
  186. if (count($cid) < 1) {
  187. $this->app->error->raiseError(500, JText::_('Select a submission to copy'));
  188. }
  189. try {
  190. // copy submissions
  191. foreach ($cid as $id) {
  192. // get submission
  193. $submission = $this->table->get($id);
  194. // copy submission
  195. $submission->id = 0; // set id to 0, to force new category
  196. $submission->name .= ' ('.JText::_('Copy').')'; // set copied name
  197. $submission->alias = $this->app->alias->submission->getUniqueAlias($id, $submission->alias.'-copy'); // set copied alias
  198. // save copied category data
  199. $this->table->save($submission);
  200. }
  201. // set redirect message
  202. $msg = JText::_('Submission Copied');
  203. } catch (AppException $e) {
  204. // raise notice on exception
  205. $this->app->error->raiseNotice(0, JText::_('Error Copying Category').' ('.$e.')');
  206. $msg = null;
  207. }
  208. $this->setRedirect($this->baseurl, $msg);
  209. }
  210. public function publish() {
  211. $this->_editState(1);
  212. }
  213. public function unpublish() {
  214. $this->_editState(0);
  215. }
  216. protected function _editState($state) {
  217. // check for request forgeries
  218. $this->app->session->checkToken() or jexit('Invalid Token');
  219. // init vars
  220. $cid = $this->app->request->get('cid', 'array', array());
  221. if (count($cid) < 1) {
  222. $this->app->error->raiseError(500, JText::_('Select a submission to edit publish state'));
  223. }
  224. try {
  225. // update item state
  226. foreach ($cid as $id) {
  227. $submission = $this->table->get($id);
  228. $submission->state = $state;
  229. $this->table->save($submission);
  230. }
  231. } catch (AppException $e) {
  232. // raise notice on exception
  233. $this->app->error->raiseNotice(0, JText::_('Error editing Submission Published State').' ('.$e.')');
  234. }
  235. $this->setRedirect($this->baseurl);
  236. }
  237. public function enableTrustedMode() {
  238. $this->_editTrustedMode(1);
  239. }
  240. public function disableTrustedMode() {
  241. $this->_editTrustedMode(0);
  242. }
  243. protected function _editTrustedMode($enabled) {
  244. // check for request forgeries
  245. $this->app->session->checkToken() or jexit('Invalid Token');
  246. // init vars
  247. $cid = $this->app->request->get('cid', 'array', array());
  248. if (count($cid) < 1) {
  249. $this->app->error->raiseError(500, JText::_('Select a submission to enable/disable Trusted Mode'));
  250. }
  251. try {
  252. // update item state
  253. foreach ($cid as $id) {
  254. $submission = $this->table->get($id);
  255. // trusted mode can only be enabled for nonpublic access
  256. if ($enabled == true) {
  257. if (!JAccess::checkGroup($this->app->zoo->getGroup($submission->access)->id, 'core.login.site')) {
  258. throw new AppException('Trusted mode can\'t be enabled for public access');
  259. }
  260. }
  261. $submission->getParams()
  262. ->set('trusted_mode', $enabled);
  263. $this->table->save($submission);
  264. }
  265. } catch (AppException $e) {
  266. // raise notice on exception
  267. $this->app->error->raiseNotice(0, JText::_('Error enabling/disabling Submission Trusted Mode').' ('.$e.')');
  268. }
  269. $this->setRedirect($this->baseurl);
  270. }
  271. }
  272. /*
  273. Class: SubmissionControllerException
  274. */
  275. class SubmissionControllerException extends AppException {}