Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
493 ddelon 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
//
4
// +----------------------------------------------------------------------+
5
// | PHP Version 4                                                        |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 1997-2002 The PHP Group                                |
8
// +----------------------------------------------------------------------+
9
// | This source file is subject to version 2.02 of the PHP license,      |
10
// | that is bundled with this package in the file LICENSE, and is        |
11
// | available at through the world-wide-web at                           |
12
// | http://www.php.net/license/3_0.txt.                                  |
13
// | If you did not receive a copy of the PHP license and are unable to   |
14
// | obtain it through the world-wide-web, please send a note to          |
15
// | license@php.net so we can mail you a copy immediately.               |
16
// +----------------------------------------------------------------------+
17
// | Authors: Harry Fuecks <hfuecks@phppatterns.com>                      |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: Decorator.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
21
//
22
/**
23
 * @package Calendar
24
 * @version $Id: Decorator.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
25
 */
26
/**
27
 * Decorates any calendar class.
28
 * Create a subclass of this class for your own "decoration".
29
 * Used for "selections"
30
 * <code>
31
 * class DayDecorator extends Calendar_Decorator
32
 * {
33
 *     function thisDay($format = 'int')
34
 *     {
35
.*         $day = parent::thisDay('timestamp');
36
.*         return date('D', $day);
37
 *     }
38
 * }
39
 * $Day = & new Calendar_Day(2003, 10, 25);
40
 * $DayDecorator = & new DayDecorator($Day);
41
 * echo $DayDecorator->thisDay(); // Outputs "Sat"
42
 * </code>
43
 * @abstract
44
 * @package Calendar
45
 */
46
class Calendar_Decorator
47
{
48
    /**
49
     * Subclass of Calendar being decorated
50
     * @var object
51
     * @access private
52
     */
53
    var $calendar;
54
 
55
    /**
56
     * Constructs the Calendar_Decorator
57
     * @param object subclass to Calendar to decorate
58
     */
59
    function Calendar_Decorator(& $calendar)
60
    {
61
        $this->calendar = & $calendar;
62
    }
63
 
64
    /**
65
     * Defines the calendar by a Unix timestamp, replacing values
66
     * passed to the constructor
67
     * @param int Unix timestamp
68
     * @return void
69
     * @access public
70
     */
71
    function setTimestamp($ts)
72
    {
73
        $this->calendar->setTimestamp($ts);
74
    }
75
 
76
    /**
77
     * Returns a timestamp from the current date / time values. Format of
78
     * timestamp depends on Calendar_Engine implementation being used
79
     * @return int timestamp
80
     * @access public
81
     */
82
    function getTimestamp()
83
    {
84
        return $this->calendar->getTimeStamp();
85
    }
86
 
87
    /**
88
     * Defines calendar object as selected (e.g. for today)
89
     * @param boolean state whether Calendar subclass
90
     * @return void
91
     * @access public
92
     */
93
    function setSelected($state = true)
94
    {
95
        $this->calendar->setSelected($state = true);
96
    }
97
 
98
    /**
99
     * True if the calendar subclass object is selected (e.g. today)
100
     * @return boolean
101
     * @access public
102
     */
103
    function isSelected()
104
    {
105
        return $this->calendar->isSelected();
106
    }
107
 
108
    /**
109
     * Adjusts the date (helper method)
110
     * @return void
111
     * @access public
112
     */
113
    function adjust()
114
    {
115
        $this->calendar->adjust();
116
    }
117
 
118
    /**
119
     * Returns the date as an associative array (helper method)
120
     * @param mixed timestamp (leave empty for current timestamp)
121
     * @return array
122
     * @access public
123
     */
124
    function toArray($stamp=null)
125
    {
126
        return $this->calendar->toArray($stamp);
127
    }
128
 
129
    /**
130
     * Returns the value as an associative array (helper method)
131
     * @param string type of date object that return value represents
132
     * @param string $format ['int' | 'array' | 'timestamp' | 'object']
133
     * @param mixed timestamp (depending on Calendar engine being used)
134
     * @param int integer default value (i.e. give me the answer quick)
135
     * @return mixed
136
     * @access private
137
     */
138
    function returnValue($returnType, $format, $stamp, $default)
139
    {
140
        return $this->calendar->returnValue($returnType, $format, $stamp, $default);
141
    }
142
 
143
    /**
144
     * Defines Day object as first in a week
145
     * Only used by Calendar_Month_Weekdays::build()
146
     * @param boolean state
147
     * @return void
148
     * @access private
149
     */
150
    function setFirst ($state = true)
151
    {
152
        if ( method_exists($this->calendar,'setFirst') ) {
153
            $this->calendar->setFirst($state);
154
        }
155
    }
156
 
157
    /**
158
     * Defines Day object as last in a week
159
     * Used only following Calendar_Month_Weekdays::build()
160
     * @param boolean state
161
     * @return void
162
     * @access private
163
     */
164
    function setLast($state = true)
165
    {
166
        if ( method_exists($this->calendar,'setLast') ) {
167
            $this->calendar->setLast($state);
168
        }
169
    }
170
 
171
    /**
172
     * Returns true if Day object is first in a Week
173
     * Only relevant when Day is created by Calendar_Month_Weekdays::build()
174
     * @return boolean
175
     * @access public
176
     */
177
    function isFirst() {
178
        if ( method_exists($this->calendar,'isFirst') ) {
179
            return $this->calendar->isFirst();
180
        }
181
    }
182
 
183
    /**
184
     * Returns true if Day object is last in a Week
185
     * Only relevant when Day is created by Calendar_Month_Weekdays::build()
186
     * @return boolean
187
     * @access public
188
     */
189
    function isLast()
190
    {
191
        if ( method_exists($this->calendar,'isLast') ) {
192
            return $this->calendar->isLast();
193
        }
194
    }
195
 
196
    /**
197
     * Defines Day object as empty
198
     * Only used by Calendar_Month_Weekdays::build()
199
     * @param boolean state
200
     * @return void
201
     * @access private
202
     */
203
    function setEmpty ($state = true)
204
    {
205
        if ( method_exists($this->calendar,'setEmpty') ) {
206
            $this->calendar->setEmpty($state);
207
        }
208
    }
209
 
210
    /**
211
     * @return boolean
212
     * @access public
213
     */
214
    function isEmpty()
215
    {
216
        if ( method_exists($this->calendar,'isEmpty') ) {
217
            return $this->calendar->isEmpty();
218
        }
219
    }
220
 
221
    /**
222
     * Build the children
223
     * @param array containing Calendar objects to select (optional)
224
     * @return boolean
225
     * @access public
226
     * @abstract
227
     */
228
    function build($sDates = array())
229
    {
230
        $this->calendar->build($sDates);
231
    }
232
 
233
    /**
234
     * Iterator method for fetching child Calendar subclass objects
235
     * (e.g. a minute from an hour object). On reaching the end of
236
     * the collection, returns false and resets the collection for
237
     * further iteratations.
238
     * @return mixed either an object subclass of Calendar or false
239
     * @access public
240
     */
241
    function fetch()
242
    {
243
        return $this->calendar->fetch();
244
    }
245
 
246
    /**
247
     * Fetches all child from the current collection of children
248
     * @return array
249
     * @access public
250
     */
251
    function fetchAll()
252
    {
253
        return $this->calendar->fetchAll();
254
    }
255
 
256
    /**
257
     * Get the number Calendar subclass objects stored in the internal
258
     * collection.
259
     * @return int
260
     * @access public
261
     */
262
    function size()
263
    {
264
        return $this->calendar->size();
265
    }
266
 
267
    /**
268
     * Determine whether this date is valid, with the bounds determined by
269
     * the Calendar_Engine. The call is passed on to
270
     * Calendar_Validator::isValid
271
     * @return boolean
272
     * @access public
273
     */
274
    function isValid()
275
    {
276
        return $this->calendar->isValid();
277
    }
278
 
279
    /**
280
     * Returns an instance of Calendar_Validator
281
     * @return Calendar_Validator
282
     * @access public
283
     */
284
    function & getValidator()
285
    {
286
        return $this->calendar->getValidator();
287
    }
288
 
289
    /**
290
     * Returns a reference to the current Calendar_Engine being used. Useful
291
     * for Calendar_Table_Helper and Caledar_Validator
292
     * @return object implementing Calendar_Engine_Inteface
293
     * @access private
294
     */
295
    function & getEngine()
296
    {
297
        return $this->calendar->getEngine();
298
    }
299
 
300
    /**
301
     * Returns the value for the previous year
302
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
303
     * @return int e.g. 2002 or timestamp
304
     * @access public
305
     */
306
    function prevYear($format = 'int')
307
    {
308
        return $this->calendar->prevYear($format);
309
    }
310
 
311
    /**
312
     * Returns the value for this year
313
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
314
     * @return int e.g. 2003 or timestamp
315
     * @access public
316
     */
317
    function thisYear($format = 'int')
318
    {
319
        return $this->calendar->thisYear($format);
320
    }
321
 
322
    /**
323
     * Returns the value for next year
324
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
325
     * @return int e.g. 2004 or timestamp
326
     * @access public
327
     */
328
    function nextYear($format = 'int')
329
    {
330
        return $this->calendar->nextYear($format);
331
    }
332
 
333
    /**
334
     * Returns the value for the previous month
335
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
336
     * @return int e.g. 4 or Unix timestamp
337
     * @access public
338
      */
339
    function prevMonth($format = 'int')
340
    {
341
        return $this->calendar->prevMonth($format);
342
    }
343
 
344
    /**
345
     * Returns the value for this month
346
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
347
     * @return int e.g. 5 or timestamp
348
     * @access public
349
     */
350
    function thisMonth($format = 'int')
351
    {
352
        return $this->calendar->thisMonth($format);
353
    }
354
 
355
    /**
356
     * Returns the value for next month
357
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
358
     * @return int e.g. 6 or timestamp
359
     * @access public
360
     */
361
    function nextMonth($format = 'int')
362
    {
363
        return $this->calendar->nextMonth($format);
364
    }
365
 
366
    /**
367
     * Returns the value for the previous week
368
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
369
     * @return int e.g. 4 or Unix timestamp
370
     * @access public
371
      */
372
    function prevWeek($format = 'n_in_month')
373
    {
374
        if ( method_exists($this->calendar,'prevWeek') ) {
375
            return $this->calendar->prevWeek($format);
376
        } else {
377
            require_once 'PEAR.php';
378
            PEAR::raiseError(
379
                'Cannot call prevWeek on Calendar object of type: '.
380
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
381
                E_USER_NOTICE, 'Calendar_Decorator::prevWeek()');
382
            return false;
383
        }
384
    }
385
 
386
    /**
387
     * Returns the value for this week
388
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
389
     * @return int e.g. 5 or timestamp
390
     * @access public
391
     */
392
    function thisWeek($format = 'n_in_month')
393
    {
394
        if ( method_exists($this->calendar,'thisWeek') ) {
395
            return $this->calendar->thisWeek($format);
396
        } else {
397
            require_once 'PEAR.php';
398
            PEAR::raiseError(
399
                'Cannot call thisWeek on Calendar object of type: '.
400
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
401
                E_USER_NOTICE, 'Calendar_Decorator::thisWeek()');
402
            return false;
403
        }
404
    }
405
 
406
    /**
407
     * Returns the value for next week
408
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
409
     * @return int e.g. 6 or timestamp
410
     * @access public
411
     */
412
    function nextWeek($format = 'n_in_month')
413
    {
414
        if ( method_exists($this->calendar,'nextWeek') ) {
415
            return $this->calendar->nextWeek($format);
416
        } else {
417
            require_once 'PEAR.php';
418
            PEAR::raiseError(
419
                'Cannot call thisWeek on Calendar object of type: '.
420
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
421
                E_USER_NOTICE, 'Calendar_Decorator::nextWeek()');
422
            return false;
423
        }
424
    }
425
 
426
    /**
427
     * Returns the value for the previous day
428
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
429
     * @return int e.g. 10 or timestamp
430
     * @access public
431
     */
432
    function prevDay($format = 'int') {
433
        return $this->calendar->prevDay($format);
434
    }
435
 
436
    /**
437
     * Returns the value for this day
438
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
439
     * @return int e.g. 11 or timestamp
440
     * @access public
441
     */
442
    function thisDay($format = 'int')
443
    {
444
        return $this->calendar->thisDay($format);
445
    }
446
 
447
    /**
448
     * Returns the value for the next day
449
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
450
     * @return int e.g. 12 or timestamp
451
     * @access public
452
     */
453
    function nextDay($format = 'int')
454
    {
455
        return $this->calendar->nextDay($format);
456
    }
457
 
458
    /**
459
     * Returns the value for the previous hour
460
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
461
     * @return int e.g. 13 or timestamp
462
     * @access public
463
     */
464
    function prevHour($format = 'int')
465
    {
466
        return $this->calendar->prevHour($format);
467
    }
468
 
469
    /**
470
     * Returns the value for this hour
471
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
472
     * @return int e.g. 14 or timestamp
473
     * @access public
474
     */
475
    function thisHour($format = 'int')
476
    {
477
        return $this->calendar->thisHour($format);
478
    }
479
 
480
    /**
481
     * Returns the value for the next hour
482
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
483
     * @return int e.g. 14 or timestamp
484
     * @access public
485
     */
486
    function nextHour($format = 'int')
487
    {
488
        return $this->calendar->nextHour($format);
489
    }
490
 
491
    /**
492
     * Returns the value for the previous minute
493
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
494
     * @return int e.g. 23 or timestamp
495
     * @access public
496
     */
497
    function prevMinute($format = 'int')
498
    {
499
        return $this->calendar->prevMinute($format);
500
    }
501
 
502
    /**
503
     * Returns the value for this minute
504
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
505
     * @return int e.g. 24 or timestamp
506
     * @access public
507
     */
508
    function thisMinute($format = 'int')
509
    {
510
        return $this->calendar->thisMinute($format);
511
    }
512
 
513
    /**
514
     * Returns the value for the next minute
515
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
516
     * @return int e.g. 25 or timestamp
517
     * @access public
518
     */
519
   function nextMinute($format = 'int')
520
    {
521
        return $this->calendar->nextMinute($format);
522
    }
523
 
524
    /**
525
     * Returns the value for the previous second
526
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
527
     * @return int e.g. 43 or timestamp
528
     * @access public
529
     */
530
    function prevSecond($format = 'int')
531
    {
532
        return $this->calendar->prevSecond($format);
533
    }
534
 
535
    /**
536
     * Returns the value for this second
537
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
538
     * @return int e.g. 44 or timestamp
539
     * @access public
540
     */
541
    function thisSecond($format = 'int')
542
    {
543
        return $this->calendar->thisSecond($format);
544
    }
545
 
546
    /**
547
     * Returns the value for the next second
548
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
549
     * @return int e.g. 45 or timestamp
550
     * @access public
551
     */
552
    function nextSecond($format = 'int')
553
    {
554
        return $this->calendar->nextSecond($format);
555
    }
556
}
557
?>