Fixed report exports to include the IP field.
[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 (!ttAccessAllowed('view_own_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 // Build a string to use as filename for the files being downloaded.
66 $filename = strtolower($i18n->getKey('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
67
68 header('Pragma: public'); // This is needed for IE8 to download files over https.
69 header('Content-Type: text/html; charset=utf-8');
70 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
71 header('Cache-Control: no-store, no-cache, must-revalidate');
72 header('Cache-Control: post-check=0, pre-check=0', false);
73 header('Cache-Control: private', false);
74
75 // Handle 2 cases of possible exports individually.
76
77 // 1) entries exported to xml
78 if ('xml' == $type) {
79   header('Content-Type: application/xml');
80   header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
81
82   print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
83   print "<rows>\n";
84
85   $group_by = $bean->getAttribute('group_by');
86   if ($totals_only) {
87     // Totals only report. Print subtotals.
88     foreach ($subtotals as $subtotal) {
89       print "<row>\n";
90       print "\t<".$group_by."><![CDATA[".$subtotal['name']."]]></".$group_by.">\n";
91       if ($bean->getAttribute('chduration')) {
92         $val = $subtotal['time'];
93         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
94           $val = time_to_decimal($val);
95         print "\t<duration><![CDATA[".$val."]]></duration>\n";
96       }
97       if ($bean->getAttribute('chcost')) {
98         print "\t<cost><![CDATA[";
99         if ($user->canManageTeam() || $user->isClient())
100           print $subtotal['cost'];
101         else
102           print $subtotal['expenses'];
103         print "]]></cost>\n";
104       }
105       print "</row>\n";
106     }
107   } else {
108     // Normal report.
109     foreach ($items as $item) {
110       print "<row>\n";
111
112       print "\t<date><![CDATA[".$item['date']."]]></date>\n";
113       if ($user->canManageTeam() || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n"; 
114       if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
115       if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
116       if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
117       if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
118       if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
119       if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
120       if ($bean->getAttribute('chduration')) {
121         $duration = $item['duration'];
122         if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
123           $duration = time_to_decimal($duration);
124           print "\t<duration><![CDATA[".$duration."]]></duration>\n";
125       }
126       if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
127       if ($bean->getAttribute('chcost')) {
128         print "\t<cost><![CDATA[";
129         if ($user->canManageTeam() || $user->isClient())
130           print $item['cost'];
131         else
132           print $item['expense'];
133         print "]]></cost>\n";
134       }
135       if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
136       if ($bean->getAttribute('chip')) {
137         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
138         print "\t<ip><![CDATA[".$ip."]]></ip>\n";
139       }
140       if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
141
142       print "</row>\n";
143     }
144   }
145
146   print "</rows>";
147 }
148
149 // 2) entries exported to csv
150 if ('csv' == $type) {
151   header('Content-Type: application/csv');
152   header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
153
154   // Print UTF8 BOM first to identify encoding.
155   $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
156   print $bom; // Without this Excel does not display UTF8 characters properly.
157
158   $group_by = $bean->getAttribute('group_by');
159   if ($totals_only) {
160     // Totals only report.
161
162     // Determine group_by header.
163     if ('cf_1' == $group_by)
164       $group_by_header = $custom_fields->fields[0]['label'];
165     else {
166       $key = 'label.'.$group_by;
167       $group_by_header = $i18n->getKey($key);
168     }
169
170     // Print headers.
171     print '"'.$group_by_header.'"';
172     if ($bean->getAttribute('chduration')) print ',"'.$i18n->getKey('label.duration').'"';
173     if ($bean->getAttribute('chcost')) print ',"'.$i18n->getKey('label.cost').'"';
174     print "\n";
175
176     // Print subtotals.
177     foreach ($subtotals as $subtotal) {
178       print '"'.$subtotal['name'].'"';
179       if ($bean->getAttribute('chduration')) {
180         $val = $subtotal['time'];
181         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
182           $val = time_to_decimal($val);
183         print ',"'.$val.'"';
184       }
185       if ($bean->getAttribute('chcost')) {
186         if ($user->canManageTeam() || $user->isClient())
187           print ',"'.$subtotal['cost'].'"';
188         else
189           print ',"'.$subtotal['expenses'].'"';
190       }
191       print "\n";
192     }
193   } else {
194     // Normal report. Print headers.
195     print '"'.$i18n->getKey('label.date').'"';
196     if ($user->canManageTeam() || $user->isClient()) print ',"'.$i18n->getKey('label.user').'"';
197     if ($bean->getAttribute('chclient')) print ',"'.$i18n->getKey('label.client').'"';
198     if ($bean->getAttribute('chproject')) print ',"'.$i18n->getKey('label.project').'"';
199     if ($bean->getAttribute('chtask')) print ',"'.$i18n->getKey('label.task').'"';
200     if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
201     if ($bean->getAttribute('chstart')) print ',"'.$i18n->getKey('label.start').'"';
202     if ($bean->getAttribute('chfinish')) print ',"'.$i18n->getKey('label.finish').'"';
203     if ($bean->getAttribute('chduration')) print ',"'.$i18n->getKey('label.duration').'"';
204     if ($bean->getAttribute('chnote')) print ',"'.$i18n->getKey('label.note').'"';
205     if ($bean->getAttribute('chcost')) print ',"'.$i18n->getKey('label.cost').'"';
206     if ($bean->getAttribute('chpaid')) print ',"'.$i18n->getKey('label.paid').'"';
207     if ($bean->getAttribute('chip')) print ',"'.$i18n->getKey('label.ip').'"';
208     if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->getKey('label.invoice').'"';
209     print "\n";
210
211     // Print items.
212     foreach ($items as $item) {
213       print '"'.$item['date'].'"';
214       if ($user->canManageTeam() || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
215       if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
216       if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
217       if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
218       if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
219       if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
220       if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
221       if ($bean->getAttribute('chduration')) {
222         $val = $item['duration'];
223         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
224           $val = time_to_decimal($val);
225         print ',"'.$val.'"';
226       }
227       if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
228       if ($bean->getAttribute('chcost')) {
229         if ($user->canManageTeam() || $user->isClient())
230           print ',"'.$item['cost'].'"';
231         else
232           print ',"'.$item['expense'].'"';
233       }
234       if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
235       if ($bean->getAttribute('chip')) {
236         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
237         print ',"'.$ip.'"';
238       }
239       if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';
240       print "\n";
241     }
242   }
243 }