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