예제 #1
0
 public function validate($data)
 {
     if (!is_array($data) or count($data) === 0) {
         throw new InvalidArgumentException(JText::_('COM_CROWDFUNDING_ERROR_INVALID_REWARDS'));
     }
     $filter = JFilterInput::getInstance();
     $params = JComponentHelper::getParams('com_crowdfunding');
     /** @var  $params Joomla\Registry\Registry */
     $money = $this->getMoneyFormatter($params);
     foreach ($data as $key => &$item) {
         $item['amount'] = $money->setAmount($item['amount'])->parse();
         // Filter data
         if (!is_numeric($item['amount'])) {
             $item['amount'] = 0.0;
         }
         $item['title'] = $filter->clean($item['title'], 'string');
         $item['title'] = JString::trim($item['title']);
         $item['title'] = JString::substr($item['title'], 0, 128);
         $item['description'] = $filter->clean($item['description'], 'string');
         $item['description'] = JString::trim($item['description']);
         $item['description'] = JString::substr($item['description'], 0, 500);
         $item['number'] = (int) $item['number'];
         $item['delivery'] = trim($item['delivery']);
         $item['delivery'] = $filter->clean($item['delivery'], 'string');
         if (!empty($item['delivery'])) {
             $item['delivery'] = CrowdfundingHelper::convertToSql($item['delivery']);
             $validatorDate = new Prism\Validator\Date($item['delivery']);
             if (!$validatorDate->isValid()) {
                 $item['delivery'] = '';
             }
         }
         if (!$item['title']) {
             throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_INVALID_TITLE'));
         }
         if (!$item['description']) {
             throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_INVALID_DESCRIPTION'));
         }
         if (!$item['amount']) {
             throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_INVALID_AMOUNT'));
         }
     }
     unset($item);
     return $data;
 }
예제 #2
0
 /**
  * Prepare and sanitise the table prior to saving.
  *
  * @param CrowdfundingTableProject $table
  * @param array $data
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  *
  * @since    1.6
  */
 protected function prepareTableData($table, $data)
 {
     $durationType = Joomla\Utilities\ArrayHelper::getValue($data, 'duration_type');
     $fundingEnd = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_end');
     $fundingDays = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_days', 0, 'int');
     switch ($durationType) {
         case 'days':
             $fundingDays = $fundingDays < 0 ? 0 : (int) $fundingDays;
             $table->set('funding_days', $fundingDays);
             // Calculate end date
             $startingDateValidator = new Prism\Validator\Date($table->get('funding_start'));
             if ($startingDateValidator->isValid()) {
                 $fundingStartDate = new Crowdfunding\Date($table->get('funding_start'));
                 $fundingEndDate = $fundingStartDate->calculateEndDate($table->get('funding_days'));
                 $table->set('funding_end', $fundingEndDate->format(Prism\Constants::DATE_FORMAT_SQL_DATE));
             } else {
                 $table->set('funding_end', Prism\Constants::DATE_DEFAULT_SQL_DATE);
             }
             break;
         case 'date':
             $fundingEnd = CrowdfundingHelper::convertToSql($fundingEnd);
             $dateValidator = new Prism\Validator\Date($fundingEnd);
             if (!$dateValidator->isValid()) {
                 throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_INVALID_DATE'));
             }
             $date = new JDate($fundingEnd);
             $table->set('funding_days', 0);
             $table->set('funding_end', $date->toSql());
             break;
         default:
             $table->set('funding_days', 0);
             $table->set('funding_end', Prism\Constants::DATE_DEFAULT_SQL_DATE);
             break;
     }
 }