/** * This method calculates start date and validate funding period. * * @param CrowdfundingTableProject $table * * @throws Exception */ protected function prepareTable(&$table) { // Calculate start and end date if the user publish a project for first time. $fundingStartDate = new Prism\Validator\Date($table->get('funding_start')); if (!$fundingStartDate->isValid()) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $fundingStart = new JDate('now', $app->get('offset')); $table->set('funding_start', $fundingStart->toSql()); // If funding type is 'days', calculate end date. if ($table->get('funding_days')) { $fundingStartDate = new Crowdfunding\Date($table->get('funding_start')); $endDate = $fundingStartDate->calculateEndDate($table->get('funding_days')); $table->set('funding_end', $endDate->toSql()); } } // Get parameters $params = JComponentHelper::getParams('com_crowdfunding'); /** @var $params Joomla\Registry\Registry */ $minDays = $params->get('project_days_minimum', 15); $maxDays = $params->get('project_days_maximum'); // If there is an ending date, validate the period. $fundingEndDate = new Prism\Validator\Date($table->get('funding_end')); if ($fundingEndDate->isValid()) { $validatorPeriod = new Crowdfunding\Validator\Project\Period($table->get('funding_start'), $table->get('funding_end'), $minDays, $maxDays); if (!$validatorPeriod->isValid()) { if (!empty($maxDays)) { throw new RuntimeException(JText::sprintf('COM_CROWDFUNDING_ERROR_INVALID_ENDING_DATE_MIN_MAX_DAYS', $minDays, $maxDays)); } else { throw new RuntimeException(JText::sprintf('COM_CROWDFUNDING_ERROR_INVALID_ENDING_DATE_MIN_DAYS', $minDays)); } } } }
/** * Prepare and sanitise the table prior to saving. * * @param CrowdfundingTableProject $table * * @throws Exception * * @since 1.6 */ protected function prepareTable($table) { $userId = (int) JFactory::getUser()->get('id'); if (!$table->get('id')) { // Get maximum order number // Set ordering to the last item if not set if (!$table->get('ordering')) { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('MAX(ordering)')->from($db->quoteName('#__crowdf_projects')); $db->setQuery($query, 0, 1); $max = $db->loadResult(); $table->set('ordering', $max + 1); } // Set state to unpublished. $table->set('published', Prism\Constants::UNPUBLISHED); // Set user ID $table->set('user_id', $userId); } else { if ($userId !== (int) $table->get('user_id')) { throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_INVALID_USER')); } } // If an alias does not exist, I will generate the new one using the title. if (!$table->get('alias')) { $table->set('alias', $table->get('title')); } $table->set('alias', JApplicationHelper::stringURLSafe($table->get('alias'))); }
/** * This method calculate start date and validate funding period. * * @param CrowdfundingTableProject $table * * @throws Exception */ protected function prepareTable(&$table) { // Calculate start and end date if the user publish a project for first time. $fundingStartDate = new Prism\Validator\Date($table->funding_start); if (!$fundingStartDate->isValid($table->funding_start)) { $fundingStart = new JDate(); $table->funding_start = $fundingStart->toSql(); // If funding type is "days", calculate end date. if ($table->get("funding_days")) { $fundingStartDate = new Crowdfunding\Date($table->get("funding_start")); $endDate = $fundingStartDate->calculateEndDate($table->get("funding_days")); $table->set("funding_end", $endDate->format("Y-m-d")); } } // Get parameters $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $params = $app->getParams(); /** @var $params Joomla\Registry\Registry */ $minDays = $params->get("project_days_minimum", 15); $maxDays = $params->get("project_days_maximum"); // If there is an ending date, validate the period. $fundingEndDate = new Prism\Validator\Date($table->get("funding_end")); if ($fundingEndDate->isValid()) { $validatorPeriod = new Crowdfunding\Validator\Project\Period($table->get("funding_start"), $table->get("funding_end"), $minDays, $maxDays); if (!$validatorPeriod->isValid()) { if (!empty($maxDays)) { throw new RuntimeException(JText::sprintf("COM_CROWDFUNDING_ERROR_INVALID_ENDING_DATE_MIN_MAX_DAYS", $minDays, $maxDays)); } else { throw new RuntimeException(JText::sprintf("COM_CROWDFUNDING_ERROR_INVALID_ENDING_DATE_MIN_DAYS", $minDays)); } } } }
/** * Prepare project images before saving. * * @param CrowdfundingTableProject $table * @param array $data * * @throws Exception * * @since 1.6 */ protected function prepareTableData($table, $data) { $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ // Set order value if (!$table->get('id') and !$table->get('ordering')) { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('MAX(ordering)')->from($db->quoteName('#__crowdf_projects')); $db->setQuery($query, 0, 1); $max = $db->loadResult(); $table->set('ordering', $max + 1); } // Prepare image. if (!empty($data['image'])) { // Delete old image if I upload a new one if ($table->get('image')) { $imagesFolder = $params->get('images_directory', 'images/crowdfunding'); // Remove an image from the filesystem $fileImage = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image')); $fileSmall = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image_small')); $fileSquare = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image_square')); if (is_file($fileImage)) { JFile::delete($fileImage); } if (is_file($fileSmall)) { JFile::delete($fileSmall); } if (is_file($fileSquare)) { JFile::delete($fileSquare); } } $table->set('image', $data['image']); $table->set('image_small', $data['image_small']); $table->set('image_square', $data['image_square']); } // Prepare pitch image. if (!empty($data['pitch_image'])) { // Delete old image if I upload a new one if ($table->get('pitch_image')) { $imagesFolder = $params->get('images_directory', 'images/crowdfunding'); // Remove an image from the filesystem $pitchImage = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('pitch_image')); if (is_file($pitchImage)) { JFile::delete($pitchImage); } } $table->set('pitch_image', $data['pitch_image']); } // If an alias does not exist, I will generate the new one using the title. if (!$table->get('alias')) { $table->set('alias', $table->get('title')); } if ((int) JFactory::getConfig()->get('unicodeslugs') === 1) { $table->set('alias', JFilterOutput::stringUrlUnicodeSlug($table->get('alias'))); } else { $table->set('alias', JFilterOutput::stringURLSafe($table->get('alias'))); } // Prepare funding duration $durationType = Joomla\Utilities\ArrayHelper::getValue($data, 'duration_type'); $fundingStart = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_start'); $fundingEnd = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_end'); $fundingDays = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_days'); // Prepare funding start date. $fundingStartValidator = new Prism\Validator\Date($fundingStart); if (!$fundingStartValidator->isValid()) { $table->funding_start = Prism\Constants::DATE_DEFAULT_SQL_DATE; } else { $date = new JDate($fundingStart); $table->funding_start = $date->toSql(); } switch ($durationType) { case 'days': // Set funding day. $table->funding_days = $fundingDays; // Calculate end date $fundingStartValidator = new Prism\Validator\Date($table->funding_start); if (!$fundingStartValidator->isValid()) { $table->funding_end = Prism\Constants::DATE_DEFAULT_SQL_DATE; } else { $fundingStartDate = new Crowdfunding\Date($table->funding_start); $fundingEndDate = $fundingStartDate->calculateEndDate($table->funding_days); $table->funding_end = $fundingEndDate->toSql(); } break; case 'date': $fundingEndValidator = new Prism\Validator\Date($fundingEnd); if (!$fundingEndValidator->isValid()) { throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_INVALID_DATE')); } $date = new JDate($fundingEnd); $table->funding_days = 0; $table->funding_end = $date->toSql(); break; default: $table->funding_days = 0; $table->funding_end = Prism\Constants::DATE_DEFAULT_SQL_DATE; break; } }
/** * 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; } }
/** * This method calculates start date and validate funding period. * * @param CrowdfundingTableProject $table * * @throws Exception */ protected function prepareTable(&$table) { // Calculate start and end date if the user publish a project for first time. $fundingStartDate = new Prism\Validator\Date($table->get('funding_start')); if (!$fundingStartDate->isValid()) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $fundingStart = new JDate('now', $app->get('offset')); $table->set('funding_start', $fundingStart->toSql()); // If funding type is 'days', calculate end date. if ($table->get('funding_days')) { $fundingStartDate = new Crowdfunding\Date($table->get('funding_start')); $endDate = $fundingStartDate->calculateEndDate($table->get('funding_days')); $table->set('funding_end', $endDate->toSql()); } } }