source: branches/version-2_13_0/data/module/Calendar/Calendar.php @ 23126

Revision 23126, 22.5 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

  • Property svn:eol-style set to LF
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Contains the Calendar and Calendar_Engine_Factory classes
6 *
7 * PHP versions 4 and 5
8 *
9 * LICENSE: Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * @category  Date and Time
31 * @package   Calendar
32 * @author    Harry Fuecks <hfuecks@phppatterns.com>
33 * @author    Lorenzo Alberton <l.alberton@quipo.it>
34 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
35 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36 * @version   CVS: $Id: Calendar.php 300729 2010-06-24 12:05:53Z quipo $
37 * @link      http://pear.php.net/package/Calendar
38 */
39
40/**
41 * Allows Calendar include path to be redefined
42 */
43if (!defined('CALENDAR_ROOT')) {
44    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
45}
46
47/**
48 * Constant which defines the calculation engine to use
49 */
50if (!defined('CALENDAR_ENGINE')) {
51    define('CALENDAR_ENGINE', 'UnixTS');
52}
53
54/**
55 * Define Calendar Month states
56 */
57define('CALENDAR_USE_MONTH',          1);
58define('CALENDAR_USE_MONTH_WEEKDAYS', 2);
59define('CALENDAR_USE_MONTH_WEEKS',    3);
60
61/**
62 * Contains a factory method to return a Singleton instance of a class
63 * implementing the Calendar_Engine_Interface.<br>
64 * <b>Note:</b> this class must be modified to "register" alternative
65 * Calendar_Engines. The engine used can be controlled with the constant
66 * CALENDAR_ENGINE
67 *
68 * @category  Date and Time
69 * @package   Calendar
70 * @author    Harry Fuecks <hfuecks@phppatterns.com>
71 * @author    Lorenzo Alberton <l.alberton@quipo.it>
72 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
73 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
74 * @link      http://pear.php.net/package/Calendar
75 * @see       Calendar_Engine_Interface
76 * @access    protected
77 */
78class Calendar_Engine_Factory
79{
80    /**
81     * Returns an instance of the engine
82     *
83     * @return object instance of a calendar calculation engine
84     * @access protected
85     */
86    function & getEngine()
87    {
88        static $engine = false;
89        switch (CALENDAR_ENGINE) {
90        case 'PearDate':
91            $class = 'Calendar_Engine_PearDate';
92            break;
93        case 'UnixTS':
94        default:
95            $class = 'Calendar_Engine_UnixTS';
96            break;
97        }
98        if (!$engine) {
99            if (!class_exists($class)) {
100                include_once CALENDAR_ROOT.'Engine'.DIRECTORY_SEPARATOR.CALENDAR_ENGINE.'.php';
101            }
102            $engine = new $class;
103        }
104        return $engine;
105    }
106}
107
108/**
109 * Base class for Calendar API. This class should not be instantiated directly.
110 *
111 * @category  Date and Time
112 * @package   Calendar
113 * @author    Harry Fuecks <hfuecks@phppatterns.com>
114 * @author    Lorenzo Alberton <l.alberton@quipo.it>
115 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
116 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
117 * @link      http://pear.php.net/package/Calendar
118 * @abstract
119 */
120class Calendar
121{
122    /**
123     * Instance of class implementing calendar engine interface
124     * @var object
125     * @access private
126     */
127    var $cE;
128
129    /**
130     * Instance of Calendar_Validator (lazy initialized when isValid() or
131     * getValidor() is called
132     * @var Calendar_Validator
133     * @access private
134     */
135    var $validator;
136
137    /**
138     * Year for this calendar object e.g. 2003
139     * @access private
140     * @var int
141     */
142    var $year;
143
144    /**
145     * Month for this calendar object e.g. 9
146     * @access private
147     * @var int
148     */
149    var $month;
150
151    /**
152     * Day of month for this calendar object e.g. 23
153     * @access private
154     * @var int
155     */
156    var $day;
157
158    /**
159     * Hour of day for this calendar object e.g. 13
160     * @access private
161     * @var int
162     */
163    var $hour;
164
165    /**
166     * Minute of hour this calendar object e.g. 46
167     * @access private
168     * @var int
169     */
170    var $minute;
171
172    /**
173     * Second of minute this calendar object e.g. 34
174     * @access private
175     * @var int
176     */
177    var $second;
178
179    /**
180     * Marks this calendar object as selected (e.g. 'today')
181     * @access private
182     * @var boolean
183     */
184    var $selected = false;
185
186    /**
187     * Collection of child calendar objects created from subclasses
188     * of Calendar. Type depends on the object which created them.
189     * @access private
190     * @var array
191     */
192    var $children = array();
193
194    /**
195     * Constructs the Calendar
196     *
197     * @param int $y year
198     * @param int $m month
199     * @param int $d day
200     * @param int $h hour
201     * @param int $i minute
202     * @param int $s second
203     *
204     * @access protected
205     */
206    function Calendar($y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0)
207    {
208        static $cE = null;
209        if (!isset($cE)) {
210            $cE = & Calendar_Engine_Factory::getEngine();
211        }
212        $this->cE     = & $cE;
213        $this->year   = (int)$y;
214        $this->month  = (int)$m;
215        $this->day    = (int)$d;
216        $this->hour   = (int)$h;
217        $this->minute = (int)$i;
218        $this->second = (int)$s;
219    }
220
221    /**
222     * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values
223     * passed to the constructor
224     *
225     * @param int|string $ts Unix or ISO-8601 timestamp
226     *
227     * @return void
228     * @access public
229     */
230    function setTimestamp($ts)
231    {
232        $this->year   = $this->cE->stampToYear($ts);
233        $this->month  = $this->cE->stampToMonth($ts);
234        $this->day    = $this->cE->stampToDay($ts);
235        $this->hour   = $this->cE->stampToHour($ts);
236        $this->minute = $this->cE->stampToMinute($ts);
237        $this->second = $this->cE->stampToSecond($ts);
238    }
239
240    /**
241     * Returns a timestamp from the current date / time values. Format of
242     * timestamp depends on Calendar_Engine implementation being used
243     *
244     * @return int|string timestamp
245     * @access public
246     */
247    function getTimestamp()
248    {
249        return $this->cE->dateToStamp(
250            $this->year, $this->month, $this->day,
251            $this->hour, $this->minute, $this->second);
252    }
253
254    /**
255     * Defines calendar object as selected (e.g. for today)
256     *
257     * @param boolean $state whether Calendar subclass
258     *
259     * @return void
260     * @access public
261     */
262    function setSelected($state = true)
263    {
264        $this->selected = $state;
265    }
266
267    /**
268     * True if the calendar subclass object is selected (e.g. today)
269     *
270     * @return boolean
271     * @access public
272     */
273    function isSelected()
274    {
275        return $this->selected;
276    }
277
278    /**
279     * Checks if the current Calendar object is today's date
280     *
281     * @return boolean
282     * @access public
283     */
284    function isToday()
285    {
286        return $this->cE->isToday($this->getTimeStamp());
287    }
288
289    /**
290     * Adjusts the date (helper method)
291     *
292     * @return void
293     * @access public
294     */
295    function adjust()
296    {
297        $stamp        = $this->getTimeStamp();
298        $this->year   = $this->cE->stampToYear($stamp);
299        $this->month  = $this->cE->stampToMonth($stamp);
300        $this->day    = $this->cE->stampToDay($stamp);
301        $this->hour   = $this->cE->stampToHour($stamp);
302        $this->minute = $this->cE->stampToMinute($stamp);
303        $this->second = $this->cE->stampToSecond($stamp);
304    }
305
306    /**
307     * Returns the date as an associative array (helper method)
308     *
309     * @param mixed $stamp timestamp (leave empty for current timestamp)
310     *
311     * @return array
312     * @access public
313     */
314    function toArray($stamp=null)
315    {
316        if (is_null($stamp)) {
317            $stamp = $this->getTimeStamp();
318        }
319        return array(
320            'year'   => $this->cE->stampToYear($stamp),
321            'month'  => $this->cE->stampToMonth($stamp),
322            'day'    => $this->cE->stampToDay($stamp),
323            'hour'   => $this->cE->stampToHour($stamp),
324            'minute' => $this->cE->stampToMinute($stamp),
325            'second' => $this->cE->stampToSecond($stamp)
326        );
327    }
328
329    /**
330     * Returns the value as an associative array (helper method)
331     *
332     * @param string $returnType type of date object that return value represents
333     * @param string $format     ['int' | 'array' | 'timestamp' | 'object']
334     * @param mixed  $stamp      timestamp (depending on Calendar engine being used)
335     * @param int    $default    default value (i.e. give me the answer quick)
336     *
337     * @return mixed
338     * @access private
339     */
340    function returnValue($returnType, $format, $stamp, $default)
341    {
342        switch (strtolower($format)) {
343        case 'int':
344            return $default;
345        case 'array':
346            return $this->toArray($stamp);
347            break;
348        case 'object':
349            include_once CALENDAR_ROOT.'Factory.php';
350            return Calendar_Factory::createByTimestamp($returnType, $stamp);
351            break;
352        case 'timestamp':
353        default:
354            return $stamp;
355            break;
356        }
357    }
358
359    /**
360     * Abstract method for building the children of a calendar object.
361     * Implemented by Calendar subclasses
362     *
363     * @param array $sDates array containing Calendar objects to select (optional)
364     *
365     * @return boolean
366     * @access public
367     * @abstract
368     */
369    function build($sDates = array())
370    {
371        include_once 'PEAR.php';
372        PEAR::raiseError('Calendar::build is abstract', null, PEAR_ERROR_TRIGGER,
373            E_USER_NOTICE, 'Calendar::build()');
374        return false;
375    }
376
377    /**
378     * Abstract method for selected data objects called from build
379     *
380     * @param array $sDates array of Calendar objects to select
381     *
382     * @return boolean
383     * @access public
384     * @abstract
385     */
386    function setSelection($sDates)
387    {
388        include_once 'PEAR.php';
389        PEAR::raiseError(
390            'Calendar::setSelection is abstract', null, PEAR_ERROR_TRIGGER,
391            E_USER_NOTICE, 'Calendar::setSelection()');
392        return false;
393    }
394
395    /**
396     * Iterator method for fetching child Calendar subclass objects
397     * (e.g. a minute from an hour object). On reaching the end of
398     * the collection, returns false and resets the collection for
399     * further iteratations.
400     *
401     * @return mixed either an object subclass of Calendar or false
402     * @access public
403     */
404    function fetch()
405    {
406        $child = each($this->children);
407        if ($child) {
408            return $child['value'];
409        } else {
410            reset($this->children);
411            return false;
412        }
413    }
414
415    /**
416     * Fetches all child from the current collection of children
417     *
418     * @return array
419     * @access public
420     */
421    function fetchAll()
422    {
423        return $this->children;
424    }
425
426    /**
427     * Get the number Calendar subclass objects stored in the internal collection
428     *
429     * @return int
430     * @access public
431     */
432    function size()
433    {
434        return count($this->children);
435    }
436
437    /**
438     * Determine whether this date is valid, with the bounds determined by
439     * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid
440     *
441     * @return boolean
442     * @access public
443     */
444    function isValid()
445    {
446        $validator = & $this->getValidator();
447        return $validator->isValid();
448    }
449
450    /**
451     * Returns an instance of Calendar_Validator
452     *
453     * @return Calendar_Validator
454     * @access public
455     */
456    function & getValidator()
457    {
458        if (!isset($this->validator)) {
459            include_once CALENDAR_ROOT.'Validator.php';
460            $this->validator = new Calendar_Validator($this);
461        }
462        return $this->validator;
463    }
464
465    /**
466     * Returns a reference to the current Calendar_Engine being used. Useful
467     * for Calendar_Table_Helper and Calendar_Validator
468     *
469     * @return object implementing Calendar_Engine_Inteface
470     * @access protected
471     */
472    function & getEngine()
473    {
474        return $this->cE;
475    }
476
477    /**
478     * Set the CALENDAR_FIRST_DAY_OF_WEEK constant to the $firstDay value
479     * if the constant is not set yet.
480     *
481     * @param integer $firstDay first day of the week (0=sunday, 1=monday, ...)
482     *
483     * @return integer
484     * @throws E_USER_WARNING this method throws a WARNING if the
485     *    CALENDAR_FIRST_DAY_OF_WEEK constant is already defined and
486     *    the $firstDay parameter is set to a different value
487     * @access protected
488     */
489    function defineFirstDayOfWeek($firstDay = null)
490    {
491        if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) {
492            if (!is_null($firstDay) && ($firstDay != CALENDAR_FIRST_DAY_OF_WEEK)) {
493                $msg = 'CALENDAR_FIRST_DAY_OF_WEEK constant already defined.'
494                  .' The $firstDay parameter will be ignored.';
495                trigger_error($msg, E_USER_WARNING);
496            }
497            return CALENDAR_FIRST_DAY_OF_WEEK;
498        }
499        if (is_null($firstDay)) {
500            $firstDay = $this->cE->getFirstDayOfWeek(
501                $this->thisYear(),
502                $this->thisMonth(),
503                $this->thisDay()
504            );
505        }
506        define ('CALENDAR_FIRST_DAY_OF_WEEK', $firstDay);
507        return CALENDAR_FIRST_DAY_OF_WEEK;
508    }
509
510    /**
511     * Returns the value for the previous year
512     *
513     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
514     *
515     * @return int e.g. 2002 or timestamp
516     * @access public
517     */
518    function prevYear($format = 'int')
519    {
520        $ts = $this->cE->dateToStamp($this->year-1, 1, 1, 0, 0, 0);
521        return $this->returnValue('Year', $format, $ts, $this->year-1);
522    }
523
524    /**
525     * Returns the value for this year
526     *
527     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
528     *
529     * @return int e.g. 2003 or timestamp
530     * @access public
531     */
532    function thisYear($format = 'int')
533    {
534        $ts = $this->cE->dateToStamp($this->year, 1, 1, 0, 0, 0);
535        return $this->returnValue('Year', $format, $ts, $this->year);
536    }
537
538    /**
539     * Returns the value for next year
540     *
541     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
542     *
543     * @return int e.g. 2004 or timestamp
544     * @access public
545     */
546    function nextYear($format = 'int')
547    {
548        $ts = $this->cE->dateToStamp($this->year+1, 1, 1, 0, 0, 0);
549        return $this->returnValue('Year', $format, $ts, $this->year+1);
550    }
551
552    /**
553     * Returns the value for the previous month
554     *
555     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
556     *
557     * @return int e.g. 4 or Unix timestamp
558     * @access public
559     */
560    function prevMonth($format = 'int')
561    {
562        $ts = $this->cE->dateToStamp($this->year, $this->month-1, 1, 0, 0, 0);
563        return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts));
564    }
565
566    /**
567     * Returns the value for this month
568     *
569     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
570     *
571     * @return int e.g. 5 or timestamp
572     * @access public
573     */
574    function thisMonth($format = 'int')
575    {
576        $ts = $this->cE->dateToStamp($this->year, $this->month, 1, 0, 0, 0);
577        return $this->returnValue('Month', $format, $ts, $this->month);
578    }
579
580    /**
581     * Returns the value for next month
582     *
583     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
584     *
585     * @return int e.g. 6 or timestamp
586     * @access public
587     */
588    function nextMonth($format = 'int')
589    {
590        $ts = $this->cE->dateToStamp($this->year, $this->month+1, 1, 0, 0, 0);
591        return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts));
592    }
593
594    /**
595     * Returns the value for the previous day
596     *
597     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
598     *
599     * @return int e.g. 10 or timestamp
600     * @access public
601     */
602    function prevDay($format = 'int')
603    {
604        $ts = $this->cE->dateToStamp(
605            $this->year, $this->month, $this->day-1, 0, 0, 0);
606        return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts));
607    }
608
609    /**
610     * Returns the value for this day
611     *
612     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
613     *
614     * @return int e.g. 11 or timestamp
615     * @access public
616     */
617    function thisDay($format = 'int')
618    {
619        $ts = $this->cE->dateToStamp(
620            $this->year, $this->month, $this->day, 0, 0, 0);
621        return $this->returnValue('Day', $format, $ts, $this->day);
622    }
623
624    /**
625     * Returns the value for the next day
626     *
627     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
628     *
629     * @return int e.g. 12 or timestamp
630     * @access public
631     */
632    function nextDay($format = 'int')
633    {
634        $ts = $this->cE->dateToStamp(
635            $this->year, $this->month, $this->day+1, 0, 0, 0);
636        return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts));
637    }
638
639    /**
640     * Returns the value for the previous hour
641     *
642     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
643     *
644     * @return int e.g. 13 or timestamp
645     * @access public
646     */
647    function prevHour($format = 'int')
648    {
649        $ts = $this->cE->dateToStamp(
650            $this->year, $this->month, $this->day, $this->hour-1, 0, 0);
651        return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts));
652    }
653
654    /**
655     * Returns the value for this hour
656     *
657     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
658     *
659     * @return int e.g. 14 or timestamp
660     * @access public
661     */
662    function thisHour($format = 'int')
663    {
664        $ts = $this->cE->dateToStamp(
665            $this->year, $this->month, $this->day, $this->hour, 0, 0);
666        return $this->returnValue('Hour', $format, $ts, $this->hour);
667    }
668
669    /**
670     * Returns the value for the next hour
671     *
672     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
673     *
674     * @return int e.g. 14 or timestamp
675     * @access public
676     */
677    function nextHour($format = 'int')
678    {
679        $ts = $this->cE->dateToStamp(
680            $this->year, $this->month, $this->day, $this->hour+1, 0, 0);
681        return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts));
682    }
683
684    /**
685     * Returns the value for the previous minute
686     *
687     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
688     *
689     * @return int e.g. 23 or timestamp
690     * @access public
691     */
692    function prevMinute($format = 'int')
693    {
694        $ts = $this->cE->dateToStamp(
695            $this->year, $this->month, $this->day,
696            $this->hour, $this->minute-1, 0);
697        return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts));
698    }
699
700    /**
701     * Returns the value for this minute
702     *
703     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
704     *
705     * @return int e.g. 24 or timestamp
706     * @access public
707     */
708    function thisMinute($format = 'int')
709    {
710        $ts = $this->cE->dateToStamp(
711            $this->year, $this->month, $this->day,
712            $this->hour, $this->minute, 0);
713        return $this->returnValue('Minute', $format, $ts, $this->minute);
714    }
715
716    /**
717    * Returns the value for the next minute
718    *
719     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
720     *
721     * @return int e.g. 25 or timestamp
722     * @access public
723     */
724    function nextMinute($format = 'int')
725    {
726        $ts = $this->cE->dateToStamp(
727            $this->year, $this->month, $this->day,
728            $this->hour, $this->minute+1, 0);
729        return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts));
730    }
731
732    /**
733     * Returns the value for the previous second
734     *
735     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
736     *
737     * @return int e.g. 43 or timestamp
738     * @access public
739     */
740    function prevSecond($format = 'int')
741    {
742        $ts = $this->cE->dateToStamp(
743            $this->year, $this->month, $this->day,
744            $this->hour, $this->minute, $this->second-1);
745        return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts));
746    }
747
748    /**
749     * Returns the value for this second
750     *
751    * @param string $format return value format ['int'|'timestamp'|'object'|'array']
752    *
753     * @return int e.g. 44 or timestamp
754     * @access public
755     */
756    function thisSecond($format = 'int')
757    {
758        $ts = $this->cE->dateToStamp(
759            $this->year, $this->month, $this->day,
760            $this->hour, $this->minute, $this->second);
761        return $this->returnValue('Second', $format, $ts, $this->second);
762    }
763
764    /**
765     * Returns the value for the next second
766     *
767     * @param string $format return value format ['int'|'timestamp'|'object'|'array']
768     *
769     * @return int e.g. 45 or timestamp
770     * @access public
771     */
772    function nextSecond($format = 'int')
773    {
774        $ts = $this->cE->dateToStamp(
775            $this->year, $this->month, $this->day,
776            $this->hour, $this->minute, $this->second+1);
777        return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts));
778    }
779}
780?>
Note: See TracBrowser for help on using the repository browser.