/** * Calculate days left. * * <code> * $fundingDays = 30; * $fundingStart = "01-06-2014"; * $fundingEnd = "30-06-2014"; * * $today = new Crowdfunding\Date(); * $daysLeft = $today->calculateDaysLeft($fundingDays, $fundingStart, $fundingEnd); * </code> * * @param int $fundingDays * @param string $fundingStart * @param string $fundingEnd * * @return int */ public function calculateDaysLeft($fundingDays, $fundingStart, $fundingEnd) { // Calculate days left $fundingDays = (int) abs($fundingDays); if ($fundingDays > 0) { $validatorDate = new Prism\Validator\Date($fundingStart); // Validate starting date. // If there is not starting date, return number of day. if (!$validatorDate->isValid()) { return (int) $fundingDays; } $endingDate = new \DateTime($fundingStart); $endingDate->modify('+' . (int) $fundingDays . ' days'); } else { $validatorDate = new Prism\Validator\Date($fundingEnd); // Validate end date. // If there is not valid end date, create one. if (!$validatorDate->isValid()) { $today = clone $this; $today->modify('+1 month'); $fundingEnd = $today->format('Y-m-d'); } $endingDate = new \DateTime($fundingEnd); } $today = clone $this; $interval = $today->diff($endingDate); $daysLeft = $interval->format('%r%a'); if ($daysLeft < 0) { $daysLeft = 0; } return (int) $daysLeft; }
/** * Calculate days left. * * <code> * $fundingDays = 30; * $fundingStart = "01-06-2014"; * $fundingEnd = "30-06-2014"; * * $today = new Crowdfunding\Date(); * $daysLeft = $today->calculateDaysLeft($fundingDays, $fundingStart, $fundingEnd); * </code> * * @param int $fundingDays * @param string $fundingStart * @param string $fundingEnd * * @return int */ public function calculateDaysLeft($fundingDays, $fundingStart, $fundingEnd) { // Calculate days left $today = clone $this; if (!empty($fundingDays)) { $validatorDate = new Prism\Validator\Date($fundingStart); // Validate starting date. // If there is not starting date, set number of day. if (!$validatorDate->isValid($fundingStart)) { return (int) $fundingDays; } $endingDate = new \DateTime($fundingStart); $endingDate->modify("+" . (int) $fundingDays . " days"); } else { $endingDate = new \DateTime($fundingEnd); } $interval = $today->diff($endingDate); $daysLeft = $interval->format("%r%a"); if ($daysLeft < 0) { $daysLeft = 0; } return abs($daysLeft); }
/** * Load project data from database. * * <code> * $projectId = 1; * * $project = new Crowdfunding\Project(\JFactory::getDbo()); * $project->load($projectId); * </code> * * @param array|int $keys * @param array $options * * @throws \UnexpectedValueException * @throws \InvalidArgumentException * @throws \RuntimeException */ public function load($keys, array $options = array()) { if (!$keys) { throw new \InvalidArgumentException(\JText::_('LIB_CROWDFUNDING_INVALID_KEYS')); } $query = $this->db->getQuery(true); $query->select('a.id, a.title, a.alias, a.short_desc, a.description, a.image, a.image_square, a.image_small, ' . 'a.location_id, a.goal, a.funded, a.funding_type, a.funding_start, a.funding_end, a.funding_days, ' . 'a.pitch_video, a.pitch_image, a.hits, a.created, a.featured, a.published, a.approved, ' . 'a.ordering, a.catid, a.type_id, a.user_id, ' . $query->concatenate(array('a.id', 'a.alias'), ':') . ' AS slug, ' . $query->concatenate(array('b.id', 'b.alias'), ':') . ' AS catslug')->from($this->db->quoteName('#__crowdf_projects', 'a'))->leftJoin($this->db->quoteName('#__categories', 'b') . ' ON a.catid = b.id'); if (is_array($keys)) { foreach ($keys as $key => $value) { $query->where($this->db->quoteName('a.' . $key) . ' = ' . $this->db->quote($value)); } } else { $query->where('a.id = ' . (int) $keys); } // Filter by state. $filter = ArrayHelper::getValue($options, 'state'); if ($filter !== null and is_numeric($filter)) { $query->where('a.published = ' . (int) $filter); } // Filter by approved state. $filter = ArrayHelper::getValue($options, 'approved'); if ($filter !== null and is_numeric($filter)) { $query->where('a.approved = ' . (int) $filter); } $this->db->setQuery($query); $result = (array) $this->db->loadAssoc(); $this->bind($result); // Calculate funded percent if (!$this->goal) { $this->fundedPercent = 0; } else { $this->fundedPercent = MathHelper::calculatePercentage($this->funded, $this->goal, 0); } // Calculate end date if ($this->funding_days > 0) { $fundingStartDateValidator = new Prism\Validator\Date($this->funding_start); if (!$fundingStartDateValidator->isValid()) { $this->funding_end = Prism\Constants::DATE_DEFAULT_SQL_DATE; } else { $fundingStartDate = new Date($this->funding_start); $fundingEndDate = $fundingStartDate->calculateEndDate($this->funding_days); $this->funding_end = $fundingEndDate->format('Y-m-d'); } } // Calculate days left $today = new Date(); $this->daysLeft = $today->calculateDaysLeft($this->funding_days, $this->funding_start, $this->funding_end); }
/** * Calculate a project amount for full period of the campaign. * * <code> * $projectId = 1; * * $statistics = new CrowdfundingStatisticsProject(\JFactory::getDbo(), $projectId); * $amount = $statistics->getFullPeriodAmounts(); * </code> * * @return int */ public function getFullPeriodAmounts() { $query = $this->db->getQuery(true); $query->select("a.funding_start, a.funding_end")->from($this->db->quoteName("#__crowdf_projects", "a"))->where("a.id = " . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); // Validate dates $fundingStartDate = new Prism\Validator\Date($result->funding_start); $fundingEndDate = new Prism\Validator\Date($result->funding_end); if (!$fundingStartDate->isValid() or !$fundingEndDate->isValid()) { return array(); } $dataset = array(); $date = new Prism\Date(); $timezone = $date->getTimezone(); $date1 = new \JDate($result->funding_start); $date2 = new \JDate($result->funding_end); $period = $date->getDaysPeriod($date1, $date2); $query = $this->db->getQuery(true); $query->select("a.txn_date as date, SUM(a.txn_amount) as amount")->from($this->db->quoteName("#__crowdf_transactions", "a"))->where("a.project_id = " . (int) $this->id)->group("DATE(a.txn_date)"); $this->db->setQuery($query); $results = (array) $this->db->loadAssocList(); // Prepare data $data = array(); foreach ($results as $result) { $date = new \JDate($result["date"]); $index = $date->format("d.m"); $data[$index] = $result; } /** @var $day \JDate */ foreach ($period as $day) { $day->setTimezone($timezone); $dayMonth = $day->format("d.m"); if (isset($data[$dayMonth])) { $amount = $data[$dayMonth]["amount"]; } else { $amount = 0; } $dataset[] = array("date" => $dayMonth, "amount" => $amount); } return $dataset; }
/** * Send emails to the administrator, project owner and the user who have made a donation. * * @param \stdClass $paymentResult * @param Registry $params * * @throws \InvalidArgumentException * @return void */ protected function sendMails($paymentResult, $params) { if (!\JComponentHelper::isInstalled('com_emailtemplates')) { \JLog::add(\JText::_('LIB_CROWDFUNDING_EMAIL_TEMPLATES_INSTALLATION'), \JLog::WARNING, 'com_crowdfunding'); return; } $transaction = $paymentResult->transaction; /** @var Crowdfunding\Transaction\Transaction $transaction */ $project = $paymentResult->project; /** @var Crowdfunding\Project $project */ $reward = $paymentResult->reward; /** @var Crowdfunding\Reward $reward */ // Get website $uri = \JUri::getInstance(); $website = $uri->toString(array('scheme', 'host')); $emailMode = $this->params->get('email_mode', 'plain'); $moneyHash = Prism\Utilities\StringHelper::generateMd5Hash(Crowdfunding\Constants::CONTAINER_FORMATTER_MONEY, $params->get('project_currency')); $money = $this->container->get($moneyHash); /** @var Prism\Money\Money $money */ // Prepare data for parsing. $data = array('site_name' => $this->app->get('sitename'), 'site_url' => \JUri::root(), 'item_title' => $project->getTitle(), 'item_url' => $website . \JRoute::_(\CrowdfundingHelperRoute::getDetailsRoute($project->getSlug(), $project->getCatSlug())), 'amount' => $money->setAmount($transaction->getAmount())->formatCurrency(), 'transaction_id' => $transaction->getTransactionId(), 'reward_title' => '', 'delivery_date' => '', 'payer_name' => '', 'payer_email' => ''); // Prepare data about payer if he is NOT anonymous ( is registered user with profile ). if ((int) $transaction->getInvestorId() > 0) { $investor = \JFactory::getUser($transaction->getInvestorId()); $data['payer_email'] = $investor->get('email'); $data['payer_name'] = $investor->get('name'); } // Set reward data. if (is_object($reward)) { $data['reward_title'] = $reward->getTitle(); $dateValidator = new Prism\Validator\Date($reward->getDeliveryDate()); if ($dateValidator->isValid()) { $date = new \JDate($reward->getDeliveryDate()); $data['delivery_date'] = $date->format($this->params->get('date_format_views', \JText::_('DATE_FORMAT_LC3'))); } } // Send mail to the administrator $emailId = (int) $this->params->get('admin_mail_id', 0); if ($emailId > 0) { $email = new Emailtemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($this->app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($this->app->get('mailfrom')); } $recipientId = (int) $params->get('administrator_id', 0); if ($recipientId > 0) { $recipient = \JFactory::getUser($recipientId); $recipientName = $recipient->get('name'); $recipientMail = $recipient->get('email'); } else { $recipientName = $this->app->get('fromname'); $recipientMail = $this->app->get('mailfrom'); } // Prepare data for parsing $data['sender_name'] = $email->getSenderName(); $data['sender_email'] = $email->getSenderEmail(); $data['recipient_name'] = $recipientName; $data['recipient_email'] = $recipientMail; // DEBUG JDEBUG ? $this->log->add(\JText::_($this->textPrefix . '_DEBUG_SEND_MAIL_ADMINISTRATOR'), $this->debugType, $data) : null; $email->parse($data); $subject = $email->getSubject(); $body = $email->getBody($emailMode); $mailer = \JFactory::getMailer(); if (strcmp('html', $emailMode) === 0) { // Send as HTML message $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML); } else { // Send as plain text. $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN); } // Check for an error. if ($return !== true) { $this->log->add(\JText::_($this->textPrefix . '_ERROR_MAIL_SENDING_ADMIN'), $this->errorType, $mailer->ErrorInfo); } } // Send mail to project owner. $emailId = (int) $this->params->get('creator_mail_id', 0); if ($emailId > 0) { $email = new Emailtemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($this->app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($this->app->get('mailfrom')); } $user = \JFactory::getUser($transaction->getReceiverId()); $recipientName = $user->get('name'); $recipientMail = $user->get('email'); // Prepare data for parsing $data['sender_name'] = $email->getSenderName(); $data['sender_email'] = $email->getSenderEmail(); $data['recipient_name'] = $recipientName; $data['recipient_email'] = $recipientMail; // DEBUG JDEBUG ? $this->log->add(\JText::_($this->textPrefix . '_DEBUG_SEND_MAIL_PROJECT_OWNER'), $this->debugType, $data) : null; $email->parse($data); $subject = $email->getSubject(); $body = $email->getBody($emailMode); $mailer = \JFactory::getMailer(); if (strcmp('html', $emailMode) === 0) { // Send as HTML message $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML); } else { // Send as plain text. $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN); } // Check for an error. if ($return !== true) { $this->log->add(\JText::_($this->textPrefix . '_ERROR_MAIL_SENDING_PROJECT_OWNER'), $this->errorType, $mailer->ErrorInfo); } } // Send mail to backer. $emailId = (int) $this->params->get('user_mail_id', 0); if ($emailId > 0 and (int) $transaction->getInvestorId() > 0) { $email = new Emailtemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($this->app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($this->app->get('mailfrom')); } $user = \JFactory::getUser($transaction->getInvestorId()); $recipientName = $user->get('name'); $recipientMail = $user->get('email'); // Prepare data for parsing $data['sender_name'] = $email->getSenderName(); $data['sender_email'] = $email->getSenderEmail(); $data['recipient_name'] = $recipientName; $data['recipient_email'] = $recipientMail; // DEBUG JDEBUG ? $this->log->add(\JText::_($this->textPrefix . '_DEBUG_SEND_MAIL_BACKER'), $this->debugType, $data) : null; $email->parse($data); $subject = $email->getSubject(); $body = $email->getBody($emailMode); $mailer = \JFactory::getMailer(); if (strcmp('html', $emailMode) === 0) { // Send as HTML message $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML); } else { // Send as plain text. $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN); } // Check for an error. if ($return !== true) { $this->log->add(\JText::_($this->textPrefix . '_ERROR_MAIL_SENDING_PROJECT_OWNER'), $this->errorType, $mailer->ErrorInfo); } } }
/** * Load project data from database. * * <code> * $projectId = 1; * * $project = new Crowdfunding\Project(\JFactory::getDbo()); * $project->load($projectId); * </code> * * @param int $keys * @param array $options * * @throws \UnexpectedValueException */ public function load($keys, $options = array()) { if (!$keys) { throw new \UnexpectedValueException(\JText::_("LIB_CROWDFUNDING_INVALID_PROJECT_ID")); } $query = $this->db->getQuery(true); $query->select("a.id, a.title, a.alias, a.short_desc, a.description, a.image, a.image_square, a.image_small, " . "a.location_id, a.goal, a.funded, a.funding_type, a.funding_start, a.funding_end, a.funding_days, " . "a.pitch_video, a.pitch_image, a.hits, a.created, a.featured, a.published, a.approved, " . "a.ordering, a.catid, a.type_id, a.user_id, " . $query->concatenate(array("a.id", "a.alias"), ":") . " AS slug, " . $query->concatenate(array("b.id", "b.alias"), ":") . " AS catslug")->from($this->db->quoteName("#__crowdf_projects", "a"))->leftJoin($this->db->quoteName("#__categories", "b") . " ON a.catid = b.id")->where("a.id = " . (int) $keys); $this->db->setQuery($query); $result = $this->db->loadAssoc(); if (!$result) { $result = array(); } $this->bind($result); // Calculate funded percent if (!$this->goal) { $this->fundedPercent = 0; } else { $math = new Prism\Math(); $math->calculatePercentage($this->funded, $this->goal, 0); $this->fundedPercent = (string) $math; } // Calculate end date if (!empty($this->funding_days)) { $fundingStartDateValidator = new Prism\Validator\Date($this->funding_start); if (!$fundingStartDateValidator->isValid()) { $this->funding_end = "0000-00-00"; } else { $fundingStartDate = new Date($this->funding_start); $fundingEndDate = $fundingStartDate->calculateEndDate($this->funding_days); $this->funding_end = $fundingEndDate->format("Y-m-d"); } } // Calculate days left $today = new Date(); $this->daysLeft = $today->calculateDaysLeft($this->funding_days, $this->funding_start, $this->funding_end); }
/** * Load project data from database. * * <code> * $projectId = 1; * * $project = new Crowdfunding\Project(\JFactory::getDbo()); * $project->load($projectId); * </code> * * @param int $keys * @param array $options * * @throws \UnexpectedValueException */ public function load($keys, $options = array()) { if (!$keys) { throw new \UnexpectedValueException(\JText::_('LIB_CROWDFUNDING_INVALID_PROJECT_ID')); } $query = $this->db->getQuery(true); $query->select('a.id, a.title, a.alias, a.short_desc, a.description, a.image, a.image_square, a.image_small, ' . 'a.location_id, a.goal, a.funded, a.funding_type, a.funding_start, a.funding_end, a.funding_days, ' . 'a.pitch_video, a.pitch_image, a.hits, a.created, a.featured, a.published, a.approved, ' . 'a.ordering, a.catid, a.type_id, a.user_id, ' . $query->concatenate(array('a.id', 'a.alias'), ':') . ' AS slug, ' . $query->concatenate(array('b.id', 'b.alias'), ':') . ' AS catslug')->from($this->db->quoteName('#__crowdf_projects', 'a'))->leftJoin($this->db->quoteName('#__categories', 'b') . ' ON a.catid = b.id')->where('a.id = ' . (int) $keys); $this->db->setQuery($query); $result = (array) $this->db->loadAssoc(); $this->bind($result); // Calculate funded percent if (!$this->goal) { $this->fundedPercent = 0; } else { $this->fundedPercent = MathHelper::calculatePercentage($this->funded, $this->goal, 0); } // Calculate end date if ($this->funding_days > 0) { $fundingStartDateValidator = new Validator\Date($this->funding_start); if (!$fundingStartDateValidator->isValid()) { $this->funding_end = '0000-00-00'; } else { $fundingStartDate = new Date($this->funding_start); $fundingEndDate = $fundingStartDate->calculateEndDate($this->funding_days); $this->funding_end = $fundingEndDate->format('Y-m-d'); } } // Calculate days left $today = new Date(); $this->daysLeft = $today->calculateDaysLeft($this->funding_days, $this->funding_start, $this->funding_end); }