MonthPayload_Decorator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 41
dl 0
loc 76
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 9 2
A build() 0 26 3
B setSelection() 0 13 9
1
<?php
2
/**
3
 * Description: demonstrates a decorator used to "attach a payload" to a selection
4
 * to make it available when iterating over calendar children.
5
 */
6
7
//if you use ISO-8601 dates, switch to PearDate engine
8
define('CALENDAR_ENGINE', 'PearDate');
9
10
if (!@require_once __DIR__ . '/Calendar/Calendar.php') {
11
    define('CALENDAR_ROOT', '../../');
12
}
13
14
require_once CALENDAR_ROOT . 'Month/Weekdays.php';
15
require_once CALENDAR_ROOT . 'Day.php';
16
require_once CALENDAR_ROOT . 'Decorator.php';
17
18
// accepts multiple entries
19
20
/**
21
 * Class DiaryEvent.
22
 */
23
class DiaryEvent extends Calendar_Decorator
24
{
25
    public $entries = [];
26
27
    /**
28
     * @param $calendar
29
     */
30
    public function __construct($calendar)
31
    {
32
        parent::__construct($calendar);
33
    }
34
35
    /**
36
     * @param $entry
37
     */
38
    public function addEntry($entry)
39
    {
40
        $this->entries[] = $entry;
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function getEntry()
47
    {
48
        $entry = each($this->entries);
0 ignored issues
show
Bug Best Practice introduced by
The property entries does not exist on DiaryEvent. Did you maybe forget to declare it?
Loading history...
49
        if ($entry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entry of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            return $entry['value'];
51
        }
52
        reset($this->entries);
53
54
        return false;
55
    }
56
}
57
58
/**
59
 * Class MonthPayload_Decorator.
60
 */
61
class MonthPayload_Decorator extends Calendar_Decorator
62
{
63
    //Calendar engine
64
    public $cE;
65
    public $tableHelper;
66
67
    public $year;
68
    public $month;
69
    public $firstDay = false;
70
71
    /**
72
     * @param array $events
73
     *
74
     * @return bool
75
     */
76
    public function build($events = [])
77
    {
78
        require_once CALENDAR_ROOT . 'Day.php';
79
        require_once CALENDAR_ROOT . 'Table/Helper.php';
80
81
        $this->tableHelper = new Calendar_Table_Helper($this, $this->firstDay);
0 ignored issues
show
Bug introduced by
$this->firstDay of type boolean is incompatible with the type integer expected by parameter $firstDay of Calendar_Table_Helper::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $this->tableHelper = new Calendar_Table_Helper($this, /** @scrutinizer ignore-type */ $this->firstDay);
Loading history...
82
        $this->cE          = $this->getEngine();
83
        $this->year        = $this->thisYear();
84
        $this->month       = $this->thisMonth();
85
86
        $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month);
87
        for ($i = 1; $i <= $daysInMonth; ++$i) {
88
            $Day = new Calendar_Day(2000, 1, 1); // Create Day with dummy values
89
            $Day->setTimestamp($this->cE->dateToStamp($this->year, $this->month, $i));
90
            $this->children[$i] = new DiaryEvent($Day);
0 ignored issues
show
Bug Best Practice introduced by
The property children does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
        }
92
        if (count($events) > 0) {
93
            $this->setSelection($events);
94
        }
95
        $calMonthWeekdays = new Calendar_Month_Weekdays($this->year, $this->month);
96
        $calMonthWeekdays->buildEmptyDaysBefore();
97
        $calMonthWeekdays->shiftDays();
98
        $calMonthWeekdays->buildEmptyDaysAfter();
99
        $calMonthWeekdays->setWeekMarkers();
100
101
        return true;
102
    }
103
104
    /**
105
     * @param $events
106
     */
107
    public function setSelection($events)
108
    {
109
        $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month);
110
        for ($i = 1; $i <= $daysInMonth; ++$i) {
111
            $stamp1 = $this->cE->dateToStamp($this->year, $this->month, $i);
112
            $stamp2 = $this->cE->dateToStamp($this->year, $this->month, $i + 1);
113
            foreach ($events as $event) {
114
                if (($stamp1 >= $event['start'] && $stamp1 < $event['end'])
115
                    || ($stamp2 >= $event['start']
116
                        && $stamp2 < $event['end'])
117
                    || ($stamp1 <= $event['start'] && $stamp2 > $event['end'])) {
118
                    $this->children[$i]->addEntry($event);
119
                    $this->children[$i]->setSelected();
120
                }
121
            }
122
        }
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function fetch()
129
    {
130
        $child = each($this->children);
131
        if ($child) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $child of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132
            return $child['value'];
133
        }
134
        reset($this->children);
135
136
        return false;
137
    }
138
}
139
140
// Calendar instance used to get the dates in the preferred format:
141
// you can switch Calendar Engine and the example still works
142
$cal = new Calendar();
143
144
$events = [];
145
//add some events
146
$events[] = [
147
    'start' => $cal->cE->dateToStamp(2004, 6, 1, 10),
148
    'end'   => $cal->cE->dateToStamp(2004, 6, 1, 12),
149
    'desc'  => 'Important meeting',
150
];
151
$events[] = [
152
    'start' => $cal->cE->dateToStamp(2004, 6, 1, 21),
153
    'end'   => $cal->cE->dateToStamp(2004, 6, 1, 23, 59),
154
    'desc'  => 'Dinner with the boss',
155
];
156
$events[] = [
157
    'start' => $cal->cE->dateToStamp(2004, 6, 5),
158
    'end'   => $cal->cE->dateToStamp(2004, 6, 10, 23, 59),
159
    'desc'  => 'Holidays!',
160
];
161
162
$Month          = new Calendar_Month_Weekdays(2004, 6);
163
$MonthDecorator = new MonthPayload_Decorator($Month);
164
$MonthDecorator->build($events);
165
166
?>
167
<!DOCTYPE html>
168
<html>
169
<head>
170
    <title> Calendar </title>
171
    <style text="text/css">
172
        table {
173
            border-collapse: collapse;
174
        }
175
176
        caption {
177
            font-family: verdana, sans-serif;
178
            font-size: 14pt;
179
            padding-bottom: 4pt;
180
        }
181
182
        th {
183
            font-family: verdana, sans-serif;
184
            font-size: 11px;
185
            text-align: center;
186
            background-color: #e7e3e7;
187
            padding: 5pt;
188
            line-height: 150%;
189
            border: 1px solid #ccc;
190
        }
191
192
        td {
193
            font-family: verdana, sans-serif;
194
            font-size: 11px;
195
            text-align: left;
196
            vertical-align: top;
197
        }
198
199
        td.calCell {
200
            border: 1px solid #b5bece;
201
            padding: 3px;
202
        }
203
204
        td.calCellEmpty {
205
            background-color: #f3f3f7;
206
        }
207
208
        td.calCellBusy {
209
            background-color: #efeffa;
210
        }
211
212
        div.dayNumber {
213
            text-align: right;
214
            background-color: #f8f8f8;
215
            border-bottom: 1px solid #ccc;
216
        }
217
218
        ul {
219
            margin-left: 0;
220
            margin-top: 5pt;
221
            padding: 0 10pt 0 12pt;
222
            list-style-type: square;
223
        }
224
    </style>
225
</head>
226
227
<body>
228
229
<h2>Sample Calendar Payload Decorator (using <?php echo CALENDAR_ENGINE; ?> engine)</h2>
230
<table class="calendar" width="98%" cellspacing="0" cellpadding="0">
231
    <caption>
232
        <?php echo $MonthDecorator->thisMonth() . ' / ' . $MonthDecorator->thisYear(); ?>
233
    </caption>
234
    <tr>
235
        <th>Monday</th>
236
        <th>Tuesday</th>
237
        <th>Wednesday</th>
238
        <th>Thursday</th>
239
        <th>Friday</th>
240
        <th>Saturday</th>
241
        <th>Sunday</th>
242
    </tr>
243
    <?php
244
    while (false !== ($Day = $MonthDecorator->fetch())) {
245
        if ($Day->isFirst()) {
246
            echo "<tr>\n";
247
        }
248
249
        echo '<td class="calCell';
250
        if ($Day->isSelected()) {
251
            echo ' calCellBusy';
252
        } elseif ($Day->isEmpty()) {
253
            echo ' calCellEmpty';
254
        }
255
        echo '">';
256
        echo '<div class="dayNumber">' . $Day->thisDay() . '</div>';
257
258
        if ($Day->isEmpty()) {
259
            echo '&nbsp;';
260
        } else {
261
            echo '<div class="dayContents"><ul>';
262
            while ($entry = $Day->getEntry()) {
263
                echo '<li>' . $entry['desc'] . '</li>';
264
                //you can print the time range as well
265
            }
266
            echo '</ul></div>';
267
        }
268
        echo '</td>';
269
270
        if ($Day->isLast()) {
271
            echo "</tr>\n";
272
        }
273
    }
274
    ?>
275
</table>
276
</body>
277
</html>
278