Daily.php
1 month ago
Hourly.php
2 years ago
Monthly.php
2 years ago
Schedule.php
1 year ago
SpecificTime.php
2 years ago
Weekly.php
1 year ago
Weekly.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Scheduler\Schedule; |
| 10 | |
| 11 | use Exception; |
| 12 | /** |
| 13 | * Weekly class is used to schedule tasks every week. |
| 14 | * |
| 15 | * @see \Piwik\Scheduler\Task |
| 16 | */ |
| 17 | class Weekly extends \Piwik\Scheduler\Schedule\Schedule |
| 18 | { |
| 19 | /** |
| 20 | * @see ScheduledTime::getRescheduledTime |
| 21 | * @return int |
| 22 | */ |
| 23 | public function getRescheduledTime() |
| 24 | { |
| 25 | $currentTime = $this->getTime(); |
| 26 | $daysFromNow = 7; |
| 27 | // Adjusts the scheduled day |
| 28 | if ($this->day !== null) { |
| 29 | $daysFromNow = ($this->day - date('N', $currentTime) + 7) % 7; |
| 30 | if ($daysFromNow == 0) { |
| 31 | $daysFromNow = 7; |
| 32 | } |
| 33 | } |
| 34 | // Adds correct number of days |
| 35 | $rescheduledTime = mktime(date('H', $currentTime), date('i', $currentTime), date('s', $currentTime), date('n', $currentTime), date('j', $currentTime) + $daysFromNow, date('Y', $currentTime)); |
| 36 | // Adjusts the scheduled hour |
| 37 | $rescheduledTime = $this->adjustHour($rescheduledTime); |
| 38 | $rescheduledTime = $this->adjustTimezone($rescheduledTime); |
| 39 | return $rescheduledTime; |
| 40 | } |
| 41 | /** |
| 42 | * @param int $day the day to set, has to be >= 1 and < 8 |
| 43 | * @throws Exception if parameter _day is invalid |
| 44 | */ |
| 45 | public function setDay($day) |
| 46 | { |
| 47 | if (!is_int($day)) { |
| 48 | $day = self::getDayIntFromString($day); |
| 49 | } |
| 50 | if (!($day >= 1 && $day < 8)) { |
| 51 | throw new Exception("Invalid day parameter, must be >=1 and < 8"); |
| 52 | } |
| 53 | $this->day = $day; |
| 54 | } |
| 55 | public static function getDayIntFromString($dayString) |
| 56 | { |
| 57 | $time = strtotime($dayString); |
| 58 | if ($time === \false) { |
| 59 | throw new Exception("Invalid day string '{$dayString}'. Must be 'monday', 'tuesday', etc."); |
| 60 | } |
| 61 | return date("N", $time); |
| 62 | } |
| 63 | } |
| 64 |