More improvements to access checks.
[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 // Access check.
39 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
40   header('Location: access_denied.php');
41   exit();
42 }
43
44 // Check whether TCPDF library is available.
45 if (!file_exists('WEB-INF/lib/tcpdf/'))
46   die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
47
48 // Include TCPDF library.
49 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
50
51 // Use custom fields plugin if it is enabled.
52 if ($user->isPluginEnabled('cf')) {
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->get($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 // Build a string to use as filename for the files being downloaded.
91 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
92
93 // Start preparing HTML to build PDF from.
94 $styleHeader = 'style="background-color:#a6ccf7;"';
95 $styleSubtotal = 'style="background-color:#e0e0e0;"';
96 $styleCentered = 'style="text-align:center;"';
97 $styleRightAligned = 'style="text-align:right;"';
98
99 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
100 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
101 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
102
103 if ($totals_only) {
104   // We are building a "totals only" report with only subtotals and total.
105   $colspan = 1; // Column span for an empty row.
106   // Table header.
107   $html .= '<thead>';
108   $html .= "<tr $styleHeader>";
109   $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
110   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
111   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
112   $html .= '</tr>';
113   $html .= '</thead>';
114   // Print subtotals.
115   foreach ($subtotals as $subtotal) {
116     $html .= '<tr>';
117     $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
118     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
119     if ($bean->getAttribute('chcost')) {
120       $html .= "<td $styleRightAligned>";
121       if ($user->canManageTeam() || $user->isClient())
122         $html .= $subtotal['cost'];
123       else
124         $html .= $subtotal['expenses'];
125       $html .= '</td>'; 
126     }
127     $html .= '</tr>';
128   }
129   // Print totals.
130   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
131   $html .= "<tr $styleSubtotal>";
132   $html .= '<td>'.$i18n->get('label.total').'</td>';
133   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
134   if ($bean->getAttribute('chcost')) {
135       $html .= "<td $styleRightAligned>";
136       $html .= htmlspecialchars($user->currency).' ';
137       if ($user->canManageTeam() || $user->isClient())
138         $html .= $totals['cost'];
139       else
140         $html .= $totals['expenses'];
141       $html .= '</td>';
142     }
143   $html .= '</tr>';
144   $html .= '</table>';
145 } else {
146   // We are building a normal report with items, optionally grouped with subtotals, and total.
147   $colspan = 1; // Column span for an empty row.
148   // Table header.
149   $html .= '<thead>';
150   $html .= "<tr $styleHeader>";
151   $html .= '<td>'.$i18n->get('label.date').'</td>';
152   if ($user->canManageTeam() || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->get('label.user').'</td>'; }
153   if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->get('label.client').'</td>'; }
154   if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->get('label.project').'</td>'; }
155   if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->get('label.task').'</td>'; }
156   if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
157   if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.start').'</td>'; }
158   if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.finish').'</td>'; }
159   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
160   if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->get('label.note').'</td>'; }
161   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
162   if ($bean->getAttribute('chpaid')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.paid').'</td>'; }
163   if ($bean->getAttribute('chip')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.ip').'</td>'; }
164   if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->get('label.invoice').'</td>'; }
165   $html .= '</tr>';
166   $html .= '</thead>';
167
168   foreach ($items as $item) {
169     // Print a subtotal for a block of grouped values.
170     $cur_date = $item['date'];
171     if ($print_subtotals) {
172       $cur_grouped_by = $item['grouped_by'];
173       if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
174         $html .= '<tr style="background-color:#e0e0e0;">';
175         $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
176         if ($user->canManageTeam() || $user->isClient()) {
177             $html .= '<td>';
178             if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
179             $html .= '</td>';
180         }
181         if ($bean->getAttribute('chclient')) {
182             $html .= '<td>';
183             if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
184             $html .= '</td>';
185         }
186         if ($bean->getAttribute('chproject')) {
187             $html .= '<td>';
188             if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
189             $html .= '</td>';
190         }
191         if ($bean->getAttribute('chtask')) {
192             $html .= '<td>';
193             if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
194             $html .= '</td>';
195         }
196         if ($bean->getAttribute('chcf_1')) {
197             $html .= '<td>';
198             if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
199             $html .= '</td>';
200         }
201         if ($bean->getAttribute('chstart')) $html .= '<td></td>';
202         if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
203         if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
204         if ($bean->getAttribute('chnote')) $html .= '<td></td>';
205         if ($bean->getAttribute('chcost')) {
206           $html .= "<td $styleRightAligned>";
207           if ($user->canManageTeam() || $user->isClient())
208             $html .= $subtotals[$prev_grouped_by]['cost'];
209           else
210             $html .= $subtotals[$prev_grouped_by]['expenses'];
211           $html .= '</td>';
212         }
213         if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
214         if ($bean->getAttribute('chip')) $html .= '<td></td>';
215         if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
216         $html .= '</tr>';
217         $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
218       }
219       $first_pass = false; 
220     }
221
222     // Print a regular row.
223     $html .= '<tr>';
224     $html .= '<td>'.$item['date'].'</td>';
225     if ($user->canManageTeam() || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
226     if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
227     if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
228     if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
229     if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
230     if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
231     if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
232     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
233     if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
234     if ($bean->getAttribute('chcost')) {
235       $html .= "<td $styleRightAligned>";
236       if ($user->canManageTeam() || $user->isClient())
237         $html .= $item['cost'];
238       else
239         $html .= $item['expense'];
240       $html .= '</td>';
241     }
242     if ($bean->getAttribute('chpaid')) {
243         $html .= '<td>';
244         $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
245         $html .= '</td>';
246     }
247     if ($bean->getAttribute('chip')) {
248         $html .= '<td>';
249         $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
250         $html .= '</td>';
251     }
252     if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
253     $html .= '</tr>';
254
255     $prev_date = $item['date'];
256     if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
257   }
258
259   // Print a terminating subtotal.
260   if ($print_subtotals) {
261     $html .= '<tr style="background-color:#e0e0e0;">';
262     $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
263     if ($user->canManageTeam() || $user->isClient()) {
264       $html .= '<td>';
265       if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
266       $html .= '</td>';
267     }
268     if ($bean->getAttribute('chclient')) {
269       $html .= '<td>';
270       if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
271       $html .= '</td>';
272     }
273     if ($bean->getAttribute('chproject')) {
274       $html .= '<td>';
275       if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
276       $html .= '</td>';
277     }
278     if ($bean->getAttribute('chtask')) {
279       $html .= '<td>';
280       if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
281       $html .= '</td>';
282     }
283     if ($bean->getAttribute('chcf_1')) {
284       $html .= '<td>';
285       if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
286       $html .= '</td>';
287     }
288     if ($bean->getAttribute('chstart')) $html .= '<td></td>';
289     if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
290     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
291     if ($bean->getAttribute('chnote')) $html .= '<td></td>';
292     if ($bean->getAttribute('chcost')) {
293       $html .= "<td $styleRightAligned>";
294       if ($user->canManageTeam() || $user->isClient())
295         $html .= $subtotals[$prev_grouped_by]['cost'];
296       else
297         $html .= $subtotals[$prev_grouped_by]['expenses'];
298       $html .= '</td>';
299     }
300     if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
301     if ($bean->getAttribute('chip')) $html .= '<td></td>';
302     if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
303     $html .= '</tr>';
304   }
305
306   // Print totals.
307   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
308   $html .= '<tr style="background-color:#e0e0e0;">';
309   $html .= '<td>'.$i18n->get('label.total').'</td>';
310   if ($user->canManageTeam() || $user->isClient()) $html .= '<td></td>';
311   if ($bean->getAttribute('chclient')) $html .= '<td></td>';
312   if ($bean->getAttribute('chproject')) $html .= '<td></td>';
313   if ($bean->getAttribute('chtask')) $html .= '<td></td>';
314   if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
315   if ($bean->getAttribute('chstart')) $html .= '<td></td>';
316   if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
317   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
318   if ($bean->getAttribute('chnote')) $html .= '<td></td>';
319   if ($bean->getAttribute('chcost')) {
320     $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
321     if ($user->canManageTeam() || $user->isClient())
322       $html .= $totals['cost'];
323     else
324       $html .= $totals['expenses'];
325     $html .= '</td>';
326   }
327   if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
328   if ($bean->getAttribute('chip')) $html .= '<td></td>';
329   if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
330   $html .= '</tr>';
331   $html .= '</table>';
332 }
333
334 // Output footer.
335 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
336   $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
337
338 // By this time we have html ready.
339
340 // Determine title for report.
341 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
342
343 header('Pragma: public'); // This is needed for IE8 to download files over https.
344 header('Content-Type: text/html; charset=utf-8');
345 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
346 header('Cache-Control: no-store, no-cache, must-revalidate');
347 header('Cache-Control: post-check=0, pre-check=0', false);
348 header('Cache-Control: private', false);
349
350 header('Content-Type: application/pdf');
351 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
352
353
354 // Beginning of TCPDF code here.
355
356 // Extend TCPDF class so that we can use custom header and footer.
357 class ttPDF extends TCPDF {
358
359   public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
360   public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
361
362   // SetImageFile - sets image file name.
363   public function SetImageFile($imgFile) {
364     $this->image_file = $imgFile;
365   }
366
367   // SetPageWord - sets page word for footer.
368   public function SetPageWord($pageWord) {
369     $this->page_word = $pageWord;
370   }
371
372   // Page header.
373   public function Header() {
374     // Print logo, which is the only element of our custom header.
375     $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
376   }
377
378   // Page footer.
379   public function Footer() {
380     // Position at 15 mm from bottom.
381     $this->SetY(-15);
382     // Set font.
383     $this->SetFont('freeserif', 'I', 8);
384     // Print localized page number.
385     $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
386   }
387 }
388
389 // Create new PDF document.
390 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
391
392 // If custom logo file exists - set it.
393 if (file_exists('images/'.$user->team_id.'.png'))
394   $pdf->SetImageFile('images/'.$user->team_id.'.png');
395
396 // Set page word for the footer.
397 $pdf->SetPageWord($i18n->get('label.page'));
398
399 // Set document information.
400 $pdf->SetCreator(PDF_CREATOR);
401 $pdf->SetAuthor('Anuko Time Tracker');
402 $pdf->SetTitle('Anuko Time Tracker Report');
403 $pdf->SetSubject('Anuko Time Tracker Report');
404 $pdf->SetKeywords('Anuko, time, tracker, report');
405
406 // Set margins.
407 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
408 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
409 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
410
411 // Set auto page breaks.
412 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
413
414 // Set image scale factor.
415 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
416
417 // Add a page.
418 $pdf->AddPage();
419
420 // Set font (freeserif seems to work for all languages).
421 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
422
423 // Write HTML.
424 $pdf->writeHTML($html, true, false, false, false, '');
425
426 // Close and output PDF document.
427 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
428 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
429
430 // End of of TCPDF code.