/** * 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)); } } } }
/** * Validate user data that comes from step "Funding". * * @param array $data * @param Joomla\Registry\Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * * @return array */ protected function validateStepFunding(&$data, &$params) { $result = array('success' => false, 'message' => ''); // Validate minimum and maximum amount. if ($this->params->get('validate_amount', 1)) { $goal = Joomla\Utilities\ArrayHelper::getValue($data, 'goal', 0, 'float'); $minAmount = (double) $params->get('project_amount_minimum', 100); $maxAmount = (double) $params->get('project_amount_maximum'); // Verify minimum amount if ($goal < $minAmount) { $result['message'] = JText::_('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_GOAL'); return $result; } // Verify maximum amount if ($maxAmount > 0 and $goal > $maxAmount) { $result['message'] = JText::_('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_GOAL'); return $result; } } // Validate funding duration - days or date. if ($this->params->get('validate_funding_duration', 1)) { $minDays = (int) $params->get('project_days_minimum', 15); $maxDays = (int) $params->get('project_days_maximum', 0); $fundingType = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_duration_type'); // Validate funding type 'days' if (strcmp('days', $fundingType) === 0) { $days = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_days', 0, 'integer'); if ($days < $minDays) { $result['message'] = JText::_('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_DAYS'); return $result; } if ($maxDays > 0 and $days > $maxDays) { $result['message'] = JText::_('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_DAYS'); return $result; } } else { // Validate funding type 'date' $fundingEndDate = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_end'); $dateValidator = new Prism\Validator\Date($fundingEndDate); if (!$dateValidator->isValid()) { $result['message'] = JText::_('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_DATE'); return $result; } } } // Validate funding duration when the projects is published and approved. if ($this->params->get('validate_funding_duration_approved', 1)) { // Get item and check it for active state ( published and approved ). $itemId = Joomla\Utilities\ArrayHelper::getValue($data, 'id'); $userId = JFactory::getUser()->get('id'); $item = $this->getItem($itemId, $userId); // Validate date if user want to edit date, while the project is published. if ($item->published and $item->approved) { $minDays = (int) $params->get('project_days_minimum', 15); $maxDays = (int) $params->get('project_days_maximum', 0); $fundingType = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_duration_type'); // Generate funding end date from days. if (strcmp('days', $fundingType) === 0) { // Get funding days. $days = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_days', 0, 'integer'); $fundingStartDate = new Crowdfunding\Date($item->funding_start); $endDate = $fundingStartDate->calculateEndDate($days); $fundingEndDate = $endDate->format('Y-m-d'); } else { // Get funding end date from request $fundingEndDate = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_end'); } // Validate the period. $dateValidator = new Crowdfunding\Validator\Project\Period($item->funding_start, $fundingEndDate, $minDays, $maxDays); if (!$dateValidator->isValid()) { $result['message'] = $maxDays > 0 ? JText::sprintf('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_ENDING_DATE_MIN_MAX_DAYS', $minDays, $maxDays) : JText::sprintf('PLG_CONTENT_CROWDFUNDINGVALIDATOR_ERROR_INVALID_ENDING_DATE_MIN_DAYS', $minDays); return $result; } } } // Validations completed successfully. $result = array('success' => true); return $result; }
/** * 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)); } } } }
/** * Method to change the published state of one or more records. * * @param array &$pks A list of the primary keys to change. * @param integer $value The value of the published state. * * @throws Exception * * @return boolean True on success. * * @since 12.2 */ public function publish(&$pks, $value = 0) { $table = $this->getTable(); /** @var $table CrowdfundingTableProject */ $pks = (array) $pks; // Access checks. foreach ($pks as $pk) { $table->reset(); if ($table->load($pk)) { if ($value == Prism\Constants::PUBLISHED) { // Publish a project // Validate funding period $fundingEndValidator = new Prism\Validator\Date($table->funding_end); if (!$table->funding_days and !$fundingEndValidator->isValid()) { throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_INVALID_DURATION_PERIOD")); } // Calculate starting date if the user publish a project for first time. $fundingStartValidator = new Prism\Validator\Date($table->funding_start); if (!$fundingStartValidator->isValid()) { $fundingStart = new JDate(); $table->funding_start = $fundingStart->toSql(); // If funding type is "days", calculate end date. if (!empty($table->funding_days)) { $fundingStartDate = new Crowdfunding\Date($table->funding_start); $fundingEndDate = $fundingStartDate->calculateEndDate($table->funding_days); $table->funding_end = $fundingEndDate->toSql(); } } // Validate the period if the funding type is days $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ $minDays = $params->get("project_days_minimum", 15); $maxDays = $params->get("project_days_maximum"); $fundingStartValidator = new Prism\Validator\Date($table->funding_start); if ($fundingStartValidator->isValid()) { $dateValidator = new Crowdfunding\Validator\Project\Period($table->funding_start, $table->funding_end, $minDays, $maxDays); if (!$dateValidator->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)); } } } $table->set("published", Prism\Constants::PUBLISHED); $table->store(); } else { // Set other states - unpublished, trash,... $table->publish(array($pk), $value); } } } // Trigger change state event $context = $this->option . '.' . $this->name; // Include the content plugins for the change of state event. JPluginHelper::importPlugin('content'); // Trigger the onContentChangeState event. $dispatcher = JEventDispatcher::getInstance(); $result = $dispatcher->trigger($this->event_change_state, array($context, $pks, $value)); if (in_array(false, $result, true)) { throw new Exception(JText::_("COM_CROWDFUNDING_ERROR_CHANGE_STATE")); } // Clear the component's cache $this->cleanCache(); }
/** * Publish or not an item. If state is going to be published, * we have to calculate end date. * * @param integer $itemId * @param integer $userId * @param integer $state * * @throws Exception */ public function saveState($itemId, $userId, $state) { $keys = array('id' => $itemId, 'user_id' => $userId); /** @var $row CrowdfundingTableProject */ $row = $this->getTable(); $row->load($keys); // Prepare data only if the user publish the project. if ((int) $state === Prism\Constants::PUBLISHED) { // Get number of transactions. $statistics = new Crowdfunding\Statistics\Project($this->getDbo(), $row->get('id')); $transactionsNumber = (int) $statistics->getTransactionsNumber(); // If it is not approve and there are no transactions, reset starting date. if ($transactionsNumber === 0 and (int) $row->get('approved') === Prism\Constants::NOT_APPROVED) { $row->set('funding_start', Prism\Constants::DATE_DEFAULT_SQL_DATE); } $this->prepareTable($row); // Validate dates $params = JComponentHelper::getParams('com_crowdfunding'); /** @var $params Joomla\Registry\Registry */ $minDays = (int) $params->get('project_days_minimum', 15); $maxDays = (int) $params->get('project_days_maximum'); // If there is an ending date, validate the period. $fundingEndDate = new Prism\Validator\Date($row->get('funding_end')); if ($fundingEndDate->isValid()) { $validatorPeriod = new Crowdfunding\Validator\Project\Period($row->get('funding_start'), $row->get('funding_end'), $minDays, $maxDays); if (!$validatorPeriod->isValid()) { if ($maxDays > 0) { 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)); } } } } $row->set('published', (int) $state); $row->store(); // Trigger the event $context = $this->option . '.project'; $pks = array($row->get('id')); // Include the content plugins for the change of state event. JPluginHelper::importPlugin('content'); // Trigger the onContentChangeState event. $dispatcher = JEventDispatcher::getInstance(); $results = $dispatcher->trigger('onContentChangeState', array($context, $pks, $state)); if (in_array(false, $results, true)) { throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_CHANGE_STATE')); } }