/** * Remove the temporary images if a user upload or crop a picture, * but he does not store it or reload the page. * * @param CrowdfundingModelProject $model */ protected function removeTemporaryImages($model) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ // Remove old image if it exists. $oldImage = $app->getUserState(Crowdfunding\Constants::TEMPORARY_IMAGE_CONTEXT); if (!empty($oldImage)) { $temporaryFolder = CrowdfundingHelper::getTemporaryImagesFolder(); $oldImage = JPath::clean($temporaryFolder . "/" . basename($oldImage)); if (JFile::exists($oldImage)) { JFile::delete($oldImage); } } // Set the name of the image in the session. $app->setUserState(Crowdfunding\Constants::TEMPORARY_IMAGE_CONTEXT, null); // Remove the temporary images if they exist. $temporaryImages = $app->getUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT); if (!empty($temporaryImages)) { $temporaryFolder = CrowdfundingHelper::getTemporaryImagesFolder(); $model->removeTemporaryImages($temporaryImages, $temporaryFolder); } // Reset the temporary images. $app->setUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT, null); }
public function save($key = null, $urlVar = null) { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $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 = Joomla\Utilities\ArrayHelper::getValue($data, 'id', 0, 'int'); $terms = Joomla\Utilities\ArrayHelper::getValue($data, 'terms', false, 'bool'); $redirectOptions = array('view' => 'project', 'id' => $itemId); $model = $this->getModel(); /** @var $model CrowdfundingModelProject */ // Get component parameters $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_FORM_CANNOT_BE_LOADED')); } // Validate data. $validData = $model->validate($form, $data); if ($validData === false) { $this->displayNotice($form->getErrors(), $redirectOptions); return; } if ($itemId > 0) { // Validate owner if the item already exists. $userId = JFactory::getUser()->get('id'); $validator = new Crowdfunding\Validator\Project\Owner(JFactory::getDbo(), $itemId, $userId); if (!$validator->isValid()) { $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), $redirectOptions); return; } } else { // Verify terms of use during the process of creating a project. if ($params->get('project_terms', 0) and !$terms) { $redirectOptions = array('view' => 'project'); $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_TERMS_NOT_ACCEPTED'), $redirectOptions); return; } } // Include plugins to validate content. $dispatcher = JEventDispatcher::getInstance(); JPluginHelper::importPlugin('content'); // Trigger onContentValidate event. $context = $this->option . '.basic'; $results = $dispatcher->trigger('onContentValidate', array($context, &$validData, &$params)); // If there is an error, redirect to current step. foreach ($results as $result) { if ((bool) $result['success'] === false) { $this->displayWarning(Joomla\Utilities\ArrayHelper::getValue($result, 'message'), $redirectOptions); return; } } try { // Store the project data. $itemId = $model->save($validData); // Set the project ID to redirect options. $redirectOptions['id'] = $itemId; // Get the images from the session. $images = $app->getUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT); // Store the images to the project record. if ($images !== null and is_array($images) and $itemId > 0) { // Get the folder where the images will be stored $temporaryFolder = CrowdfundingHelper::getTemporaryImagesFolder(JPATH_BASE); // Move the pictures from the temporary folder to the images folder. // Store the names of the pictures in project record. $model->updateImages($itemId, $images, $temporaryFolder); // Remove the pictures from the session. $app->setUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT, null); } } catch (RuntimeException $e) { $this->displayWarning($e->getMessage(), $redirectOptions); return; } catch (InvalidArgumentException $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' => 'funding', 'id' => $itemId); $this->displayMessage(JText::_('COM_CROWDFUNDING_PROJECT_SUCCESSFULLY_SAVED'), $redirectOptions); }
public function cancelImageCrop() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $response = new Prism\Response\Json(); $userId = JFactory::getUser()->get("id"); if (!$userId) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'))->failure(); echo $response; $app->close(); } try { // Get the folder where the images will be stored $temporaryFolder = CrowdfundingHelper::getTemporaryImagesFolder(); // Remove old image if it exists. $oldImage = $app->getUserState(Crowdfunding\Constants::TEMPORARY_IMAGE_CONTEXT); if (!empty($oldImage)) { $oldImage = JPath::clean($temporaryFolder . "/" . basename($oldImage)); if (JFile::exists($oldImage)) { JFile::delete($oldImage); } } // Set the name of the image in the session. $app->setUserState(Crowdfunding\Constants::TEMPORARY_IMAGE_CONTEXT, null); } catch (Exception $e) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'))->failure(); echo $response; $app->close(); } $response->setTitle(JText::_('COM_CROWDFUNDING_SUCCESS'))->setText(JText::_('COM_CROWDFUNDING_IMAGE_RESET_SUCCESSFULLY'))->success(); echo $response; $app->close(); }