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: Weekday.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
22
//
23
/**
24
 * @package Calendar
25
 * @version $Id: Weekday.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
26
 */
27
 
28
/**
29
 * Allows Calendar include path to be redefined
30
 * @ignore
31
 */
32
if (!defined('CALENDAR_ROOT')) {
33
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
34
}
35
 
36
/**
37
 * Load Calendar decorator base class
38
 */
39
require_once CALENDAR_ROOT.'Decorator.php';
40
 
41
/**
42
 * Load a Calendar_Day
43
 */
44
require_once CALENDAR_ROOT.'Day.php';
45
/**
46
 * Decorator for fetching the day of the week
47
 * <code>
48
 * $Day = new Calendar_Day(2003, 10, 23);
49
 * $Weekday = & new Calendar_Decorator_Weekday($Day);
50
 * $Weekday->setFirstDay(0); // Set first day of week to Sunday (default Mon)
51
 * echo $Weekday->thisWeekDay(); // Displays 5 - fifth day of week relative to Sun
52
 * </code>
53
 * @package Calendar
54
 * @access public
55
 */
56
class Calendar_Decorator_Weekday extends Calendar_Decorator
57
{
58
    /**
59
     * First day of week
60
     * @var int (default = 1 for Monday)
61
     * @access private
62
     */
63
    var $firstDay = 1;
64
 
65
    /**
66
     * Constructs Calendar_Decorator_Weekday
67
     * @param object subclass of Calendar
68
     * @access public
69
     */
70
    function Calendar_Decorator_Weekday(& $Calendar)
71
    {
72
        parent::Calendar_Decorator($Calendar);
73
    }
74
 
75
    /**
76
     * Sets the first day of the week (0 = Sunday, 1 = Monday (default) etc)
77
     * @param int first day of week
78
     * @return void
79
     * @access public
80
     */
81
    function setFirstDay($firstDay) {
82
        $this->firstDay = (int)$firstDay;
83
    }
84
 
85
    /**
86
     * Returns the previous weekday
87
     * @param string (default = 'int') return value format
88
     * @return int numeric day of week or timestamp
89
     * @access public
90
     */
91
    function prevWeekDay($format = 'int')
92
    {
93
        $ts = $this->calendar->prevDay('timestamp');
94
        $Day = new Calendar_Day(2000,1,1);
95
        $Day->setTimeStamp($ts);
96
        $day = $this->calendar->cE->getDayOfWeek($Day->thisYear(),$Day->thisMonth(),$Day->thisDay());
97
        $day = $this->adjustWeekScale($day);
98
        return $this->returnValue('Day', $format, $ts, $day);
99
    }
100
 
101
    /**
102
     * Returns the current weekday
103
     * @param string (default = 'int') return value format
104
     * @return int numeric day of week or timestamp
105
     * @access public
106
     */
107
    function thisWeekDay($format = 'int')
108
    {
109
        $ts = $this->calendar->thisDay('timestamp');
110
        $day = $this->calendar->cE->getDayOfWeek($this->calendar->year,$this->calendar->month,$this->calendar->day);
111
        $day = $this->adjustWeekScale($day);
112
        return $this->returnValue('Day', $format, $ts, $day);
113
    }
114
 
115
    /**
116
     * Returns the next weekday
117
     * @param string (default = 'int') return value format
118
     * @return int numeric day of week or timestamp
119
     * @access public
120
     */
121
    function nextWeekDay($format = 'int')
122
    {
123
        $ts = $this->calendar->nextDay('timestamp');
124
        $Day = new Calendar_Day(2000,1,1);
125
        $Day->setTimeStamp($ts);
126
        $day = $this->calendar->cE->getDayOfWeek($Day->thisYear(),$Day->thisMonth(),$Day->thisDay());
127
        $day = $this->adjustWeekScale($day);
128
        return $this->returnValue('Day', $format, $ts, $day);
129
    }
130
 
131
    /**
132
     * Adjusts the day of the week relative to the first day of the week
133
     * @param int day of week calendar from Calendar_Engine
134
     * @return int day of week adjusted to first day
135
     * @access private
136
     */
137
    function adjustWeekScale($dayOfWeek) {
138
        $dayOfWeek = $dayOfWeek - $this->firstDay;
139
        if ( $dayOfWeek >= 0 ) {
140
            return $dayOfWeek;
141
        } else {
142
            return $this->calendar->cE->getDaysInWeek(
143
                $this->calendar->year,$this->calendar->month,$this->calendar->day
144
                ) + $dayOfWeek;
145
        }
146
    }
147
}
148
?>