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
// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
19
// +----------------------------------------------------------------------+
20
//
21
// $Id: Calendar.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
22
//
23
/**
24
 * @package Calendar
25
 * @version $Id: Calendar.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
26
 */
27
 
28
/**
29
 * Allows Calendar include path to be redefined
30
 */
31
if (!defined('CALENDAR_ROOT')) {
32
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
33
}
34
 
35
/**
36
 * Constant which defines the calculation engine to use
37
 */
38
if (!defined('CALENDAR_ENGINE')) {
39
    define('CALENDAR_ENGINE', 'UnixTS');
40
}
41
 
42
/**
43
 * Define Calendar Month states
44
 */
45
define('CALENDAR_USE_MONTH',          1);
46
define('CALENDAR_USE_MONTH_WEEKDAYS', 2);
47
define('CALENDAR_USE_MONTH_WEEKS',    3);
48
 
49
/**
50
 * Contains a factory method to return a Singleton instance of a class
51
 * implementing the Calendar_Engine_Interface.<br>
52
 * <b>Note:</b> this class must be modified to "register" alternative
53
 * Calendar_Engines. The engine used can be controlled with the constant
54
 * CALENDAR_ENGINE
55
 * @see Calendar_Engine_Interface
56
 * @package Calendar
57
 * @access protected
58
 */
59
class Calendar_Engine_Factory
60
{
61
    /**
62
     * Returns an instance of the engine
63
     * @return object instance of a calendar calculation engine
64
     * @access protected
65
     */
66
    function & getEngine()
67
    {
68
        static $engine = false;
69
        switch (CALENDAR_ENGINE) {
70
            case 'PearDate':
71
                $class = 'Calendar_Engine_PearDate';
72
                break;
73
            case 'UnixTS':
74
            default:
75
                $class = 'Calendar_Engine_UnixTS';
76
            break;
77
        }
78
        if (!$engine) {
79
            if (!class_exists($class)) {
80
                require_once CALENDAR_ROOT.'Engine'.DIRECTORY_SEPARATOR.CALENDAR_ENGINE.'.php';
81
            }
82
            $engine = new $class;
83
        }
84
        return $engine;
85
    }
86
}
87
 
88
/**
89
 * Base class for Calendar API. This class should not be instantiated
90
 * directly.
91
 * @abstract
92
 * @package Calendar
93
 */
94
class Calendar
95
{
96
    /**
97
     * Instance of class implementing calendar engine interface
98
     * @var object
99
     * @access private
100
     */
101
    var $cE;
102
 
103
    /**
104
     * Instance of Calendar_Validator (lazy initialized when isValid() or
105
     * getValidor() is called
106
     * @var Calendar_Validator
107
     * @access private
108
     */
109
    var $validator;
110
 
111
    /**
112
     * Year for this calendar object e.g. 2003
113
     * @access private
114
     * @var int
115
     */
116
   var $year;
117
 
118
    /**
119
     * Month for this calendar object e.g. 9
120
     * @access private
121
     * @var int
122
     */
123
    var $month;
124
 
125
    /**
126
     * Day of month for this calendar object e.g. 23
127
     * @access private
128
     * @var int
129
     */
130
    var $day;
131
 
132
    /**
133
     * Hour of day for this calendar object e.g. 13
134
     * @access private
135
     * @var int
136
     */
137
    var $hour;
138
 
139
    /**
140
     * Minute of hour this calendar object e.g. 46
141
     * @access private
142
     * @var int
143
     */
144
    var $minute;
145
 
146
    /**
147
     * Second of minute this calendar object e.g. 34
148
     * @access private
149
     * @var int
150
     */
151
    var $second;
152
 
153
    /**
154
     * Marks this calendar object as selected (e.g. 'today')
155
     * @access private
156
     * @var boolean
157
     */
158
    var $selected = false;
159
 
160
    /**
161
     * Collection of child calendar objects created from subclasses
162
     * of Calendar. Type depends on the object which created them.
163
     * @access private
164
     * @var array
165
     */
166
    var $children = array();
167
 
168
    /**
169
     * Constructs the Calendar
170
     * @param int year
171
     * @param int month
172
     * @param int day
173
     * @param int hour
174
     * @param int minute
175
     * @param int second
176
     * @access protected
177
     */
178
    function Calendar($y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0)
179
    {
180
        static $cE = null;
181
        if (!isset($cE)) {
182
            $cE = & Calendar_Engine_Factory::getEngine();
183
        }
184
        $this->cE = & $cE;
185
        $this->year   = (int)$y;
186
        $this->month  = (int)$m;
187
        $this->day    = (int)$d;
188
        $this->hour   = (int)$h;
189
        $this->minute = (int)$i;
190
        $this->second = (int)$s;
191
    }
192
 
193
    /**
194
     * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values
195
     * passed to the constructor
196
     * @param int|string Unix or ISO-8601 timestamp
197
     * @return void
198
     * @access public
199
     */
200
    function setTimestamp($ts)
201
    {
202
        $this->year   = $this->cE->stampToYear($ts);
203
        $this->month  = $this->cE->stampToMonth($ts);
204
        $this->day    = $this->cE->stampToDay($ts);
205
        $this->hour   = $this->cE->stampToHour($ts);
206
        $this->minute = $this->cE->stampToMinute($ts);
207
        $this->second = $this->cE->stampToSecond($ts);
208
    }
209
 
210
    /**
211
     * Returns a timestamp from the current date / time values. Format of
212
     * timestamp depends on Calendar_Engine implementation being used
213
     * @return int|string timestamp
214
     * @access public
215
     */
216
    function getTimestamp()
217
    {
218
        return $this->cE->dateToStamp(
219
            $this->year, $this->month, $this->day,
220
            $this->hour, $this->minute, $this->second);
221
    }
222
 
223
    /**
224
     * Defines calendar object as selected (e.g. for today)
225
     * @param boolean state whether Calendar subclass
226
     * @return void
227
     * @access public
228
     */
229
    function setSelected($state = true)
230
    {
231
        $this->selected = $state;
232
    }
233
 
234
    /**
235
     * True if the calendar subclass object is selected (e.g. today)
236
     * @return boolean
237
     * @access public
238
     */
239
    function isSelected()
240
    {
241
        return $this->selected;
242
    }
243
 
244
    /**
245
     * Adjusts the date (helper method)
246
     * @return void
247
     * @access public
248
     */
249
    function adjust()
250
    {
251
        $stamp = $this->getTimeStamp();
252
        $this->year   = $this->cE->stampToYear($stamp);
253
        $this->month  = $this->cE->stampToMonth($stamp);
254
        $this->day    = $this->cE->stampToDay($stamp);
255
        $this->hour   = $this->cE->stampToHour($stamp);
256
        $this->minute = $this->cE->stampToMinute($stamp);
257
        $this->second = $this->cE->stampToSecond($stamp);
258
    }
259
 
260
    /**
261
     * Returns the date as an associative array (helper method)
262
     * @param mixed timestamp (leave empty for current timestamp)
263
     * @return array
264
     * @access public
265
     */
266
    function toArray($stamp=null)
267
    {
268
        if (is_null($stamp)) {
269
            $stamp = $this->getTimeStamp();
270
        }
271
        return array(
272
            'year'   => $this->cE->stampToYear($stamp),
273
            'month'  => $this->cE->stampToMonth($stamp),
274
            'day'    => $this->cE->stampToDay($stamp),
275
            'hour'   => $this->cE->stampToHour($stamp),
276
            'minute' => $this->cE->stampToMinute($stamp),
277
            'second' => $this->cE->stampToSecond($stamp)
278
        );
279
    }
280
 
281
    /**
282
     * Returns the value as an associative array (helper method)
283
     * @param string type of date object that return value represents
284
     * @param string $format ['int' | 'array' | 'timestamp' | 'object']
285
     * @param mixed timestamp (depending on Calendar engine being used)
286
     * @param int integer default value (i.e. give me the answer quick)
287
     * @return mixed
288
     * @access private
289
     */
290
    function returnValue($returnType, $format, $stamp, $default)
291
    {
292
        switch (strtolower($format)) {
293
            case 'int':
294
                return $default;
295
            case 'array':
296
                return $this->toArray($stamp);
297
                break;
298
            case 'object':
299
                require_once CALENDAR_ROOT.'Factory.php';
300
                return Calendar_Factory::createByTimestamp($returnType,$stamp);
301
                break;
302
            case 'timestamp':
303
            default:
304
                return $stamp;
305
                break;
306
        }
307
    }
308
 
309
    /**
310
     * Abstract method for building the children of a calendar object.
311
     * Implemented by Calendar subclasses
312
     * @param array containing Calendar objects to select (optional)
313
     * @return boolean
314
     * @access public
315
     * @abstract
316
     */
317
    function build($sDates = array())
318
    {
319
        require_once 'PEAR.php';
320
        PEAR::raiseError(
321
            'Calendar::build is abstract', null, PEAR_ERROR_TRIGGER,
322
            E_USER_NOTICE, 'Calendar::build()');
323
        return false;
324
    }
325
 
326
    /**
327
     * Abstract method for selected data objects called from build
328
     * @param array
329
     * @return boolean
330
     * @access public
331
     * @abstract
332
     */
333
    function setSelection($sDates)
334
    {
335
        require_once 'PEAR.php';
336
        PEAR::raiseError(
337
            'Calendar::setSelection is abstract', null, PEAR_ERROR_TRIGGER,
338
            E_USER_NOTICE, 'Calendar::setSelection()');
339
        return false;
340
    }
341
 
342
    /**
343
     * Iterator method for fetching child Calendar subclass objects
344
     * (e.g. a minute from an hour object). On reaching the end of
345
     * the collection, returns false and resets the collection for
346
     * further iteratations.
347
     * @return mixed either an object subclass of Calendar or false
348
     * @access public
349
     */
350
    function fetch()
351
    {
352
        $child = each($this->children);
353
        if ($child) {
354
            return $child['value'];
355
        } else {
356
            reset($this->children);
357
            return false;
358
        }
359
    }
360
 
361
    /**
362
     * Fetches all child from the current collection of children
363
     * @return array
364
     * @access public
365
     */
366
    function fetchAll()
367
    {
368
        return $this->children;
369
    }
370
 
371
    /**
372
     * Get the number Calendar subclass objects stored in the internal
373
     * collection.
374
     * @return int
375
     * @access public
376
     */
377
    function size()
378
    {
379
        return count($this->children);
380
    }
381
 
382
    /**
383
     * Determine whether this date is valid, with the bounds determined by
384
     * the Calendar_Engine. The call is passed on to
385
     * Calendar_Validator::isValid
386
     * @return boolean
387
     * @access public
388
     */
389
    function isValid()
390
    {
391
        $validator = & $this->getValidator();
392
        return $validator->isValid();
393
    }
394
 
395
    /**
396
     * Returns an instance of Calendar_Validator
397
     * @return Calendar_Validator
398
     * @access public
399
     */
400
    function & getValidator()
401
    {
402
        if (!isset($this->validator)) {
403
            require_once CALENDAR_ROOT.'Validator.php';
404
            $this->validator = & new Calendar_Validator($this);
405
        }
406
        return $this->validator;
407
    }
408
 
409
    /**
410
     * Returns a reference to the current Calendar_Engine being used. Useful
411
     * for Calendar_Table_Helper and Caledar_Validator
412
     * @return object implementing Calendar_Engine_Inteface
413
     * @access private
414
     */
415
    function & getEngine()
416
    {
417
        return $this->cE;
418
    }
419
 
420
    /**
421
     * Returns the value for the previous year
422
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
423
     * @return int e.g. 2002 or timestamp
424
     * @access public
425
     */
426
    function prevYear($format = 'int')
427
    {
428
        $ts = $this->cE->dateToStamp($this->year-1, 1, 1, 0, 0, 0);
429
        return $this->returnValue('Year', $format, $ts, $this->year-1);
430
    }
431
 
432
    /**
433
     * Returns the value for this year
434
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
435
     * @return int e.g. 2003 or timestamp
436
     * @access public
437
     */
438
    function thisYear($format = 'int')
439
    {
440
        $ts = $this->cE->dateToStamp($this->year, 1, 1, 0, 0, 0);
441
        return $this->returnValue('Year', $format, $ts, $this->year);
442
    }
443
 
444
    /**
445
     * Returns the value for next year
446
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
447
     * @return int e.g. 2004 or timestamp
448
     * @access public
449
     */
450
    function nextYear($format = 'int')
451
    {
452
        $ts = $this->cE->dateToStamp($this->year+1, 1, 1, 0, 0, 0);
453
        return $this->returnValue('Year', $format, $ts, $this->year+1);
454
    }
455
 
456
    /**
457
     * Returns the value for the previous month
458
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
459
     * @return int e.g. 4 or Unix timestamp
460
     * @access public
461
     */
462
    function prevMonth($format = 'int')
463
    {
464
        $ts = $this->cE->dateToStamp($this->year, $this->month-1, 1, 0, 0, 0);
465
        return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts));
466
    }
467
 
468
    /**
469
     * Returns the value for this month
470
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
471
     * @return int e.g. 5 or timestamp
472
     * @access public
473
     */
474
    function thisMonth($format = 'int')
475
    {
476
        $ts = $this->cE->dateToStamp($this->year, $this->month, 1, 0, 0, 0);
477
        return $this->returnValue('Month', $format, $ts, $this->month);
478
    }
479
 
480
    /**
481
     * Returns the value for next month
482
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
483
     * @return int e.g. 6 or timestamp
484
     * @access public
485
     */
486
    function nextMonth($format = 'int')
487
    {
488
        $ts = $this->cE->dateToStamp($this->year, $this->month+1, 1, 0, 0, 0);
489
        return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts));
490
    }
491
 
492
    /**
493
     * Returns the value for the previous day
494
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
495
     * @return int e.g. 10 or timestamp
496
     * @access public
497
     */
498
    function prevDay($format = 'int')
499
    {
500
        $ts = $this->cE->dateToStamp(
501
            $this->year, $this->month, $this->day-1, 0, 0, 0);
502
        return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts));
503
    }
504
 
505
    /**
506
     * Returns the value for this day
507
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
508
     * @return int e.g. 11 or timestamp
509
     * @access public
510
     */
511
    function thisDay($format = 'int')
512
    {
513
        $ts = $this->cE->dateToStamp(
514
            $this->year, $this->month, $this->day, 0, 0, 0);
515
        return $this->returnValue('Day', $format, $ts, $this->day);
516
    }
517
 
518
    /**
519
     * Returns the value for the next day
520
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
521
     * @return int e.g. 12 or timestamp
522
     * @access public
523
     */
524
    function nextDay($format = 'int')
525
    {
526
        $ts = $this->cE->dateToStamp(
527
            $this->year, $this->month, $this->day+1, 0, 0, 0);
528
        return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts));
529
    }
530
 
531
    /**
532
     * Returns the value for the previous hour
533
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
534
     * @return int e.g. 13 or timestamp
535
     * @access public
536
     */
537
    function prevHour($format = 'int')
538
    {
539
        $ts = $this->cE->dateToStamp(
540
            $this->year, $this->month, $this->day, $this->hour-1, 0, 0);
541
        return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts));
542
    }
543
 
544
    /**
545
     * Returns the value for this hour
546
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
547
     * @return int e.g. 14 or timestamp
548
     * @access public
549
     */
550
    function thisHour($format = 'int')
551
    {
552
        $ts = $this->cE->dateToStamp(
553
            $this->year, $this->month, $this->day, $this->hour, 0, 0);
554
        return $this->returnValue('Hour', $format, $ts, $this->hour);
555
    }
556
 
557
    /**
558
     * Returns the value for the next hour
559
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
560
     * @return int e.g. 14 or timestamp
561
     * @access public
562
     */
563
    function nextHour($format = 'int')
564
    {
565
        $ts = $this->cE->dateToStamp(
566
            $this->year, $this->month, $this->day, $this->hour+1, 0, 0);
567
        return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts));
568
    }
569
 
570
    /**
571
     * Returns the value for the previous minute
572
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
573
     * @return int e.g. 23 or timestamp
574
     * @access public
575
     */
576
    function prevMinute($format = 'int')
577
    {
578
        $ts = $this->cE->dateToStamp(
579
            $this->year, $this->month, $this->day,
580
            $this->hour, $this->minute-1, 0);
581
        return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts));
582
    }
583
 
584
    /**
585
     * Returns the value for this minute
586
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
587
     * @return int e.g. 24 or timestamp
588
     * @access public
589
     */
590
    function thisMinute($format = 'int')
591
    {
592
        $ts = $this->cE->dateToStamp(
593
            $this->year, $this->month, $this->day,
594
            $this->hour, $this->minute, 0);
595
        return $this->returnValue('Minute', $format, $ts, $this->minute);
596
    }
597
 
598
    /**
599
    * Returns the value for the next minute
600
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
601
     * @return int e.g. 25 or timestamp
602
     * @access public
603
     */
604
    function nextMinute($format = 'int')
605
    {
606
        $ts = $this->cE->dateToStamp(
607
            $this->year, $this->month, $this->day,
608
            $this->hour, $this->minute+1, 0);
609
        return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts));
610
    }
611
 
612
    /**
613
     * Returns the value for the previous second
614
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
615
     * @return int e.g. 43 or timestamp
616
     * @access public
617
     */
618
    function prevSecond($format = 'int')
619
    {
620
        $ts = $this->cE->dateToStamp(
621
            $this->year, $this->month, $this->day,
622
            $this->hour, $this->minute, $this->second-1);
623
        return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts));
624
    }
625
 
626
    /**
627
     * Returns the value for this second
628
    * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
629
     * @return int e.g. 44 or timestamp
630
     * @access public
631
     */
632
    function thisSecond($format = 'int')
633
    {
634
        $ts = $this->cE->dateToStamp(
635
            $this->year, $this->month, $this->day,
636
            $this->hour, $this->minute, $this->second);
637
        return $this->returnValue('Second', $format, $ts, $this->second);
638
    }
639
 
640
    /**
641
     * Returns the value for the next second
642
     * @param string return value format ['int' | 'timestamp' | 'object' | 'array']
643
     * @return int e.g. 45 or timestamp
644
     * @access public
645
     */
646
    function nextSecond($format = 'int')
647
    {
648
        $ts = $this->cE->dateToStamp(
649
            $this->year, $this->month, $this->day,
650
            $this->hour, $this->minute, $this->second+1);
651
        return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts));
652
    }
653
}
654
?>