PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
 10	Class: SubmissionController
 11		The controller class for submission
 12*/
 13class SubmissionController extends AppController {
 14
 15	public $application;
 16
 17	public function __construct($default = array()) {
 18		parent::__construct($default);
 19
 20		// set table
 21		$this->table = $this->app->table->submission;
 22
 23		// get application
 24		$this->application 	= $this->app->zoo->getApplication();
 25
 26		// check ACL
 27		if (!$this->application->isAdmin()) {
 28			throw new ConfigurationControllerException("Invalid Access Permissions!", 1);
 29		}
 30
 31		// set base url
 32		$this->baseurl = $this->app->link(array('controller' => $this->controller), false);
 33
 34		// register tasks
 35        $this->registerTask('add', 'edit');
 36		$this->registerTask('apply', 'save');
 37		$this->registerTask('save2new', 'save' );
 38	}
 39
 40	public function display($cachable = false, $urlparams = false) {
 41
 42		jimport('joomla.html.pagination');
 43
 44		// set toolbar items
 45		$this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Items'));
 46		$this->app->toolbar->addNew();
 47		$this->app->toolbar->editList();
 48		$this->app->toolbar->custom('docopy', 'copy.png', 'copy_f2.png', 'Copy');
 49		$this->app->toolbar->deleteList();
 50		$this->app->zoo->toolbarHelp();
 51
 52		$this->app->html->_('behavior.tooltip');
 53
 54		$state_prefix       = $this->option.'_'.$this->application->id.'.submission';
 55		$filter_order	    = $this->app->system->application->getUserStateFromRequest($state_prefix.'filter_order', 'filter_order', 'name', 'cmd');
 56		$filter_order_Dir   = $this->app->system->application->getUserStateFromRequest($state_prefix.'filter_order_Dir', 'filter_order_Dir', 'desc', 'word');
 57
 58        // get data from the table
 59		$where = array();
 60
 61		// application filter
 62		$where[] = 'application_id = ' . (int) $this->application->id;
 63
 64		$options = array(
 65			'conditions' => array(implode(' AND ', $where)),
 66			'order' => $filter_order.' '.$filter_order_Dir);
 67
 68		$this->submissions = $this->table->all($options);
 69        $this->submissions = array_merge($this->submissions);
 70
 71		// table ordering and search filter
 72		$this->lists['order_Dir'] = $filter_order_Dir;
 73		$this->lists['order']	  = $filter_order;
 74
 75		// display view
 76		$this->getView()->display();
 77	}
 78
 79	public function edit() {
 80
 81		// disable menu
 82		$this->app->request->setVar('hidemainmenu', 1);
 83
 84		// get request vars
 85		$cid  = $this->app->request->get('cid.0', 'int');
 86		$edit = $cid > 0;
 87
 88		// get item
 89		if ($edit) {
 90			$this->submission = $this->table->get($cid);
 91		} else {
 92			$this->submission = $this->app->object->create('Submission');
 93			$this->submission->application_id = $this->application->id;
 94            $this->submission->access = 1;
 95		}
 96
 97		// set toolbar items
 98		$this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Submission').': '.$this->submission->name.' <small><small>[ '.($edit ? JText::_('Edit') : JText::_('New')).' ]</small></small>');
 99		$this->app->toolbar->apply();
100		$this->app->toolbar->save();
101		$this->app->toolbar->save2new();
102		$this->app->toolbar->cancel('cancel', $edit ? 'Close' : 'Cancel');
103		$this->app->zoo->toolbarHelp();
104
105        // published select
106		$this->lists['select_published'] = $this->app->html->_('select.booleanlist', 'state', null, $this->submission->state);
107
108		// access select
109		$this->lists['select_access'] = $this->app->html->_('zoo.accesslevel', array(), 'access', 'class="inputbox"', 'value', 'text',$this->submission->access);
110
111        // tooltip select
112		$this->lists['select_tooltip'] = $this->app->html->_('select.booleanlist', 'params[show_tooltip]', null, $this->submission->showTooltip());
113
114		// item captcha select
115		$options = array($this->app->html->_('select.option', '', '- '.JText::_('Select Plugin').' -'));
116		$this->lists['select_item_captcha'] = $this->app->html->_('zoo.pluginlist', $options, 'params[captcha]', '', 'value', 'text', $this->submission->getParams()->get('captcha', null), null, true, 'captcha');
117
118        // type select
119        $this->types = array();
120        foreach ($this->application->getTypes() as $type) {
121
122            // list types with submission layouts only
123            if (count($this->app->type->layouts($type, 'submission')) > 0) {
124
125                $form = $this->submission->getForm($type->id);
126
127                $this->types[$type->id]['name'] = $type->name;
128
129                $options = array($this->app->html->_('select.option', '', '- '.JText::_('not submittable').' -'));
130                $this->types[$type->id]['select_layouts'] = $this->app->html->_('zoo.layoutlist', $type, 'submission', $options, 'params[form]['.$type->id.'][layout]', '', 'value', 'text', $form->get('layout'));
131
132                $options = array($this->app->html->_('select.option', '', '- '.JText::_('uncategorized').' -'));
133                $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'));
134
135            }
136        }
137
138        // display view
139		$this->getView()->setLayout('edit')->display();
140	}
141
142	public function save() {
143
144		// check for request forgeries
145		$this->app->session->checkToken() or jexit('Invalid Token');
146
147		// init vars
148		$post = $this->app->request->get('post:', 'array', array());
149		$cid  = $this->app->request->get('cid.0', 'int');
150
151		try {
152
153			// get item
154			if ($cid) {
155				$submission = $this->table->get($cid);
156			} else {
157				$submission = $this->app->object->create('Submission');
158				$submission->application_id = $this->application->id;
159			}
160
161			// bind submission data
162			self::bind($submission, $post, array('params'));
163
164			// Force alias to be set
165			if (!strlen(trim($submission->alias))) {
166				$submission->alias = $this->app->string->sluggify($submission->name);
167			}
168
169            // generate unique slug
170            $submission->alias = $this->app->alias->submission->getUniqueAlias($submission->id, $this->app->string->sluggify($submission->alias));
171
172			// set params
173			$submission->getParams()
174                ->set('form.', @$post['params']['form'])
175                ->set('trusted_mode', @$post['params']['trusted_mode'])
176				->set('show_tooltip', @$post['params']['show_tooltip'])
177				->set('max_submissions', @$post['params']['max_submissions'])
178				->set('captcha', @$post['params']['captcha'])
179				->set('captcha_guest_only', @$post['params']['captcha_guest_only'])
180				->set('email_notification', @$post['params']['email_notification'])
181				->set('config.', @$post['params']['config'])
182				->set('content.', @$post['params']['content']);
183
184			// save submission
185			$this->table->save($submission);
186
187			// set redirect message
188			$msg = JText::_('Submission Saved');
189
190		} catch (AppException $e) {
191
192			// raise notice on exception
193			$this->app->error->raiseNotice(0, JText::_('Error Saving Submission').' ('.$e.')');
194			$this->_task = 'apply';
195			$msg = null;
196
197		}
198
199		$link = $this->baseurl;
200		switch ($this->getTask()) {
201			case 'apply' :
202				$link .= '&task=edit&cid[]='.$submission->id;
203				break;
204			case 'save2new' :
205				$link .= '&task=add';
206				break;
207		}
208
209		$this->setRedirect($link, $msg);
210	}
211
212	public function remove() {
213
214		// check for request forgeries
215		$this->app->session->checkToken() or jexit('Invalid Token');
216
217		// init vars
218		$cid = $this->app->request->get('cid', 'array', array());
219
220		if (count($cid) < 1) {
221			$this->app->error->raiseError(500, JText::_('Select a submission to delete'));
222		}
223
224		try {
225
226			// delete items
227			foreach ($cid as $id) {
228				$this->table->delete($this->table->get($id));
229			}
230
231			// set redirect message
232			$msg = JText::_('Submission Deleted');
233
234		} catch (AppException $e) {
235
236			// raise notice on exception
237			$this->app->error->raiseWarning(0, JText::_('Error Deleting Submission').' ('.$e.')');
238			$msg = null;
239
240		}
241
242		$this->setRedirect($this->baseurl, $msg);
243	}
244
245	public function docopy() {
246		// check for request forgeries
247		$this->app->session->checkToken() or jexit('Invalid Token');
248
249		// init vars
250		$cid = $this->app->request->get('cid', 'array', array());
251
252		if (count($cid) < 1) {
253			$this->app->error->raiseError(500, JText::_('Select a submission to copy'));
254		}
255
256		try {
257
258			// copy submissions
259			foreach ($cid as $id) {
260
261				// get submission
262				$submission = $this->table->get($id);
263
264				// copy submission
265				$submission->id         = 0;                         // set id to 0, to force new category
266				$submission->name      .= ' ('.JText::_('Copy').')'; // set copied name
267				$submission->alias      = $this->app->alias->submission->getUniqueAlias($id, $submission->alias.'-copy'); // set copied alias
268
269				// save copied category data
270				$this->table->save($submission);
271			}
272
273            // set redirect message
274			$msg = JText::_('Submission Copied');
275
276		} catch (AppException $e) {
277
278			// raise notice on exception
279			$this->app->error->raiseNotice(0, JText::_('Error Copying Category').' ('.$e.')');
280			$msg = null;
281
282		}
283
284		$this->setRedirect($this->baseurl, $msg);
285	}
286
287	public function publish() {
288		$this->_editState(1);
289	}
290
291	public function unpublish() {
292		$this->_editState(0);
293	}
294
295	protected function _editState($state) {
296
297		// check for request forgeries
298		$this->app->session->checkToken() or jexit('Invalid Token');
299
300		// init vars
301		$cid = $this->app->request->get('cid', 'array', array());
302
303		if (count($cid) < 1) {
304			$this->app->error->raiseError(500, JText::_('Select a submission to edit publish state'));
305		}
306
307		try {
308
309			// update item state
310			foreach ($cid as $id) {
311				$submission = $this->table->get($id);
312				$submission->state = $state;
313				$this->table->save($submission);
314			}
315
316		} catch (AppException $e) {
317
318			// raise notice on exception
319			$this->app->error->raiseNotice(0, JText::_('Error editing Submission Published State').' ('.$e.')');
320
321		}
322
323		$this->setRedirect($this->baseurl);
324	}
325
326	public function enableTrustedMode() {
327		$this->_editTrustedMode(1);
328	}
329
330	public function disableTrustedMode() {
331		$this->_editTrustedMode(0);
332	}
333
334	protected function _editTrustedMode($enabled) {
335
336		// check for request forgeries
337		$this->app->session->checkToken() or jexit('Invalid Token');
338
339		// init vars
340		$cid = $this->app->request->get('cid', 'array', array());
341
342		if (count($cid) < 1) {
343			$this->app->error->raiseError(500, JText::_('Select a submission to enable/disable Trusted Mode'));
344		}
345
346		try {
347
348			// update item state
349			foreach ($cid as $id) {
350				$submission = $this->table->get($id);
351
352				// trusted mode can only be enabled for nonpublic access
353				if ($enabled == true) {
354					if (!JAccess::checkGroup($this->app->zoo->getGroup($submission->access)->id, 'core.login.site')) {
355						throw new AppException('Trusted mode can\'t be enabled for public access');
356					}
357				}
358
359				$submission->getParams()
360					->set('trusted_mode', $enabled);
361
362				$this->table->save($submission);
363			}
364
365		} catch (AppException $e) {
366
367			// raise notice on exception
368			$this->app->error->raiseNotice(0, JText::_('Error enabling/disabling Submission Trusted Mode').' ('.$e.')');
369
370		}
371
372		$this->setRedirect($this->baseurl);
373	}
374
375}
376
377/*
378	Class: SubmissionControllerException
379*/
380class SubmissionControllerException extends AppException {}