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_Month extends Horde_Date_Repeater
24{
25    /**
26     * 30 * 24 * 60 * 60
27     */
28    const MONTH_SECONDS = 2592000;
29
30    public $currentMonthStart;
31
32    public function next($pointer = 'future')
33    {
34        parent::next($pointer);
35
36        if (!$this->currentMonthStart) {
37            $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => 1));
38        }
39        $direction = ($pointer == 'future') ? 1 : -1;
40        $this->currentMonthStart->month += $direction;
41
42        $end = clone $this->currentMonthStart;
43        $end->month++;
44        return new Horde_Date_Span($this->currentMonthStart, $end);
45    }
46
47    public function this($pointer = 'future')
48    {
49        parent::this($pointer);
50
51        switch ($pointer) {
52        case 'future':
53            $monthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1));
54            $monthEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month + 1, 'day' => 1));
55            break;
56
57        case 'past':
58            $monthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => 1));
59            $monthEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day));
60            break;
61
62        case 'none':
63            $monthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => 1));
64            $monthEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month + 1, 'day' => 1));
65            break;
66        }
67
68        return new Horde_Date_Span($monthStart, $monthEnd);
69    }
70
71    public function offset($span, $amount, $pointer)
72    {
73        $direction = ($pointer == 'future') ? 1 : -1;
74        return $span->add(array('month' => $amount * $direction));
75    }
76
77    public function width()
78    {
79        return self::MONTH_SECONDS;
80    }
81
82    public function __toString()
83    {
84        return parent::__toString() . '-month';
85    }
86
87}
88