Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
 
4
/**
5
 * Contains the Calendar_Util_Textual class
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: Textual.php 247250 2007-11-28 19:42:01Z quipo $
37
 * @link      http://pear.php.net/package/Calendar
38
 */
39
 
40
/**
41
 * @package Calendar
42
 * @version $Id: Textual.php 247250 2007-11-28 19:42:01Z quipo $
43
 */
44
 
45
/**
46
 * Allows Calendar include path to be redefined
47
 * @ignore
48
 */
49
if (!defined('CALENDAR_ROOT')) {
50
    define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
51
}
52
 
53
/**
54
 * Load Calendar decorator base class
55
 */
56
require_once CALENDAR_ROOT.'Decorator.php';
57
 
58
/**
59
 * Static utlities to help with fetching textual representations of months and
60
 * days of the week.
61
 *
62
 * @category  Date and Time
63
 * @package   Calendar
64
 * @author    Harry Fuecks <hfuecks@phppatterns.com>
65
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
66
 * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton
67
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
68
 * @link      http://pear.php.net/package/Calendar
69
 * @access    public
70
 */
71
class Calendar_Util_Textual
72
{
73
 
74
    /**
75
     * Returns an array of 12 month names (first index = 1)
76
     *
77
     * @param string $format (optional) format of returned months (one|two|short|long)
78
     *
79
     * @return array
80
     * @access public
81
     * @static
82
     */
83
    function monthNames($format = 'long')
84
    {
85
        $formats = array(
86
            'one'   => '%b',
87
            'two'   => '%b',
88
            'short' => '%b',
89
            'long'  => '%B',
90
        );
91
        if (!array_key_exists($format, $formats)) {
92
            $format = 'long';
93
        }
94
        $months = array();
95
        for ($i=1; $i<=12; $i++) {
96
            $stamp = mktime(0, 0, 0, $i, 1, 2003);
97
            $month = strftime($formats[$format], $stamp);
98
            switch($format) {
99
            case 'one':
100
                $month = substr($month, 0, 1);
101
                break;
102
            case 'two':
103
                $month = substr($month, 0, 2);
104
                break;
105
            }
106
            $months[$i] = $month;
107
        }
108
        return $months;
109
    }
110
 
111
    /**
112
     * Returns an array of 7 week day names (first index = 0)
113
     *
114
     * @param string $format (optional) format of returned days (one,two,short or long)
115
     *
116
     * @return array
117
     * @access public
118
     * @static
119
     */
120
    function weekdayNames($format = 'long')
121
    {
122
        $formats = array(
123
            'one'   => '%a',
124
            'two'   => '%a',
125
            'short' => '%a',
126
            'long'  => '%A',
127
        );
128
        if (!array_key_exists($format, $formats)) {
129
            $format = 'long';
130
        }
131
        $days = array();
132
        for ($i=0; $i<=6; $i++) {
133
            $stamp = mktime(0, 0, 0, 11, $i+2, 2003);
134
            $day = strftime($formats[$format], $stamp);
135
            switch($format) {
136
            case 'one':
137
                $day = substr($day, 0, 1);
138
                break;
139
            case 'two':
140
                $day = substr($day, 0, 2);
141
                break;
142
            }
143
            $days[$i] = $day;
144
        }
145
        return $days;
146
    }
147
 
148
    /**
149
     * Returns textual representation of the previous month of the decorated calendar object
150
     *
151
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
152
     * @param string $format   (optional) format of returned months (one,two,short or long)
153
     *
154
     * @return string
155
     * @access public
156
     * @static
157
     */
158
    function prevMonthName($Calendar, $format = 'long')
159
    {
160
        $months = Calendar_Util_Textual::monthNames($format);
161
        return $months[$Calendar->prevMonth()];
162
    }
163
 
164
    /**
165
     * Returns textual representation of the month of the decorated calendar object
166
     *
167
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
168
     * @param string $format   (optional) format of returned months (one,two,short or long)
169
     *
170
     * @return string
171
     * @access public
172
     * @static
173
     */
174
    function thisMonthName($Calendar, $format = 'long')
175
    {
176
        $months = Calendar_Util_Textual::monthNames($format);
177
        return $months[$Calendar->thisMonth()];
178
    }
179
 
180
    /**
181
     * Returns textual representation of the next month of the decorated calendar object
182
     *
183
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
184
     * @param string $format   (optional) format of returned months (one,two,short or long)
185
     *
186
     * @return string
187
     * @access public
188
     * @static
189
     */
190
    function nextMonthName($Calendar, $format = 'long')
191
    {
192
        $months = Calendar_Util_Textual::monthNames($format);
193
        return $months[$Calendar->nextMonth()];
194
    }
195
 
196
    /**
197
     * Returns textual representation of the previous day of week of the decorated calendar object
198
     * <b>Note:</b> Requires PEAR::Date
199
     *
200
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
201
     * @param string $format   (optional) format of returned months (one,two,short or long)
202
     *
203
     * @return string
204
     * @access public
205
     * @static
206
     */
207
    function prevDayName($Calendar, $format = 'long')
208
    {
209
        $days = Calendar_Util_Textual::weekdayNames($format);
210
        $stamp = $Calendar->prevDay('timestamp');
211
        $cE = $Calendar->getEngine();
212
        include_once 'Date/Calc.php';
213
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
214
            $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
215
        return $days[$day];
216
    }
217
 
218
    /**
219
     * Returns textual representation of the day of week of the decorated calendar object
220
     * <b>Note:</b> Requires PEAR::Date
221
     *
222
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
223
     * @param string $format   (optional) format of returned months (one,two,short or long)
224
     *
225
     * @return string
226
     * @access public
227
     * @static
228
     */
229
    function thisDayName($Calendar, $format='long')
230
    {
231
        $days = Calendar_Util_Textual::weekdayNames($format);
232
        include_once 'Date/Calc.php';
233
        $day = Date_Calc::dayOfWeek($Calendar->thisDay(), $Calendar->thisMonth(), $Calendar->thisYear());
234
        return $days[$day];
235
    }
236
 
237
    /**
238
     * Returns textual representation of the next day of week of the decorated calendar object
239
     *
240
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
241
     * @param string $format   (optional) format of returned months (one,two,short or long)
242
     *
243
     * @return string
244
     * @access public
245
     * @static
246
     */
247
    function nextDayName($Calendar, $format='long')
248
    {
249
        $days = Calendar_Util_Textual::weekdayNames($format);
250
        $stamp = $Calendar->nextDay('timestamp');
251
        $cE = $Calendar->getEngine();
252
        include_once 'Date/Calc.php';
253
        $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
254
            $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
255
        return $days[$day];
256
    }
257
 
258
    /**
259
     * Returns the days of the week using the order defined in the decorated
260
     * calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks
261
     * and Calendar_Week. Otherwise the returned array will begin on Sunday
262
     *
263
     * @param object $Calendar subclass of Calendar e.g. Calendar_Month
264
     * @param string $format   (optional) format of returned months (one,two,short or long)
265
     *
266
     * @return array ordered array of week day names
267
     * @access public
268
     * @static
269
     */
270
    function orderedWeekdays($Calendar, $format = 'long')
271
    {
272
        $days = Calendar_Util_Textual::weekdayNames($format);
273
 
274
        if (isset($Calendar->tableHelper)) {
275
            $ordereddays = $Calendar->tableHelper->getDaysOfWeek();
276
        } else {
277
            //default: start from Sunday
278
            $firstDay = 0;
279
            //check if defined / set
280
            if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) {
281
                $firstDay = CALENDAR_FIRST_DAY_OF_WEEK;
282
            } elseif(isset($Calendar->firstDay)) {
283
                $firstDay = $Calendar->firstDay;
284
            }
285
            $ordereddays = array();
286
            for ($i = $firstDay; $i < 7; $i++) {
287
                $ordereddays[] = $i;
288
            }
289
            for ($i = 0; $i < $firstDay; $i++) {
290
                $ordereddays[] = $i;
291
            }
292
        }
293
 
294
        $ordereddays = array_flip($ordereddays);
295
        $i = 0;
296
        $returndays = array();
297
        foreach ($ordereddays as $key => $value) {
298
            $returndays[$i] = $days[$key];
299
            $i++;
300
        }
301
        return $returndays;
302
    }
303
}
304
?>