Subversion Repositories Applications.gtt

Rev

Rev 61 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 jpm 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.3 2005/10/22 10:29:46 quipo Exp $
21
//
22
/**
23
 * @package Calendar
24
 * @version $Id: Decorator.php,v 1.3 2005/10/22 10:29:46 quipo 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
        $validator = $this->calendar->getValidator();
287
        return $validator;
288
    }
289
 
290
    /**
291
     * Returns a reference to the current Calendar_Engine being used. Useful
292
     * for Calendar_Table_Helper and Calendar_Validator
293
     * @return object implementing Calendar_Engine_Inteface
294
     * @access private
295
     */
296
    function & getEngine()
297
    {
298
        return $this->calendar->getEngine();
299
    }
300
 
301
    /**
302
     * Returns the value for the previous year
303
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
304
     * @return int e.g. 2002 or timestamp
305
     * @access public
306
     */
307
    function prevYear($format = 'int')
308
    {
309
        return $this->calendar->prevYear($format);
310
    }
311
 
312
    /**
313
     * Returns the value for this year
314
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
315
     * @return int e.g. 2003 or timestamp
316
     * @access public
317
     */
318
    function thisYear($format = 'int')
319
    {
320
        return $this->calendar->thisYear($format);
321
    }
322
 
323
    /**
324
     * Returns the value for next year
325
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
326
     * @return int e.g. 2004 or timestamp
327
     * @access public
328
     */
329
    function nextYear($format = 'int')
330
    {
331
        return $this->calendar->nextYear($format);
332
    }
333
 
334
    /**
335
     * Returns the value for the previous month
336
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
337
     * @return int e.g. 4 or Unix timestamp
338
     * @access public
339
      */
340
    function prevMonth($format = 'int')
341
    {
342
        return $this->calendar->prevMonth($format);
343
    }
344
 
345
    /**
346
     * Returns the value for this month
347
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
348
     * @return int e.g. 5 or timestamp
349
     * @access public
350
     */
351
    function thisMonth($format = 'int')
352
    {
353
        return $this->calendar->thisMonth($format);
354
    }
355
 
356
    /**
357
     * Returns the value for next month
358
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
359
     * @return int e.g. 6 or timestamp
360
     * @access public
361
     */
362
    function nextMonth($format = 'int')
363
    {
364
        return $this->calendar->nextMonth($format);
365
    }
366
 
367
    /**
368
     * Returns the value for the previous week
369
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
370
     * @return int e.g. 4 or Unix timestamp
371
     * @access public
372
      */
373
    function prevWeek($format = 'n_in_month')
374
    {
375
        if ( method_exists($this->calendar,'prevWeek') ) {
376
            return $this->calendar->prevWeek($format);
377
        } else {
378
            require_once 'PEAR.php';
379
            PEAR::raiseError(
380
                'Cannot call prevWeek on Calendar object of type: '.
381
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
382
                E_USER_NOTICE, 'Calendar_Decorator::prevWeek()');
383
            return false;
384
        }
385
    }
386
 
387
    /**
388
     * Returns the value for this week
389
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
390
     * @return int e.g. 5 or timestamp
391
     * @access public
392
     */
393
    function thisWeek($format = 'n_in_month')
394
    {
395
        if ( method_exists($this->calendar,'thisWeek') ) {
396
            return $this->calendar->thisWeek($format);
397
        } else {
398
            require_once 'PEAR.php';
399
            PEAR::raiseError(
400
                'Cannot call thisWeek on Calendar object of type: '.
401
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
402
                E_USER_NOTICE, 'Calendar_Decorator::thisWeek()');
403
            return false;
404
        }
405
    }
406
 
407
    /**
408
     * Returns the value for next week
409
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
410
     * @return int e.g. 6 or timestamp
411
     * @access public
412
     */
413
    function nextWeek($format = 'n_in_month')
414
    {
415
        if ( method_exists($this->calendar,'nextWeek') ) {
416
            return $this->calendar->nextWeek($format);
417
        } else {
418
            require_once 'PEAR.php';
419
            PEAR::raiseError(
420
                'Cannot call thisWeek on Calendar object of type: '.
421
                get_class($this->calendar), 133, PEAR_ERROR_TRIGGER,
422
                E_USER_NOTICE, 'Calendar_Decorator::nextWeek()');
423
            return false;
424
        }
425
    }
426
 
427
    /**
428
     * Returns the value for the previous day
429
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
430
     * @return int e.g. 10 or timestamp
431
     * @access public
432
     */
433
    function prevDay($format = 'int') {
434
        return $this->calendar->prevDay($format);
435
    }
436
 
437
    /**
438
     * Returns the value for this day
439
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
440
     * @return int e.g. 11 or timestamp
441
     * @access public
442
     */
443
    function thisDay($format = 'int')
444
    {
445
        return $this->calendar->thisDay($format);
446
    }
447
 
448
    /**
449
     * Returns the value for the next day
450
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
451
     * @return int e.g. 12 or timestamp
452
     * @access public
453
     */
454
    function nextDay($format = 'int')
455
    {
456
        return $this->calendar->nextDay($format);
457
    }
458
 
459
    /**
460
     * Returns the value for the previous hour
461
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
462
     * @return int e.g. 13 or timestamp
463
     * @access public
464
     */
465
    function prevHour($format = 'int')
466
    {
467
        return $this->calendar->prevHour($format);
468
    }
469
 
470
    /**
471
     * Returns the value for this hour
472
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
473
     * @return int e.g. 14 or timestamp
474
     * @access public
475
     */
476
    function thisHour($format = 'int')
477
    {
478
        return $this->calendar->thisHour($format);
479
    }
480
 
481
    /**
482
     * Returns the value for the next hour
483
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
484
     * @return int e.g. 14 or timestamp
485
     * @access public
486
     */
487
    function nextHour($format = 'int')
488
    {
489
        return $this->calendar->nextHour($format);
490
    }
491
 
492
    /**
493
     * Returns the value for the previous minute
494
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
495
     * @return int e.g. 23 or timestamp
496
     * @access public
497
     */
498
    function prevMinute($format = 'int')
499
    {
500
        return $this->calendar->prevMinute($format);
501
    }
502
 
503
    /**
504
     * Returns the value for this minute
505
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
506
     * @return int e.g. 24 or timestamp
507
     * @access public
508
     */
509
    function thisMinute($format = 'int')
510
    {
511
        return $this->calendar->thisMinute($format);
512
    }
513
 
514
    /**
515
     * Returns the value for the next minute
516
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
517
     * @return int e.g. 25 or timestamp
518
     * @access public
519
     */
520
   function nextMinute($format = 'int')
521
    {
522
        return $this->calendar->nextMinute($format);
523
    }
524
 
525
    /**
526
     * Returns the value for the previous second
527
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
528
     * @return int e.g. 43 or timestamp
529
     * @access public
530
     */
531
    function prevSecond($format = 'int')
532
    {
533
        return $this->calendar->prevSecond($format);
534
    }
535
 
536
    /**
537
     * Returns the value for this second
538
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
539
     * @return int e.g. 44 or timestamp
540
     * @access public
541
     */
542
    function thisSecond($format = 'int')
543
    {
544
        return $this->calendar->thisSecond($format);
545
    }
546
 
547
    /**
548
     * Returns the value for the next second
549
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
550
     * @return int e.g. 45 or timestamp
551
     * @access public
552
     */
553
    function nextSecond($format = 'int')
554
    {
555
        return $this->calendar->nextSecond($format);
556
    }
557
}
558
?>