Removed PHP closing tag in more files
[timetracker.git] / topdf.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 /*
30  * This file generates a report in PDF format using TCPDF library from http://www.tcpdf.org/.
31  * If installed, it is expected to be in WEB-INF/lib/tcpdf/ folder.
32  */
33 require_once('initialize.php');
34 import('form.Form');
35 import('form.ActionForm');
36 import('ttReportHelper');
37
38 // Check whether TCPDF library is available.
39 if (!file_exists('WEB-INF/lib/tcpdf/'))
40   die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
41
42 // Include TCPDF library.
43 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
44
45 // Access check.
46 if (!ttAccessCheck(right_view_reports)) {
47   header('Location: access_denied.php');
48   exit();
49 }
50
51 // Use custom fields plugin if it is enabled.
52 if (in_array('cf', explode(',', $user->plugins))) {
53   require_once('plugins/CustomFields.class.php');
54   $custom_fields = new CustomFields($user->team_id);
55 }
56
57 // Report settings are stored in session bean before we get here.
58 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
59
60 // There are 2 variations of report: totals only, or normal. Totals only means that the report
61 // is grouped by either date, user, client, project, task or cf_1 and user only needs to see subtotals by group.
62 $totals_only = ($bean->getAttribute('chtotalsonly') == '1');
63
64 // Determine group by header.
65 $group_by = $bean->getAttribute('group_by');
66 if ('no_grouping' != $group_by) {
67   if ('cf_1' == $group_by)
68     $group_by_header = $custom_fields->fields[0]['label'];
69   else {
70     $key = 'label.'.$group_by;
71     $group_by_header = $i18n->getKey($key);
72   }
73 }
74
75 // Obtain items for report.
76 if (!$totals_only)
77   $items = ttReportHelper::getItems($bean); // Individual entries.
78 if ($totals_only || 'no_grouping' != $group_by)
79   $subtotals = ttReportHelper::getSubtotals($bean); // Subtotals for groups of items.
80 $totals = ttReportHelper::getTotals($bean); // Totals for the entire report.
81
82 // Assign variables that are used to print subtotals.
83 if ($items && 'no_grouping' != $group_by) {
84   $print_subtotals = true;
85   $first_pass = true;
86   $prev_grouped_by = '';
87   $cur_grouped_by = '';
88 }
89
90 // Start preparing HTML to build PDF from.
91 $styleHeader = 'style="background-color:#a6ccf7;"';
92 $styleSubtotal = 'style="background-color:#e0e0e0;"';
93 $styleCentered = 'style="text-align:center;"';
94 $styleRightAligned = 'style="text-align:right;"';
95
96 $title = $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
97 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
98 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
99
100 if ($totals_only) {
101   // We are building a "totals only" report with only subtotals and total.
102   $colspan = 1; // Column span for an empty row.
103   // Table header.
104   $html .= '<thead>';
105   $html .= "<tr $styleHeader>";
106   $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
107   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.duration').'</td>'; }
108   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.cost').'</td>'; }
109   $html .= '</tr>';
110   $html .= '</thead>';
111   // Print subtotals.
112   foreach ($subtotals as $subtotal) {
113     $html .= '<tr>';
114     $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
115     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
116     if ($bean->getAttribute('chcost')) {
117       $html .= "<td $styleRightAligned>";
118       if ($user->canManageTeam() || $user->isClient())
119         $html .= $subtotal['cost'];
120       else
121         $html .= $subtotal['expenses'];
122       $html .= '</td>'; 
123     }
124     $html .= '</tr>';
125   }
126   // Print totals.
127   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
128   $html .= "<tr $styleSubtotal>";
129   $html .= '<td>'.$i18n->getKey('label.total').'</td>';
130   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
131   if ($bean->getAttribute('chcost')) {
132       $html .= "<td $styleRightAligned>";
133       $html .= htmlspecialchars($user->currency).' ';
134       if ($user->canManageTeam() || $user->isClient())
135         $html .= $totals['cost'];
136       else
137         $html .= $totals['expenses'];
138       $html .= '</td>';
139     }
140   $html .= '</tr>';
141   $html .= '</table>';
142 } else {
143   // We are building a normal report with items, optionally grouped with subtotals, and total.
144   $colspan = 1; // Column span for an empty row.
145   // Table header.
146   $html .= '<thead>';
147   $html .= "<tr $styleHeader>";
148   $html .= '<td>'.$i18n->getKey('label.date').'</td>';
149   if ($user->canManageTeam() || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->getKey('label.user').'</td>'; }
150   if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.client').'</td>'; }
151   if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.project').'</td>'; }
152   if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.task').'</td>'; }
153   if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
154   if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.start').'</td>'; }
155   if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.finish').'</td>'; }
156   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.duration').'</td>'; }
157   if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.note').'</td>'; }
158   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.cost').'</td>'; }
159   if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.invoice').'</td>'; }
160   $html .= '</tr>';
161   $html .= '</thead>';
162   
163   foreach ($items as $item) {
164     // Print a subtotal for a block of grouped values.
165     $cur_date = $item['date'];
166     if ($print_subtotals) {
167       $cur_grouped_by = $item['grouped_by'];
168       if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
169         $html .= '<tr style="background-color:#e0e0e0;">';
170         $html .= '<td>'.$i18n->getKey('label.subtotal').'</td>';
171         if ($user->canManageTeam() || $user->isClient()) {
172             $html .= '<td>';
173             if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
174             $html .= '</td>';
175         }
176         if ($bean->getAttribute('chclient')) {
177             $html .= '<td>';
178             if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
179             $html .= '</td>';
180         }
181         if ($bean->getAttribute('chproject')) {
182             $html .= '<td>';
183             if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
184             $html .= '</td>';
185         }
186         if ($bean->getAttribute('chtask')) {
187             $html .= '<td>';
188             if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
189             $html .= '</td>';
190         }
191         if ($bean->getAttribute('chcf_1')) {
192             $html .= '<td>';
193             if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
194             $html .= '</td>';
195         }
196         if ($bean->getAttribute('chstart')) $html .= '<td></td>';
197         if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
198         if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
199         if ($bean->getAttribute('chnote')) $html .= '<td></td>';
200         if ($bean->getAttribute('chcost')) {
201           $html .= "<td $styleRightAligned>";
202           if ($user->canManageTeam() || $user->isClient())
203             $html .= $subtotals[$prev_grouped_by]['cost'];
204           else
205             $html .= $subtotals[$prev_grouped_by]['expenses'];
206           $html .= '</td>';
207         }
208         if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
209         $html .= '</tr>';
210         $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
211       }
212       $first_pass = false; 
213     }
214
215     // Print a regular row.
216     $html .= '<tr>';
217     $html .= '<td>'.$item['date'].'</td>';
218     if ($user->canManageTeam() || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
219     if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
220     if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
221     if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
222     if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
223     if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
224     if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
225     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
226     if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
227     if ($bean->getAttribute('chcost')) {
228       $html .= "<td $styleRightAligned>";
229       if ($user->canManageTeam() || $user->isClient())
230         $html .= $item['cost'];
231       else
232         $html .= $item['expense'];
233       $html .= '</td>';
234     }
235     if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
236     $html .= '</tr>';
237     
238     $prev_date = $item['date'];
239     if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
240   }
241   
242   // Print a terminating subtotal.
243   if ($print_subtotals) {
244     $html .= '<tr style="background-color:#e0e0e0;">';
245     $html .= '<td>'.$i18n->getKey('label.subtotal').'</td>';
246     if ($user->canManageTeam() || $user->isClient()) {
247       $html .= '<td>';
248       if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
249       $html .= '</td>';
250     }
251     if ($bean->getAttribute('chclient')) {
252       $html .= '<td>';
253       if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
254       $html .= '</td>';
255     }
256     if ($bean->getAttribute('chproject')) {
257       $html .= '<td>';
258       if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
259       $html .= '</td>';
260     }
261     if ($bean->getAttribute('chtask')) {
262       $html .= '<td>';
263       if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
264       $html .= '</td>';
265     }
266     if ($bean->getAttribute('chcf_1')) {
267       $html .= '<td>';
268       if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
269       $html .= '</td>';
270     }
271     if ($bean->getAttribute('chstart')) $html .= '<td></td>';
272     if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
273     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
274     if ($bean->getAttribute('chnote')) $html .= '<td></td>';
275     if ($bean->getAttribute('chcost')) {
276       $html .= "<td $styleRightAligned>";
277       if ($user->canManageTeam() || $user->isClient())
278         $html .= $subtotals[$prev_grouped_by]['cost'];
279       else
280         $html .= $subtotals[$prev_grouped_by]['expenses'];
281       $html .= '</td>';
282     }
283     if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
284     $html .= '</tr>';
285   }
286   
287   // Print totals.
288   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
289   $html .= '<tr style="background-color:#e0e0e0;">';
290   $html .= '<td>'.$i18n->getKey('label.total').'</td>';
291   if ($user->canManageTeam() || $user->isClient()) $html .= '<td></td>';
292   if ($bean->getAttribute('chclient')) $html .= '<td></td>';
293   if ($bean->getAttribute('chproject')) $html .= '<td></td>';
294   if ($bean->getAttribute('chtask')) $html .= '<td></td>';
295   if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
296   if ($bean->getAttribute('chstart')) $html .= '<td></td>';
297   if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
298   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
299   if ($bean->getAttribute('chnote')) $html .= '<td></td>';
300   if ($bean->getAttribute('chcost')) {
301     $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
302     if ($user->canManageTeam() || $user->isClient())
303       $html .= $totals['cost'];
304     else
305       $html .= $totals['expenses'];  
306     $html .= '</td>';
307   }
308   if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
309   $html .= '</tr>';
310   $html .= '</table>';
311 }
312
313 // Output footer.
314 $html .= '<p style="text-align: center;">'.$i18n->getKey('form.mail.footer').'</p>';
315 // By this time we have html ready.
316
317 // Determine title for report.
318 $title = $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
319
320 header('Pragma: public'); // This is needed for IE8 to download files over https.
321 header('Content-Type: text/html; charset=utf-8');
322 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
323 header('Cache-Control: no-store, no-cache, must-revalidate');
324 header('Cache-Control: post-check=0, pre-check=0', false);
325 header('Cache-Control: private', false);
326
327 header('Content-Type: application/pdf');
328 header('Content-Disposition: attachment; filename="timesheet.pdf"');
329
330
331 // Beginning of TCPDF code here.
332
333 // Extend TCPDF class so that we can use custom header and footer.
334 class MyyPDF extends TCPDF {
335
336   public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
337   public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
338   
339   // SetImageFile - sets image file name.
340   public function SetImageFile($imgFile) {
341     $this->image_file = $imgFile;
342   }
343   
344   // SetPageWord - sets page word for footer.
345   public function SetPageWord($pageWord) {
346     $this->page_word = $pageWord;
347   }
348   
349   // Page header.
350   public function Header() {
351     // Print logo, which is the only element of our custom header.
352     $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
353   }
354
355   // Page footer.
356   public function Footer() {
357     // Position at 15 mm from bottom.
358     $this->SetY(-15);
359     // Set font.
360     $this->SetFont('freeserif', 'I', 8);
361     // Print localized page number.
362     $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
363   }
364 }
365
366 // Create new PDF document.
367 $pdf = new MyyPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
368
369 // If custom logo file exists - set it.
370 if (file_exists('images/'.$user->team_id.'.png'))
371   $pdf->SetImageFile('images/'.$user->team_id.'.png');
372
373 // Set page word for the footer.
374 $pdf->SetPageWord($i18n->getKey('label.page'));
375
376 // Set document information.
377 $pdf->SetCreator(PDF_CREATOR);
378 $pdf->SetAuthor('Anuko Time Tracker');
379 $pdf->SetTitle('Anuko Time Tracker Report');
380 $pdf->SetSubject('Anuko Time Tracker Report');
381 $pdf->SetKeywords('Anuko, time, tracker, report');
382
383 // Set margins.
384 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
385 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
386 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
387
388 // Set auto page breaks.
389 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
390
391 // Set image scale factor.
392 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
393
394 // Add a page.
395 $pdf->AddPage();
396
397 // Set font (freeserif seems to work for all languages).
398 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
399
400 // Write HTML.
401 $pdf->writeHTML($html, true, false, false, false, '');
402
403 // Close and output PDF document.
404 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
405 $pdf->Output('timesheet.pdf', 'D'); // D is for downloads.
406
407 // End of of TCPDF code.