-
Notifications
You must be signed in to change notification settings - Fork 8
/
imagerenderer.class.php
330 lines (277 loc) · 13.8 KB
/
imagerenderer.class.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
<?php
require_once("conf.php");
require_once("booking.class.php");
require_once("color_mode.php");
class ImageRenderer
{
/**
* @param array<Booking> $booked_stations
* @param DateTime $MASTER_DATE
* @param int $next_airport_row_distance
* @return false|GdImage
*/
static function simple_render(array $booked_stations, DateTime $MASTER_DATE, int $next_airport_row_distance=1) : false|GdImage
{
$main_stations = [];
foreach ($booked_stations as $s){
if(!in_array($s->callsign, $main_stations)) {
$main_stations[] = $s->callsign;
}
}
sort($main_stations);
return self::render([], $main_stations, $booked_stations, $MASTER_DATE, [], $next_airport_row_distance);
}
/**
* @param array<string> $allstations
* @param array<Booking> $booked_stations
* @param DateTime $MASTER_DATE
* @param int $next_airport_row_distance
* @return false|GdImage
*/
static function render_allshown(array $allstations, array $booked_stations, DateTime $MASTER_DATE, int $next_airport_row_distance=1) : false|GdImage
{
//sort($allstations);
return self::render($allstations, $allstations, $booked_stations, $MASTER_DATE, [], $next_airport_row_distance);
}
/**
* @param array<string> $min_stations
* @param array<string> $main_stations
* @param array<Booking> $booked_stations
* @param DateTime $MASTER_DATE
* @param $weeklyBookings
* @param int $next_airport_row_distance
* @return false|GdImage
*/
static function render(array $min_stations, array $main_stations, array $booked_stations, DateTime $MASTER_DATE, $weeklyBookings, int $next_airport_row_distance=2) : false|GdImage
{
$booking_matrix = [];
// Iterate over the stations and create a booking matrix
for ($i = 0; $i < count($main_stations); $i++) {
// First row contains the station
$booking_matrix[$i][0] = $main_stations[$i];
$day = clone $MASTER_DATE;
// Loop over the next days
for ($j = 1; $j < 8; $j++) {
$found_booking = false;
$cellObject = [];
// Check if there is a booking at the specific date
for ($k = 0; $k < count($booked_stations); $k++) {
$booking_date_start = DateTime::createFromFormat('Y-m-d\TH:i:s', $booked_stations[$k]->time_start);
$booking_date_end = DateTime::createFromFormat('Y-m-d\TH:i:s', $booked_stations[$k]->time_end);
// append all bookings as array
if ($booking_date_start->format("Y-m-d") === $day->format("Y-m-d") && $main_stations[$i] == $booked_stations[$k]->callsign) {
$cellContent = $booked_stations[$k]->abbreviation . " " . $booking_date_start->format("Hi") . "-" . $booking_date_end->format("Hi");
$isTraining = $booked_stations[$k]->training;
$isEvent = $booked_stations[$k]->event;
$cellObject[] = ["content" => $cellContent, "isTraining" => $isTraining, "isEvent" => $isEvent];
$found_booking = true;
}
}
// If no booking found, set it to open
if (!$found_booking) {
if(in_array($main_stations[$i], $min_stations)){
$cellObject[] = ["content" => "open", "isTraining" => false, "isEvent" => false];
}
else {
$cellObject[] = ["content" => null, "isTraining" => null, "isEvent" => null];
}
}
$booking_matrix[$i][$j] = $cellObject;
$day->add(new DateInterval("P1D"));
}
}
$tempBookingMatrix = [];
$additionalBookings = 0;
for($i = 0; $i<count($booking_matrix); $i++){
$hasBookings = false;
for($j = 1; $j<count($booking_matrix[$i]); $j++){
for($k = 0; $k<count($booking_matrix[$i][$j]);$k++){
if($booking_matrix[$i][$j][$k]['content'] !== null){
$hasBookings = true;
break;
}
}
}
if($hasBookings){
$tempBookingMatrix[] = $booking_matrix[$i];
for ($k = 1; $k<count($booking_matrix[$i]); $k++){
if(count($booking_matrix[$i][$k]) > 1){
$additionalBookings = $additionalBookings + (count($booking_matrix[$i][$k]));
}
}
}
}
// Set array with all users
$all_users = [];
for ($i = 0; $i < count($booked_stations); $i++) {
$all_users[] = ["name" => $booked_stations[$i]->name, "abbreviation" => $booked_stations[$i]->abbreviation];
}
$booking_matrix = $tempBookingMatrix;
$booking_groups = 0;
$imageHeight = 5000; //temporary height
$imageWidth = 2080;
// Create images
$im = @imagecreate($imageWidth, $imageHeight) or die("Cannot Initialize new GD image stream");
$background_color = get_color_in_mode($im, "background");
imagefill($im,0,0, $background_color);
$color_black = get_color_in_mode($im, "black");
$color_gray = get_color_in_mode($im, "gray");
$color_red = get_color_in_mode($im, "red");
$color_blue = get_color_in_mode($im, "blue");
$color_orange = get_color_in_mode($im, "orange");
$row = 1;
$lineHeight = 47;
$vertOffset = 9;
$cell_width = 260;
// Set date header columns
$day = clone $MASTER_DATE;
for ($i = 1; $i < 8; $i++) {
self::write_string($im, 3, $cell_width * $i, $row * $lineHeight, $day->format("D d.m."), $color_black);
$day->add(new DateInterval('P1D'));
}
$row = 2;
imageline($im, 0, $row * $lineHeight + 7, $imageWidth, $row * $lineHeight + 7, $color_gray);
$row = 3;
// Draw matrix
for ($i = 0; $i < count($booking_matrix); $i++) {
$day = clone $MASTER_DATE;
// Write station
imageline($im, 0, ($row) * $lineHeight, $imageWidth, ($row) * $lineHeight, $color_gray);
imageline($im, 0, ($row+1) * $lineHeight, $imageWidth, ($row+1) * $lineHeight, $color_gray);
self::write_string($im, 3, 5, ($lineHeight * $row) + $vertOffset, $booking_matrix[$i][0], $color_black);
$maxHeight = 1;
//Loop over the days from the stations
for ($j = 1; $j < count($booking_matrix[$i]); $j++) {
// Check if there was a cell with more than one booking (so maxHeight is > 1)
if (count($booking_matrix[$i][$j]) > $maxHeight) {
$maxHeight = count($booking_matrix[$i][$j]);
}
// Write bookings
for ($k = 0; $k < count($booking_matrix[$i][$j]); $k++) {
$color = $color_gray;
if ($booking_matrix[$i][$j][$k] === null || $booking_matrix[$i][$j][$k]['content'] !== "open"){
$color = $color_black;
}
if($booking_matrix[$i][$j][$k]['content'] === "open" && self::isWeeklyPosition($weeklyBookings, $day, $booking_matrix[$i][0])){
$color = $color_red;
}
if (isset($booking_matrix[$i][$j][$k]['isTraining']) && $booking_matrix[$i][$j][$k]['isTraining']) {
$color = $color_blue;
}
if (isset($booking_matrix[$i][$j][$k]['isEvent']) && $booking_matrix[$i][$j][$k]['isEvent']) {
$color = $color_orange;
}
// If is requested but open than make it to WANTED
if ($color === $color_red && $booking_matrix[$i][$j][$k]['content'] == "open") {
$booking_matrix[$i][$j][$k]['content'] = "WANTED";
}
self::write_string($im, 3, $cell_width * $j, ($lineHeight * $row) + $vertOffset, $booking_matrix[$i][$j][$k]['content'], $color);
$row++;
}
// Set row back to the first row if the current station for the next day
$row = $row - count($booking_matrix[$i][$j]);
$day->add(new DateInterval('P1D'));
}
// Add the maximum rows from the specific station at the specific date to the current row to be in the correct row again
$row = $row + $maxHeight;
// if next station is set and has other airport or center designator add empty line to it
if (isset($booking_matrix[$i + 1]) && !self::next_station_is_same($booking_matrix[$i][0], $booking_matrix[$i + 1][0])) {
$row += $next_airport_row_distance;//2;
}
}
for ($i = 1; $i < 8; $i++) {
imageline($im, ($cell_width *$i)-23 , 0, ($cell_width *$i)-23, $lineHeight*$row, $color_gray);
}
self::write_string($im, 3, 23, ($lineHeight * $row) + $vertOffset, "Wanted", $color_red);
self::write_string($im, 3, $cell_width, ($lineHeight * $row) + $vertOffset, "Training", $color_blue);
self::write_string($im, 3, $cell_width * 2 , ($lineHeight * $row) + $vertOffset, "Event", $color_orange);
$row = $row + 3;
// Remove duplicated users and sort them by their first names
$all_users = array_map("unserialize", array_unique(array_map("serialize", $all_users)));
array_multisort($all_users);
$column = 0;
// Draw Users
for ($i = 0; $i < count($all_users); $i++) {
self::write_string($im, 3, $column * 585 + 12, $lineHeight * $row, $all_users[$i]['abbreviation'] . ": " . $all_users[$i]['name'], $color_black);
if ($column === 2) {
$column = 0;
$row++;
} else {
$column++;
}
}
$row += 2;
$generated_time = new DateTime();
self::write_string($im, 2, 11, $lineHeight * $row, "Generated " . $generated_time->format("d.m.Y H:i:s"), $color_gray);
$newHeight = ($row+1)*$lineHeight;
$dstim = imagecreatetruecolor($imageWidth, $newHeight);
imagecopyresized($dstim,$im,0,0,0,0,$imageWidth,$newHeight,$imageWidth,$newHeight);
imagedestroy($im);
return $dstim;
}
private static function next_station_is_same($currentElement, $nextElement) : bool
{
if (substr($currentElement, 0, 4) === substr($nextElement, 0, 4)) {
return true;
}
return false;
}
/**
* @param $weeklyObject
* @param DateTime $day
* @param $bookingStation
* @return bool
*/
private static function isWeeklyPosition($weeklyObject, DateTime $day, $bookingStation) : bool
{
$day_number = $day->format("N");
if(!$weeklyObject) return false;
$check_all_bookings = function ($entry) use ($bookingStation) {
foreach($entry->booking as $station){
$len = strlen($station);
if(substr($bookingStation, 0, $len) == $station) return true;
}
return false;
};
foreach($weeklyObject as $entry) {
if(property_exists($entry, 'rule') && ($entry->rule != 'weekday' || empty($entry->rule))){
switch ($entry->rule){
case 'every_X_days':
//{ "rule": "every_X_days", "one_date": "2022-01-01", "days": "14", "booking": ["EDDL", "EDGG_P"] }
$one_date = new DateTime($entry->one_date);
$day = new DateTime($day->format('Y-m-d'));
$one_date = new DateTime($one_date->format('Y-m-d'));
$day_interval = $one_date->diff($day);
if(($day_interval->days % $entry->days) === 0 && $check_all_bookings($entry)) return true;
break;
/*case 'week_X_in_month':
//{ "rule": "week_X_in_month", "week_in_month": "1", "weekday": "1", "booking": ["EDDL", "EDGG_P"] }
$firstDayOfMonth = new DateTime($day->format('Y-m-1'));
$week_nr = ceil((intval($firstDayOfMonth->format('N')) + intval($day->format('j')) - 1) / 7);
if($week_nr === $entry->week_in_month && $day_number === $entry->weekday && $check_all_bookings($entry)) return true;
break;*/
case 'every_X_day_in_month':
//{ "rule": "every_X_day_in_month", "day_of_week": 3, "number_day_in_month": 2, "booking": ["EDDK"] }
$firstDayOfMonth = new DateTime($day->format('Y-m-1'));
$day_interval = $firstDayOfMonth->diff($day);
if(floor($day_interval->d/7) == $entry->number_day_in_month
&& $entry->day_of_week == $day_number && $check_all_bookings($entry)) return true;
break;
}
} else {
//{ "day": "1", "booking": ["EDDL", "EDGG_P"] }
if ($entry->day === $day_number && $check_all_bookings($entry)) return true;
}
}
return false;
}
private static function write_string($im, $font, $x, $y, $string, $color) : void
{
putenv('GDFONTPATH=' . realpath('.'));
if (file_exists(dirname(__FILE__) . "/MyriadProRegular.ttf")) {
imagettftext($im, 24, 0, $x, $y + 25, $color, dirname(__FILE__) . "/MyriadProRegular.ttf", $string);
} else {
imagestring($im, $font, $x, $y, $string, $color);
}
}
}