srchub-old

srchub-old Mercurial Source Tree


Root/pluf/src/Pluf/Calendar.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Calendar to display a list of events in a calendar table.
 *
 * The calendar is independent of other elements of Pluf, you can use
 * it standalone if you want.
 *
 * The principle is that you set options and feed the calendar with a
 * list of events. Based on the options, the render() method will
 * produce different views of the calendar.
 */
class Pluf_Calendar
{
    /**
     * The list of events to display.
     */
    var $events = array();
    var $summary = '';
 
    /**
     * The display options of the calendar.
     */
    var $opts = array();
 
    // When updating an interval, if a col span more rows and columns,
    // store the info for the next rows to compensate as needed.
    var $bspans = array();
 
    /**
     * List of events without the events not between the start/end
     * days.
     */
    var $_events = array();
 
    /**
     * List of time intervals in the $_events list.
     */
    var $_time_intervals = array();
 
    /**
     * Simultaneous events at a given time slot, for a given group.
     *
     * array('2007-03-25' =>
     *       array(array('time' => '10:15',
     *                   'start' => 4 ,
     *                   'continued' => 5),
     *             array('time' => '11:30',
     *                   'start' => 3 ,
     *                   'continued' => 0),
     *             )
     *       '2007-03-24' =>
     *       array(array('time' => '11:30',
     *                   'start' => 2 ,
     *                   'continued' => 3),
     *            )
     *       )
     *
     */
    var $_simultaneous = array();
    var $_max_simultaneous = array();
    var $_groups = array();
 
    /**
     * Render the calendar based on the options.
     */
    public function render()
    {
        if (count($this->events) == 0) {
            return '';
        }
        $this->cleanEventList();
        $this->getTimeIntervals();
        $this->getSimultaneous();
        $this->getMaxSimultaneous();
        $s = '';
        if ($this->summary) {
            $s = 'summary="'.htmlspecialchars($this->summary).'" ';
        }
        $out = '<table '.$s.'cellspacing="0" class="px-calendar">'."\n";
        $out .= $this->getHead();
        $out .= $this->getBody();
        $out .= '</table>'."\n";
        return Pluf_Template_SafeString::markSafe($out);
    }
 
    /**
     * Event are grouped by day per default, you can group as you
     * want, just subclass this method. Groups are used to make
     * columns in the table with the headings.
     */
    function getEventGroup($event)
    {
        return substr($event['start'], 0, 10);
    }
 
    /**
     * Get all the available groups.
     */
    function getGroups()
    {
        if (count($this->_groups)) {
            return $this->_groups;
        }
        foreach ($this->_events as $event) {
            $group = $this->getEventGroup($event);
            if (!in_array($group, $this->_groups)) {
                $this->_groups[] = $group;
            }
        }
        return $this->_groups;
    }
 
    /**
     * Get the name of a group to print in the headers.
     */
    function getGroupName($group)
    {
        $dw = $this->daysOfWeek();
        $days = date('w', strtotime($group));
        return htmlspecialchars($dw[$days%7]);
    }
 
    /**
     * Generate the body of the calendar.
     */
    function getBody()
    {
        $out = '<tbody>'."\n";
        $inters = $this->getTimeIntervals();
        $groups = $this->getGroups();
        for ($i=0;$i<(count($inters)-1);$i++) {
            $out .= '<tr>'."\n";
            $out .= '  <th scope="row">'.$inters[$i].' - '.$inters[$i+1].'</th>'."\n";
            foreach ($groups as $group) {
                $out .= $this->getEventCell($group, $inters[$i]);
            }
            $out .= '</tr>'."\n";
        }
        $out .= '</tbody>'."\n";
        return $out;
    }
 
 
    /**
     * Get the value to print for the given cell
     *
     * @param string Current group
     * @param string Current interval
     * @return string Table cells
     */
    function getEventCell($group, $inter)
    {
        $out = '';
        $max = $this->getMaxSimultaneous();
        $fullspanevent = false;
        foreach ($this->_events as $event) {
            // Get the start time of the event
            $e_start = substr($event['start'], 11, 5);
            if ($e_start != $inter) {
                // If the event does not start at the current time,
                // skip it
                continue;
            }
            if ($group != $this->getEventGroup($event)) {
                // Check if full span even at this time interval
                if (!empty($event['fullspan'])) {
                    $fullspanevent = true;
                }
                continue;
            }
            // Find how many rows the event will span
            $extra = '';
            $content = '';
            if (!isset($event['content'])) $event['content'] = '';
            $row_span = $this->getEventRowSpanning($event, $this->_time_intervals);
            if ($row_span > 1) {
                $extra .= ' rowspan="'.$row_span.'"';
            }
            if (!empty($event['fullspan'])) {
                $colspan = 0;
                foreach ($max as $_s) {
                    $colspan += $_s;
                }
                $extra .= ' colspan="'.$colspan.'"';
                $fullspanevent = true;
            }
            if (strlen($event['color']) > 0) {
                $extra .= ' style="background-color: '.$event['color'].';"';
            }
            if (strlen($event['content']) > 0) {
                $content .= $event['content'];
            }
            if (strlen($event['url']) > 0) {
                $content .= '<a href="'.$event['url'].'">'.htmlspecialchars($event['title']).'</a>';
            }
            if (strlen($event['content']) == 0 and strlen($event['url']) == 0) {
                $content .= htmlspecialchars($event['title']);
            }
            $out .= '  <td'.$extra.'>'.$content.'</td>'."\n";
        }
        if (!$fullspanevent) {
            $sim = null;
            foreach ($this->_simultaneous[$group] as $_sim) {
                if ($_sim['time'] == $inter) {
                    $sim = $_sim;
                    break;
                }
            }
            $diff = $max[$group] - ($sim['start'] + $sim['continued']);
            for ($k=0; $k<$diff; $k++) {
                $out .= '  <td class="empty"> </td>'."\n";
            }
        }
        return $out;
    }
 
    /**
     * Get event spanning over the rows.
     *
     * @param array Event
     * @param array Intervals
     * @return int Spanning
     */
    function getEventRowSpanning($event, $inters)
    {
        $start = substr($event['start'], 11, 5);
        $end = substr($event['end'], 11, 5);
        $span = 1;
        foreach ($inters as $inter) {
            if ($inter < $end and $inter > $start) {
                $span++;
            }
        }
        return $span;
    }
 
    /**
     * Generate the head of the calendar.
     */
    function getHead()
    {
        $out = '<thead>'."\n".'<tr>'."\n".'  <th> </th>'."\n";
        // Print the groups.
        $groups = $this->getGroups();
        $max = $this->getMaxSimultaneous();
        foreach ($groups as $group) {
            if ($max[$group] > 1) {
                $span = ' colspan="'.$max[$group].'"';
            } else {
                $span = '';
            }
            $out .= '  <th scope="col"'.$span.'>'.$this->getGroupName($group).'</th>'."\n";
        }
        $out .= '</tr>'."\n".'</thead>'."\n";
        return $out;
    }
 
    /**
     * Get the rowspan for each day.
     */
    function getDaySpanning()
    {
        list($start, $end) = $this->getStartEndDays();
        $inters = $this->getTimeIntervals($start, $end);
        $n = $this->getDayNumber($start, $end);
        $inter_n = array_fill(0, count($inters), 0);
        $day_span = array_fill(0, $n+1, $inter_n);
        foreach ($this->events as $event) {
            // The event must be between $start and $end
            $e_dstart = substr($event['start'], 0, 10);
            $e_dend = substr($event['end'], 0, 10);
            if ($e_dend < $start or $e_dstart > $end) {
                continue;
            }
             
            $day = $this->getDayNumber($start, substr($event['end'], 0, 10));
            $e_start = substr($event['start'], 11, 5);
            $e_end = substr($event['end'], 11, 5);
            $i = 0;
            foreach ($inters as $inter) {
                if ($inter < $e_end and $inter >= $e_start) {
                    $day_span[$day][$i]++;
                }
                $i++;
            }
        }
        return $day_span;
    }
 
    /**
     * Get an array with the days of the week.
     */
    function daysOfWeek()
    {
        return array(
                     __('Sunday'),
                     __('Monday'),
                     __('Tuesday'),
                     __('Wednesday'),
                     __('Thursday'),
                     __('Friday'),
                     __('Saturday'),
                     );
    }
 
    /**
     * Get the number of days to list.
     *
     * @param string Start date
     * @param string End date
     * @return int Number of days
     */
    function getDayNumber($start, $end)
    {
        Pluf::loadFunction('Pluf_Date_Compare');
        $diff = Pluf_Date_Compare($start.' 00:00:00', $end.' 00:00:00');
        return (int) $diff/86400;
    }
 
    /**
     * Get the start and end dates based on the event list.
     *
     * @return array (start day, end day)
     */
    function getStartEndDays()
    {
        $start = '9999-12-31';
        $end = '0000-01-01';
        if (!isset($this->opts['start-day'])
            or !isset($this->opts['end-day'])) {
            foreach ($this->events as $event) {
                $t_start = substr($event['start'], 0, 10);
                $t_end = substr($event['end'], 0, 10);
                if ($t_start < $start) {
                    $start = $t_start;
                }
                if ($t_end > $end) {
                    $end = $t_end;
                }
            }
        }
        if (isset($this->opts['start-day'])) {
            $start = $this->opts['start-day'];
        } else {
            $this->opts['start-day'] = $start;
        }
        if (isset($this->opts['end-day'])) {
            $end = $this->opts['end-day'];
        } else {
            $this->opts['end-day'] = $end;
        }
        return array($start, $end);
    }
 
    /**
     * Clean event list.
     */
    function cleanEventList()
    {
        list($start, $end) = $this->getStartEndDays();
        $this->_events = array();
        foreach ($this->events as $event) {
            $e_dstart = substr($event['start'], 0, 10);
            $e_dend = substr($event['end'], 0, 10);
            if ($e_dend < $start or $e_dstart > $end) {
                continue;
            }
            $this->_events[] = $event;
        }
        return true;
    }
 
    /**
     * Get the time intervals. They span all the groups.
     */
    function getTimeIntervals($start='', $end='')
    {
        if (count($this->_time_intervals)) {
            return $this->_time_intervals;
        }
        $intervals = array();
        foreach ($this->_events as $event) {
            $t = substr($event['start'], 11, 5);
            if (!in_array($t, $intervals)) {
                $intervals[] = $t;
            }
            $t = substr($event['end'], 11, 5);
            if (!in_array($t, $intervals)) {
                $intervals[] = $t;
            }
        }
        sort($intervals);
        $this->_time_intervals = $intervals;
        return $intervals;
    }
 
    /**
     * Get simultaneous events at the same time slot and same group.
     */
    function getSimultaneous()
    {
        foreach ($this->getGroups() as $group) {
            $this->_simultaneous[$group] = array();
            foreach ($this->_time_intervals as $inter) {
                $this->_simultaneous[$group][] = array('time' => $inter,
                                                       'start' => 0,
                                                       'continued' => 0);
            }
        }
        foreach ($this->_events as $event) {
            $group = $this->getEventGroup($event);
            $e_tstart = substr($event['start'], 11, 5);
            $e_tend = substr($event['end'], 11, 5);
            foreach ($this->_simultaneous[$group] as $index=>$inter) {
                if ($e_tstart == $inter['time']) {
                    $inter['start'] += 1;
                    $this->_simultaneous[$group][$index] = $inter;
                    continue;
                }
                if ($e_tstart < $inter['time'] and $e_tend > $inter['time']) {
                    $inter['continued'] += 1;
                    $this->_simultaneous[$group][$index] = $inter;
                    continue;
                }
            }
        }
        return $this->_simultaneous;
    }
 
    /**
     * Get maximum simultaneous events
     */
    function getMaxSimultaneous()
    {
        if (count($this->_max_simultaneous) > 0) {
            return $this->_max_simultaneous;
        }
        foreach ($this->getGroups() as $group) {
            $this->_max_simultaneous[$group] = 0;
        }
        foreach ($this->_simultaneous as $group=>$choices) {
            foreach ($choices as $count) {
                if ($this->_max_simultaneous[$group] < $count['start'] + $count['continued']) {
                    $this->_max_simultaneous[$group] = $count['start'] + $count['continued'];
                }
            }
        }
        return $this->_max_simultaneous;
    }
 
 
    /**
     * Overloading of the get method.
     *
     * @param string Property to get
     */
    function __get($prop)
    {
        if ($prop == 'render') return $this->render();
        return $this->$prop;
    }
 
}
Source at commit 55305a934bac created 10 years 8 months ago.
By Nathan Adams, Fixing bug where password would not be hashed in database if user updated password

Archive Download this file

Branches

Tags

Page rendered in 1.06160s using 11 queries.