-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.php
executable file
·366 lines (336 loc) · 15.4 KB
/
print.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
<?php
/*
* Copyright 2009 Bion Oren
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
date_default_timezone_set("America/Chicago");
error_reporting(E_ALL);
$path = "./";
require_once($path."functions.php");
require_once($path."Meeting.php");
/**
* Handles display of a class schedule.
*
* All input should be in the query string. This class expects the following parameters:
* sem=YYYYXX where YYYY is the 4 digit year and XX is a 2 letter semester code (one of SP, SU, or FA).
* classes=LIST where LIST is a ~ seperated list with each element in the following format:
* 1::2::3::4::5 where each number corresponds as follows:
* 1: INTEGER - Bit string of days this class occurs on (see $DAY_NUMBERS below)
* 2: FLOAT - 24 floating point representation of the class' start time (hours + minutes/60)
* 3: FLOAT - class' end time (see formatting notes for 2)
* 4: STRING - location of the class
* 5: STRING - Name of the class
* [trad=[trad/non]] (Default trad)
* [overlayClasses=LIST] where LIST has the same definition as for classes above
* NOTE: It is assumed that the overlay classes will overlap with the times and days of the other classes.
*
* @author Bion Oren
* @version 1.0
*/
class SchedulePrinter {
/** ARRAY bits corresponding to each day of the week. */
public static $DAY_NUMBERS = array(1, 2, 4, 8, 16, 32, 64);
/** ARRAY names for each day of the week. */
public static $DAYS = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
//IMAGE RELATED CONSTANTS
/** INTEGER Image width. */
protected static $width = 438;//670;
/** INTEGER Image height. */
protected static $height = 0;
/** INTEGER Default start day (Monday). */
protected static $defaultStartDay = 1;
/** INTEGER Default number of days (5 = Monday-Friday default). */
protected static $defaultNumDays = 5;
/** INTEGER Base font size. */
protected static $fontSize = 8;//13;
//INFORMATION SPECIFIC TO THIS SCHEDULE
/**
* INTEGER The start day for this schedule.
* @see $defaultStartDay
*/
protected $startDay;
/**
* INTEGER The number of days in this schedule.
* @see $defaultNumDays
*/
protected $numDays;
/** INTEGER The number (24 hour clock) of the first hour in this schedule. */
protected $startTime;
/** INTEGER The number (24 hour clock) of the last hour in this schedule. */
protected $endTime;
/** INTEGER Width of a day in pixels. */
protected $dayWidth;
/** INTEGER Height of an hour in pixels. */
protected $hourHeight;
/** ARRAY List of classes in this schedule. */
protected $classes = array();
//IMAGE INFORMATION FOR THIS SCHEDULE
/** INTEGER X offset for the schedule (leaves room for the hour row headers). */
protected $offsetX = 26;
/** INTEGER Y offset for the schedule (leaves room for the day column headers). */
protected $offsetY = 25;
/** STRING Location of a font to use. Tahoma is from the open source WINE project. */
protected $font = "layout/tahoma.ttf";
/** RESOURCE Default background color. */
protected $background;
/** RESOURCE Default text color. */
protected $foreground;
/** RESOURCE Default background color for class blocks. */
protected $classBackground;
/** RESOURCE Internal image resource. */
protected $img;
/** Whether or not an image has been rendered. */
protected $displayed = false;
/**
* Initializes color and image information and processes arguments from the query string.
*/
public function __construct($size=null) {
if(!$size) {
$size = 438;
}
$this::$width = $size;
$this::$height = round($this::$width*880/670);
$this->setImage(1, 1);
$this->allocateColors();
$this->setImage(SchedulePrinter::$width, SchedulePrinter::$height);
$this->classes = $this->parseClasses($_REQUEST["classes"]);
if(substr($_REQUEST["sem"], -2) != "SU" && $_REQUEST["trad"] != "non") {
//add chapel
$tmp = array();
$tmp[] = 2+8+32;
$tmp[] = 10.83333333333;
$tmp[] = 11.5;
$tmp[] = "Belcher";
$tmp[] = "Chapel";
$this->classes[] = $tmp;
}
if(empty($this->classes)) {
die();
}
//find the first and last class
$this->startTime = 24;
$this->endTime = 0;
$this->startDay = SchedulePrinter::$defaultStartDay;
$this->numDays = SchedulePrinter::$defaultNumDays;
foreach($this->classes as $class) {
if($class[1] < $this->startTime) {
$this->startTime = floor($class[1]);
}
if($class[2] > $this->endTime) {
$this->endTime = ceil($class[2]);
}
if($class[0] & 1) {
//class on Sunday
$this->numDays++;
$this->startDay = 0;
}
if($class[0] & 64) {
//class on Saturday
$this->numDays++;
}
}
}
/**
* Allocates color objects.
*
* @return VOID
*/
public function allocateColors() {
$this->background = imagecolorallocate($this->img, 255, 255, 255);
$this->foreground = imagecolorallocate($this->img, 0, 0, 0);
$this->classBackground = imagecolorallocate($this->img, 253, 255, 79);
}
/**
* Displays the current schedule image.
*
* @return VOID
*/
public function display() {
if(!$this->displayed) {
header('Content-type: image/png');
imagepng($this->img);
$this->displayed = true;
}
}
/**
* Draws an individual class.
*
* @param INTEGER $classDays Bit string of days this class occurs on (see $DAY_NUMBERS below).
* @param FLOAT $startTime 24 floating point representation of the class' start time (hours + minutes/60).
* @param FLOAT $endTime class' end time (see formatting notes for 2).
* @param STRING $location location of the class.
* @param STRING $title Name of the class.
* @return VOID
*/
function drawClass($classDays, $startTime, $endTime, $location, $title, $isOverlay=false) {
for($i = $this->startDay; $i <= $this->numDays; $i++) {
if($classDays & SchedulePrinter::$DAY_NUMBERS[$i]) {
$start = $startTime-$this->startTime;
$end = $endTime-$this->startTime;
if($i == $this->numDays) {
$rectEnd = $this::$width-2;
} else {
$rectEnd = $this->offsetX-2+$this->dayWidth*$i;
}
imagefilledrectangle($this->img, $this->offsetX+$this->dayWidth*($i-1), $this->offsetY+5+$start*$this->hourHeight, $rectEnd, $this->offsetY-5+$end*$this->hourHeight, $this->classBackground);
imagefilledrectangle($this->img, $this->offsetX+5+$this->dayWidth*($i-1), $this->offsetY+$start*$this->hourHeight, $rectEnd-5, $this->offsetY+$end*$this->hourHeight, $this->classBackground);
//rounded edges
//ul
imagefilledellipse($this->img, $this->offsetX+6+$this->dayWidth*($i-1), $this->offsetY+5+$start*$this->hourHeight, 10, 10, $this->classBackground);
//ll
imagefilledellipse($this->img, $this->offsetX+6+$this->dayWidth*($i-1), $this->offsetY-5+$end*$this->hourHeight, 10, 10, $this->classBackground);
//ur
imagefilledellipse($this->img, $rectEnd-5, $this->offsetY+5+$start*$this->hourHeight, 10, 10, $this->classBackground);
//lr
imagefilledellipse($this->img, $rectEnd-5, $this->offsetY-5+$end*$this->hourHeight, 10, 10, $this->classBackground);
$pos = imagettftext($this->img, $this::$fontSize, 0, $this->offsetX+2+$this->dayWidth*($i-1), $this->offsetY+16+$start*$this->hourHeight, $this->foreground, $this->font, $this->wrap($this::$fontSize, 0, $title, $this->dayWidth));
$tmp = $pos[1]-$pos[7]+20;
if($isOverlay) {
$y = $this->offsetY-$tmp/2+$end*$this->hourHeight;
} else {
$y = $this->offsetY+$tmp+$start*$this->hourHeight;
}
//11
//imagettftext($this->img, $this::$fontSize, 0, $this->offsetX+4+$this->dayWidth*($i-1), $y, $this->foreground, $this->font, $location);
if(!$isOverlay) {
//$tmp += 16;
imagettftext($this->img, $this::$fontSize, 0, $this->offsetX+2+$this->dayWidth*($i-1), $this->offsetY+$tmp+$start*$this->hourHeight, $this->foreground, $this->font, Meeting::displayTime($startTime)." - ".Meeting::displayTime($endTime));
}
}
}
}
/**
* Draws all of the classes in this schedule.
*
* @return VOID
*/
public function drawClasses() {
foreach($this->classes as $class) {
$this->drawClass($class[0], $class[1], $class[2], $class[3], $class[4]);
}
if(isset($_REQUEST["overlayClasses"])) {
$this->classBackground = imagecolorallocatealpha($this->img, 80, 80, 255, 80);
$overlay = $this->parseClasses($_REQUEST["overlayClasses"]);
foreach($overlay as $class) {
$this->drawClass($class[0], $class[1], $class[2], $class[4], "", true);
}
}
}
/**
* Draws the hour and day headers and bounding boxes for the schedule.
*
* @return VOID
*/
function drawFrame() {
imagesetthickness($this->img, 1);
//border
imagerectangle($this->img, 0, 1, $this->offsetX - 1, $this->offsetY - 1, $this->foreground);
$this->dayWidth = (SchedulePrinter::$width-$this->offsetX)/$this->numDays;
for($i = 0; $i < $this->numDays; $i++) {
$x = $this->offsetX+$this->dayWidth*$i;
$x1 = $this->offsetX+$this->dayWidth*($i+1);
$midpt = ($x1-$x)/2;
imagerectangle($this->img, $x - 1, 1, $x+$this->dayWidth - 1, SchedulePrinter::$height-1, $this->foreground);
$text = SchedulePrinter::$DAYS[$i+$this->startDay];
$box = imagettfbbox($this::$fontSize+1, 0, $this->font, $text);
$width = $box[4] - $box[0];
imagettftext($this->img, $this::$fontSize+1, 0, round($x+($midpt-$width/2)), 19, $this->foreground, $this->font, $text);
}
//hour headers
$startHour = $this->startTime-1;
$numHours = $this->endTime-$this->startTime;
$this->hourHeight = (SchedulePrinter::$height - $this->offsetY)/$numHours;
for($i = 0; $i < $numHours; $i++) {
$y = $this->offsetY+$this->hourHeight*$i;
imagerectangle($this->img, 0, $y - 1, SchedulePrinter::$width-1, $y+$this->hourHeight - 1, $this->foreground);
imagettftext($this->img, $this::$fontSize-1, 0, 2, $y+12, $this->foreground, $this->font, (($i+$startHour)%12+1).":00");
}
}
/**
* Parses a list of classes into an array of classes' information.
*
* @param STRING $data Data string of the form specified above.
* @return ARRAY List of classes' information.
* @see $classes
*/
protected function parseClasses($data) {
$ret = array();
$tmp = explode("~", urldecode($data));
foreach($tmp as $class) {
if(!empty($class)) {
$tmp = explode("::", $class);
if($tmp[1] != "TBA" && $tmp[1] > 0 && $tmp[0] > 0) {
$ret[] = $tmp;
}
}
}
return $ret;
}
/**
* Creates a new image of the specified size filled with the background color, and cleans up the old one.
*
* @param INTEGER $width Desired image width.
* @param INTEGER $height Desired image height.
* @return VOID
*/
protected function setImage($width, $height) {
if($this->img) {
imagecolordeallocate($this->img, $this->background);
imagecolordeallocate($this->img, $this->foreground);
imagecolordeallocate($this->img, $this->classBackground);
imagedestroy($this->img);
}
if($width && $height) {
$this->img = imagecreatetruecolor($width, $height);
$this->allocateColors();
imagefill($this->img, 0, 0, $this->background);
}
}
/**
* Wraps text.
*
* @param INTEGER $fontSize The size of the font.
* @param INTEGER $angle The angle of the text from vertical.
* @param STRING $string The string to wrap.
* @param INTEGER $width The width of the container.
* @return STRING Text wrapped with newlines.
*/
function wrap($fontSize, $angle, $string, $width){
$ret = "";
$arr = explode(' ', str_replace("/", " /", htmlspecialchars_decode($string)));
foreach($arr as $word) {
$testbox = imagettfbbox($fontSize, $angle, $this->font, $ret.' '.$word);
if ($testbox[2] > $width) {
$ret .= "\n".$word;
} else {
$ret .= ' '.$word;
}
}
return substr($ret, 1);
}
/**
* Cleans up the image associated with this class and writes out a 1x1 placeholder with the current background color.
*
* @return VOID
*/
public function __destruct() {
$this->setImage(1,1);
$this->display();
//yes, the 0, 0 is important. Memory management in destructors is a bit shaky in 5.4RC3
$this->setImage(0, 0);
}
}
$printer = new SchedulePrinter(@$_REQUEST["size"]);
$printer->drawFrame();
$printer->drawClasses();
$printer->display();
?>