10 defined(
'JPATH_PLATFORM') or die;
71 public function __construct($config = array())
73 parent::__construct($config);
76 if (empty($this->option))
78 $this->option =
'com_' . strtolower($this->getName());
82 if (empty($this->text_prefix))
84 $this->text_prefix = strtoupper($this->option);
88 if (empty($this->context))
91 if (!preg_match(
'/(.*)Controller(.*)/i', get_class($this), $r))
93 throw new Exception(
JText::_(
'JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
95 $this->context = strtolower($r[2]);
99 if (empty($this->view_item))
101 $this->view_item = $this->context;
105 if (empty($this->view_list))
113 array(
'/(x|ch|ss|sh)$/i',
"$1es"),
114 array(
'/([^aeiouy]|qu)y$/i',
"$1ies"),
115 array(
'/([^aeiouy]|qu)ies$/i',
"$1y"),
116 array(
'/(bu)s$/i',
"$1ses"),
121 foreach ($plural as $pattern)
123 if (preg_match($pattern[0], $this->view_item))
125 $this->view_list = preg_replace($pattern[0], $pattern[1], $this->view_item);
132 $this->registerTask(
'apply',
'save');
133 $this->registerTask(
'save2new',
'save');
134 $this->registerTask(
'save2copy',
'save');
144 public function add()
147 $context =
"$this->option.edit.$this->context";
150 if (!$this->allowAdd())
153 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'));
154 $this->setMessage($this->getError(),
'error');
158 'index.php?option=' . $this->option .
'&view=' . $this->view_list
159 . $this->getRedirectToListAppend(),
false
167 $app->setUserState($context .
'.data', null);
172 'index.php?option=' . $this->option .
'&view=' . $this->view_item
173 . $this->getRedirectToItemAppend(),
false
191 protected function allowAdd($data = array())
194 return ($user->authorise(
'core.create', $this->option) || count($user->getAuthorisedCategories($this->option,
'core.create')));
209 protected function allowEdit($data = array(), $key =
'id')
226 protected function allowSave($data, $key =
'id')
228 $recordId = isset($data[$key]) ? $data[$key] :
'0';
232 return $this->allowEdit($data, $key);
236 return $this->allowAdd($data);
249 public function batch($model)
251 $vars = $this->input->post->get(
'batch', array(),
'array');
252 $cid = $this->input->post->get(
'cid', array(),
'array');
256 foreach ($cid as $id)
259 if (isset($this->extension))
261 $option = $this->extension;
265 $option = $this->option;
267 $contexts[$id] = $option .
'.' . $this->context .
'.' . $id;
271 if ($model->batch($vars, $cid, $contexts))
273 $this->setMessage(
JText::_(
'JLIB_APPLICATION_SUCCESS_BATCH'));
279 $this->setMessage(
JText::sprintf(
'JLIB_APPLICATION_ERROR_BATCH_FAILED', $model->getError()),
'warning');
294 public function cancel($key = null)
299 $model = $this->getModel();
300 $table = $model->getTable();
301 $checkin = property_exists($table,
'checked_out');
302 $context =
"$this->option.edit.$this->context";
306 $key = $table->getKeyName();
309 $recordId = $app->input->getInt($key);
316 if ($model->checkin($recordId) ===
false)
319 $this->setError(
JText::sprintf(
'JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
320 $this->setMessage($this->getError(),
'error');
324 'index.php?option=' . $this->option .
'&view=' . $this->view_item
325 . $this->getRedirectToItemAppend($recordId, $key),
false
335 $this->releaseEditId($context, $recordId);
336 $app->setUserState($context .
'.data', null);
340 'index.php?option=' . $this->option .
'&view=' . $this->view_list
341 . $this->getRedirectToListAppend(),
false
359 public function edit($key = null, $urlVar = null)
362 $model = $this->getModel();
363 $table = $model->getTable();
364 $cid = $this->input->post->get(
'cid', array(),
'array');
365 $context =
"$this->option.edit.$this->context";
370 $key = $table->getKeyName();
380 $recordId = (int) (count($cid) ? $cid[0] : $this->input->getInt($urlVar));
381 $checkin = property_exists($table,
'checked_out');
384 if (!$this->allowEdit(array($key => $recordId), $key))
386 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
387 $this->setMessage($this->getError(),
'error');
391 'index.php?option=' . $this->option .
'&view=' . $this->view_list
392 . $this->getRedirectToListAppend(),
false
400 if ($checkin && !$model->checkout($recordId))
403 $this->setError(
JText::sprintf(
'JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()));
404 $this->setMessage($this->getError(),
'error');
408 'index.php?option=' . $this->option .
'&view=' . $this->view_item
409 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
418 $this->holdEditId($context, $recordId);
419 $app->setUserState($context .
'.data', null);
423 'index.php?option=' . $this->option .
'&view=' . $this->view_item
424 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
443 public function getModel($name =
'', $prefix =
'', $config = array(
'ignore_request' =>
true))
447 $name = $this->context;
450 return parent::getModel($name, $prefix, $config);
463 protected function getRedirectToItemAppend($recordId = null, $urlVar =
'id')
465 $tmpl = $this->input->get(
'tmpl');
466 $layout = $this->input->get(
'layout',
'edit');
472 $append .=
'&tmpl=' . $tmpl;
477 $append .=
'&layout=' . $layout;
482 $append .=
'&' . $urlVar .
'=' . $recordId;
495 protected function getRedirectToListAppend()
503 $append .=
'&tmpl=' . $tmpl;
520 protected function postSaveHook(
JModelLegacy $model, $validData = array())
531 public function loadhistory()
535 $model = $this->getModel();
537 $historyId = $app->input->get(
'version_id', null,
'integer');
538 $context =
"$this->option.edit.$this->context";
540 if (!$model->loadhistory($historyId, $table))
542 $this->setMessage($model->
getError(),
'error');
546 'index.php?option=' . $this->option .
'&view=' . $this->view_list
547 . $this->getRedirectToListAppend(),
false
557 $key = $table->getKeyName();
560 $recordId = $table->$key;
563 $urlVar = empty($this->urlVar) ? $key : $this->urlVar;
566 if (!$this->allowEdit(array($key => $recordId), $key))
568 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
569 $this->setMessage($this->getError(),
'error');
573 'index.php?option=' . $this->option .
'&view=' . $this->view_list
574 . $this->getRedirectToListAppend(),
false
585 'index.php?option=' . $this->option .
'&view=' . $this->view_item
586 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
593 $this->postSaveHook($model);
608 public function save($key = null, $urlVar = null)
615 $model = $this->getModel();
617 $data = $this->input->post->get(
'jform', array(),
'array');
618 $checkin = property_exists($table,
'checked_out');
619 $context =
"$this->option.edit.$this->context";
620 $task = $this->getTask();
625 $key = $table->getKeyName();
634 $recordId = $this->input->getInt($urlVar);
637 $data[$key] = $recordId;
640 if ($task ==
'save2copy')
643 if ($checkin && $model->checkin($data[$key]) ===
false)
647 $this->setMessage($this->getError(),
'error');
651 'index.php?option=' . $this->option .
'&view=' . $this->view_item
652 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
665 if (!$this->allowSave($data, $key))
667 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
668 $this->setMessage($this->getError(),
'error');
672 'index.php?option=' . $this->option .
'&view=' . $this->view_list
673 . $this->getRedirectToListAppend(),
false
682 $form = $model->getForm($data,
false);
686 $app->enqueueMessage($model->
getError(),
'error');
692 $validData = $model->validate($form, $data);
695 if ($validData ===
false)
701 for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
703 if ($errors[$i] instanceof Exception)
705 $app->enqueueMessage($errors[$i]->getMessage(),
'warning');
709 $app->enqueueMessage($errors[$i],
'warning');
714 $app->setUserState($context .
'.data', $data);
719 'index.php?option=' . $this->option .
'&view=' . $this->view_item
720 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
727 if (!isset($validData[
'tags']))
729 $validData[
'tags'] = null;
733 if (!$model->save($validData))
736 $app->setUserState($context .
'.data', $validData);
740 $this->setMessage($this->getError(),
'error');
744 'index.php?option=' . $this->option .
'&view=' . $this->view_item
745 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
753 if ($checkin && $model->checkin($validData[$key]) ===
false)
756 $app->setUserState($context .
'.data', $validData);
760 $this->setMessage($this->getError(),
'error');
764 'index.php?option=' . $this->option .
'&view=' . $this->view_item
765 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
774 ($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ?
'_SUBMIT' :
'') .
'_SAVE_SUCCESS')
776 :
'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ?
'_SUBMIT' :
'') .
'_SAVE_SUCCESS'
785 $recordId = $model->
getState($this->context .
'.id');
786 $this->holdEditId($context, $recordId);
787 $app->setUserState($context .
'.data', null);
788 $model->checkout($recordId);
793 'index.php?option=' . $this->option .
'&view=' . $this->view_item
794 . $this->getRedirectToItemAppend($recordId, $urlVar),
false
801 $this->releaseEditId($context, $recordId);
802 $app->setUserState($context .
'.data', null);
807 'index.php?option=' . $this->option .
'&view=' . $this->view_item
808 . $this->getRedirectToItemAppend(null, $urlVar),
false
815 $this->releaseEditId($context, $recordId);
816 $app->setUserState($context .
'.data', null);
821 'index.php?option=' . $this->option .
'&view=' . $this->view_list
822 . $this->getRedirectToListAppend(),
false
829 $this->postSaveHook($model, $validData);