Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authormattab <matthieu.aubry@gmail.com>2014-05-02 04:16:40 +0400
committermattab <matthieu.aubry@gmail.com>2014-05-02 04:16:40 +0400
commit02e030ee78aabfe2b8453dbe03f05459a353d7eb (patch)
treedfcc0012980bee87535cbc1b8e481794d4ecc176 /core
parentff3bb73aafc4a03a2edc69b16e2182a37f3f5e3d (diff)
Refs #5037 Refactor the factory out of the Period class in its own Period\Factory class
Diffstat (limited to 'core')
-rw-r--r--core/Archive.php3
-rw-r--r--core/DataTable.php2
-rw-r--r--core/DataTable/Renderer/Csv.php2
-rw-r--r--core/Period.php94
-rw-r--r--core/Period/Factory.php110
-rw-r--r--core/Period/Range.php4
-rw-r--r--core/Plugin/Controller.php58
7 files changed, 161 insertions, 112 deletions
diff --git a/core/Archive.php b/core/Archive.php
index e5884aea70..f4732c2751 100644
--- a/core/Archive.php
+++ b/core/Archive.php
@@ -13,6 +13,7 @@ use Piwik\Archive\Parameters;
use Piwik\ArchiveProcessor\Rules;
use Piwik\DataAccess\ArchiveSelector;
use Piwik\Period\Range;
+use Piwik\Period\Factory;
/**
* The **Archive** class is used to query cached analytics statistics
@@ -204,7 +205,7 @@ class Archive
$allPeriods = $oPeriod->getSubperiods();
} else {
$timezone = count($websiteIds) == 1 ? Site::getTimezoneFor($websiteIds[0]) : false;
- $oPeriod = Period::makePeriodFromQueryParams($timezone, $period, $strDate);
+ $oPeriod = Factory::makePeriodFromQueryParams($timezone, $period, $strDate);
$allPeriods = array($oPeriod);
}
$segment = new Segment($segment, $websiteIds);
diff --git a/core/DataTable.php b/core/DataTable.php
index b70ef4bde7..6d75b5f71b 100644
--- a/core/DataTable.php
+++ b/core/DataTable.php
@@ -139,7 +139,7 @@ require_once PIWIK_INCLUDE_PATH . '/core/Common.php';
*
* $dataTable = \Piwik\Plugins\Referrers\API::getInstance()->getSearchEngines($idSite = 1, $period = 'day', $date = '2007-07-24');
* $oldPeriod = $dataTable->metadata['period'];
- * $dataTable->metadata['period'] = Period::factory('week', Date::factory('2013-10-18'));
+ * $dataTable->metadata['period'] = Period\Factory::build('week', Date::factory('2013-10-18'));
*
* **Serializing & unserializing**
*
diff --git a/core/DataTable/Renderer/Csv.php b/core/DataTable/Renderer/Csv.php
index cc9030d4a0..75d6d68fc0 100644
--- a/core/DataTable/Renderer/Csv.php
+++ b/core/DataTable/Renderer/Csv.php
@@ -341,7 +341,7 @@ class Csv extends Renderer
} else if (strpos($date, ',') !== false) {
$period = new Range('range', $date);
} else {
- $period = Period::factory($period, Date::factory($date));
+ $period = Period\Factory::build($period, Date::factory($date));
}
$prettyDate = $period->getLocalizedLongString();
diff --git a/core/Period.php b/core/Period.php
index f97fbcaa33..f224dfc326 100644
--- a/core/Period.php
+++ b/core/Period.php
@@ -11,9 +11,9 @@ namespace Piwik;
use Exception;
use Piwik\Period\Day;
use Piwik\Period\Month;
-use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
+use Piwik\Period\Range;
/**
* Date range representation.
@@ -26,16 +26,8 @@ use Piwik\Period\Year;
* There are five types of periods in Piwik: day, week, month, year and range,
* where **range** is any date range. The reason the other periods exist instead
* of just **range** is that Piwik will pre-archive reports for days, weeks, months
- * and years, while every other date range is archived on-demand.
- *
- * ### Examples
- *
- * **Building a period from 'date' and 'period' query parameters**
- *
- * $date = Common::getRequestVar('date', null, 'string');
- * $period = Common::getRequestVar('period', null, 'string');
- * $periodObject = Period::advancedFactory($period, $date);
- *
+ * and years, while every custom date range is archived on-demand.
+ *
* @api
*/
abstract class Period
@@ -69,51 +61,6 @@ abstract class Period
}
/**
- * Creates a new Period instance with a period ID and {@link Date} instance.
- *
- * _Note: This method cannot create {@link Period\Range} periods._
- *
- * @param string $strPeriod `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
- * @param Date|string $date A date within the period or the range of dates.
- * @throws Exception If `$strPeriod` is invalid.
- * @return \Piwik\Period
- */
- static public function factory($strPeriod, $date)
- {
- if (is_string($date)) {
- if (Period::isMultiplePeriod($date, $strPeriod) || $strPeriod == 'range') {
- return new Range($strPeriod, $date);
- }
-
- $date = Date::factory($date);
- }
-
- switch ($strPeriod) {
- case 'day':
- return new Day($date);
- break;
-
- case 'week':
- return new Week($date);
- break;
-
- case 'month':
- return new Month($date);
- break;
-
- case 'year':
- return new Year($date);
- break;
-
- default:
- $message = Piwik::translate(
- 'General_ExceptionInvalidPeriod', array($strPeriod, 'day, week, month, year, range'));
- throw new Exception($message);
- break;
- }
- }
-
- /**
* Returns true if `$dateString` and `$period` represent multiple periods.
*
* Will return true for date/period combinations where date references multiple
@@ -126,8 +73,8 @@ abstract class Period
* etc.
*
* @static
- * @param $dateString The **date** query parameter value.
- * @param $period The **period** query parameter value.
+ * @param $dateString string The **date** query parameter value.
+ * @param $period string The **period** query parameter value.
* @return boolean
*/
public static function isMultiplePeriod($dateString, $period)
@@ -138,37 +85,6 @@ abstract class Period
&& $period != 'range';
}
- /**
- * Creates a Period instance using a period, date and timezone.
- *
- * @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`,
- * `'yesterday'` or `'yesterdaySameTime'`.
- * @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
- * @param string $date The date or date range string. Can be a special value including
- * `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`.
- * @return \Piwik\Period
- */
- public static function makePeriodFromQueryParams($timezone, $period, $date)
- {
- if (empty($timezone)) {
- $timezone = 'UTC';
- }
-
- if ($period == 'range') {
- $oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone));
- } else {
- if (!($date instanceof Date)) {
- if ($date == 'now' || $date == 'today') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
- } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
- }
- $date = Date::factory($date);
- }
- $oPeriod = Period::factory($period, $date);
- }
- return $oPeriod;
- }
/**
* Returns the first day of the period.
diff --git a/core/Period/Factory.php b/core/Period/Factory.php
new file mode 100644
index 0000000000..93cbe0fdef
--- /dev/null
+++ b/core/Period/Factory.php
@@ -0,0 +1,110 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Period;
+
+use Exception;
+use Piwik\Date;
+use Piwik\Period;
+use Piwik\Piwik;
+
+class Factory
+{
+ /**
+ * Creates a new Period instance with a period ID and {@link Date} instance.
+ *
+ * _Note: This method cannot create {@link Period\Range} periods._
+ *
+ * @param string $period `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
+ * @param Date|string $date A date within the period or the range of dates.
+ * @throws Exception If `$strPeriod` is invalid.
+ * @return \Piwik\Period
+ */
+ static public function build($period, $date)
+ {
+ 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':
+ return new Day($date);
+ break;
+
+ case 'week':
+ return new Week($date);
+ break;
+
+ case 'month':
+ return new Month($date);
+ break;
+
+ case 'year':
+ return new Year($date);
+ break;
+ }
+
+ self::throwExceptionInvalidPeriod($period);
+ }
+
+ private static function checkPeriodIsEnabled($period)
+ {
+ $enabledPeriodsInAPI = array();
+ if(!in_array($period, $enabledPeriodsInAPI)) {
+ self::throwExceptionInvalidPeriod($period);
+ }
+ }
+
+ /**
+ * @param $strPeriod
+ * @throws \Exception
+ */
+ private static function throwExceptionInvalidPeriod($strPeriod)
+ {
+ $message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, 'day, week, month, year, range'));
+ throw new Exception($message);
+ }
+
+
+ /**
+ * Creates a Period instance using a period, date and timezone.
+ *
+ * @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`,
+ * `'yesterday'` or `'yesterdaySameTime'`.
+ * @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
+ * @param string $date The date or date range string. Can be a special value including
+ * `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`.
+ * @return \Piwik\Period
+ */
+ public static function makePeriodFromQueryParams($timezone, $period, $date)
+ {
+ if (empty($timezone)) {
+ $timezone = 'UTC';
+ }
+
+ if ($period == 'range') {
+ $oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone));
+ } else {
+ if (!($date instanceof Date)) {
+ if ($date == 'now' || $date == 'today') {
+ $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
+ } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
+ $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
+ }
+ $date = Date::factory($date);
+ }
+ $oPeriod = Factory::build($period, $date);
+ }
+ return $oPeriod;
+ }
+} \ No newline at end of file
diff --git a/core/Period/Range.php b/core/Period/Range.php
index ab542fe838..b60cc4ca61 100644
--- a/core/Period/Range.php
+++ b/core/Period/Range.php
@@ -338,14 +338,14 @@ class Range extends Period
protected function fillArraySubPeriods($startDate, $endDate, $period)
{
$arrayPeriods = array();
- $endSubperiod = Period::factory($period, $endDate);
+ $endSubperiod = Period\Factory::build($period, $endDate);
$arrayPeriods[] = $endSubperiod;
// set end date to start of end period since we're comparing against start date.
$endDate = $endSubperiod->getDateStart();
while ($endDate->isLater($startDate)) {
$endDate = $endDate->addPeriod(-1, $period);
- $subPeriod = Period::factory($period, $endDate);
+ $subPeriod = Period\Factory::build($period, $endDate);
$arrayPeriods[] = $subPeriod;
}
$arrayPeriods = array_reverse($arrayPeriods);
diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php
index 4cce5d0711..078188a4a6 100644
--- a/core/Plugin/Controller.php
+++ b/core/Plugin/Controller.php
@@ -190,6 +190,40 @@ abstract class Controller
}
/**
+ * @return array
+ */
+ private static function getEnabledPeriodsNames()
+ {
+ $availablePeriods = self::getEnabledPeriodsInUI();
+ $periodNames = array(
+ 'day' => array(
+ 'singular' => Piwik::translate('CoreHome_PeriodDay'),
+ 'plural' => Piwik::translate('CoreHome_PeriodDays')
+ ),
+ 'week' => array(
+ 'singular' => Piwik::translate('CoreHome_PeriodWeek'),
+ 'plural' => Piwik::translate('CoreHome_PeriodWeeks')
+ ),
+ 'month' => array(
+ 'singular' => Piwik::translate('CoreHome_PeriodMonth'),
+ 'plural' => Piwik::translate('CoreHome_PeriodMonths')
+ ),
+ 'year' => array(
+ 'singular' => Piwik::translate('CoreHome_PeriodYear'),
+ 'plural' => Piwik::translate('CoreHome_PeriodYears')
+ ),
+ // Note: plural is not used for date range
+ 'range' => array(
+ 'singular' => Piwik::translate('General_DateRangeInPeriodList'),
+ 'plural' => Piwik::translate('General_DateRangeInPeriodList')
+ ),
+ );
+
+ $periodNames = array_intersect_key($periodNames, array_fill_keys($availablePeriods, true));
+ return $periodNames;
+ }
+
+ /**
* Returns the name of the default method that will be called
* when visiting: index.php?module=PluginName without the action parameter.
*
@@ -491,7 +525,7 @@ abstract class Controller
$periodStr = Common::getRequestVar('period');
if ($periodStr != 'range') {
$date = Date::factory($this->strDate);
- $period = Period::factory($periodStr, $date);
+ $period = Period\Factory::build($periodStr, $date);
} else {
$period = new Range($periodStr, $rawDate, $this->site->getTimezone());
}
@@ -693,26 +727,14 @@ abstract class Controller
$view->displayUniqueVisitors = SettingsPiwik::isUniqueVisitorsEnabled($currentPeriod);
$availablePeriods = self::getEnabledPeriodsInUI();
if (!in_array($currentPeriod, $availablePeriods)) {
- throw new Exception("Period must be one of: " . implode(",", $availablePeriods));
+ throw new Exception("Period must be one of: " . implode(", ", $availablePeriods));
}
- $periodNames = array(
- 'day' => array('singular' => Piwik::translate('CoreHome_PeriodDay'), 'plural' => Piwik::translate('CoreHome_PeriodDays')),
- 'week' => array('singular' => Piwik::translate('CoreHome_PeriodWeek'), 'plural' => Piwik::translate('CoreHome_PeriodWeeks')),
- 'month' => array('singular' => Piwik::translate('CoreHome_PeriodMonth'), 'plural' => Piwik::translate('CoreHome_PeriodMonths')),
- 'year' => array('singular' => Piwik::translate('CoreHome_PeriodYear'), 'plural' => Piwik::translate('CoreHome_PeriodYears')),
- // Note: plural is not used for date range
- 'range' => array('singular' => Piwik::translate('General_DateRangeInPeriodList'), 'plural' => Piwik::translate('General_DateRangeInPeriodList')),
- );
-
- $periodNames = array_intersect_key($periodNames, array_fill_keys($availablePeriods, true));
-
$found = array_search($currentPeriod, $availablePeriods);
- if ($found !== false) {
- unset($availablePeriods[$found]);
- }
+ unset($availablePeriods[$found]);
+
$view->period = $currentPeriod;
$view->otherPeriods = $availablePeriods;
- $view->periodsNames = $periodNames;
+ $view->periodsNames = self::getEnabledPeriodsNames();
}
/**
@@ -895,7 +917,7 @@ abstract class Controller
*/
public static function getPrettyDate($date, $period)
{
- return self::getCalendarPrettyDate(Period::factory($period, Date::factory($date)));
+ return self::getCalendarPrettyDate(Period\Factory::build($period, Date::factory($date)));
}
/**