1<?php
2/**
3 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @author   Chuck Hagenbuch <chuck@horde.org>
9 * @category Horde
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL
11 * @package  Date
12 */
13
14/**
15 * Date repeater.
16 *
17 * @author    Chuck Hagenbuch <chuck@horde.org>
18 * @category  Horde
19 * @copyright 2009-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl21 LGPL
21 * @package   Date
22 */
23class Horde_Date_Repeater_Fortnight extends Horde_Date_Repeater
24{
25    // (14 * 24 * 60 * 60)
26    const FORTNIGHT_SECONDS = 1209600;
27
28    public $currentFortnightStart;
29
30    public function next($pointer = 'future')
31    {
32        parent::next($pointer);
33
34        if (!$this->currentFortnightStart) {
35            switch ($pointer) {
36            case 'future':
37                $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
38                $sundayRepeater->now = $this->now;
39                $nextSundaySpan = $sundayRepeater->next('future');
40                $this->currentFortnightStart = $nextSundaySpan->begin;
41                break;
42
43            case 'past':
44                $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
45                $sundayRepeater->now = clone $this->now;
46                $sundayRepeater->now->day++;
47                $sundayRepeater->next('past');
48                $sundayRepeater->next('past');
49                $lastSundaySpan = $sundayRepeater->next('past');
50                $this->currentFortnightStart = $lastSundaySpan->begin;
51                break;
52            }
53        } else {
54            $direction = ($pointer == 'future') ? 1 : -1;
55            $this->currentFortnightStart->add($direction * self::FORTNIGHT_SECONDS);
56        }
57
58        return new Horde_Date_Span($this->currentFortnightStart, $this->currentFortnightStart->add(self::FORTNIGHT_SECONDS));
59    }
60
61    public function this($pointer = 'future')
62    {
63        parent::this($pointer);
64
65        switch ($pointer) {
66        case 'future':
67        case 'none':
68            $thisFortnightStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour + 1));
69            $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
70            $sundayRepeater->now = $this->now;
71            $sundayRepeater->this('future');
72            $thisSundaySpan = $sundayRepeater->this('future');
73            $thisFortnightEnd = $thisSundaySpan->begin;
74            return new Horde_Date_Span($thisFortnightStart, $thisFortnightEnd);
75
76        case 'past':
77            $thisFortnightEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour));
78            $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
79            $sundayRepeater->now = $this->now;
80            $lastSundaySpan = $sundayRepeater->next('past');
81            $thisFortnightStart = $lastSundaySpan->begin;
82            return new Horde_Date_Span($thisFortnightStart, $thisFortnightEnd);
83        }
84    }
85
86    public function offset($span, $amount, $pointer)
87    {
88        $direction = ($pointer == 'future') ? 1 : -1;
89        return $span->add($direction * $amount * self::FORTNIGHT_SECONDS);
90    }
91
92    public function width()
93    {
94        return self::FORTNIGHT_SECONDS;
95    }
96
97    public function __toString()
98    {
99        return parent::__toString() . '-fortnight';
100    }
101
102}
103