source: branches/2.2.0.1/calendar/js/dhtmlx/server/pdfWrapper.php @ 4001

Revision 4001, 24.4 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #1615 - Componente novo para agenda......................................

Line 
1<?php
2/*
3Copyright DHTMLX LTD. http://www.dhtmlx.com
4*/
5
6
7require_once 'tcpdf_ext.php';
8
9class pdfWrapper {
10        private $cb;
11        private $footenotes;
12        private $colWidth = 50; // column width in month mode for footenotes
13        private $colOffset = 5;  // spaces between columns in month mode for footenotes
14
15        public function pdfWrapper($offsetTop, $offsetRight, $offsetBottom, $offsetLeft, $orientation) {
16                $this->cb = new TCPDFExt($orientation, 'mm', 'A4', true, 'UTF-8', false);
17
18                $this->offsetLeft = $offsetLeft;
19                $this->offsetRight = $offsetRight;
20                $this->offsetTop = $offsetTop;
21                $this->offsetBottom = $offsetBottom;
22
23                // sets header and footer
24                $this->cb->setPrintHeader(false);
25                $this->cb->setPrintFooter(false);
26                $this->cb->SetMargins($this->offsetLeft, $this->offsetTop, $this->offsetRight);
27                $this->cb->SetAutoPageBreak(FALSE, $this->offsetBottom);
28                $this->cb->SetFooterMargin($this->offsetBottom);
29
30                // sets output PDF information
31                $this->cb->SetCreator('DHTMLX LTD');
32                $this->cb->SetAuthor('DHTMLX LTD');
33                $this->cb->SetTitle('dhtmlxScheduler');
34                $this->cb->SetSubject('DHTMLX LTD');
35                $this->cb->SetKeywords('');
36
37                // sets font family and size
38                $this->cb->SetFont('freesans', '', 8);
39        }
40
41
42        // draws scheduler header in month mode
43        public function drawMonthHeader($orientation, $topScale, $topScaleHeight, $bgColor, $lineColor, $monthHeaderFontSize) {
44                $this->cb->AddPage($orientation);
45                $this->bgColor = $this->convertColor($bgColor);
46                $this->lineColor = $this->convertColor($lineColor);
47                $this->topScaleHeight = $topScaleHeight;
48                $this->topScale = $topScale;
49
50                $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '2', 'color' => $this->lineColor);
51                $this->cb->SetLineStyle($this->lineStyle);
52                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
53                $this->cb->SetFontSize($monthHeaderFontSize);
54
55                // calculates day width
56                $this->dayWidth = ($this->cb->getPageWidth() - $this->offsetLeft - $this->offsetRight)/7;
57                // circle: for every column of header draw cell
58                for ($i = 0; $i < count($topScale); $i++) {
59                        $this->cb->Cell($this->dayWidth, $topScaleHeight, $topScale[$i], 1, 0, 'C', 1);
60                }
61        }
62
63
64        // draws scheduler grid on month mode
65        public function drawMonthGrid($dayHeader, $dayHeaderHeight, $dayHeaderColor, $dayBodyColor, $dayHeaderColorInactive, $dayBodyColorInactive, $monthDayHeaderFontSize) {
66                // sets starting coordinates
67                $this->cb->setX($this->offsetLeft);
68                $this->cb->setY($this->offsetTop + $this->topScaleHeight, false);
69
70                $this->dayHeaderColor = $this->convertColor($dayHeaderColor);
71                $this->dayBodyColor = $this->convertColor($dayBodyColor);
72                $this->dayHeaderColorInactive = $this->convertColor($dayHeaderColorInactive);
73                $this->dayBodyColorInactive = $this->convertColor($dayBodyColorInactive);
74
75                // calculates height of day container header, body and whole height of day container
76                $this->dayHeaderHeight = $dayHeaderHeight;
77                $this->dayHeader = $dayHeader;
78                $this->dayBodyHeight = ($this->cb->getPageHeight() - $this->offsetTop - $this->offsetBottom - $this->topScaleHeight - $this->dayHeaderHeight*count($dayHeader))/count($dayHeader);
79                $this->dayHeight = $this->dayHeaderHeight + $this->dayBodyHeight;
80
81                // sets font size
82                $this->cb->SetFontSize($monthDayHeaderFontSize);
83                // creates array of values where result['week']['day'] is true if day container in current month and false otherwise
84                $activeDays = $this->setActiveDays($dayHeader);
85
86                // circle for drawing day containers
87                for ($i = 0; $i < count($dayHeader); $i++) {
88                        // draws day headers for every cell of row
89                        for ($j = 0; $j < count($dayHeader[$i]); $j++) {
90                                $ln = ($j == 6) ? 1 : 0;
91                                $color = ($activeDays[$i][$j] == true) ? $this->dayHeaderColor : $this->dayHeaderColorInactive;
92                                $this->cb->SetFillColor($color['R'], $color['G'], $color['B']);
93                                $this->cb->Cell($this->dayWidth, $this->dayHeaderHeight, $dayHeader[$i][$j], 1, $ln, 'R', 1);
94                                $this->dayEventsHeight[$i][$j] = 0;
95                        }
96                        // draws day body for every cell of row
97                        for ($j = 0; $j < 7; $j++) {
98                                $ln = ($j == 6) ? 1 : 0;
99                                $color = ($activeDays[$i][$j] == true) ? $this->dayBodyColor : $this->dayBodyColorInactive;
100                                $this->cb->SetFillColor($color['R'], $color['G'], $color['B']);
101                                $this->cb->Cell($this->dayWidth, $this->dayBodyHeight, '', 1, $ln, 'R', 1);
102                        }
103                }
104        }
105
106
107        // draws events in month mode
108        public function drawMonthEvents($events, $eventColor, $eventBorderColor, $monthEventFontSize, $mode, $offset = 1, $offsetLine = 6) {
109                $this->eventColor = $this->convertColor($eventColor);
110                $this->eventBorderColor = $this->convertColor($eventBorderColor);
111                $this->events = $events;
112
113                $this->cb->setFontSize($monthEventFontSize);
114
115                $this->cb->SetFillColor($this->eventColor['R'], $this->eventColor['G'], $this->eventColor['B']);               
116                // initial value for footnote number
117                $footNum = 0;
118                // initial values for checking if in some day footenote has already drawed
119                $day = -1;
120                $week = -1;
121
122                // circle for drawing every event
123                for ($i = 0; $i < count($this->events); $i++) {
124                        $event = $this->events[$i];
125                        if ($event['week'] >= count($this->dayHeader)) {
126                                continue;
127                        }
128                        // calculation x-, y- positions, width and height of event
129                        $x = $this->offsetLeft + $event['day']*$this->dayWidth;
130                        $y = $this->offsetTop + $this->topScaleHeight + $event['week']*$this->dayHeight + $this->dayHeaderHeight + $this->dayEventsHeight[$event['week']][$event['day']] + $offset;
131                        if ($event['type'] == 'event_line') {
132                                $width = $event['width']*$this->dayWidth/100;
133                                $x += 1.5;
134                        } else {
135                                $width = $this->dayWidth - 1;
136                        }
137                        $height = 4;
138
139                        $this->cb->setX($x);
140                        $this->cb->setY($y, false);
141
142                        // checks if event can be drawed in day container
143                        if ($y + $height > ($this->offsetTop + $this->topScaleHeight + ($event['week'] + 1)*$this->dayHeight)) {
144                                // checks if footenote hasn't already drawed for current day and week values
145                                if (!(($week == $event['week'])&&($day == $event['day']))) {
146                                        $footNum++;
147                                        $x1Line = $this->offsetLeft + $this->dayWidth*($event['day'] + 1);
148                                        $x2Line = $x1Line - $offsetLine;
149                                        $y1Line = $this->offsetTop + $this->topScaleHeight + $this->dayHeight*($event['week'] + 1) - $offsetLine;
150                                        $y2Line = $this->offsetTop + $this->topScaleHeight + $this->dayHeight*($event['week'] + 1);
151                                        $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '2', 'color' => $this->lineColor);
152                                        $this->cb->SetLineStyle($this->lineStyle);
153                                        $this->cb->Line($x1Line, $y1Line, $x2Line, $y2Line);
154                                        $textWidth = $this->cb->getStringWidth($footNum);
155                                        $xText = $x1Line - $textWidth - 0.5;
156                                        $yText = $y2Line - 0.6;
157
158                                        $day = $event['day'];
159                                        $week = $event['week'];
160                                        $this->cb->Text($xText, $yText, $footNum);
161                                }
162                                // sets variable which means that this event will print in footenotes
163                                $this->events[$i]['foot'] = true;
164                        } else {
165                                // draws event in current day
166                                $this->lineStyle = Array('width' => 0.3, 'cap' => 'round', 'join' => 'round', 'dash' => '0', 'color' => $this->eventBorderColor);
167                                $this->cb->SetLineStyle($this->lineStyle);
168                                $text = $this->textWrap($event['text'], $width, $monthEventFontSize);
169
170                                // sets event color
171                                $color = $this->processEventColor($event['color']);
172                                if (($color == 'transparent')||($mode !== 'color')) {
173                                        $this->cb->SetFillColor($this->eventColor['R'], $this->eventColor['G'], $this->eventColor['B']);
174                                } else {
175                                        $color = $this->convertColor($color);
176                                        $this->cb->SetFillColor($color['R'], $color['G'], $color['B']);
177                                }
178
179                                if ($event['type'] == 'event_clear') {
180                                        $this->cb->Cell($width, $height, $text, 0, 0, 'L', 0);
181                                } else {
182                                        $this->cb->Cell($width, $height, $text, 1, 0, 'L', 1);
183                                }
184                                for ($j = $event['day']; ($j < $event['day'] + ceil($width/$this->dayWidth))&&($j < 7); $j++) {
185                                        if ($event['week'] < count($this->dayHeader)) {
186                                                $this->dayEventsHeight[$event['week']][$j] += $height + $offset;
187                                        }
188                                }
189                                $this->events[$i]['foot'] = false;
190                        }
191                }
192                // draws footenotes
193                if ($footNum > 0) {
194                        $this->drawMonthFootenotes();
195                }
196        }
197
198
199        // draws pages with events which can not being showed in day container
200        private function drawMonthFootenotes() {
201                $this->cb->addPage();
202
203                // initials x-, y- coordinates
204                $x = $this->offsetLeft;
205                $y = $this->offsetTop;
206               
207                // variables for checking if events have the same date
208                $week = -1;
209                $day = -1;
210                $footNum = 1;
211                // circle for all events
212                for ($i = 0; $i < count($this->events); $i++) {
213                        // checks not printed events
214                        if (($this->events[$i]['week'] < count($this->dayHeader))&&($this->events[$i]['foot'] == true)) {
215                                $event = $this->events[$i];
216                                $text = $event['text'];
217                                // checks if it's necessary to print footenote number
218                                if (!(($week == $event['week'])&&($day == $event['day']))) {
219                                        $textTop = $this->dayHeader[$event['week']][$event['day']].'['.$footNum.']';
220                                        $linesNum = $this->cb->getNumLines($text, $this->colWidth);
221                                        $heightEvent = $linesNum*$this->cb->getFontSize() + $this->cb->getFontSize()*0.5*($linesNum + 1);
222                                        $linesNum = $this->cb->getNumLines($textTop, $this->colWidth);
223                                        $heightHeader = $linesNum*$this->cb->getFontSize() + $this->cb->getFontSize()*0.5*($linesNum + 1);
224                                        // checks if the current column is over
225                                        if ($y + $heightEvent + $heightHeader > $this->cb->getPageHeight() - $this->offsetBottom) {
226                                                $x += $this->colWidth + $this->colOffset;
227                                                $y = $this->offsetTop;
228                                                // checks if it's necessary to add new page
229                                                if ($x + $this->colWidth > $this->cb->getPageWidth() - $this->offsetRight) {
230                                                        $this->cb->addPage();
231                                                        $x = $this->offsetLeft;
232                                                        $y = $this->offsetTop;
233                                                }
234                                        }
235                                       
236                                        $this->cb->MultiCell($this->colWidth, 5, $textTop, 0, 'C', 0, 0, $x, $y);
237                                        $y += $this->cb->getLastH();
238                                        $footNum++;
239                                }
240
241                                // checks if currrent column is over
242                                if ($y + $heightEvent > $this->cb->getPageHeight() - $this->offsetBottom) {
243                                        $x += $this->colWidth + $this->colOffset;
244                                        $y = $this->offsetTop;
245                                        // checks if it's necessary to add new page
246                                        if ($x + $this->colWidth > $this->cb->getPageWidth() - $this->offsetRight) {
247                                                $this->cb->addPage();
248                                                $x = $this->offsetLeft;
249                                                $y = $this->offsetTop;
250                                        }
251                                        $textTop = $this->dayHeader[$event['week']][$event['day']].'['.($footNum - 1).']';
252                                        $this->cb->MultiCell($this->colWidth, 5, $textTop, 0, 'C', 0, 0, $x, $y);
253                                        $y += $this->cb->getLastH();
254                                }
255                                // draws event
256                                $this->cb->MultiCell($this->colWidth, 5, $text, 1, 'L', 1, 0, $x, $y);
257                                $y += $this->cb->getLastH();
258                                $week = $event['week'];
259                                $day = $event['day'];
260                        }
261                }
262        }
263
264
265        // creates array with active/inactive option for every day
266        private function setActiveDays($dayHeader) {
267                if ($dayHeader[0][0] == '1') {
268                        // month grid starts from first day of month
269                        $flag = true;
270                        $flagCount = 1;
271                } else {
272                        // month grid starts from day of previous month
273                        $flag = false;
274                        $flagCount = 0;
275                }
276                $activeDays = Array();
277                $prevDay = (int) $dayHeader[0][0];
278               
279                for ($i = 0; $i < count($dayHeader); $i++) {
280                        for ($j = 0; $j < count($dayHeader[$i]); $j++) {
281                                // check if previous day value is less then current day value
282                                if (((int) $dayHeader[$i][$j] < $prevDay)&&($flagCount < 2)) {
283                                        $flag = !$flag;
284                                        $flagCount++;
285                                }
286                                $activeDays[$i][$j] = $flag;
287                                $prevDay = (int) $dayHeader[$i][$j];
288                        }
289                }
290                return $activeDays;
291        }
292       
293       
294        //
295        public function drawYear($orientation, $yearValues, $events, $headerHeight, $bgColor, $lineColor, $dayColor, $dayColorInactive, $eventColor, $yearHeaderFontSize, $yearFontSize) {
296                $this->cb->AddPage($orientation);
297                $this->bgColor = $this->convertColor($bgColor);
298                $this->lineColor = $this->convertColor($lineColor);
299                $this->dayColor = $this->convertColor($dayColor);
300                $this->dayColorInactive = $this->convertColor($dayColorInactive);
301                $this->eventColor = $this->convertColor($eventColor);
302                $this->yearValues = $yearValues;
303                $this->events = $events;
304                $this->headerHeight = $headerHeight;
305
306                // offset between monthes
307                $offset = 5;
308
309                // sets line style and color
310                $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '2', 'color' => $this->lineColor);
311                $this->cb->SetLineStyle($this->lineStyle);
312                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
313
314                // calculates month width and height
315                $monthWidth = ($this->cb->getPageWidth() - $this->offsetLeft - $this->offsetRight - $offset*3)/4;
316                $monthHeight = ($this->cb->getPageHeight() - $this->offsetTop - $this->offsetBottom - $offset*2)/3;
317
318                // circles for drawing all monthes
319                for ($i = 0; $i < 3; $i++) {
320                        for ($j = 0; $j < 4; $j++) {
321                                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
322                                $num = $i*4 + $j;
323                                // calculates x- and y-coordinates of current month
324                                $x = $this->offsetLeft + $j*$monthWidth + $offset*($j);
325                                $y = $this->offsetTop + $i*$monthHeight + $offset*($i);
326                                $this->cb->setX($x);
327                                $this->cb->setY($y, false);
328                                $this->cb->SetFontSize($yearHeaderFontSize);
329                                // draws name of month
330                                $this->cb->Cell($monthWidth, $this->headerHeight, $this->yearValues[$num]['label'], 1, 1, 'C', 1);
331                                // calculates day width and height
332                                $dayWidth = $monthWidth/7;
333                                $dayHeight = ($monthHeight - $headerHeight)/7;
334                                $y += $headerHeight;
335                                // create array of day in current month
336                                $activeDays = $this->setActiveDays($this->yearValues[$num]['rows']);
337
338                                // draws day names
339                                for ($k = 0; $k < count($this->yearValues[$num]['columns']); $k++) {
340                                        $this->cb->setX($x);
341                                        $this->cb->setY($y, false);
342                                        $this->cb->Cell($dayWidth, $dayHeight, $this->yearValues[$num]['columns'][$k], 1, 1, 'C', 1);
343                                        $x += $dayWidth;
344                                }
345                                $y += $dayHeight;
346                                $x -= $dayWidth*7;
347                                $this->cb->SetFontSize($yearFontSize);
348                                // draws days
349                                for ($k = 0; $k < count($this->yearValues[$num]['rows']); $k++) {
350                                        for ($l = 0; $l < count($this->yearValues[$num]['rows'][$k]); $l++) {
351                                                $this->cb->setX($x);
352                                                $this->cb->setY($y, false);
353                                                // checks if day of current month
354                                                if ($activeDays[$k][$l] == true) {
355                                                        // checks if the day have events
356                                                        if (in_array($num.'_'.$k.'_'.$l, $this->events)) {
357                                                                $this->cb->SetFillColor($this->eventColor['R'], $this->eventColor['G'], $this->eventColor['B']);
358                                                        } else {
359                                                                $this->cb->SetFillColor($this->dayColor['R'], $this->dayColor['G'], $this->dayColor['B']);
360                                                        }
361                                                        $text = $this->yearValues[$num]['rows'][$k][$l];
362                                                } else {
363                                                        // it's the day of previous or next month
364                                                        $this->cb->SetFillColor($this->dayColorInactive['R'], $this->dayColorInactive['G'], $this->dayColorInactive['B']);
365                                                        $text = '';
366                                                }
367                                                // draw day
368                                                $this->cb->Cell($dayWidth, $dayHeight, $text, 1, 1, 'C', 1);
369                                                $x += $dayWidth;
370                                        }
371                                        $y += $dayHeight;
372                                        $x -= $dayWidth*7;
373                                }
374                        }
375                }
376        }
377
378
379        // draws agenda mode
380        public function drawAgenda($agendaHeader, $events, $rowHeight, $bgColor, $lineColor, $scaleColorOne, $scaleColorTwo, $fontSize, $today, $todayFontSize) {
381                $this->bgColor = $this->convertColor($bgColor);
382                $this->lineColor = $this->convertColor($lineColor);
383                $this->scaleColorOne = $this->convertColor($scaleColorOne);
384                $this->scaleColorTwo = $this->convertColor($scaleColorTwo);
385                $this->cb->addPage();
386                $this->drawToday($today, $todayFontSize);
387                $this->cb->setFontSize($fontSize);
388
389                // sets column
390                $timeColWidth = 50;
391                $dscColWidth = $this->cb->getPageWidth() - $this->offsetLeft - $this->offsetRight - $timeColWidth;
392                $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '2', 'color' => $this->lineColor);
393                $this->cb->SetLineStyle($this->lineStyle);
394                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
395
396                $text = $this->textWrap($agendaHeader[0], $timeColWidth, $fontSize);
397                $this->cb->Cell($timeColWidth, $rowHeight, $text, 1, 0, 'C', 1);
398                $text = $this->textWrap($agendaHeader[1], $dscColWidth, $fontSize);
399                $this->cb->Cell($dscColWidth, $rowHeight, $text, 1, 1, 'C', 1);
400
401                for ($i = 0; $i < count($events); $i++) {
402                        // checks if the page is over
403                        if ($this->cb->getY() + $rowHeight > $this->cb->getPageHeight() - $this->offsetBottom) {
404                                // add new page, draws page num, header and today label
405                                $this->cb->setPrintFooter(true);
406                                $this->cb->addPage();
407                                $this->drawToday($today, $todayFontSize);
408                                $this->cb->setFontSize($fontSize);
409                                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
410                                $text = $this->textWrap($agendaHeader[0], $timeColWidth, $fontSize);
411                                $this->cb->Cell($timeColWidth, $rowHeight, $text, 1, 0, 'C', 1);
412                                $text = $this->textWrap($agendaHeader[1], $dscColWidth, $fontSize);
413                                $this->cb->Cell($dscColWidth, $rowHeight, $text, 1, 1, 'C', 1);
414                        }
415                        // selects scale color
416                        if ($i%2 == 0) {
417                                $this->cb->SetFillColor($this->scaleColorOne['R'], $this->scaleColorOne['G'], $this->scaleColorOne['B']);
418                        } else {
419                                $this->cb->SetFillColor($this->scaleColorTwo['R'], $this->scaleColorTwo['G'], $this->scaleColorTwo['B']);
420                        }
421                        // draws time cell
422                        $text = $this->textWrap($events[$i]['head'], $timeColWidth, $fontSize);
423                        $this->cb->Cell($timeColWidth, $rowHeight, $text, 1, 0, 'L', 1);
424                        // draws description cell
425                        $text = $this->textWrap($events[$i]['body'], $dscColWidth, $fontSize);
426                        $this->cb->Cell($dscColWidth, $rowHeight, $text, 1, 1, 'L', 1);
427                }
428        }
429
430
431        // draws headers and grid for day and week mode
432        public function drawDayWeekHeader($columnHeader, $rowHeader, $topHeight, $leftWidth, $bgColor, $lineColor, $scaleColorOne, $scaleColorTwo, $dayHeaderFontSize, $dayScaleFontSize, $mode) {
433                $this->cb->addPage();
434                $this->bgColor = $this->convertColor($bgColor);
435                $this->lineColor = $this->convertColor($lineColor);
436                $this->scaleColorOne = $this->convertColor($scaleColorOne);
437                $this->scaleColorTwo = $this->convertColor($scaleColorTwo);
438
439                $this->leftWidth = $leftWidth;
440                $this->topHeight = $topHeight;
441
442                $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '2', 'color' => $this->lineColor);
443                $this->cb->SetLineStyle($this->lineStyle);
444                $this->cb->SetFillColor($this->bgColor['R'], $this->bgColor['G'], $this->bgColor['B']);
445
446                // calculates cell width and height
447                $this->colWidth = ($this->cb->getPageWidth() - $this->offsetLeft - $this->offsetRight - $leftWidth)/count($columnHeader);
448                $this->colHeight = ($this->cb->getPageHeight() - $this->offsetTop - $this->offsetBottom - $topHeight)/count($rowHeader);
449
450                $this->cb->setX($this->offsetLeft + $leftWidth);
451                $this->cb->setY($this->offsetTop, false);
452               
453                // draws scales on top
454                $this->cb->setFontSize($dayHeaderFontSize);
455                for ($i = 0; $i < count($columnHeader); $i++) {
456                        $Ln = ($i == count($columnHeader) - 1) ? 1 : 0;
457                        $this->cb->Cell($this->colWidth, $topHeight, $columnHeader[$i], 1, $Ln, 'C', 1);
458                }
459
460                $this->cb->setFontSize($dayScaleFontSize);
461                // draws scales on left
462                for ($i = 0; $i < count($rowHeader); $i++) {
463                        $this->cb->Cell($leftWidth, $this->colHeight, $rowHeader[$i], 1, 1, 'C', 1);
464                }
465
466                $this->cb->setX($this->offsetLeft + $leftWidth);
467                $this->cb->setY($this->offsetTop + $topHeight, false);
468                // circle for drawing scales
469
470                for ($i = 0; $i < count($rowHeader); $i++) {
471                        // draws white line
472                        $this->cb->SetFillColor($this->scaleColorOne['R'], $this->scaleColorOne['G'], $this->scaleColorOne['B']);
473                        $border = (($i == 0)||($mode == 'bw')) ? 'LRT' : 'LR';
474                        $this->cb->Cell($this->colWidth*count($columnHeader), $this->colHeight/2, '', $border, 1, 'C', 1);
475                        // draws blue line
476                        $this->cb->setX($this->offsetLeft + $leftWidth);
477                        $this->cb->SetFillColor($this->scaleColorTwo['R'], $this->scaleColorTwo['G'], $this->scaleColorTwo['B']);
478                        $border = (($i == count($rowHeader) - 1)||($mode == 'bw')) ? 'LRB' : 'LR';
479                        $this->cb->Cell($this->colWidth*count($columnHeader), $this->colHeight/2, '', $border, 1, 'C', 1);
480                        $this->cb->setX($this->offsetLeft + $leftWidth);
481                }
482                // draws lines delemiters between days if it's week mode
483                if (count($columnHeader > 1)) {
484                        for ($i = 0; $i < count($columnHeader) - 1; $i++) {
485                                $x = $this->offsetLeft + $leftWidth + ($i + 1)*$this->colWidth;
486                                $y1 = $this->offsetTop + $topHeight;
487                                $y2 = $this->cb->getPageHeight() - $this->offsetBottom;
488                                $this->cb->line($x, $y1, $x, $y2);
489                        }
490                }
491        }
492
493
494        // draws events in day and week modes
495        public function drawDayWeekEvents($events, $eventColor, $eventBorderColor, $dayEventHeaderFontSize, $dayEventBodyFontSize, $mode) {
496                $this->eventColor = $this->convertColor($eventColor);
497                $this->eventBorderColor = $this->convertColor($eventBorderColor);
498               
499                $eventHeaderHeight = 4;
500               
501                $this->lineStyle = Array('width' => 0.1, 'cap' => 'round', 'join' => 'round', 'dash' => '0', 'color' => $this->eventBorderColor);
502                $this->cb->setLineStyle($this->lineStyle);
503               
504
505                // circle for every event
506                for ($i = 0; $i < count($events); $i++) {
507                        $event = $events[$i];
508                        // sets event color
509                        $color = $this->processEventColor($event['color']);
510                        if (($color == 'transparent')||($mode !== 'color')) {
511                                $this->cb->SetFillColor($this->eventColor['R'], $this->eventColor['G'], $this->eventColor['B']);
512                        } else {
513                                $color = $this->convertColor($color);
514                                $this->cb->SetFillColor($color['R'], $color['G'], $color['B']);
515                        }
516
517                        // calculates x-, y-coordinates, width and height of event
518                        $x = $this->offsetLeft + $this->leftWidth + $event['x']*$this->colWidth/100;
519                        $y = $this->offsetTop + $this->topHeight + $event['y']*$this->colHeight/100;
520                        $width = $event['width']*$this->colWidth/100 - $this->colWidth/60;
521                        $height = $event['height']*$this->colHeight/100;
522                       
523                        // draws event container
524                        $this->cb->RoundedRect($x, $y, $width, $height, 1, '1111', 'DF', array(), $this->eventColor);
525                        $this->cb->setX($x);
526                        $this->cb->setY($y, false);
527                        // draws event header
528                        $this->cb->setFontSize($dayEventHeaderFontSize);
529                        $text = $this->textWrap($event['head'], $width, $dayEventHeaderFontSize);
530                        $this->cb->Cell($width, $eventHeaderHeight, $text, 0, 0, 'C', 0);
531                        $y += $eventHeaderHeight;
532                        $height -= $eventHeaderHeight;
533                        // draws event body
534                        $this->cb->setFontSize($dayEventBodyFontSize);
535                        $this->cb->MultiCell($width, $height, $event['body'], 0, 'L', 0, 0, $x, $y,  true, 0, false, true, $height);
536                        // draws separate line
537                        $this->cb->line($x, $y, $x + $width, $y);
538                }
539        }
540
541
542        // draws today value
543        public function drawToday($today, $todayFontSize) {
544                $this->cb->setFontSize($todayFontSize);
545                $this->cb->Text($this->offsetLeft, $this->offsetTop - 2, $today);
546        }
547
548
549        // wraps text accordigly getted width and font size
550        private function textWrap($text, $width, $fontSize) {
551                $strWidth = $this->cb->GetStringWidth($text, '', '', $fontSize, false);
552                // if text should be wrapped
553                if ($strWidth >= $width) {
554                        $newStr = '';
555                        $newW = 0;
556                        $i = 0;
557                        // adds one symbol and checks text width
558                        while ($newW < $width - 1) {
559                                $newStr .= $text[$i];
560                                $newW = $this->cb->GetStringWidth($newStr.$text[$i + 1].'...', '', '', $fontSize, false);
561                                $i++;
562                        }
563                        return $newStr.'...';
564                } else {
565                        return $text;
566                }
567        }
568
569
570        // outputs PDF in browser
571        public function pdfOut($date) {
572                // send PDF-file in browser
573
574                $this->cb->Output('Cal_'.$date.'.pdf', 'I');
575        }
576
577
578        // converts color from "ffffff" to Array('R' => 255, 'G' => 255, 'B' => 255)
579        private function convertColor($colorHex) {
580                $final = Array();
581                $final['R'] = hexdec(substr($colorHex, 0, 2));
582                $final['G'] = hexdec(substr($colorHex, 2, 2));
583                $final['B'] = hexdec(substr($colorHex, 4, 2));
584                return $final;
585        }
586       
587       
588        // convert event color ot RGB-format
589        private function processEventColor($color) {
590                if ($color == 'transparent') {
591                        return $color;
592                }
593
594                if (preg_match("/#[0-9A-Fa-f]{6}/", $color)) {
595                        return substr($color, 1);
596                }
597                $result = preg_match_all("/rgb\s?\((\d{1,3})\s?,\s?(\d{1,3})\s?,\s?(\d{1,3})\)/", $color, $rgb);
598               
599                if ($result) {
600                        $color = '';
601                        for ($i = 1; $i <= 3; $i++) {
602                                $comp = dechex($rgb[$i][0]);
603                                if (strlen($comp) == 1) {
604                                        $comp = '0'.$comp;
605                                        }
606                                $color .= $comp;
607                        }
608                        return $color;
609                } else {
610                        return 'transparent';
611                }
612        }
613
614}
615
616?>
Note: See TracBrowser for help on using the repository browser.