Skip to content
Extraits de code Groupes Projets
Period.php 9,59 ko
Newer Older
  • Learn to ignore specific revisions
  • <?php
    /**
     * Piwik - Open source web analytics
    
    robocoder's avatar
    robocoder a validé
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
    
    robocoder's avatar
    robocoder a validé
     * @category Piwik
     * @package Piwik
    
    namespace Piwik;
    
    use Piwik\Period\Day;
    use Piwik\Period\Month;
    use Piwik\Period\Range;
    use Piwik\Period\Week;
    
    use Piwik\Period\Year;
    
     * Date range representation.
     * 
    
     * Piwik allows users to view aggregated statistics for single days and for date
     * ranges consisting of several days. When requesting data, a **date** string and
     * a **period** string must be used to specify the date range that the data regards.
     * This is the class Piwik uses to represent and manipulate those date ranges.
    
     * 
     * 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);
     * 
    
    robocoder's avatar
    robocoder a validé
     * @package Piwik
    
     * @subpackage Period
    
    abstract class Period
    
    sgiehl's avatar
    sgiehl a validé
        /**
         * Array of subperiods
    
         * @var \Piwik\Period[]
    
    sgiehl's avatar
    sgiehl a validé
         */
        protected $subperiods = array();
    
        protected $subperiodsProcessed = false;
    
    sgiehl's avatar
    sgiehl a validé
    
        /**
         * @var string
         */
        protected $label = null;
    
    sgiehl's avatar
    sgiehl a validé
    
    
    sgiehl's avatar
    sgiehl a validé
    
        /**
    
         * Constructor.
         * 
    
         * @ignore
    
    sgiehl's avatar
    sgiehl a validé
         */
    
        public function __construct(Date $date)
    
         * Creates a new Period instance with a period ID and {@link Date} instance.
    
         * _Note: This method cannot create {@link Period\Range} periods._
    
    Thomas Steur's avatar
    Thomas Steur a validé
         * @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);
            }
    
    
                    return new Year($date);
    
                    $message = Piwik::translate(
    
                        'General_ExceptionInvalidPeriod', array($strPeriod, 'day, week, month, year, range'));
                    throw new Exception($message);
    
         * Returns true if `$dateString` and `$period` represent multiple periods.
         * 
         * Will return true for date/period combinations where date references multiple
         * dates and period is not `'range'`. For example, will return true for:
         * 
         * - **date** = `2012-01-01,2012-02-01` and **period** = `'day'`
         * - **date** = `2012-01-01,2012-02-01` and **period** = `'week'`
         * - **date** = `last7` and **period** = `'month'`
         * 
         * etc.
         * 
    
         * @param  $dateString The **date** query parameter value.
         * @param  $period The **period** query parameter value.
    
         * @return boolean
         */
        public static function isMultiplePeriod($dateString, $period)
        {
    
            return is_string($dateString)
    
                && (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs)
    
                    || Range::parseDateRange($dateString))
    
         * 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));
    
                    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());
    
                $oPeriod = Period::factory($period, $date);
    
         * Returns the first day of the period.
    
         * @return Date
    
            $this->generate();
    
            if (count($this->subperiods) == 0) {
                return $this->getDate();
            }
            $periods = $this->getSubperiods();
    
            $currentPeriod = $periods[0];
            while ($currentPeriod->getNumberOfSubperiods() > 0) {
                $periods = $currentPeriod->getSubperiods();
                $currentPeriod = $periods[0];
            }
            return $currentPeriod->getDate();
        }
    
         * Returns the last day of the period.
    
         * @return Date
    
            $this->generate();
    
            if (count($this->subperiods) == 0) {
                return $this->getDate();
            }
            $periods = $this->getSubperiods();
    
            $currentPeriod = $periods[count($periods) - 1];
            while ($currentPeriod->getNumberOfSubperiods() > 0) {
                $periods = $currentPeriod->getSubperiods();
                $currentPeriod = $periods[count($periods) - 1];
            }
            return $currentPeriod->getDate();
        }
    
        /**
         * Returns the period ID.
         * 
    
         * @return int A unique integer for this type of period.
    
        public function getId()
        {
            return Piwik::$idPeriods[$this->getLabel()];
        }
    
    sgiehl's avatar
    sgiehl a validé
        /**
    
         * Returns the label for the current period.
         * 
         * @return string `"day"`, `"week"`, `"month"`, `"year"`, `"range"`
    
    sgiehl's avatar
    sgiehl a validé
         */
        public function getLabel()
    
         */
        protected function getDate()
        {
            return $this->date;
        }
    
    sgiehl's avatar
    sgiehl a validé
    
    
        protected function generate()
        {
            $this->subperiodsProcessed = true;
        }
    
    sgiehl's avatar
    sgiehl a validé
    
        /**
    
         * Returns the number of available subperiods.
         * 
    
    sgiehl's avatar
    sgiehl a validé
         * @return int
         */
        public function getNumberOfSubperiods()
    
            $this->generate();
    
         * Returns the set of Period instances that together make up this period. For a year,
         * this would be 12 months. For a month this would be 28-31 days. Etc.
         * 
    
            $this->generate();
    
            return $this->subperiods;
        }
    
        /**
         * Add a date to the period.
         *
    
         * Protected because adding periods after initialization is not supported.
    
         * @param \Piwik\Period $period Valid Period object
    
         */
        protected function addSubperiod($period)
        {
            $this->subperiods[] = $period;
        }
    
    sgiehl's avatar
    sgiehl a validé
    
        /**
    
         * Returns a list of strings representing the current period.
    
    sgiehl's avatar
    sgiehl a validé
         *
    
         * @param string $format The format of each individual day.
         * @return array An array of string dates that this period consists of.
    
    sgiehl's avatar
    sgiehl a validé
         */
        public function toString($format = "Y-m-d")
    
            $this->generate();
    
            $dateString = array();
            foreach ($this->subperiods as $period) {
                $dateString[] = $period->toString($format);
            }
            return $dateString;
        }
    
    
         * See {@link toString()}.
    
        public function __toString()
        {
            return implode(",", $this->toString());
        }
    
    
        /**
         * Returns a pretty string describing this period.
         * 
         * @return string
         */
    
        abstract public function getPrettyString();
    
    
        /**
         * Returns a short string description of this period that is localized with the currently used
         * language.
         * 
         * @return string
         */
    
        abstract public function getLocalizedShortString();
    
    
        /**
         * Returns a long string description of this period that is localized with the currently used
         * language.
         * 
         * @return string
         */
    
        abstract public function getLocalizedLongString();
    
        /**
         * Returns a succinct string describing this period.
         * 
         * @return string eg, `'2012-01-01,2012-01-31'`.
         */
    
            return $this->getDateStart()->toString("Y-m-d") . "," . $this->getDateEnd()->toString("Y-m-d");
    
    Thomas Steur's avatar
    Thomas Steur a validé
    }