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: Week.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
22
//
23
/**
24
 * @package Calendar
25
 * @version $Id: Week.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 base class
38
 */
39
require_once CALENDAR_ROOT.'Calendar.php';
40
 
41
/**
42
 * Represents a Week and builds Days in tabular format<br>
43
 * <code>
44
 * require_once 'Calendar'.DIRECTORY_SEPARATOR.'Week.php';
45
 * $Week = & new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week
46
 * echo '<tr>';
47
 * while ($Day = & $Week->fetch()) {
48
 *     if ($Day->isEmpty()) {
49
 *         echo '<td>&nbsp;</td>';
50
 *     } else {
51
 *         echo '<td>'.$Day->thisDay().'</td>';
52
 *      }
53
 * }
54
 * echo '</tr>';
55
 * </code>
56
 * @package Calendar
57
 * @access public
58
 */
59
class Calendar_Week extends Calendar
60
{
61
    /**
62
     * Instance of Calendar_Table_Helper
63
     * @var Calendar_Table_Helper
64
     * @access private
65
     */
66
    var $tableHelper;
67
 
68
    /**
69
     * Stores the timestamp of the first day of this week
70
     * @access private
71
     * @var object
72
     */
73
    var $thisWeek;
74
 
75
    /**
76
     * Stores the timestamp of first day of previous week
77
     * @access private
78
     * @var object
79
     */
80
    var $prevWeek;
81
 
82
    /**
83
     * Stores the timestamp of first day of next week
84
     * @access private
85
     * @var object
86
     */
87
    var $nextWeek;
88
 
89
    /**
90
     * Used by build() to set empty days
91
     * @access private
92
     * @var boolean
93
     */
94
    var $firstWeek = false;
95
 
96
    /**
97
     * Used by build() to set empty days
98
     * @access private
99
     * @var boolean
100
     */
101
    var $lastWeek = false;
102
 
103
    /**
104
     * First day of the week (0=sunday, 1=monday...)
105
     * @access private
106
     * @var boolean
107
     */
108
    var $firstDay = 1;
109
 
110
    /**
111
     * Constructs Week
112
     * @param int year e.g. 2003
113
     * @param int month e.g. 5
114
     * @param int a day of the desired week
115
     * @param int (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.)
116
     * @access public
117
     */
118
    function Calendar_Week($y, $m, $d, $firstDay=false)
119
    {
120
        require_once CALENDAR_ROOT.'Table'.DIRECTORY_SEPARATOR.'Helper.php';
121
        Calendar::Calendar($y, $m, $d);
122
        if ($firstDay !== false) {
123
            $this->firstDay = $firstDay;
124
        }
125
        $this->tableHelper = & new Calendar_Table_Helper($this, $firstDay);
126
        $this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $firstDay);
127
        $this->prevWeek = $this->tableHelper->getWeekStart($y, $m, $d - $this->cE->getDaysInWeek(
128
            $this->thisYear(),
129
            $this->thisMonth(),
130
            $this->thisDay()), $firstDay);
131
        $this->nextWeek = $this->tableHelper->getWeekStart($y, $m, $d + $this->cE->getDaysInWeek(
132
            $this->thisYear(),
133
            $this->thisMonth(),
134
            $this->thisDay()), $firstDay);
135
    }
136
 
137
    /**
138
     * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values
139
     * passed to the constructor
140
     * @param int|string Unix or ISO-8601 timestamp
141
     * @return void
142
     * @access public
143
     */
144
    function setTimestamp($ts)
145
    {
146
        parent::setTimestamp($ts);
147
 
148
            $this->year, $this->month, $this->day, $this->firstDay
149
        );
150
        $this->prevWeek = $this->tableHelper->getWeekStart(
151
 
152
                $this->thisYear(),
153
                $this->thisMonth(),
154
                $this->thisDay()), $this->firstDay
155
        );
156
        $this->nextWeek = $this->tableHelper->getWeekStart(
157
            $this->year, $this->month, $this->day + $this->cE->getDaysInWeek(
158
 
159
                $this->thisMonth(),
160
                $this->thisDay()), $this->firstDay
161
        );
162
    }
163
164
    /**
165
     * Builds Calendar_Day objects for this Week
166
 
167
     * @return boolean
168
     * @access public
169
     */
170
    function build($sDates = array())
171
    {
172
        require_once CALENDAR_ROOT.'Day.php';
173
        $year  = $this->cE->stampToYear($this->thisWeek);
174
        $month = $this->cE->stampToMonth($this->thisWeek);
175
        $day   = $this->cE->stampToDay($this->thisWeek);
176
        $end   = $this->cE->getDaysInWeek(
177
            $this->thisYear(),
178
            $this->thisMonth(),
179
            $this->thisDay()
180
        );
181
182
        for ($i=1; $i <= $end; $i++) {
183
            $stamp = $this->cE->dateToStamp($year, $month, $day++);
184
 
185
                                $this->cE->stampToYear($stamp),
186
                                $this->cE->stampToMonth($stamp),
187
                                $this->cE->stampToDay($stamp));
188
        }
189
190
        //set empty days (@see Calendar_Month_Weeks::build())
191
        if ($this->firstWeek) {
192
 
193
            for ($i=1; $i <= $eBefore; $i++) {
194
                $this->children[$i]->setEmpty();
195
            }
196
        }
197
        if ($this->lastWeek) {
198
            $eAfter = $this->tableHelper->getEmptyDaysAfterOffset();
199
            for ($i = $eAfter+1; $i <= $end; $i++) {
200
                $this->children[$i]->setEmpty();
201
            }
202
        }
203
204
        if (count($sDates) > 0) {
205
            $this->setSelection($sDates);
206
 
207
        return true;
208
    }
209
210
    /**
211
     * @param boolean
212
 
213
     * @access private
214
     */
215
    function setFirst($state=true)
216
    {
217
        $this->firstWeek = $state;
218
    }
219
220
    /**
221
     * @param boolean
222
 
223
     * @access private
224
     */
225
    function setLast($state=true)
226
    {
227
        $this->lastWeek = $state;
228
    }
229
230
    /**
231
     * Called from build()
232
 
233
     * @return void
234
     * @access private
235
     */
236
    function setSelection($sDates)
237
    {
238
        foreach ($sDates as $sDate) {
239
            foreach ($this->children as $key => $child) {
240
                if ($child->thisDay() == $sDate->thisDay() &&
241
                    $child->thisMonth() == $sDate->thisMonth() &&
242
 
243
                ) {
244
 
245
                    $this->children[$key]->setSelected();
246
 
247
            }
248
 
249
        reset($this->children);
250
    }
251
 
252
    /**
253
 
254
     *
255
 
256
     * @return mixed
257
 
258
     */
259
 
260
    {
261
 
262
            case 'int':
263
 
264
                return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1;
265
 
266
            case 'n_in_year':
267
                return $this->cE->getWeekNInYear(
268
                    $this->cE->stampToYear($this->prevWeek),
269
                    $this->cE->stampToMonth($this->prevWeek),
270
                    $this->cE->stampToDay($this->prevWeek));
271
                break;
272
            case 'array':
273
                return $this->toArray($this->prevWeek);
274
                break;
275
            case 'object':
276
                require_once CALENDAR_ROOT.'Factory.php';
277
                return Calendar_Factory::createByTimestamp('Week',$this->prevWeek);
278
                break;
279
            case 'timestamp':
280
            default:
281
                return $this->prevWeek;
282
                break;
283
        }
284
    }
285
286
    /**
287
     * Gets the value of the current week, according to the requested format
288
     *
289
     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
290
     * @return mixed
291
     * @access public
292
     */
293
    function thisWeek($format = 'n_in_month')
294
    {
295
        switch (strtolower($format)) {
296
            case 'int':
297
            case 'n_in_month':
298
                if ($this->firstWeek) {
299
 
300
                }
301
                if ($this->lastWeek) {
302
                    return $this->cE->getWeeksInMonth(
303
                        $this->cE->stampToYear($this->thisWeek),
304
                        $this->cE->stampToMonth($this->thisWeek),
305
                        $this->firstDay);
306
                }
307
                return $this->cE->getWeekNInMonth(
308
                    $this->cE->stampToYear($this->thisWeek),
309
                    $this->cE->stampToMonth($this->thisWeek),
310
                    $this->cE->stampToDay($this->thisWeek),
311
                    $this->firstDay);
312
                break;
313
            case 'n_in_year':
314
                return $this->cE->getWeekNInYear(
315
                    $this->cE->stampToYear($this->thisWeek),
316
                    $this->cE->stampToMonth($this->thisWeek),
317
                    $this->cE->stampToDay($this->thisWeek));
318
                break;
319
            case 'array':
320
                return $this->toArray($this->thisWeek);
321
                break;
322
            case 'object':
323
                require_once CALENDAR_ROOT.'Factory.php';
324
                return Calendar_Factory::createByTimestamp('Week',$this->thisWeek);
325
                break;
326
            case 'timestamp':
327
            default:
328
                return $this->thisWeek;
329
                break;
330
        }
331
    }
332
333
    /**
334
     * Gets the value of the following week, according to the requested format
335
     *
336
     * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array']
337
     * @return mixed
338
     * @access public
339
     */
340
    function nextWeek($format = 'n_in_month')
341
    {
342
        switch (strtolower($format)) {
343
            case 'int':
344
            case 'n_in_month':
345
                return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1;
346
 
347
            case 'n_in_year':
348
                return $this->cE->getWeekNInYear(
349
                    $this->cE->stampToYear($this->nextWeek),
350
                    $this->cE->stampToMonth($this->nextWeek),
351
                    $this->cE->stampToDay($this->nextWeek));
352
                break;
353
            case 'array':
354
                return $this->toArray($this->nextWeek);
355
                break;
356
            case 'object':
357
                require_once CALENDAR_ROOT.'Factory.php';
358
                return Calendar_Factory::createByTimestamp('Week',$this->nextWeek);
359
                break;
360
            case 'timestamp':
361
            default:
362
                    return $this->nextWeek;
363
                    break;
364
        }
365
    }
366
367
    /**
368
     * Returns the instance of Calendar_Table_Helper.
369
     * Called from Calendar_Validator::isValidWeek
370
     * @return Calendar_Table_Helper
371
     * @access protected
372
     */
373
    function & getHelper()
374
    {
375
        return $this->tableHelper;
376
    }
377
378
    /**
379
     * Makes sure theres a value for $this->day
380
 
381
     * @access private
382
     */
383
    function findFirstDay()
384
    {
385
        if (!count($this->children) > 0) {
386
            $this->build();
387
            foreach ($this->children as $Day) {
388
                if (!$Day->isEmpty()) {
389
                    $this->day = $Day->thisDay();
390
                    break;
391
 
392
            }
393
        }
394
    }
395
}
396
?>