1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9namespace Piwik\Plugins\Annotations;
10
11use Piwik\Date;
12use Piwik\Period;
13use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution as EvolutionViz;
14
15/**
16 * Annotations plugins. Provides the ability to attach text notes to
17 * dates for each sites. Notes can be viewed, modified, deleted or starred.
18 *
19 */
20class Annotations extends \Piwik\Plugin
21{
22    /**
23     * @see \Piwik\Plugin::registerEvents
24     */
25    public function registerEvents()
26    {
27        return array(
28            'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
29            'AssetManager.getJavaScriptFiles' => 'getJsFiles',
30            'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
31        );
32    }
33
34    public function getClientSideTranslationKeys(&$translationKeys)
35    {
36        $translationKeys[] = 'Intl_Today';
37    }
38
39    /**
40     * Adds css files for this plugin to the list in the event notification.
41     */
42    public function getStylesheetFiles(&$stylesheets)
43    {
44        $stylesheets[] = "plugins/Annotations/stylesheets/annotations.less";
45    }
46
47    /**
48     * Adds js files for this plugin to the list in the event notification.
49     */
50    public function getJsFiles(&$jsFiles)
51    {
52        $jsFiles[] = "plugins/Annotations/javascripts/annotations.js";
53    }
54
55
56    /**
57     * Returns start & end dates for the range described by a period and optional lastN
58     * argument.
59     *
60     * @param string|bool $date The start date of the period (or the date range of a range
61     *                           period).
62     * @param string $period The period type ('day', 'week', 'month', 'year' or 'range').
63     * @param bool|int $lastN Whether to include the last N periods in the range or not.
64     *                         Ignored if period == range.
65     *
66     * @return Date[]   array of Date objects or array(false, false)
67     */
68    public static function getDateRangeForPeriod($date, $period, $lastN = false)
69    {
70        if ($date === false) {
71            return array(false, false);
72        }
73
74        $isMultiplePeriod = Period\Range::isMultiplePeriod($date, $period);
75
76        // if the range is just a normal period (or the period is a range in which case lastN is ignored)
77        if ($period == 'range') {
78            $oPeriod = new Period\Range('day', $date);
79            $startDate = $oPeriod->getDateStart();
80            $endDate = $oPeriod->getDateEnd();
81        } else if ($lastN == false && !$isMultiplePeriod) {
82            $oPeriod = Period\Factory::build($period, Date::factory($date));
83            $startDate = $oPeriod->getDateStart();
84            $endDate = $oPeriod->getDateEnd();
85        } else { // if the range includes the last N periods or is a multiple period
86            if (!$isMultiplePeriod) {
87                list($date, $lastN) = EvolutionViz::getDateRangeAndLastN($period, $date, $lastN);
88            }
89            list($startDate, $endDate) = explode(',', $date);
90
91            $startDate = Date::factory($startDate);
92            $endDate = Date::factory($endDate);
93        }
94        return array($startDate, $endDate);
95    }
96}
97