/**
  * Method to remove records via AJAX.
  *
  * @throws  Exception
  * @return  void
  */
 public function remove()
 {
     // Get the input
     $app = JFactory::getApplication();
     $pks = $app->input->post->get('rid', array(), 'array');
     $userId = JFactory::getUser()->get('id');
     $response = new Prism\Response\Json();
     // Sanitize the input
     $pks = Joomla\Utilities\ArrayHelper::toInteger($pks);
     // Validate user
     if (!$userId) {
         $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'))->failure();
         echo $response;
         $app->close();
     }
     // Validate primary keys
     if (!$pks) {
         $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_INVALID_REWARDS_SELECTED'))->failure();
         echo $response;
         $app->close();
     }
     $rewardId = Joomla\Utilities\ArrayHelper::getValue($pks, 0);
     // 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_REWARDS_SELECTED'))->failure();
         echo $response;
         $app->close();
     }
     // Get the model
     $model = $this->getModel();
     try {
         $reward = new Crowdfunding\Reward(JFactory::getDbo());
         $reward->load($rewardId);
         // If the reward is part of transaction,
         // set it as trashed.
         if ($reward->isSelectedByUser()) {
             $reward->trash();
         } else {
             // Get the folder where the images are stored
             $imagesFolder = CrowdfundingHelper::getImagesFolder($userId, JPATH_ROOT);
             $model->remove($rewardId, $imagesFolder);
         }
     } catch (RuntimeException $e) {
         $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText($e->getMessage())->failure();
         echo $response;
         $app->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;
         $app->close();
     }
     $response->setTitle(JText::_('COM_CROWDFUNDING_SUCCESS'))->setText(JText::_('COM_CROWDFUNDING_REWARD_SUCCESSFULY_REMOVED'))->success();
     echo $response;
     $app->close();
 }
 /**
  * Get the data that is going to be passed to the layout
  *
  * @return  array
  */
 public function getLayoutData()
 {
     // Get the basic field data
     $data = parent::getLayoutData();
     // Load the current username if available.
     $item = new Crowdfunding\Reward(JFactory::getDbo());
     $title = '';
     if (is_numeric($this->value)) {
         $options = array('fields' => array('a.id', 'a.title'));
         $item->load($this->value, $options);
         $title = $item->get('title');
     }
     $extraData = array('rewardTitle' => $title);
     return array_merge($data, $extraData);
 }
Exemple #3
0
 /**
  * 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);
 }
 /**
  * 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);
 }