/** * Pre-processor for $table->delete($pk) * * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used. * * @return void * * @since 3.1.2 * @throws UnexpectedValueException */ public function onBeforeDelete($pk) { $userId = CrowdfundingHelper::getUserIdByRewardId($this->table->id); $imagesFolder = CrowdfundingHelper::getImagesFolder($userId); // Remove image. if (!empty($this->table->image)) { $fileSource = $imagesFolder . DIRECTORY_SEPARATOR . $this->table->image; if (JFile::exists($fileSource)) { JFile::delete($fileSource); } } // Remove thumbnail. if (!empty($this->table->image_thumb)) { $fileSource = $imagesFolder . DIRECTORY_SEPARATOR . $this->table->image_thumb; if (JFile::exists($fileSource)) { JFile::delete($fileSource); } } // Remove square image. if (!empty($this->table->image_square)) { $fileSource = $imagesFolder . DIRECTORY_SEPARATOR . $this->table->image_square; if (JFile::exists($fileSource)) { JFile::delete($fileSource); } } }
/** * Store the temporary images to project record. * Remove the old images and move the new ones from temporary folder to the images folder. * * @param int $projectId * @param array $images The names of the pictures. * @param string $source Path to the temporary folder. */ public function updateImages($projectId, $images, $source) { $project = Crowdfunding\Project::getInstance(JFactory::getDbo(), $projectId); if (!$project->getId()) { throw new InvalidArgumentException(JText::_("COM_CROWDFUNDING_ERROR_INVALID_PROJECT")); } // Prepare the path to the pictures. $fileImage = $source . DIRECTORY_SEPARATOR . $images["image"]; $fileSmall = $source . DIRECTORY_SEPARATOR . $images["image_small"]; $fileSquare = $source . DIRECTORY_SEPARATOR . $images["image_square"]; if (is_file($fileImage) and is_file($fileSmall) and is_file($fileSquare)) { // Get the folder where the pictures are stored. $imagesFolder = CrowdfundingHelper::getImagesFolder(); // Remove an image from the filesystem $oldFileImage = $imagesFolder . DIRECTORY_SEPARATOR . $project->getImage(); $oldFileSmall = $imagesFolder . DIRECTORY_SEPARATOR . $project->getSmallImage(); $oldFileSquare = $imagesFolder . DIRECTORY_SEPARATOR . $project->getSquareImage(); if (is_file($oldFileImage)) { JFile::delete($oldFileImage); } if (is_file($oldFileSmall)) { JFile::delete($oldFileSmall); } if (is_file($oldFileSquare)) { JFile::delete($oldFileSquare); } // Move the new files to the images folder. $newFileImage = $imagesFolder . DIRECTORY_SEPARATOR . $images["image"]; $newFileSmall = $imagesFolder . DIRECTORY_SEPARATOR . $images["image_small"]; $newFileSquare = $imagesFolder . DIRECTORY_SEPARATOR . $images["image_square"]; JFile::move($fileImage, $newFileImage); JFile::move($fileSmall, $newFileSmall); JFile::move($fileSquare, $newFileSquare); // Store the newest pictures. $project->bind($images); $project->store(); } }
/** * Method to remove image via AJAX. * * @throws Exception * * @return void */ public function removeImage() { // Get the input $rewardId = $this->input->post->get('rid', 0, 'int'); $userId = JFactory::getUser()->get('id'); $response = new Prism\Response\Json(); // Validate user if (!$userId) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'))->failure(); echo $response; JFactory::getApplication()->close(); } $params = JComponentHelper::getParams('com_crowdfunding'); /** @var $params Joomla\Registry\Registry */ if (!$params->get('rewards_images', 0)) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_INVALID_REWARD'))->failure(); echo $response; JFactory::getApplication()->close(); } // Validate reward owner. $validator = new Crowdfunding\Validator\Reward\Owner(JFactory::getDbo(), $rewardId, $userId); if (!$rewardId or !$validator->isValid()) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_INVALID_REWARD'))->failure(); echo $response; JFactory::getApplication()->close(); } // Get the model $model = $this->getModel(); try { // Get the folder where the images will be stored $imagesFolder = CrowdfundingHelper::getImagesFolder($userId, JPATH_ROOT); $model->removeImage($rewardId, $imagesFolder); } catch (RuntimeException $e) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText($e->getMessage())->failure(); echo $response; JFactory::getApplication()->close(); } catch (Exception $e) { JLog::add($e->getMessage(), JLog::ERROR, 'com_crowdfunding'); $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_REWARD_IMAGE_REMOVED_SUCCESSFULLY'))->success(); echo $response; JFactory::getApplication()->close(); }
public function save() { // 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; } $params = JComponentHelper::getParams('com_crowdfunding'); /** @var $params Joomla\Registry\Registry */ // Get the data from the form POST $projectId = $this->input->post->get('id', 0, 'int'); // Check if rewards are enabled. if (!$params->get('rewards_enabled', 1)) { $redirectOptions = array('view' => 'project', 'layout' => 'manager', 'id' => $projectId); $this->displayNotice(JText::_('COM_CROWDFUNDING_ERROR_REWARDS_DISABLED'), $redirectOptions); return; } $data_ = $this->input->post->get('rewards', array(), 'array'); $actionSubmit = $this->input->post->getCmd('btn_submit', 'save'); // Reorder items. $data = array(); foreach ($data_ as $item) { $ordering = array_key_exists('ordering', $item) ? (int) abs($item['ordering']) : 0; if (!$ordering or $ordering > 30) { continue; } $data[$ordering] = $item; } unset($data_); $images = $this->input->files->get('images', array(), 'array'); // Get wizard type $wizardType = $params->get('project_wizard_type', 'five_steps'); $fiveStepsWizard = strcmp($wizardType, 'five_steps') === 0; // 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. $validator = new Crowdfunding\Validator\Project\Owner(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 count($images) > 0 and count($rewardsIds) > 0) { // Get the folder where the images will be stored $imagesFolder = CrowdfundingHelper::getImagesFolder($userId, JPATH_ROOT); if (!JFolder::exists($imagesFolder)) { CrowdfundingHelper::createFolder($imagesFolder); } $options = array('temporary_path' => JFactory::getApplication()->get('tmp_path'), 'destination_path' => $imagesFolder); $images = $model->uploadImages($images, $rewardsIds, $options, $params); if (count($images) > 0) { $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(), JLog::ERROR, 'com_crowdfunding'); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } // Redirect to next page $this->displayMessage(JText::_('COM_CROWDFUNDING_REWARDS_SUCCESSFULLY_SAVED'), $redirectOptions); }
public function save($key = null, $urlVar = null) { // 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; } // Get the data from the form POST $data = $this->input->post->get('jform', array(), 'array'); $itemId = Joomla\Utilities\ArrayHelper::getValue($data, "id", 0, "int"); $redirectOptions = array("view" => "project", "layout" => "story", "id" => $itemId); $model = $this->getModel(); /** @var $model CrowdfundingModelStory */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_("COM_CROWDFUNDING_ERROR_FORM_CANNOT_BE_LOADED")); } // Check for valid data. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { $this->displayNotice($form->getErrors(), $redirectOptions); return; } // Validate project owner. $validator = new Crowdfunding\Validator\Project\Owner(JFactory::getDbo(), $itemId, $userId); if (!$validator->isValid()) { $redirectOptions = array("view" => "discover"); $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 . ".story"; $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(Joomla\Utilities\ArrayHelper::getValue($result, "message"), $redirectOptions); return; } } try { // Get image $image = $this->input->files->get('jform', array(), 'array'); $image = Joomla\Utilities\ArrayHelper::getValue($image, "pitch_image"); // Upload image if (!empty($image['name'])) { $destination = CrowdfundingHelper::getImagesFolder(); $imageName = $model->uploadImage($image, $destination); if (!empty($imageName)) { $validData["pitch_image"] = $imageName; } } $itemId = $model->save($validData); $redirectOptions["id"] = $itemId; } catch (RuntimeException $e) { $this->displayWarning($e->getMessage(), $redirectOptions); return; } catch (InvalidArgumentException $e) { $this->displayWarning(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"), $redirectOptions); return; } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } // Trigger the event onContentValidateAfterSave. $item = $model->getItem($itemId, $userId); $results = $dispatcher->trigger("onContentValidateAfterSave", array($context, &$item, &$params)); // If there is an error, redirect to current step. foreach ($results as $result) { if ($result["success"] == false) { $this->displayWarning(Joomla\Utilities\ArrayHelper::getValue($result, "message"), $redirectOptions); return; } } // Redirect to next page $redirectOptions = array("view" => "project", "layout" => "rewards", "id" => $itemId); $this->displayMessage(JText::_("COM_CROWDFUNDING_STORY_SUCCESSFULLY_SAVED"), $redirectOptions); }
/** * Delete image */ public function removeImage() { // Check for request forgeries. JSession::checkToken("get") or jexit(JText::_('JINVALID_TOKEN')); // Get item id $itemId = $this->input->get->getInt("id"); $redirectOptions = array("view" => "reward", "layout" => "edit", "id" => $itemId); // Create an reward object. $reward = new Crowdfunding\Reward(JFactory::getDbo()); $reward->load($itemId); // Check for registered user if (!$reward->getId()) { $this->displayNotice(JText::_('COM_CROWDFUNDING_ERROR_INVALID_IMAGE'), $redirectOptions); return; } $imagesFolder = CrowdfundingHelper::getImagesFolder($reward->getUserId(), JPATH_ROOT); try { jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); jimport('joomla.filesystem.path'); $model = $this->getModel(); $model->removeImage($itemId, $imagesFolder); } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } $this->displayMessage(JText::_('COM_CROWDFUNDING_IMAGE_DELETED'), $redirectOptions); }
public function save() { // 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; } $params = JComponentHelper::getParams("com_crowdfunding"); /** @var $params Joomla\Registry\Registry */ // Get the data from the form POST $projectId = $this->input->post->get('id', 0, 'int'); // Check if rewards are enabled. if (!$params->get("rewards_enabled", 1)) { $redirectOptions = array("view" => "project", "layout" => "manager", "id" => $projectId); $this->displayNotice(JText::_("COM_CROWDFUNDING_ERROR_REWARDS_DISABLED"), $redirectOptions); return; } $data = $this->input->post->get('rewards', array(), 'array'); $actionSubmit = $this->input->post->getCmd('btn_submit', 'save'); $images = $this->input->files->get('images', array(), 'array'); // 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. $validator = new Crowdfunding\Validator\Project\Owner(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); }
/** * Delete image */ public function removeImage() { // Check for request forgeries. JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN')); // Get item id $itemId = $this->input->get->getInt('id'); $redirectOptions = array('view' => 'reward', 'layout' => 'edit', 'id' => $itemId); // Create an reward object. $reward = new Crowdfunding\Reward(JFactory::getDbo()); $reward->load($itemId); // Check for registered user if (!$reward->getId()) { $this->displayNotice(JText::_('COM_CROWDFUNDING_ERROR_INVALID_IMAGE'), $redirectOptions); return; } $imagesFolder = CrowdfundingHelper::getImagesFolder($reward->getUserId(), JPATH_ROOT); try { $model = $this->getModel(); $model->removeImage($itemId, $imagesFolder); } catch (Exception $e) { JLog::add($e->getMessage(), JLog::ERROR, 'com_crowdfunding'); throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM')); } $this->displayMessage(JText::_('COM_CROWDFUNDING_IMAGE_DELETED'), $redirectOptions); }