public function addExtraImage() { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ jimport("itprism.response.json"); $response = new ITPrismResponseJson(); $userId = JFactory::getUser()->get("id"); if (!$userId) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'))->failure(); echo $response; JFactory::getApplication()->close(); } $projectId = $this->input->post->get("id"); // Get the model $model = $this->getModel(); /** @var $model CrowdFundingModelStory * */ // Validate project owner. jimport("crowdfunding.validator.project.owner"); $validator = new CrowdFundingValidatorProjectOwner(JFactory::getDbo(), $projectId, $userId); if (!$projectId or !$validator->isValid()) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'))->failure(); echo $response; JFactory::getApplication()->close(); } $files = $this->input->files->get("files"); if (!$files) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED'))->failure(); echo $response; JFactory::getApplication()->close(); } // Get component parameters $params = $app->getParams("com_crowdfunding"); /** @var $params Joomla\Registry\Registry */ // Prepare the size of additional thumbnails $thumbWidth = $params->get("extre_image_thumb_width", 100); $thumbHeight = $params->get("extre_image_thumb_height", 100); if ($thumbWidth < 25 or $thumbHeight < 25) { $thumbWidth = 50; $thumbHeight = 50; } $scale = $app->input->post->get("extra_images_thumb_scale", JImage::SCALE_INSIDE); $images = array(); try { // Get the folder where the images will be stored $destination = CrowdFundingHelper::getImagesFolder($userId); $options = array("thumb_width" => $thumbWidth, "thumb_height" => $thumbHeight, "thumb_scale" => $scale, "destination" => $destination); // Get the folder where the images will be stored $imagesUri = CrowdFundingHelper::getImagesFolderUri($userId); $images = $model->uploadExtraImages($files, $options); $images = $model->storeExtraImage($images, $projectId, $imagesUri); } catch (Exception $e) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'))->failure(); echo $response; JFactory::getApplication()->close(); } $response->setTitle(JText::_('COM_CROWDFUNDING_SUCCESS'))->setText(JText::_('COM_CROWDFUNDING_IMAGE_SAVED'))->setData($images)->success(); echo $response; JFactory::getApplication()->close(); }
/** * Delete image */ public function removeImage() { // Check for request forgeries. JSession::checkToken("get") or jexit(JText::_('JINVALID_TOKEN')); // Check for registered user $userId = JFactory::getUser()->get("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; } // Get item id $itemId = $this->input->get->getInt("id"); $redirectOptions = array("view" => "project"); // Validate project owner. jimport("crowdfunding.validator.project.owner"); $validator = new CrowdFundingValidatorProjectOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemId or !$validator->isValid()) { $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_IMAGE'), $redirectOptions); return; } try { $model = $this->getModel(); $model->removeImage($itemId, $userId); } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } $redirectOptions["id"] = $itemId; $this->displayMessage(JText::_('COM_CROWDFUNDING_IMAGE_DELETED'), $redirectOptions); }
public function save($key = null, $urlVar = null) { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $userId = JFactory::getUser()->get("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; } // Get the data from the form POST $data = $this->input->post->get('jform', array(), 'array'); $itemId = JArrayHelper::getValue($data, "id"); $redirectOptions = array("view" => "project", "layout" => "funding", "id" => $itemId); $model = $this->getModel(); /** @var $model CrowdFundingModelFunding */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_("COM_CROWDFUNDING_ERROR_FORM_CANNOT_BE_LOADED")); } // Test if the data is valid. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { $this->displayNotice($form->getErrors(), $redirectOptions); return; } // Validate project owner. jimport("crowdfunding.validator.project.owner"); $validator = new CrowdFundingValidatorProjectOwner(JFactory::getDbo(), $itemId, $userId); if (!$itemId or !$validator->isValid()) { $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), $redirectOptions); return; } // Get component parameters. $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ // Include plugins to validate content. $dispatcher = JEventDispatcher::getInstance(); JPluginHelper::importPlugin('content'); // Trigger onContentValidate event. $context = $this->option . ".funding"; $results = $dispatcher->trigger("onContentValidate", array($context, &$validData, &$params)); // If there is an error, redirect to current step. foreach ($results as $result) { if ($result["success"] == false) { $this->displayWarning(JArrayHelper::getValue($result, "message"), $redirectOptions); return; } } try { // Save data $itemId = $model->save($validData); $redirectOptions["id"] = $itemId; } 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 $redirectOptions = array("view" => "project", "layout" => "story", "id" => $itemId); $this->displayMessage(JText::_("COM_CROWDFUNDING_FUNDING_SUCCESSFULLY_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); }