/** * Save an item */ public function save($key = null, $urlVar = null) { JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $data = $this->input->post->get('jform', array(), 'array'); $itemId = JArrayHelper::getValue($data, "id"); $dataFile = $this->input->files->get('jform', array(), 'array'); $image = JArrayHelper::getValue($dataFile, "image"); $redirectOptions = array("task" => $this->getTask(), "id" => $itemId); $model = $this->getModel(); /** @var $model CrowdFundingModelReward */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_("COM_CROWDFUNDING_ERROR_FORM_CANNOT_BE_LOADED"), 500); } // Validate the form $validData = $model->validate($form, $data); // Check for errors. if ($validData === false) { $this->displayNotice($form->getErrors(), $redirectOptions); return; } $params = JComponentHelper::getParams("com_crowdfunding"); /** @var $params Joomla\Registry\Registry */ try { $itemId = $model->save($validData); $redirectOptions["id"] = $itemId; // Upload an image $imagesAllowed = $params->get("rewards_images", 0); // Upload images. if ($imagesAllowed and !empty($image) and !empty($itemId)) { jimport("crowdfunding.reward"); $reward = new CrowdFundingReward(JFactory::getDbo()); $reward->load($itemId); // Get the folder where the images will be stored $imagesFolder = CrowdFundingHelper::getImagesFolder($reward->getUserId()); jimport("joomla.filesystem.folder"); if (!JFolder::exists($imagesFolder)) { CrowdFundingHelper::createFolder($imagesFolder); } $images = $model->uploadImage($image, $imagesFolder); if (!empty($images)) { $model->storeImage($images, $imagesFolder, $itemId); } } } catch (RuntimeException $e) { $this->displayError($e->getMessage(), $redirectOptions); return; } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } $this->displayMessage(JText::_('COM_CROWDFUNDING_REWARD_SAVED'), $redirectOptions); }
public function save() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $userId = JFactory::getUser()->id; if (!$userId) { $redirectOptions = array("force_direction" => "index.php?option=com_users&view=login"); $this->displayNotice(JText::_("COM_CROWDFUNDING_ERROR_NOT_LOG_IN"), $redirectOptions); return; } $params = JComponentHelper::getParams("com_crowdfunding"); /** @var $params Joomla\Registry\Registry */ // Get the data from the form POST $data = $this->input->post->get('rewards', array(), 'array'); $projectId = $this->input->post->get('id', 0, 'int'); $actionSubmit = $this->input->post->getCmd('btn_submit', 'save'); $images = $this->input->files->get('images', array(), 'array'); $userId = JFactory::getUser()->get("id"); // Get wizard type $wizardType = $params->get("project_wizard_type", "five_steps"); $fiveStepsWizard = strcmp($wizardType, "five_steps") == 0 ? true : false; // If it is five steps wizard type, redirect to manager. // If it is six steps wizard type, redirect to extras. if (!$fiveStepsWizard) { $layout = strcmp($actionSubmit, "save_continue") == 0 ? "extras" : "rewards"; } else { $layout = strcmp($actionSubmit, "save_continue") == 0 ? "manager" : "rewards"; } $redirectOptions = array("view" => "project", "layout" => $layout, "id" => $projectId); // Validate project owner. jimport("crowdfunding.validator.project.owner"); $validator = new CrowdFundingValidatorProjectOwner(JFactory::getDbo(), $projectId, $userId); if (!$projectId or !$validator->isValid()) { $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), $redirectOptions); return; } $model = $this->getModel(); /** @var $model CrowdFundingModelRewards */ try { $validData = $model->validate($data); $rewardsIds = $model->save($validData, $projectId); $imagesAllowed = $params->get("rewards_images", 0); // Upload images. if ($imagesAllowed and !empty($images) and !empty($rewardsIds)) { // Get the folder where the images will be stored $imagesFolder = CrowdFundingHelper::getImagesFolder($userId); jimport("joomla.filesystem.folder"); if (!JFolder::exists($imagesFolder)) { CrowdFundingHelper::createFolder($imagesFolder); } $images = $model->uploadImages($images, $imagesFolder, $rewardsIds); if (!empty($images)) { $model->storeImages($images, $imagesFolder); } } } catch (InvalidArgumentException $e) { $this->displayWarning($e->getMessage(), $redirectOptions); return; } catch (RuntimeException $e) { $this->displayWarning($e->getMessage(), $redirectOptions); return; } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } // Redirect to next page $this->displayMessage(JText::_("COM_CROWDFUNDING_REWARDS_SUCCESSFULLY_SAVED"), $redirectOptions); }
protected function prepareStory() { $model = JModelLegacy::getInstance("Story", "CrowdFundingModel", $config = array('ignore_request' => false)); /** @var $model CrowdFundingModelStory */ // Get state /** @var $state Joomla\Registry\Registry */ $state = $model->getState(); $this->state = $state; // Get params /** @var $params Joomla\Registry\Registry */ $params = $this->state->get("params"); $this->params = $params; // Get item $itemId = $this->state->get('story.id'); $this->item = $model->getItem($itemId, $this->userId); $this->form = $model->getForm(); $this->imageFolder = $this->params->get("images_directory", "images/crowdfunding"); $this->pitchImage = $this->item->get("pitch_image"); $this->pWidth = $this->params->get("pitch_image_width", 600); $this->pHeight = $this->params->get("pitch_image_height", 400); // Prepare extra images folder if ($this->params->get("extra_images", 0) and !empty($this->userId)) { jimport('joomla.filesystem.folder'); $userDestinationFolder = CrowdFundingHelper::getImagesFolder($this->userId); if (!JFolder::exists($userDestinationFolder)) { CrowdFundingHelper::createFolder($userDestinationFolder); } jimport("crowdfunding.images"); $this->images = new CrowdFundingImages(JFactory::getDbo()); $this->images->load($itemId); $this->extraImagesUri = CrowdFundingHelper::getImagesFolderUri($this->userId); } $this->pathwayName = JText::_("COM_CROWDFUNDING_STEP_STORY"); }