/** * Display the view */ public function display($tpl = null) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ // Initialise variables $this->state = $this->get('State'); $this->form = $this->get('Form'); $this->params = $this->state->get("params"); $userId = JFactory::getUser()->get("id"); if (!$userId) { $returnUrl = JRoute::_("index.php?option=com_userideas&view=form"); $app->enqueueMessage(JText::_("COM_USERIDEAS_ERROR_NOT_LOG_IN"), "notice"); $app->redirect(JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode($returnUrl), false)); return; } $itemId = $this->state->get("form.id"); if (!empty($itemId)) { jimport("userideas.validator.item.owner"); $itemValidator = new UserIdeasValidatorItemOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemValidator->isValid()) { $app->enqueueMessage(JText::_("COM_USERIDEAS_ERROR_INVALID_ITEM"), "notice"); $app->redirect(JRoute::_('index.php', false)); return; } } $this->version = new UserIdeasVersion(); $this->prepareDebugMode(); $this->prepareDocument(); parent::display($tpl); }
public function save($key = null, $urlVar = null) { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); // Get the data from the form POST $data = $this->input->post->get('jform', array(), 'array'); $itemId = JArrayHelper::getValue($data, "id", 0, "int"); $redirectOptions = array("view" => "form", "id" => $itemId); // Check for valid user $userId = JFactory::getUser()->id; if (!$userId) { $redirectOptions = array("force_direction" => "index.php?option=com_users&view=login"); $this->displayNotice(JText::_('COM_USERIDEAS_ERROR_NOT_LOG_IN'), $redirectOptions); return; } // Check for valid owner of the item if (!empty($itemId)) { jimport("userideas.validator.item.owner"); $itemValidator = new UserIdeasValidatorItemOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemValidator->isValid($itemId, $userId)) { $redirectOptions = array("force_direction" => UserIdeasHelperRoute::getItemsRoute()); $this->displayNotice(JText::_('COM_USERIDEAS_ERROR_INVALID_ITEM'), $redirectOptions); return; } } $model = $this->getModel(); /** @var $model UserIdeasModelForm */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_("COM_USERIDEAS_ERROR_FORM_CANNOT_BE_LOADED"), 500); } // Test if the data is valid. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { $this->displayNotice($form->getErrors(), $redirectOptions); return; } try { $validData["user_id"] = $userId; $itemId = $model->save($validData); $redirectOptions["id"] = $itemId; } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_USERIDEAS_ERROR_SYSTEM')); } // Redirect to next page $this->displayMessage(JText::_('COM_USERIDEAS_ITEM_SAVED_SUCCESSFULLY'), $redirectOptions); }
/** * Method override to check if you can edit an existing record. * * @param array $data An array of input data. * @param string $key The name of the key for the primary key; default is id. * * @return boolean * * @since 1.6 */ protected function allowEdit($data = array(), $key = 'id') { $user = JFactory::getUser(); // Validate action role. if (!$user->authorise('core.edit.own', 'com_userideas')) { return false; } // Validate item owner. $itemId = JArrayHelper::getValue($data, $key); $userId = $user->get("id"); // Validate item owner. jimport("userideas.validator.item.owner"); $itemValidator = new UserIdeasValidatorItemOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemValidator->isValid()) { return false; } return true; }
/** * Method to test whether a record can be created or edited. * * @param int $itemId Item ID/ * @param int $userId User ID. * * @return boolean True if allowed to change the state of the record. Defaults to the permission for the component. * * @since 12.2 */ public function canEditOwn($itemId, $userId) { $user = JFactory::getUser(); if (!$user->authorise('core.edit.own', "com_userideas")) { return false; } // Validate item owner. jimport("userideas.validator.item.owner"); $itemValidator = new UserIdeasValidatorItemOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemValidator->isValid()) { return false; } return true; }