Skip to content

Commit

Permalink
Fixes #5037 You can now configure periods that are available in the U…
Browse files Browse the repository at this point in the history
…I and API

; The list of periods that are available in the Piwik calendar
; Example use case: custom date range requests are processed in real time,
; so they may take a few minutes on very high traffic website: you may remove "range" below to disable this period
enabled_periods_UI = "day,week,month,year,range"
enabled_periods_API = "day,week,month,year,range"
  • Loading branch information
mattab committed May 2, 2014
1 parent ffdc6f0 commit 77fa82f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/global.ini.php
Expand Up @@ -105,7 +105,7 @@

; The list of periods that are available in the Piwik calendar
; Example use case: custom date range requests are processed in real time,
; so they may take a few minutes on very high traffic website. You may remove "range" below.
; so they may take a few minutes on very high traffic website: you may remove "range" below to disable this period
enabled_periods_UI = "day,week,month,year,range"
enabled_periods_API = "day,week,month,year,range"

Expand Down
2 changes: 1 addition & 1 deletion core/Archive.php
Expand Up @@ -201,7 +201,7 @@ public static function build($idSites, $period, $strDate, $segment = false, $_re
$websiteIds = Site::getIdSitesFromIdSitesString($idSites, $_restrictSitesToLogin);

if (Period::isMultiplePeriod($strDate, $period)) {
$oPeriod = new Range($period, $strDate);
$oPeriod = Factory::build($period, $strDate);
$allPeriods = $oPeriod->getSubperiods();
} else {
$timezone = count($websiteIds) == 1 ? Site::getTimezoneFor($websiteIds[0]) : false;
Expand Down
2 changes: 1 addition & 1 deletion core/Period.php
Expand Up @@ -62,7 +62,7 @@ public function __construct(Date $date)
}

/**
* @deprecated
* @deprecated Use Factory::build instead
* @param $period
* @param $date
* @return Period
Expand Down
25 changes: 18 additions & 7 deletions core/Period/Factory.php
Expand Up @@ -28,15 +28,15 @@ class Factory
*/
static public function build($period, $date)
{
self::checkPeriodIsEnabled($period);

if (is_string($date)) {
if (Period::isMultiplePeriod($date, $period) || $period == 'range') {
self::checkPeriodIsEnabled('range');
return new Range($period, $date);
}
$date = Date::factory($date);
}

self::checkPeriodIsEnabled($period);

switch ($period) {
case 'day':
Expand All @@ -57,7 +57,7 @@ static public function build($period, $date)
}
}

private static function checkPeriodIsEnabled($period)
public static function checkPeriodIsEnabled($period)
{
if(!self::isPeriodEnabledForAPI($period)) {
self::throwExceptionInvalidPeriod($period);
Expand All @@ -70,7 +70,9 @@ private static function checkPeriodIsEnabled($period)
*/
private static function throwExceptionInvalidPeriod($strPeriod)
{
$message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, 'day, week, month, year, range'));
$periods = self::getPeriodsEnabledForAPI();
$periods = implode(", ", $periods);
$message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods));
throw new Exception($message);
}

Expand All @@ -92,7 +94,8 @@ public static function makePeriodFromQueryParams($timezone, $period, $date)
}

if ($period == 'range') {
$oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone));
self::checkPeriodIsEnabled('range');
$oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone));
} else {
if (!($date instanceof Date)) {
if ($date == 'now' || $date == 'today') {
Expand All @@ -112,11 +115,19 @@ public static function makePeriodFromQueryParams($timezone, $period, $date)
* @return bool
*/
public static function isPeriodEnabledForAPI($period)
{
$enabledPeriodsInAPI = self::getPeriodsEnabledForAPI();
return in_array($period, $enabledPeriodsInAPI);
}

/**
* @return array
*/
private static function getPeriodsEnabledForAPI()
{
$enabledPeriodsInAPI = Config::getInstance()->General['enabled_periods_API'];
$enabledPeriodsInAPI = explode(",", $enabledPeriodsInAPI);
$enabledPeriodsInAPI = array_map('trim', $enabledPeriodsInAPI);

return in_array($period, $enabledPeriodsInAPI);
return $enabledPeriodsInAPI;
}
}
2 changes: 2 additions & 0 deletions plugins/Live/Controller.php
Expand Up @@ -105,7 +105,9 @@ public function getLastVisitsStart()
{
// hack, ensure we load today's visits by default
$_GET['date'] = 'today';
\Piwik\Period\Factory::checkPeriodIsEnabled('day');
$_GET['period'] = 'day';

$view = new View('@Live/getLastVisitsStart');
$view->idSite = $this->idSite;
$api = new Request("method=Live.getLastVisitsDetails&idSite={$this->idSite}&filter_limit=10&format=php&serialize=0&disable_generic_filters=1");
Expand Down

0 comments on commit 77fa82f

Please sign in to comment.