Refactoring, finished replacing with ttUser::isPluginEnabled().
[timetracker.git] / tofile.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 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('ttReportHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_view_reports)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 // Use custom fields plugin if it is enabled.
41 if ($user->isPluginEnabled('cf')) {
42   require_once('plugins/CustomFields.class.php');
43   $custom_fields = new CustomFields($user->team_id);
44 }
45
46 // Report settings are stored in session bean before we get here.
47 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
48
49 // This file handles 2 types of export to a file:
50 // 1) xml
51 // 2) csv
52 // Export to pdf is handled separately in topdf.php.
53 $type = $request->getParameter('type');
54
55 // Also, there are 2 variations of report: totals only, or normal. Totals only means that the report
56 // is grouped by (either date, user, client, project, task or cf_1) and user only needs to see subtotals by group.
57 $totals_only = $bean->getAttribute('chtotalsonly');
58
59 // Obtain items.
60 if ($totals_only)
61   $subtotals = ttReportHelper::getSubtotals($bean);
62 else
63   $items = ttReportHelper::getItems($bean);
64
65 header('Pragma: public'); // This is needed for IE8 to download files over https.
66 header('Content-Type: text/html; charset=utf-8');
67 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
68 header('Cache-Control: no-store, no-cache, must-revalidate');
69 header('Cache-Control: post-check=0, pre-check=0', false);
70 header('Cache-Control: private', false);
71
72 // Handle 2 cases of possible exports individually.
73
74 // 1) entries exported to xml
75 if ('xml' == $type) {
76   header('Content-Type: application/xml');
77   header('Content-Disposition: attachment; filename="timesheet.xml"');
78
79   print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
80   print "<rows>\n";
81
82   $group_by = $bean->getAttribute('group_by');
83   if ($totals_only) {
84     // Totals only report. Print subtotals.
85     foreach ($subtotals as $subtotal) {
86       print "<row>\n";
87       print "\t<".$group_by."><![CDATA[".$subtotal['name']."]]></".$group_by.">\n";
88       if ($bean->getAttribute('chduration')) {
89         $val = $subtotal['time'];
90         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
91           $val = time_to_decimal($val);
92         print "\t<duration><![CDATA[".$val."]]></duration>\n";
93       }
94       if ($bean->getAttribute('chcost')) {
95         print "\t<cost><![CDATA[";
96         if ($user->canManageTeam() || $user->isClient())
97           print $subtotal['cost'];
98         else
99           print $subtotal['expenses'];
100         print "]]></cost>\n";
101       }
102       print "</row>\n";
103     }
104   } else {
105     // Normal report.
106     foreach ($items as $item) {
107       print "<row>\n";
108
109       print "\t<date><![CDATA[".$item['date']."]]></date>\n";
110       if ($user->canManageTeam() || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n"; 
111       if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
112       if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
113       if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
114       if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
115       if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
116       if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
117       if ($bean->getAttribute('chduration')) {
118         $duration = $item['duration'];
119         if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
120           $duration = time_to_decimal($duration);
121           print "\t<duration><![CDATA[".$duration."]]></duration>\n";
122       }
123       if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
124       if ($bean->getAttribute('chcost')) {
125         print "\t<cost><![CDATA[";
126         if ($user->canManageTeam() || $user->isClient())
127           print $item['cost'];
128         else
129           print $item['expense'];
130         print "]]></cost>\n";
131       }
132       if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
133
134       print "</row>\n";
135     }
136   }
137
138   print "</rows>";
139 }
140
141 // 2) entries exported to csv
142 if ('csv' == $type) {
143   header('Content-Type: application/csv');
144   header('Content-Disposition: attachment; filename="timesheet.csv"');
145
146   // Print UTF8 BOM first to identify encoding.
147   $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
148   print $bom; // Without this Excel does not display UTF8 characters properly.
149
150   $group_by = $bean->getAttribute('group_by');
151   if ($totals_only) {
152     // Totals only report.
153
154     // Determine group_by header.
155     if ('cf_1' == $group_by)
156       $group_by_header = $custom_fields->fields[0]['label'];
157     else {
158       $key = 'label.'.$group_by;
159       $group_by_header = $i18n->getKey($key);
160     }
161
162     // Print headers.
163     print '"'.$group_by_header.'"';
164     if ($bean->getAttribute('chduration')) print ',"'.$i18n->getKey('label.duration').'"';
165     if ($bean->getAttribute('chcost')) print ',"'.$i18n->getKey('label.cost').'"';
166     print "\n";
167
168     // Print subtotals.
169     foreach ($subtotals as $subtotal) {
170       print '"'.$subtotal['name'].'"';
171       if ($bean->getAttribute('chduration')) {
172         $val = $subtotal['time'];
173         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
174           $val = time_to_decimal($val);
175         print ',"'.$val.'"';
176       }
177       if ($bean->getAttribute('chcost')) {
178         if ($user->canManageTeam() || $user->isClient())
179           print ',"'.$subtotal['cost'].'"';
180         else
181           print ',"'.$subtotal['expenses'].'"';
182       }
183       print "\n";
184     }
185   } else {
186     // Normal report. Print headers.
187     print '"'.$i18n->getKey('label.date').'"';
188     if ($user->canManageTeam() || $user->isClient()) print ',"'.$i18n->getKey('label.user').'"';
189     if ($bean->getAttribute('chclient')) print ',"'.$i18n->getKey('label.client').'"';
190     if ($bean->getAttribute('chproject')) print ',"'.$i18n->getKey('label.project').'"';
191     if ($bean->getAttribute('chtask')) print ',"'.$i18n->getKey('label.task').'"';
192     if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
193     if ($bean->getAttribute('chstart')) print ',"'.$i18n->getKey('label.start').'"';
194     if ($bean->getAttribute('chfinish')) print ',"'.$i18n->getKey('label.finish').'"';
195     if ($bean->getAttribute('chduration')) print ',"'.$i18n->getKey('label.duration').'"';
196     if ($bean->getAttribute('chnote')) print ',"'.$i18n->getKey('label.note').'"';
197     if ($bean->getAttribute('chcost')) print ',"'.$i18n->getKey('label.cost').'"';
198     if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->getKey('label.invoice').'"';
199     print "\n";
200
201     // Print items.
202     foreach ($items as $item) {
203       print '"'.$item['date'].'"';
204       if ($user->canManageTeam() || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
205       if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
206       if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
207       if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
208       if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
209       if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
210       if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
211       if ($bean->getAttribute('chduration')) {
212         $val = $item['duration'];
213         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
214           $val = time_to_decimal($val);
215         print ',"'.$val.'"';
216       }
217       if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
218       if ($bean->getAttribute('chcost')) {
219         if ($user->canManageTeam() || $user->isClient())
220           print ',"'.$item['cost'].'"';
221         else
222           print ',"'.$item['expense'].'"';
223       }
224       if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';
225       print "\n";
226     }
227   }
228 }