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: Helper.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
21
//
22
/**
23
 * @package Calendar
24
 * @version $Id: Helper.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
25
 */
26
 
27
/**
28
 * Used by Calendar_Month_Weekdays, Calendar_Month_Weeks and Calendar_Week to
29
 * help with building the calendar in tabular form
30
 * @package Calendar
31
 * @access protected
32
 */
33
class Calendar_Table_Helper
34
{
35
    /**
36
     * Instance of the Calendar object being helped.
37
     * @var object
38
     * @access private
39
     */
40
    var $calendar;
41
 
42
    /**
43
     * Instance of the Calendar_Engine
44
     * @var object
45
     * @access private
46
     */
47
    var $cE;
48
 
49
    /**
50
     * First day of the week
51
     * @access private
52
     * @var string
53
     */
54
    var $firstDay;
55
 
56
    /**
57
     * The seven days of the week named
58
     * @access private
59
     * @var array
60
     */
61
    var $weekDays;
62
 
63
    /**
64
     * Days of the week ordered with $firstDay at the beginning
65
     * @access private
66
     * @var array
67
     */
68
    var $daysOfWeek = array();
69
 
70
    /**
71
     * Days of the month built from days of the week
72
     * @access private
73
     * @var array
74
     */
75
    var $daysOfMonth = array();
76
 
77
    /**
78
     * Number of weeks in month
79
     * @var int
80
     * @access private
81
     */
82
    var $numWeeks = null;
83
 
84
    /**
85
     * Number of emtpy days before real days begin in month
86
     * @var int
87
     * @access private
88
     */
89
    var $emptyBefore = 0;
90
 
91
    /**
92
     * Constructs Calendar_Table_Helper
93
     * @param object Calendar_Month_Weekdays, Calendar_Month_Weeks, Calendar_Week
94
     * @param int (optional) first day of the week e.g. 1 for Monday
95
     * @access protected
96
     */
97
    function Calendar_Table_Helper(& $calendar, $firstDay=false)
98
    {
99
        $this->calendar = & $calendar;
100
        $this->cE = & $calendar->getEngine();
101
        if ($firstDay === false) {
102
            $firstDay = $this->cE->getFirstDayOfWeek(
103
                $this->calendar->thisYear(),
104
                $this->calendar->thisMonth(),
105
                $this->calendar->thisDay()
106
            );
107
        }
108
        $this->firstDay = $firstDay;
109
        $this->setFirstDay();
110
        $this->setDaysOfMonth();
111
    }
112
 
113
    /**
114
     * Constructs $this->daysOfWeek based on $this->firstDay
115
     * @return void
116
     * @access private
117
     */
118
    function setFirstDay()
119
    {
120
        $weekDays = $this->cE->getWeekDays(
121
            $this->calendar->thisYear(),
122
            $this->calendar->thisMonth(),
123
            $this->calendar->thisDay()
124
            );
125
        $endDays  = array();
126
        $tmpDays  = array();
127
        $begin = false;
128
        foreach ($weekDays as $day) {
129
            if ($begin == true) {
130
                $endDays[] = $day;
131
            } else if ($day === $this->firstDay) {
132
                $begin = true;
133
                $endDays[] = $day;
134
            } else {
135
                $tmpDays[] = $day;
136
            }
137
        }
138
        $this->daysOfWeek = array_merge($endDays, $tmpDays);
139
    }
140
 
141
    /**
142
     * Constructs $this->daysOfMonth
143
     * @return void
144
     * @access private
145
     */
146
    function setDaysOfMonth()
147
    {
148
        $this->daysOfMonth = $this->daysOfWeek;
149
        $daysInMonth = $this->cE->getDaysInMonth(
150
            $this->calendar->thisYear(), $this->calendar->thisMonth());
151
        $firstDayInMonth = $this->cE->getFirstDayInMonth(
152
            $this->calendar->thisYear(), $this->calendar->thisMonth());
153
        $this->emptyBefore=0;
154
        foreach ($this->daysOfMonth as $dayOfWeek) {
155
            if ($firstDayInMonth == $dayOfWeek) {
156
                break;
157
            }
158
            $this->emptyBefore++;
159
        }
160
        $this->numWeeks = ceil(
161
            ($daysInMonth + $this->emptyBefore)
162
                /
163
            $this->cE->getDaysInWeek(
164
                $this->calendar->thisYear(),
165
                $this->calendar->thisMonth(),
166
                $this->calendar->thisDay()
167
            )
168
        );
169
        for ($i=1; $i < $this->numWeeks; $i++) {
170
            $this->daysOfMonth =
171
                array_merge($this->daysOfMonth, $this->daysOfWeek);
172
        }
173
    }
174
 
175
    /**
176
     * Returns the first day of the month
177
     * @see Calendar_Engine_Interface::getFirstDayOfWeek()
178
     * @return int
179
     * @access protected
180
     */
181
    function getFirstDay()
182
    {
183
        return $this->firstDay;
184
    }
185
 
186
    /**
187
     * Returns the order array of days in a week
188
     * @return int
189
     * @access protected
190
     */
191
    function getDaysOfWeek()
192
    {
193
        return $this->daysOfWeek;
194
    }
195
 
196
    /**
197
     * Returns the number of tabular weeks in a month
198
     * @return int
199
     * @access protected
200
     */
201
    function getNumWeeks()
202
    {
203
        return $this->numWeeks;
204
    }
205
 
206
    /**
207
     * Returns the number of real days + empty days
208
     * @return int
209
     * @access protected
210
     */
211
    function getNumTableDaysInMonth()
212
    {
213
        return count($this->daysOfMonth);
214
    }
215
 
216
    /**
217
     * Returns the number of empty days before the real days begin
218
     * @return int
219
     * @access protected
220
     */
221
    function getEmptyDaysBefore()
222
    {
223
        return $this->emptyBefore;
224
    }
225
 
226
    /**
227
     * Returns the index of the last real day in the month
228
     * @todo Potential performance optimization with static
229
     * @return int
230
     * @access protected
231
     */
232
    function getEmptyDaysAfter()
233
    {
234
        // Causes bug when displaying more than one month
235
//        static $index;
236
//        if (!isset($index)) {
237
            $index = $this->getEmptyDaysBefore() + $this->cE->getDaysInMonth(
238
                $this->calendar->thisYear(), $this->calendar->thisMonth());
239
//        }
240
        return $index;
241
    }
242
 
243
    /**
244
     * Returns the index of the last real day in the month, relative to the
245
     * beginning of the tabular week it is part of
246
     * @return int
247
     * @access protected
248
     */
249
    function getEmptyDaysAfterOffset()
250
    {
251
        $eAfter = $this->getEmptyDaysAfter();
252
        return $eAfter - (
253
            $this->cE->getDaysInWeek(
254
                $this->calendar->thisYear(),
255
                $this->calendar->thisMonth(),
256
                $this->calendar->thisDay()
257
            ) * ($this->numWeeks-1) );
258
    }
259
 
260
    /**
261
     * Returns the timestamp of the first day of the current week
262
     */
263
    function getWeekStart($y, $m, $d, $firstDay=1)
264
    {
265
        $dow = $this->cE->getDayOfWeek($y, $m, $d);
266
        if ($dow > $firstDay) {
267
            $d -= ($dow - $firstDay);
268
        }
269
        if ($dow < $firstDay) {
270
            $d -= (
271
                $this->cE->getDaysInWeek(
272
                    $this->calendar->thisYear(),
273
                    $this->calendar->thisMonth(),
274
                    $this->calendar->thisDay()
275
                ) - $firstDay + $dow);
276
        }
277
        return $this->cE->dateToStamp($y, $m, $d);
278
    }
279
}
280
?>