/** * Subscribe to a Premium offer. * * @return \Cake\Network\Response */ public function subscribe() { $this->loadComponent('Transaction'); $this->loadModel('PremiumOffers'); $this->loadModel('PremiumDiscounts'); $offer = $this->PremiumOffers->find('offerByPeriod', ['period' => $this->request->data['period']]); if (!$offer) { $this->Flash->error(__("This offer does not exist.")); return $this->redirect(['action' => 'index']); } //Check the discount code. $discountPercentage = null; if (!empty($this->request->data['discount'])) { $discount = $this->PremiumDiscounts->find('discountByCodeAndOffer', ['code' => $this->request->data['discount'], 'offer_id' => $offer->id]); if (is_null($discount) || !$this->PremiumDiscounts->isDiscountValid($discount)) { $this->Flash->error(__("Your discount code isn't valid or has already been used.")); return $this->redirect(['action' => 'index']); } else { $discountPercentage = $discount->discount; } } $price = Number::format($offer->price, ['locale' => 'en_US']); $tax = Number::format($offer->tax, ['locale' => 'en_US']); $custom = ['user_id' => $this->request->session()->read('Auth.User.id'), 'offer_id' => $offer->id, 'period' => $offer->period, 'discount_id' => isset($discount) ? $discount->id : null]; $paypalUrl = $this->Transaction->getPaypalUrl($price, $tax, __n('Premium {0} month', 'Premium {0} months', $offer->period, $offer->period), http_build_query($custom), $discountPercentage); if (!$paypalUrl) { $this->Flash->error(__("Unable to get the Paypal URL, please contact an administrator or try again later.")); return $this->redirect(['action' => 'index']); } $this->redirect($paypalUrl); }
/** * Re-count the number of user and find the latest user and write it in the Cache. * * @param \Cake\Event\Event $event The event that was fired. * * @return array|false */ public function newUserStats(Event $event) { $this->Users = TableRegistry::get('Users'); $totalUsers = $this->Users->find()->count(); $totalUsers = Number::format($totalUsers); $lastRegistered = $this->Users->find('short')->order(['Users.created' => 'DESC'])->first(); $data = []; $data['TotalUsers'] = $totalUsers; $data['LastRegistered'] = $lastRegistered; if ($this->_writeCache($data, 'Users')) { return $data; } return false; }
/** * Index page. * * @return void */ public function home() { $this->loadModel('Users'); $this->loadModel('PremiumTransactions'); $usersCount = Number::format($this->Users->find()->where(['end_subscription >' => new Time()])->count()); $premiumTransactions = $this->PremiumTransactions->find()->select(['price'])->hydrate(false)->toArray(); $amountTotal = array_sum(Hash::extract($premiumTransactions, '{n}.price')); $registeredDiscounts = $this->PremiumTransactions->find()->where(function ($exp) { return $exp->isNotNull('premium_discount_id'); })->count(); $discounts = $this->PremiumTransactions->find()->contain(['PremiumDiscounts', 'PremiumOffers'])->where(function ($exp) { return $exp->isNotNull('premium_discount_id'); })->toArray(); $discountAmountTotal = []; foreach ($discounts as $discount) { array_push($discountAmountTotal, $discount->discount); } $discountAmountTotal = array_sum($discountAmountTotal); $this->set(compact('usersCount', 'amountTotal', 'registeredDiscounts', 'discountAmountTotal')); }
/** * Formats a number into the correct locale format * * Options: * * - `places` - Minimum number or decimals to use, e.g 0 * - `precision` - Maximum Number of decimal places to use, e.g. 2 * - `locale` - The locale name to use for formatting the number, e.g. fr_FR * - `before` - The string to place before whole numbers, e.g. '[' * - `after` - The string to place after decimal numbers, e.g. ']' * - `escape` - Whether or not to escape html in resulting string * * @param float $number A floating point number. * @param array $options An array with options. * @return string Formatted number * @link http://book.cakephp.org/3.0/en/views/helpers/number.html#formatting-numbers */ public function format($number, array $options = []) { $formatted = $this->_engine->format($number, $options); $options += ['escape' => true]; return $options['escape'] ? h($formatted) : $formatted; }
protected function float($field, $options = []) { return Number::format($this->helper->entity->{$field}, $this->helper->CrudData->attributes($field, 'number')); }
/** * Check if the discount is valid. * * @param array $custom The custom data passed to Paypal. * @param float $mcGross Price paid by the buyer. * @param float $tax The tax added to the price. * @param float $discount The discount applied to the price without taxes. * * @return bool */ protected function _isDiscountValid($custom, $mcGross, $tax, $discount) { $discount = Number::format($discount, ['precision' => 2, 'locale' => 'en_US']); if ($discount == 0.0 && !isset($custom['discount_id'])) { return true; } $this->_controller->loadModel('PremiumDiscounts'); $checkDiscount = $this->_controller->PremiumDiscounts->find('discountByIdAndOffer', ['id' => $custom['discount_id'], 'offer_id' => $custom['offer_id']]); if (!$checkDiscount) { return false; } $total = $mcGross - $tax + $discount; $offerDiscount = Number::format(round($checkDiscount->discount / 100, 2) * $total, ['precision' => 2, 'locale' => 'en_US']); if ($offerDiscount != $discount) { Log::error(__('The discount offer {0} does not match with the Paypal discount {1}.', $offerDiscount, $discount), 'paypal'); return false; } //Update the discount. $this->_updateDiscount($checkDiscount); return true; }
/** * Format * * Additional options * - signed * - positive * * @param $number * @param array $options * @return string */ public static function format($number, array $options = []) { $defaults = ['positive' => '+', 'signed' => false]; $options += $defaults; $sign = ''; if ($number > 0 && !empty($options['signed'])) { $sign = '+'; } if (isset($options['signed'])) { unset($options['signed']); } return $sign . parent::format($number, $options); }
/** * Index page. * * @return void */ public function home() { if (Configure::read('Analytics.enabled') === true) { $httpAdapter = new CurlHttpAdapter(); $client = new Client(Configure::read('Analytics.client_id'), Configure::read('Analytics.private_key'), $httpAdapter); $service = new Service($client); $statistics = Cache::remember('statistics', function () use($service) { $statistics = new Query(Configure::read('Analytics.profile_id')); $statistics->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setMetrics(array('ga:visits', 'ga:visitors', 'ga:pageviews', 'ga:pageviewsPerVisit', 'ga:avgtimeOnSite', 'ga:visitBounceRate', 'ga:percentNewVisits')); return $service->query($statistics); }, 'analytics'); $browsers = Cache::remember('browsers', function () use($service) { $browsers = new Query(Configure::read('Analytics.profile_id')); $browsers->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setDimensions(array('ga:browser'))->setMetrics(array('ga:pageviews'))->setSorts(array('ga:pageviews'))->setFilters(array('ga:browser==Chrome,ga:browser==Firefox,ga:browser==Internet Explorer,ga:browser==Safari,ga:browser==Opera')); return $service->query($browsers); }, 'analytics'); $continents = Cache::remember('continents', function () use($service) { $continentsRows = new Query(Configure::read('Analytics.profile_id')); $continentsRows->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setDimensions(array('ga:continent'))->setMetrics(array('ga:visitors'))->setSorts(array('ga:visitors'))->setFilters(array('ga:continent==Africa,ga:continent==Americas,ga:continent==Asia,ga:continent==Europe,ga:continent==Oceania')); $continentsRows = $service->query($continentsRows); $color = new Color("1abc9c"); $light = 1; $continents = []; foreach (array_reverse($continentsRows->getRows()) as $continentRow) { $continent = []; $continent['label'] = $continentRow[0]; $continent['data'] = $continentRow[1]; $continent['color'] = '#' . $color->lighten($light); array_push($continents, $continent); $light += 10; } return $continents; }, 'analytics'); $graphVisitors = Cache::remember('graphVisitors', function () use($service) { $graphVisitors = new Query(Configure::read('Analytics.profile_id')); $graphVisitors->setStartDate(new \DateTime('-7 days'))->setEndDate(new \DateTime())->setDimensions(array('ga:date'))->setMetrics(array('ga:visits', 'ga:pageviews'))->setSorts(array('ga:date')); return $service->query($graphVisitors); }, 'analytics'); $this->set(compact('statistics', 'browsers', 'continents', 'graphVisitors')); } $this->loadModel('Users'); //UsersGraph $usersGraphCount = $this->Users->find('all')->select(['date' => 'DATE_FORMAT(created,\'%d-%m-%Y\')', 'count' => 'COUNT(id)'])->group('DATE(created)')->order(['date' => 'desc'])->where(['UNIX_TIMESTAMP(DATE(created)) >' => (new \DateTime('-8 days'))->getTimestamp()])->toArray(); $usersGraph = array(); //Fill the new array with the date of the 8 past days and give them the value 0. for ($i = 0; $i < 8; $i++) { $date = new \DateTime("{$i} days ago"); $usersGraph[$date->format('d-m-Y')] = 0; } //Foreach value that we got in the database, parse the array by the key date, //and if the key exist, attribute the new value. foreach ($usersGraphCount as $user) { $usersGraph[$user->date] = intval($user->count); } $usersGraph = array_reverse($usersGraph); $usersCount = Number::format($this->Users->find()->count()); $this->loadModel('BlogArticles'); $articlesCount = Number::format($this->BlogArticles->find()->count()); $this->loadModel('BlogArticlesComments'); $commentsCount = Number::format($this->BlogArticlesComments->find()->count()); $this->loadModel('BlogCategories'); $categoriesCount = Number::format($this->BlogCategories->find()->count()); $this->set(compact('usersCount', 'articlesCount', 'commentsCount', 'categoriesCount', 'usersGraph')); }
/** * Get the number of articles formatted. * * @return string */ protected function _getArticleCountFormat() { return Number::format($this->article_count); }
/** * Get the number of likes formatted. * * @return string */ protected function _getLikeCountFormat() { return Number::format($this->like_count); }
/** * Get the discount of the transaction. * * @return float */ protected function _getDiscount() { return Number::format($this->premium_discount->discount * $this->premium_offer->price / 100, ['precision' => 2, 'locale' => 'en_US']); }