| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Period; |
|
|
| use Exception; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Date; |
| use Piwik\Period; |
| use Piwik\Piwik; |
| use Piwik\Plugin; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| abstract class Factory |
| { |
| public function __construct() |
| { |
| |
| } |
|
|
| |
| |
| |
| |
| |
| abstract public function shouldHandle($strPeriod, $strDate); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| abstract public function make($strPeriod, $date, $timezone); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public static function build($period, $date, $timezone = 'UTC') |
| { |
| self::checkPeriodIsEnabled($period); |
|
|
| if (is_string($date)) { |
| [$period, $date] = self::convertRangeToDateIfNeeded($period, $date); |
| if ( |
| Period::isMultiplePeriod($date, $period) |
| || $period == 'range' |
| ) { |
| return new Range($period, $date, $timezone); |
| } |
|
|
| $dateObject = Date::factory($date); |
| } elseif ($date instanceof Date) { |
| $dateObject = $date; |
| } else { |
| throw new \Exception("Invalid date supplied to Period\Factory::build(): " . gettype($date)); |
| } |
|
|
| switch ($period) { |
| case 'day': |
| return new Day($dateObject); |
| case 'week': |
| return new Week($dateObject); |
| case 'month': |
| return new Month($dateObject); |
| case 'year': |
| return new Year($dateObject); |
| } |
|
|
| $customPeriodFactories = Plugin\Manager::getInstance()->findComponents('PeriodFactory', self::class); |
| foreach ($customPeriodFactories as $customPeriodFactoryClass) { |
| $customPeriodFactory = StaticContainer::get($customPeriodFactoryClass); |
| if ($customPeriodFactory->shouldHandle($period, $date)) { |
| return $customPeriodFactory->make($period, $date, $timezone); |
| } |
| } |
|
|
| throw new \Exception("Don't know how to create a '$period' period! (date = $date)"); |
| } |
|
|
| public static function checkPeriodIsEnabled($period) |
| { |
| if (!self::isPeriodEnabledForAPI($period)) { |
| self::throwExceptionInvalidPeriod($period); |
| } |
| } |
|
|
| |
| |
| |
| |
| private static function throwExceptionInvalidPeriod($strPeriod) |
| { |
| $periods = self::getPeriodsEnabledForAPI(); |
| $periods = implode(", ", $periods); |
| $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods)); |
| throw new Exception($message); |
| } |
|
|
| private static function convertRangeToDateIfNeeded($period, $date) |
| { |
| if (is_string($period) && is_string($date) && $period === 'range') { |
| $dates = explode(',', $date); |
| if (count($dates) === 2 && $dates[0] === $dates[1]) { |
| $period = 'day'; |
| $date = $dates[0]; |
| } |
| } |
|
|
| return array($period, $date); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function makePeriodFromQueryParams($timezone, $period, $date) |
| { |
| if (empty($timezone)) { |
| $timezone = 'UTC'; |
| } |
|
|
| [$period, $date] = self::convertRangeToDateIfNeeded($period, $date); |
|
|
| if ($period == 'range') { |
| self::checkPeriodIsEnabled('range'); |
| $oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone)); |
| } else { |
| if (!($date instanceof Date)) { |
| if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) { |
| $date = Date::factoryInTimezone($date, $timezone); |
| } |
| $date = Date::factory($date); |
| } |
| $oPeriod = Factory::build($period, $date); |
| } |
| return $oPeriod; |
| } |
|
|
| |
| |
| |
| |
| public static function isPeriodEnabledForAPI($period) |
| { |
| $periodValidator = new PeriodValidator(); |
| return $periodValidator->isPeriodAllowedForAPI($period); |
| } |
|
|
| |
| |
| |
| public static function getPeriodsEnabledForAPI() |
| { |
| $periodValidator = new PeriodValidator(); |
| return $periodValidator->getPeriodsAllowedForAPI(); |
| } |
|
|
| public static function isAnyLowerPeriodDisabledForAPI($periodLabel) |
| { |
| $parentPeriod = null; |
| switch ($periodLabel) { |
| case 'week': |
| $parentPeriod = 'day'; |
| break; |
| case 'month': |
| $parentPeriod = 'week'; |
| break; |
| case 'year': |
| $parentPeriod = 'month'; |
| break; |
| default: |
| break; |
| } |
|
|
| if ($parentPeriod === null) { |
| return false; |
| } |
|
|
| return !self::isPeriodEnabledForAPI($parentPeriod) |
| || self::isAnyLowerPeriodDisabledForAPI($parentPeriod); |
| } |
| } |
|
|