diff --git a/core/Period/Range.php b/core/Period/Range.php index d88a5e2da74..4f1c5b3c7f9 100644 --- a/core/Period/Range.php +++ b/core/Period/Range.php @@ -9,6 +9,7 @@ namespace Piwik\Period; use Exception; +use Piwik\Cache; use Piwik\Common; use Piwik\Date; use Piwik\Period; @@ -32,6 +33,11 @@ class Range extends Period protected $label = 'range'; protected $today; + /** + * @var null|Date + */ + protected $defaultEndDate; + /** * Constructor. * @@ -56,6 +62,41 @@ public function __construct($strPeriod, $strDate, $timezone = 'UTC', $today = fa $this->today = $today; } + private function getCache() + { + return Cache::getTransientCache(); + } + + private function getCacheId() + { + $end = ''; + if ($this->defaultEndDate) { + $end = $this->defaultEndDate->getTimestamp(); + } + + $today = $this->today->getTimestamp(); + + return 'range' . $this->strPeriod . $this->strDate . $this->timezone . $end . $today; + } + + private function loadAllFromCache() + { + $range = $this->getCache()->fetch($this->getCacheId()); + + if (!empty($range)) { + foreach ($range as $key => $val) { + $this->$key = $val; + } + } + } + + private function cacheAll() + { + $props = get_object_vars($this); + + $this->getCache()->save($this->getCacheId(), $props); + } + /** * Returns the current period as a localized short string. * @@ -156,6 +197,12 @@ protected function generate() return; } + $this->loadAllFromCache(); + + if ($this->subperiodsProcessed) { + return; + } + parent::generate(); if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) { @@ -214,6 +261,7 @@ protected function generate() if ($this->strPeriod != 'range') { $this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod); + $this->cacheAll(); return; } @@ -221,6 +269,7 @@ protected function generate() // When period=range, we want End Date to be the actual specified end date, // rather than the end of the month / week / whatever is used for processing this range $this->endDate = $endDate; + $this->cacheAll(); } /** @@ -462,4 +511,17 @@ private function isEndOfWeekLaterThanEndDate($endDate, $endOfWeek) return $isEndOfWeekLaterThanEndDate; } + + /** + * Returns the date range string comprising two dates + * + * @return string eg, `'2012-01-01,2012-01-31'`. + */ + public function getRangeString() + { + $dateStart = $this->getDateStart(); + $dateEnd = $this->getDateEnd(); + + return $dateStart->toString("Y-m-d") . "," . $dateEnd->toString("Y-m-d"); + } }