Ejemplo n.º 1
0
 public function display($tpl = null)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $this->option = JFactory::getApplication()->input->get('option');
     // Get user ID.
     $this->userId = JFactory::getUser()->get('id');
     // Get reward ID.
     $rewardId = $app->input->getInt('id');
     // Validate reward owner
     $validator = new Crowdfunding\Validator\Reward\Owner(JFactory::getDbo(), $rewardId, $this->userId);
     if (!$validator->isValid()) {
         $app->enqueueMessage(JText::_('COM_CROWDFUNDING_ERROR_INVALID_REWARD'), 'notice');
         $app->redirect(JRoute::_(CrowdfundingHelperRoute::getDiscoverRoute()));
         return;
     }
     $this->items = $this->get('Items');
     $this->state = $this->get('State');
     $this->pagination = $this->get('Pagination');
     // Get params
     /** @var  $params Joomla\Registry\Registry */
     $params = $this->state->get('params');
     $this->params = $params;
     // Prepare an URL where user will be redirected when change the state of a reward.
     $this->redirectUrl = 'index.php?option=com_crowdfunding&view=reward&id=' . $rewardId;
     // Prepare filters
     $this->listOrder = $this->escape($this->state->get('list.ordering'));
     $this->listDirn = $this->escape($this->state->get('list.direction'));
     $this->saveOrder = strcmp($this->listOrder, 'a.ordering') === 0;
     // Load reward data.
     $this->reward = new Crowdfunding\Reward(JFactory::getDbo());
     $this->reward->load($rewardId);
     // Prepare reward delivery date.
     $dateValidator = new Prism\Validator\Date($this->reward->getDeliveryDate());
     $this->deliveryDate = $dateValidator->isValid() ? JHtml::_('date', $this->reward->getDeliveryDate(), JText::_('DATE_FORMAT_LC3')) : '--';
     // Get images folder.
     $this->imagesFolder = CrowdfundingHelper::getImagesFolderUri($this->userId);
     // Get social profile
     $socialPlatform = $this->params->get('integration_social_platform');
     if (JString::strlen($socialPlatform) > 0) {
         $this->prepareSocialIntegration($socialPlatform);
     }
     $this->prepareDocument();
     parent::display($tpl);
 }
 /**
  * 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();
 }