-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECalendarViewPagination.php
executable file
·357 lines (311 loc) · 10.1 KB
/
ECalendarViewPagination.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* ECalendarViewPagination.php
*
* @author Martin Ludvik <matolud@gmail.com>
* @copyright Copyright © 2014 by Martin Ludvik
* @license http://opensource.org/licenses/MIT MIT license
*/
Yii::import('ecalendarview.ECalendarViewPageSize');
/**
* The pagination controls which days are passed by {@link ECalendarViewDataProvider} to {@link ECalendarView}.
*/
class ECalendarViewPagination extends CComponent {
/**
* @var DateTime The current date represents the zero-index day for pagination. It is also understand by calendar as the currently selected date.
*/
private $_currentDate;
/**
* @var ECalendarViewPageSize The size of calendar page.
*/
private $_pageSize;
/**
* @var int The page index. Can be negative. Notice that url request parameter overrides this value.
*/
private $_pageIndex;
/**
* @var boolean True if Monday is considered the first day of the week, otherwise false (then Sunday is considered).
*/
private $_isMondayFirst;
/**
* @var string The request parameter that holds page index.
*/
private $_pageIndexVar;
/**
* Constructs pagination and sets it's attributes to default values.
* @param array $config The attributes as key=>value map.
*/
public function __construct(array $config = array()) {
$this->_currentDate = $this->createTodayDate();
$this->_pageSize = ECalendarViewPageSize::MONTH;
$this->_pageIndex = 0;
$this->_isMondayFirst = false;
$this->_pageIndexVar = 'page';
foreach($config as $key => $value) {
$this->$key = $value;
}
}
/**
* @see ECalendarViewPagination::$_currentDate
*/
public function setCurrentDate(DateTime $currentDate) {
$this->_currentDate = clone($currentDate);
}
/**
* @see ECalendarViewPagination::$_pageSize
*/
public function setPageSize($pageSize) {
if( ! ECalendarViewPageSize::isValidValue($pageSize)) {
throw new CException(Yii::t('ecalendarview', 'Page size is out of permitted values. See documentation for more information.'));
}
$this->_pageSize = $pageSize;
}
/**
* @see ECalendarViewPagination::$_pageIndex
*/
public function setPageIndex($pageIndex) {
$this->_pageIndex = (int) $pageIndex;
}
/**
* @see ECalendarViewPagination::$_isMondayFirst
*/
public function setIsMondayFirst($isMondayFirst) {
$this->_isMondayFirst = (boolean) $isMondayFirst;
}
/**
* @see ECalendarViewPagination::$_pageIndexVar
*/
public function setPageIndexVar($pageIndexVar) {
$this->_pageIndexVar = $pageIndexVar;
}
/**
* @see ECalendarViewPagination::$_currentDate
*/
public function getCurrentDate() {
return clone($this->_currentDate);
}
/**
* @see ECalendarViewPagination::$_pageSize
*/
public function getPageSize() {
return $this->_pageSize;
}
/**
* @see ECalendarViewPagination::$_pageIndex
*/
public function getPageIndex() {
if(isset($_GET[$this->getPageIndexVar()])) {
return (int) $_GET[$this->getPageIndexVar()];
} else {
return (int) $this->_pageIndex;
}
}
/**
* @see ECalendarViewPagination::$_isMondayFirst
*/
public function getIsMondayFirst() {
return (boolean) $this->_isMondayFirst;
}
/**
* @see ECalendarViewPagination::$_pageIndexVar
*/
public function getPageIndexVar() {
return $this->_pageIndexVar;
}
/**
* Retrieves the first date on current page that is relevant.
* @return DateTime The date.
* @see ECalendarViewPagination::isRelevantDate
*/
public function getFirstRelevantPageDate() {
switch($this->getPageSize()) {
case ECalendarViewPageSize::MONTH:
$year = (int) $this->getCurrentDate()->format('Y');
$month = (int) $this->getCurrentDate()->format('n') + $this->getPageIndex();
$date = $this->createDate($year, $month, 1);
return $date;
case ECalendarViewPageSize::WEEK:
$date = clone($this->getCurrentDate());
if($this->getPageIndex() >= 0) {
$date->add(new DateInterval('P' . 7 * $this->getPageIndex() . 'D'));
} else {
$date->sub(new DateInterval('P' . 7 * (-$this->getPageIndex()) . 'D'));
}
$dateIndex = $this->getWeekdayIndex($date);
$date->sub(new DateInterval('P' . $dateIndex . 'D'));
return $date;
case ECalendarViewPageSize::DAY:
$date = clone($this->getCurrentDate());
if($this->getPageIndex() >= 0) {
$date->add(new DateInterval('P' . $this->getPageIndex() . 'D'));
} else {
$date->sub(new DateInterval('P' . (-$this->getPageIndex()) . 'D'));
}
return $date;
}
}
/**
* Retrieves the last date on current page that is relevant.
* @return DateTime The date.
* @see ECalendarViewPagination::isRelevantDate
*/
public function getLastRelevantPageDate() {
switch($this->_pageSize) {
case ECalendarViewPageSize::MONTH:
$year = (int) $this->getCurrentDate()->format('Y');
$month = (int) $this->getCurrentDate()->format('n') + $this->getPageIndex();
$date = $this->createDate($year, $month, $this->getMonthSize($year, $month));
return $date;
case ECalendarViewPageSize::WEEK:
$date = clone($this->getCurrentDate());
if($this->getPageIndex() >= 0) {
$date->add(new DateInterval('P' . 7 * $this->getPageIndex() . 'D'));
} else {
$date->sub(new DateInterval('P' . 7 * (-$this->getPageIndex()) . 'D'));
}
$dateIndex = $this->getWeekdayReverseIndex($date);
$date->add(new DateInterval('P' . $dateIndex . 'D'));
return $date;
case ECalendarViewPageSize::DAY:
$date = clone($this->getCurrentDate());
if($this->getPageIndex() >= 0) {
$date->add(new DateInterval('P' . $this->getPageIndex() . 'D'));
} else {
$date->sub(new DateInterval('P' . (-$this->getPageIndex()) . 'D'));
}
return $date;
}
}
/**
* Retrieves the middle date on current page that is relevant.
* @return DateTime The date.
* @see ECalendarViewPagination::isRelevantDate
*/
public function getMiddleRelevantPageDate() {
switch($this->getPageSize()) {
case ECalendarViewPageSize::MONTH:
$date = $this->getFirstRelevantPageDate();
$offset = (int) ($this->getMonthSize2($date) / 2);
$date->add(new DateInterval('P' . $offset . 'D'));
return $date;
case ECalendarViewPageSize::WEEK:
$date = $this->getFirstRelevantPageDate();
$date->add(new DateInterval('P3D'));
return $date;
case ECalendarViewPageSize::DAY:
return $this->getFirstRelevantPageDate();
}
}
/**
* Retrieves the first date on current page including non-relevant days.
* @return DateTime The date.
* @see ECalendarViewPagination::isRelevantDate
*/
public function getFirstPageDate() {
switch($this->getPageSize()) {
case ECalendarViewPageSize::MONTH:
$date = $this->getFirstRelevantPageDate();
$dateIndex = $this->getWeekdayIndex($date);
$date->sub(new DateInterval('P' . $dateIndex . 'D'));
return $date;
case ECalendarViewPageSize::WEEK:
case ECalendarViewPageSize::DAY:
return $this->getFirstRelevantPageDate();
}
}
/**
* Retrieves the last date on current page including non-relevant days.
* @return DateTime The date.
* @see ECalendarViewPagination::isRelevantDate
*/
public function getLastPageDate() {
switch($this->_pageSize) {
case ECalendarViewPageSize::MONTH:
$date = $this->getFirstPageDate();
$dateIndex = $this->getWeekdayIndex($date);
$date->add(new DateInterval('P' . (41 - $dateIndex) . 'D'));
return $date;
case ECalendarViewPageSize::WEEK:
case ECalendarViewPageSize::DAY:
return $this->getLastRelevantPageDate();
}
}
/**
* Checks if given date is current.
* @param DateTime $date The date.
* @return boolean True if date is current, otherwise false.
* @see ECalendarViewPagination::$_currentDate
*/
public function isCurrentDate(DateTime $date) {
return (boolean) ($date == $this->getCurrentDate());
}
/**
* Checks if given date is relevant.
* That means that date is not used only as padding on the beginning and end of the month page.
* @param DateTime $date The date.
* @return boolean True if date is relevant, otherwise false.
*/
public function isRelevantDate(DateTime $date) {
return (boolean)
($date >= $this->getFirstRelevantPageDate() &&
$date <= $this->getLastRelevantPageDate());
}
/**
* Creates date.
* @param int $year The year.
* @param int $month The month.
* @param int $day The day.
* @return DateTime The date.
*/
private function createDate($year, $month, $day) {
$date = new DateTime();
$date->setDate($year, $month, $day);
return $date;
}
/**
* Creates today's date.
* @return DateTime The date.
*/
private function createTodayDate() {
return $this->createDate(date('Y'), date('n'), date('j'));
}
/**
* Retrieves number of days in month.
* @param int $year The year.
* @param int $month The month.
* @return int The number of days.
*/
private function getMonthSize($year, $month) {
$date = $this->createDate($year, $month, 1);
return (int) $this->getMonthSize2($date);
}
/**
* Retrieves number of days in month.
* @param DateTime $date The date that belongs to given month.
* @return int The number of days.
*/
private function getMonthSize2($date) {
return (int) $date->format('t');
}
/**
* Retrieves index of day from the beginning of week.
* @param DateTime $date The date.
* @return int The index.
*/
private function getWeekdayIndex(DateTime $date) {
$timestamp = $date->getTimestamp();
if($this->_isMondayFirst) {
return (int) ($date->format('N') - 1);
} else {
return (int) $date->format('w');
}
}
/**
* Retrieves index of day from the end of week.
* @param DateTime $date The date.
* @return int The index.
*/
private function getWeekdayReverseIndex(DateTime $date) {
return (int) (6 - $this->getWeekdayIndex($date));
}
}