MonthPayload_Decorator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 40
dl 0
loc 62
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 8 2
A build() 0 24 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 ( !@include '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
class DiaryEvent extends Calendar_Decorator
0 ignored issues
show
Bug introduced by
The type Calendar_Decorator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
{
21
    var $entries = array();
22
23
    function DiaryEvent($calendar) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
        Calendar_Decorator::Calendar_Decorator($calendar);
25
    }
26
27
    function addEntry($entry) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
        $this->entries[] = $entry;
29
    }
30
31
    function getEntry() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
        $entry = each($this->entries);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

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

32
        $entry = /** @scrutinizer ignore-deprecated */ each($this->entries);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
33
        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...
34
            return $entry['value'];
35
        } else {
36
            reset($this->entries);
37
            return false;
38
        }
39
    }
40
}
41
42
class MonthPayload_Decorator extends Calendar_Decorator
43
{
44
    //Calendar engine
45
    var $cE;
46
    var $tableHelper;
47
48
    var $year;
49
    var $month;
50
    var $firstDay = false;
51
52
    function build($events=array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
    {
54
        require_once CALENDAR_ROOT . 'Day.php';
55
        require_once CALENDAR_ROOT .  'Table/Helper.php';
56
57
        $this->tableHelper = & new Calendar_Table_Helper($this, $this->firstDay);
0 ignored issues
show
Bug introduced by
The type Calendar_Table_Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
        $this->cE = & $this->getEngine();
59
        $this->year  = $this->thisYear();
60
        $this->month = $this->thisMonth();
61
62
        $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month);
63
        for ($i=1; $i<=$daysInMonth; $i++) {
64
            $Day = new Calendar_Day(2000,1,1); // Create Day with dummy values
0 ignored issues
show
Bug introduced by
The type Calendar_Day was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
            $Day->setTimeStamp($this->cE->dateToStamp($this->year, $this->month, $i));
66
            $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...
67
        }
68
        if (count($events) > 0) {
69
            $this->setSelection($events);
70
        }
71
        Calendar_Month_Weekdays::buildEmptyDaysBefore();
0 ignored issues
show
Bug introduced by
The type Calendar_Month_Weekdays was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
        Calendar_Month_Weekdays::shiftDays();
73
        Calendar_Month_Weekdays::buildEmptyDaysAfter();
74
        Calendar_Month_Weekdays::setWeekMarkers();
75
        return true;
76
    }
77
78
    function setSelection($events)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
    {
80
        $daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month);
81
        for ($i=1; $i<=$daysInMonth; $i++) {
82
            $stamp1 = $this->cE->dateToStamp($this->year, $this->month, $i);
83
            $stamp2 = $this->cE->dateToStamp($this->year, $this->month, $i+1);
84
            foreach ($events as $event) {
85
                if (($stamp1 >= $event['start'] && $stamp1 < $event['end']) ||
86
                    ($stamp2 >= $event['start'] && $stamp2 < $event['end']) ||
87
                    ($stamp1 <= $event['start'] && $stamp2 > $event['end'])
88
                ) {
89
                    $this->children[$i]->addEntry($event);
90
                    $this->children[$i]->setSelected();
91
                }
92
            }
93
        }
94
    }
95
96
    function fetch()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
    {
98
        $child = each($this->children);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

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

98
        $child = /** @scrutinizer ignore-deprecated */ each($this->children);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
        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...
100
            return $child['value'];
101
        } else {
102
            reset($this->children);
103
            return false;
104
        }
105
    }
106
}
107
108
// Calendar instance used to get the dates in the preferred format:
109
// you can switch Calendar Engine and the example still works
110
$cal = new Calendar;
111
112
$events = array();
113
//add some events
114
$events[] = array(
115
    'start' => $cal->cE->dateToStamp(2004, 6, 1, 10),
116
    'end'   => $cal->cE->dateToStamp(2004, 6, 1, 12),
117
    'desc'  => 'Important meeting'
118
);
119
$events[] = array(
120
    'start' => $cal->cE->dateToStamp(2004, 6, 1, 21),
121
    'end'   => $cal->cE->dateToStamp(2004, 6, 1, 23, 59),
122
    'desc'  => 'Dinner with the boss'
123
);
124
$events[] = array(
125
    'start' => $cal->cE->dateToStamp(2004, 6, 5),
126
    'end'   => $cal->cE->dateToStamp(2004, 6, 10, 23, 59),
127
    'desc'  => 'Holidays!'
128
);
129
130
131
132
$Month = & new Calendar_Month_Weekdays(2004, 6);
133
$MonthDecorator = new MonthPayload_Decorator($Month);
134
$MonthDecorator->build($events);
135
136
?>
137
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
138
<html>
139
<head>
140
<title> Calendar </title>
141
<style text="text/css">
142
table {
143
    border-collapse: collapse;
144
}
145
caption {
146
    font-family: verdana;
147
    font-size: 14pt;
148
    padding-bottom: 4pt;
149
}
150
th {
151
    font-family: verdana;
152
    font-size: 11px;
153
    text-align: center;
154
    background-color: #e7e3e7;
155
    padding: 5pt;
156
    line-height: 150%;
157
    border: 1px solid #ccc;
158
}
159
td {
160
    font-family: verdana;
161
    font-size: 11px;
162
    text-align: left;
163
    vertical-align: top;
164
}
165
td.calCell {
166
    border: 1px solid #b5bece;
167
    padding: 3px;
168
}
169
td.calCellEmpty {
170
    background-color: #f3f3f7;
171
}
172
td.calCellBusy {
173
    background-color: #efeffa;
174
}
175
div.dayNumber {
176
    text-align: right;
177
    background-color: #f8f8f8;
178
    border-bottom: 1px solid #ccc;
179
}
180
ul {
181
    margin-left: 0;
182
    margin-top: 5pt;
183
    padding: 0 10pt 0 12pt;
184
    list-style-type: square;
185
}
186
</style>
187
</head>
188
189
<body>
190
191
<h2>Sample Calendar Payload Decorator (using <?php echo CALENDAR_ENGINE; ?> engine)</h2>
192
<table class="calendar" width="98%" cellspacing="0" cellpadding="0">
193
<caption>
194
    <?php echo $MonthDecorator->thisMonth().' / '.$MonthDecorator->thisYear(); ?>
195
</caption>
196
<tr>
197
    <th>Monday</th>
198
    <th>Tuesday</th>
199
    <th>Wednesday</th>
200
    <th>Thursday</th>
201
    <th>Friday</th>
202
    <th>Saturday</th>
203
    <th>Sunday</th>
204
</tr>
205
<?php
206
while ($Day = $MonthDecorator->fetch()) {
207
208
    if ($Day->isFirst()) {
209
        echo "<tr>\n";
210
    }
211
212
    echo '<td class="calCell';
213
    if ($Day->isSelected()) {
214
        echo ' calCellBusy';
215
    } elseif ($Day->isEmpty()) {
216
        echo ' calCellEmpty';
217
    }
218
    echo '">';
219
    echo '<div class="dayNumber">'.$Day->thisDay().'</div>';
220
221
    if ($Day->isEmpty()) {
222
        echo '&nbsp;';
223
    } else {
224
        echo '<div class="dayContents"><ul>';
225
        while ($entry = $Day->getEntry()) {
226
            echo  '<li>'.$entry['desc'].'</li>';
227
            //you can print the time range as well
228
        }
229
        echo '</ul></div>';
230
    }
231
    echo '</td>';
232
233
    if ($Day->isLast()) {
234
        echo "</tr>\n";
235
    }
236
}
237
?>
238
</table>
239
</body>
240
</html>