/** * Create an object or return existing one. * * <code> * $currencyId = 1; * * $currency = Crowdfunding\Currency::getInstance(\JFactory::getDbo(), $currencyId); * </code> * * @param \JDatabaseDriver $db * @param int $id * * @return null|self */ public static function getInstance(\JDatabaseDriver $db, $id) { if (!isset(self::$instances[$id])) { $item = new Currency($db); $item->load($id); self::$instances[$id] = $item; } return self::$instances[$id]; }
/** * Create an object or return existing one. * * <code> * $currencyId = 1; * * $currency = Crowdfunding\Currency::getInstance(\JFactory::getDbo(), $currencyId); * </code> * * @param \JDatabaseDriver $db * @param int $id * @param array $options * * @return null|self */ public static function getInstance(\JDatabaseDriver $db, $id, array $options = array()) { if (!array_key_exists($id, self::$instances)) { $item = new Currency($db); $item->load($id, $options); self::$instances[$id] = $item; } return self::$instances[$id]; }
/** * Return currency object. * * <code> * $currencyId = 1; * $currency = $this->getCurrency($currencyId); * </code> * * @param int $currencyId * * @throws \InvalidArgumentException * @throws \RuntimeException * * @return Currency */ protected function getCurrency($currencyId) { if (!$currencyId) { throw new \InvalidArgumentException('It is missing currency ID'); } $currency = new Currency(\JFactory::getDbo()); $currency->load($currencyId); return $currency; }
/** * Prepare and return currency object. * * <code> * $this->prepareCurrency($container, $params); * </code> * * @param Container $container * @param Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException */ protected function prepareCurrency($container, $params) { $currencyId = $params->get('project_currency'); $currencyHash = StringHelper::generateMd5Hash(Constants::CONTAINER_CURRENCY, $currencyId); // Get the currency from the container. if (!$container->exists($currencyHash)) { $currency = new Currency(\JFactory::getDbo()); $currency->load($currencyId); $container->set($currencyHash, $currency); } }
/** * This method returns an amount as currency, with a symbol and currency code. * * <code> * // Create currency object. * $currencyId = 1; * $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId); * * // Create amount object. * $options = new Joomla\Registry\Registry(); * $options->set("intl", true); * $options->set("locale", "en_GB"); * $options->set("format", "2/,/."); * * $amount = 1500.25; * * $amount = new Crowdfunding\Amount($amount, $options); * $amount->setCurrency($currency); * * // Return $1,500.25 or 1,500.25USD. * echo $amount->formatCurrency(); * </code> * * @return string */ public function formatCurrency() { $intl = (bool) $this->options->get('intl', false); $fractionDigits = abs($this->options->get('fraction_digits', 2)); $format = $this->options->get('format'); $amount = $this->value; // Use number_format. if (!$intl and \JString::strlen($format) > 0) { $value = $this->formatNumber($this->value); if (!$this->currency->getSymbol()) { // Symbol $amount = $value . $this->currency->getCode(); } else { // Code if (0 === $this->currency->getPosition()) { // Symbol at beginning. $amount = $this->currency->getSymbol() . $value; } else { // Symbol at end. $amount = $value . ' ' . $this->currency->getSymbol(); } } } // Use PHP Intl library. if ($intl and extension_loaded('intl')) { // Generate currency string using PHP NumberFormatter ( Internationalization Functions ) $locale = $this->options->get('locale'); // Get current locale code. if (!$locale) { $lang = \JFactory::getLanguage(); $locale = str_replace('-', '_', $lang->getTag()); } $numberFormat = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $numberFormat->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $this->currency->getCode()); $numberFormat->setAttribute(\NumberFormatter::FRACTION_DIGITS, $fractionDigits); $amount = $numberFormat->formatCurrency($this->value, $this->currency->getCode()); } return $amount; }
/** * Send emails to the administrator, project owner and the user who have made a donation. * * @param object $project * @param object $transaction * @param Registry $params */ protected function sendMails($project, $transaction, $params) { $app = \JFactory::getApplication(); /** @var $app \JApplicationSite */ // Get website $uri = \JUri::getInstance(); $website = $uri->toString(array("scheme", "host")); $emailMode = $this->params->get("email_mode", "plain"); $componentParams = \JComponentHelper::getParams("com_crowdfunding"); $currency = Crowdfunding\Currency::getInstance(\JFactory::getDbo(), $componentParams->get("project_currency")); $amount = new Crowdfunding\Amount($componentParams); $amount->setCurrency($currency); // Prepare data for parsing. $data = array("site_name" => $app->get("sitename"), "site_url" => \JUri::root(), "item_title" => $project->title, "item_url" => $website . \JRoute::_(\CrowdfundingHelperRoute::getDetailsRoute($project->slug, $project->catslug)), "amount" => $amount->setValue($transaction->txn_amount)->formatCurrency(), "transaction_id" => $transaction->txn_id); // Prepare data about payer if he is NOT anonymous ( is registered user with profile ). if (!empty($transaction->investor_id)) { $investor = \JFactory::getUser($transaction->investor_id); $data["payer_email"] = $investor->get("email"); $data["payer_name"] = $investor->get("name"); } // Send mail to the administrator $emailId = $this->params->get("admin_mail_id", 0); if (!empty($emailId)) { $email = new EmailTemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($app->get("fromname")); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get("mailfrom")); } // Prepare recipient data. $componentParams = \JComponentHelper::getParams("com_crowdfunding"); /** @var $componentParams Registry */ $recipientId = $componentParams->get("administrator_id"); if (!empty($recipientId)) { $recipient = \JFactory::getUser($recipientId); $recipientName = $recipient->get("name"); $recipientMail = $recipient->get("email"); } else { $recipientName = $app->get("fromname"); $recipientMail = $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; $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->debugType, $mailer->ErrorInfo); } } // Send mail to project owner. $emailId = $this->params->get("creator_mail_id", 0); if (!empty($emailId)) { $email = new EmailTemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($app->get("fromname")); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get("mailfrom")); } $user = \JFactory::getUser($transaction->receiver_id); $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; $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->debugType, $mailer->ErrorInfo); } } // Send mail to backer. $emailId = $this->params->get("user_mail_id", 0); if (!empty($emailId) and !empty($transaction->investor_id)) { $email = new EmailTemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($app->get("fromname")); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get("mailfrom")); } $user = \JFactory::getUser($transaction->investor_id); $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; $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->debugType, $mailer->ErrorInfo); } } }
/** * Send emails to the administrator, project owner and the user who have made a donation. * * @param \stdClass $project * @param \stdClass $transaction * @param Registry $params * @param \stdClass $reward */ protected function sendMails(&$project, &$transaction, &$params, &$reward) { $app = \JFactory::getApplication(); /** @var $app \JApplicationSite */ // Get website $uri = \JUri::getInstance(); $website = $uri->toString(array('scheme', 'host')); $emailMode = $this->params->get('email_mode', 'plain'); $componentParams = \JComponentHelper::getParams('com_crowdfunding'); $currency = Crowdfunding\Currency::getInstance(\JFactory::getDbo(), $componentParams->get('project_currency')); $amount = new Crowdfunding\Amount($componentParams); $amount->setCurrency($currency); // Prepare data for parsing. $data = array('site_name' => $app->get('sitename'), 'site_url' => \JUri::root(), 'item_title' => $project->title, 'item_url' => $website . \JRoute::_(\CrowdfundingHelperRoute::getDetailsRoute($project->slug, $project->catslug)), 'amount' => $amount->setValue($transaction->txn_amount)->formatCurrency(), 'transaction_id' => $transaction->txn_id, 'reward_title' => '', 'delivery_date' => '', 'payer_name' => '', 'payer_email' => ''); // Set reward data. if (is_object($reward)) { $data['reward_title'] = $reward->title; if ($reward->delivery !== '0000-00-00') { $date = new \JDate($reward->delivery); $data['delivery_date'] = $date->format('d F Y'); } } // Prepare data about payer if he is NOT anonymous ( is registered user with profile ). if ((int) $transaction->investor_id > 0) { $investor = \JFactory::getUser($transaction->investor_id); $data['payer_email'] = $investor->get('email'); $data['payer_name'] = $investor->get('name'); } // 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($app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get('mailfrom')); } // Prepare recipient data. $componentParams = \JComponentHelper::getParams('com_crowdfunding'); /** @var $componentParams Registry */ $recipientId = (int) $componentParams->get('administrator_id', 0); if ($recipientId > 0) { $recipient = \JFactory::getUser($recipientId); $recipientName = $recipient->get('name'); $recipientMail = $recipient->get('email'); } else { $recipientName = $app->get('fromname'); $recipientMail = $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; $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->debugType, $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($app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get('mailfrom')); } $user = \JFactory::getUser($transaction->receiver_id); $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; $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->debugType, $mailer->ErrorInfo); } } // Send mail to backer. $emailId = (int) $this->params->get('user_mail_id', 0); if ($emailId > 0 and (int) $transaction->investor_id > 0) { $email = new EmailTemplates\Email(); $email->setDb(\JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get('mailfrom')); } $user = \JFactory::getUser($transaction->investor_id); $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; $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->debugType, $mailer->ErrorInfo); } } }
/** * Calculate three types of project amount - goal, funded amount and remaining amount. * * <code> * $projectId = 1; * * $statistics = new CrowdfundingStatisticsProject(\JFactory::getDbo(), $projectId); * $data = $statistics->getFundedAmount(); * </code> * * @return array * * # Example result: * array( * "goal" = array("label" => "Goal", "amount" => 1000), * "funded" = array("label" => "Funded", "amount" => 100), * "remaining" = array("label" => "Remaining", "amount" => 900) * ) */ public function getFundedAmount() { $data = array(); $query = $this->db->getQuery(true); $query->select("a.funded, a.goal")->from($this->db->quoteName("#__crowdf_projects", "a"))->where("a.id = " . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); /** @var $result object */ if (empty($result->funded) or empty($result->goal)) { return $data; } // Get currency $params = \JComponentHelper::getParams("com_crowdfunding"); /** @var $params Registry */ $currencyId = $params->get("project_currency"); $currency = Currency::getInstance(\JFactory::getDbo(), $currencyId, $params); $amount = new Amount(); $amount->setCurrency($currency); $data["goal"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_GOAL_S", $amount->setValue($result->goal)->formatCurrency()), "amount" => (double) $result->goal); $data["funded"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_FUNDED_S", $amount->setValue($result->funded)->formatCurrency()), "amount" => (double) $result->funded); $remaining = (double) ($result->goal - $result->funded); if ($remaining < 0) { $remaining = 0; } $data["remaining"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_REMAINING_S", $amount->setValue($remaining)->formatCurrency()), "amount" => $remaining); return $data; }
/** * Calculate three types of project amount - goal, funded amount and remaining amount. * * <code> * $projectId = 1; * * $statistics = new Crowdfunding\Statistics\Project(\JFactory::getDbo(), $projectId); * $data = $statistics->getFundedAmount(); * </code> * * @return array * * # Example result: * array( * "goal" = array("label" => "Goal", "amount" => 1000), * "funded" = array("label" => "Funded", "amount" => 100), * "remaining" = array("label" => "Remaining", "amount" => 900) * ) */ public function getFundedAmount() { $data = array(); $query = $this->db->getQuery(true); $query->select('a.funded, a.goal')->from($this->db->quoteName('#__crowdf_projects', 'a'))->where('a.id = ' . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); /** @var $result \stdClass */ if ($result->funded === null or $result->goal === null) { return $data; } // Get currency $params = \JComponentHelper::getParams('com_crowdfunding'); /** @var $params Registry */ $currencyId = $params->get('project_currency'); $currency = Currency::getInstance(\JFactory::getDbo(), $currencyId, $params); $amount = new Amount(); $amount->setCurrency($currency); $data['goal'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_GOAL_S', $amount->setValue($result->goal)->formatCurrency()), 'amount' => (double) $result->goal); $data['funded'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_FUNDED_S', $amount->setValue($result->funded)->formatCurrency()), 'amount' => (double) $result->funded); $remaining = (double) ($result->goal - $result->funded); if ($remaining < 0) { $remaining = 0; } $data['remaining'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_REMAINING_S', $amount->setValue($remaining)->formatCurrency()), 'amount' => $remaining); return $data; }
/** * Create a currency object and return it. * * <code> * $options = array( * "ids" => array(1,2,3,4,5), * "codes" => array("USD", "GBP") * ); * * $currencies = new Crowdfunding\Currencies(\JFactory::getDbo()); * $currencies->load($options); * * $currencyId = 1; * $currency = $currencies->getCurrency($currencyId); * </code> * * @param int|string $id Currency ID or Currency code. * * @throws \UnexpectedValueException * * @return Currency */ public function getCurrency($id) { if (!$id) { throw new \UnexpectedValueException(\JText::_('LIB_CROWDFUNDING_INVALID_CURRENCY_ID')); } $currency = null; foreach ($this->items as $item) { if (is_numeric($id) and (int) $id === (int) $item['id']) { $currency = new Currency($this->db); $currency->bind($this->items[$id]); break; } elseif (strcmp($id, $item['code']) === 0) { $currency = new Currency($this->db); $currency->bind($item); break; } } return $currency; }
/** * Return the currencies as array with objects. * * <code> * $options = array( * "ids" => array(1,2,3,4,5), * "codes" => array("USD", "GBP") * ); * * $currencies = new Crowdfunding\Currencies(\JFactory::getDbo()); * $currencies->load($options); * * $items = $currencies->getCurrencies(); * </code> * * @return array */ public function getCurrencies() { $results = array(); $i = 0; foreach ($this->items as $item) { $currency = new Currency($this->db); $currency->bind($item); $results[$i] = $currency; $i++; } return $results; }
/** * Create a currency object and return it. * * <code> * $options = array( * "ids" => array(1,2,3,4,5), * "codes" => array("USD", "GBP") * ); * * $currencies = new Crowdfunding\Currencies(\JFactory::getDbo()); * $currencies->load($options); * * $currencyId = 1; * $currency = $currencies->getCurrency($currencyId); * </code> * * @param int $id * * @throws \UnexpectedValueException * * @return null|Currency */ public function getCurrency($id) { if (!$id) { throw new \UnexpectedValueException(\JText::_("LIB_CROWDFUNDING_INVALID_CURRENCY_ID")); } $currency = null; foreach ($this->items as $item) { if ($id == $item["id"]) { $currency = new Currency(); $currency->bind($item); break; } } return $currency; }