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

Monthly.php « ScheduledTime « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4900530e8d397d5e91e564f2a842880437c78fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\ScheduledTime;

use Exception;
use Piwik\ScheduledTime;

/**
 * Monthly class is used to schedule tasks every month.
 *
 * @see ScheduledTask
 *
 */
class Monthly extends ScheduledTime
{
    /**
     * List of available week number strings used in setDayOfWeekFromString.
     */
    private static $weekNumberStringToInt = array('first' => 0, 'second' => 1, 'third' => 2, 'fourth' => 3);

    /**
     * Day of the week for scheduled time.
     *
     * @var int
     */
    private $dayOfWeek = null;

    /**
     * Week number for scheduled time.
     *
     * @var int
     */
    private $week = null;

    public function setDayOfWeekFromString($day)
    {
        @list($weekNumberString, $dayNumberString) = explode(' ', $day);

        // get day number
        $day = Weekly::getDayIntFromString($dayNumberString) % 7;

        // get week number
        $week = false;
        $weekNumberString = strtolower($weekNumberString);
        if (isset(self::$weekNumberStringToInt[$weekNumberString])) {
            $week = self::$weekNumberStringToInt[$weekNumberString];
        } else {
            throw new Exception("Invalid week describer in ScheduledTime\\Monthly::setDayOfWeekFromString: '$weekNumberString'. "
                              . "Supported values are 'first', 'second', 'third', 'fourth'.");
        }

        $this->setDayOfWeek($day, $week);
    }

    /**
     * @return int
     */
    public function getRescheduledTime()
    {
        $currentTime = $this->getTime();

        // Adds one month
        $rescheduledTime = mktime(date('H', $currentTime),
            date('i', $currentTime),
            date('s', $currentTime),
            date('n', $currentTime) + 1,
            1,
            date('Y', $currentTime)
        );

        $nextMonthLength = date('t', $rescheduledTime);

        // Sets scheduled day
        $scheduledDay = date('j', $currentTime);

        if ($this->day !== null) {
            $scheduledDay = $this->day;
        }

        if ($this->dayOfWeek !== null
            && $this->week !== null
        ) {
            $newTime = $rescheduledTime + $this->week * 7 * 86400;
            while (date("w", $newTime) != $this->dayOfWeek % 7) // modulus for sanity check
            {
                $newTime += 86400;
            }
            $scheduledDay = ($newTime - $rescheduledTime) / 86400 + 1;
        }

        // Caps scheduled day
        if ($scheduledDay > $nextMonthLength) {
            $scheduledDay = $nextMonthLength;
        }

        // Adjusts the scheduled day
        $rescheduledTime += ($scheduledDay - 1) * 86400;

        // Adjusts the scheduled hour
        $rescheduledTime = $this->adjustHour($rescheduledTime);
        $rescheduledTime = $this->adjustTimezone($rescheduledTime);

        return $rescheduledTime;
    }

    /**
     * @param int $_day the day to set, has to be >= 1 and < 32
     * @throws Exception if parameter _day is invalid
     */
    public function setDay($_day)
    {
        if (!($_day >= 1 && $_day < 32)) {
            throw new Exception ("Invalid day parameter, must be >=1 and < 32");
        }

        $this->day = $_day;
    }

    /**
     * Makes this scheduled time execute on a particular day of the week on each month.
     *
     * @param int $_day the day of the week to use, between 0-6 (inclusive). 0 -> Sunday
     * @param int $_week the week to use, between 0-3 (inclusive)
     * @throws Exception if either parameter is invalid
     */
    public function setDayOfWeek($_day, $_week)
    {
        if (!($_day >= 0 && $_day < 7)) {
            throw new Exception("Invalid day of week parameter, must be >= 0 & < 7");
        }

        if (!($_week >= 0 && $_week < 4)) {
            throw new Exception("Invalid week number, must be >= 1 & < 4");
        }

        $this->dayOfWeek = $_day;
        $this->week = $_week;
    }
}