1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15:
16: class Horde_Date_Repeater_DayPortion extends Horde_Date_Repeater
17: {
18: 19: 20:
21: public static $morning = array(21600, 43200);
22:
23: 24: 25:
26: public static $afternoon = array(46800, 61200);
27:
28: 29: 30:
31: public static $evening = array(61200, 72000);
32:
33: 34: 35:
36: public static $night = array(72000, 86400);
37:
38: public $range;
39: public $currentSpan;
40: public $type;
41:
42: public function __construct($type)
43: {
44: $this->type = $type;
45:
46: if (is_int($type)) {
47: $this->range = array(($type * 3600), (($type + 12) * 3600));
48: } else {
49: $lookup = array(
50: 'am' => array(0, (12 * 3600 - 1)),
51: 'pm' => array((12 * 3600), (24 * 3600 - 1)),
52: 'morning' => self::$morning,
53: 'afternoon' => self::$afternoon,
54: 'evening' => self::$evening,
55: 'night' => self::$night,
56: );
57: if (!isset($lookup[$type])) {
58: throw new InvalidArgumentException("Invalid type '$type' for Repeater_DayPortion");
59: }
60: $this->range = $lookup[$type];
61: }
62: }
63:
64: public function next($pointer = 'future')
65: {
66: parent::next($pointer);
67:
68: if (!$this->currentSpan) {
69: $nowSeconds = $this->now->hour * 3600 + $this->now->min * 60 + $this->now->sec;
70: if ($nowSeconds < $this->range[0]) {
71: switch ($pointer) {
72: case 'future':
73: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
74: break;
75:
76: case 'past':
77: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1, 'sec' => $this->range[0]));
78: break;
79: }
80: } elseif ($nowSeconds > $this->range[1]) {
81: switch ($pointer) {
82: case 'future':
83: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1, 'sec' => $this->range[0]));
84: break;
85:
86: case 'past':
87: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
88: break;
89: }
90: } else {
91: switch ($pointer) {
92: case 'future':
93: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1, 'sec' => $this->range[0]));
94: break;
95:
96: case 'past':
97: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1, 'sec' => $this->range[0]));
98: break;
99: }
100: }
101:
102: $rangeEnd = $rangeStart->add($this->range[1] - $this->range[0]);
103: $this->currentSpan = new Horde_Date_Span($rangeStart, $rangeEnd);
104: } else {
105: switch ($pointer) {
106: case 'future':
107: $this->currentSpan = $this->currentSpan->add(array('day' => 1));
108: break;
109:
110: case 'past':
111: $this->currentSpan = $this->currentSpan->add(array('day' => -1));
112: break;
113: }
114: }
115:
116: return $this->currentSpan;
117: }
118:
119: public function this($context = 'future')
120: {
121: parent::this($context);
122:
123: $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
124: $this->currentSpan = new Horde_Date_Span($rangeStart, $rangeStart->add($this->range[1] - $this->range[0]));
125: return $this->currentSpan;
126: }
127:
128: public function offset($span, $amount, $pointer)
129: {
130: $this->now = $span->begin;
131: $portionSpan = $this->next($pointer);
132: $direction = ($pointer == 'future') ? 1 : -1;
133: return $portionSpan->add(array('day' => $direction * ($amount - 1)));
134: }
135:
136: public function width()
137: {
138: if (!$this->range) {
139: throw new Horde_Date_Repeater_Exception('Range has not been set');
140: }
141:
142: if ($this->currentSpan) {
143: return $this->currentSpan->width();
144: }
145:
146: if (is_int($this->type)) {
147: return (12 * 3600);
148: } else {
149: return $this->range[1] - $this->range[0];
150: }
151: }
152:
153: public function __toString()
154: {
155: return parent::__toString() . '-dayportion-' . $this->type;
156: }
157:
158: }
159: