Fixed fav report export by adding missing fields.
[timetracker.git] / WEB-INF / lib / ttGroupExportHelper.class.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 // ttGroupExportHelper - this class is used to write data for a single group
30 // to a file. When group contains other groups, it reuses itself recursively.
31 class ttGroupExportHelper {
32
33   var $group_id = null;     // Group we are exporting.
34   var $file     = null;     // File to write to.
35   var $indentation = null;  // A string consisting of a number of spaces.
36   var $subgroups = array(); // Immediate subgroups.
37
38   // The following arrays are maps between entity ids in the file versus the database.
39   // We write to the file sequentially (1,2,3...) while in the database the entities have different ids.
40   var $userMap    = array();
41   var $roleMap    = array();
42   var $taskMap    = array();
43   var $projectMap = array();
44   var $clientMap  = array();
45   var $timesheetMap = array();
46   var $invoiceMap = array();
47   var $logMap     = array();
48   var $customFieldMap = array();
49   var $customFieldOptionMap = array();
50   var $favReportMap = array();
51
52   // Constructor.
53   function __construct($group_id, $file, $indentation) {
54     global $user;
55
56     $this->group_id = $group_id;
57     $this->file = $file;
58     $this->indentation = $indentation;
59
60     // Build a list of subgroups.
61     $mdb2 = getConnection();
62     $sql =  "select id from tt_groups".
63             " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
64     $res = $mdb2->query($sql);
65     if (!is_a($res, 'PEAR_Error')) {
66       while ($val = $res->fetchRow()) {
67         $this->subgroups[] = $val;
68       }
69     }
70   }
71
72   // getGroupAttrs obtains group attributes for export.
73   private function getGroupAttrs() {
74     global $user;
75     $mdb2 = getConnection();
76
77     $sql =  "select * from tt_groups".
78             " where status = 1 and id = $this->group_id and org_id = $user->org_id";
79     $res = $mdb2->query($sql);
80     if (!is_a($res, 'PEAR_Error')) {
81       $val = $res->fetchRow();
82     }
83     return $val;
84   }
85
86   // The getUsers obtains all users in group for the purpose of export.
87   private function getUsers() {
88     global $user;
89     $mdb2 = getConnection();
90     $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
91       " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
92     $res = $mdb2->query($sql);
93     $result = array();
94     if (!is_a($res, 'PEAR_Error')) {
95       while ($val = $res->fetchRow()) {
96         $result[] = $val;
97       }
98       return $result;
99     }
100     return false;
101   }
102
103   // getRecordsFromTable - obtains all fields from a given table for a group.
104   function getRecordsFromTable($table_name) {
105     global $user;
106     $mdb2 = getConnection();
107
108     $result = array();
109     $sql = "select * from $table_name where group_id = $this->group_id and org_id = $user->org_id";
110     $res = $mdb2->query($sql);
111     $result = array();
112     if (!is_a($res, 'PEAR_Error')) {
113       while ($val = $res->fetchRow()) {
114         $result[] = $val;
115       }
116       return $result;
117     }
118     return false;
119   }
120
121   // writeData writes group data into file.
122   function writeData() {
123
124     // Write group info.
125     $group = $this->getGroupAttrs();
126     $group_part = "<group name=\"".htmlspecialchars($group['name'])."\"";
127     $group_part .= " description=\"".htmlspecialchars($group['description'])."\"";
128     $group_part .= " currency=\"".htmlspecialchars($group['currency'])."\"";
129     $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
130     $group_part .= " lang=\"".$group['lang']."\"";
131     $group_part .= " date_format=\"".$group['date_format']."\"";
132     $group_part .= " time_format=\"".$group['time_format']."\"";
133     $group_part .= " week_start=\"".$group['week_start']."\"";
134     $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
135     $group_part .= " project_required=\"".$group['project_required']."\"";
136     $group_part .= " task_required=\"".$group['task_required']."\"";
137     $group_part .= " record_type=\"".$group['record_type']."\"";
138     $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
139     $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
140     $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
141     $group_part .= " plugins=\"".$group['plugins']."\"";
142     $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
143     $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
144     $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
145     $group_part .= " config=\"".$group['config']."\"";
146     $group_part .= ">\n";
147
148     // Write group info.
149     fwrite($this->file, $this->indentation.$group_part);
150     unset($group);
151     unset($group_part);
152
153     // Prepare user map.
154     $users = $this->getUsers();
155     foreach ($users as $key=>$user_item)
156       $this->userMap[$user_item['id']] = $key + 1;
157
158     // Prepare role map.
159     $roles = $this->getRecordsFromTable('tt_roles');
160     foreach ($roles as $key=>$role_item)
161       $this->roleMap[$role_item['id']] = $key + 1;
162
163     // Prepare task map.
164     $tasks = $this->getRecordsFromTable('tt_tasks');
165     foreach ($tasks as $key=>$task_item)
166       $this->taskMap[$task_item['id']] = $key + 1;
167
168     // Prepare project map.
169     $projects = $this->getRecordsFromTable('tt_projects');
170     foreach ($projects as $key=>$project_item)
171       $this->projectMap[$project_item['id']] = $key + 1;
172
173     // Prepare client map.
174     $clients = $this->getRecordsFromTable('tt_clients');
175     foreach ($clients as $key=>$client_item)
176       $this->clientMap[$client_item['id']] = $key + 1;
177
178     // Prepare timesheet map.
179     $timesheets = $this->getRecordsFromTable('tt_timesheets');
180     foreach ($timesheets as $key=>$timesheet_item)
181       $this->timesheetMap[$timesheet_item['id']] = $key + 1;
182
183     // Prepare invoice map.
184     $invoices = ttTeamHelper::getAllInvoices();
185     foreach ($invoices as $key=>$invoice_item)
186       $this->invoiceMap[$invoice_item['id']] = $key + 1;
187
188     // Prepare custom fields map.
189     $custom_fields = ttTeamHelper::getAllCustomFields($this->group_id);
190     foreach ($custom_fields as $key=>$custom_field)
191       $this->customFieldMap[$custom_field['id']] = $key + 1;
192
193     // Prepare custom field options map.
194     $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($this->group_id);
195     foreach ($custom_field_options as $key=>$option)
196       $this->customFieldOptionMap[$option['id']] = $key + 1;
197
198     // Prepare favorite report map.
199     $fav_reports = $this->getRecordsFromTable('tt_fav_reports');
200     foreach ($fav_reports as $key=>$fav_report)
201       $this->favReportMap[$fav_report['id']] = $key + 1;
202
203     // Write roles.
204     fwrite($this->file, $this->indentation."  <roles>\n");
205     foreach ($roles as $role) {
206       $role_part = $this->indentation.'    '."<role id=\"".$this->roleMap[$role['id']]."\"";
207       $role_part .= " name=\"".htmlspecialchars($role['name'])."\"";
208       $role_part .= " description=\"".htmlspecialchars($role['description'])."\"";
209       $role_part .= " rank=\"".$role['rank']."\"";
210       $role_part .= " rights=\"".htmlspecialchars($role['rights'])."\"";
211       $role_part .= " status=\"".$role['status']."\"";
212       $role_part .= "></role>\n";
213       fwrite($this->file, $role_part);
214     }
215     fwrite($this->file, $this->indentation."  </roles>\n");
216     unset($roles);
217     unset($role_part);
218
219     // Write tasks.
220     if (count($tasks) > 0) {
221       fwrite($this->file, $this->indentation."  <tasks>\n");
222       foreach ($tasks as $task) {
223         $task_part = $this->indentation.'    '."<task id=\"".$this->taskMap[$task['id']]."\"";
224         $task_part .= " name=\"".htmlspecialchars($task['name'])."\"";
225         $task_part .= " description=\"".htmlspecialchars($task['description'])."\"";
226         $task_part .= " status=\"".$task['status']."\"";
227         $task_part .= "></task>\n";
228         fwrite($this->file, $task_part);
229       }
230       fwrite($this->file, $this->indentation."  </tasks>\n");
231       unset($tasks);
232       unset($task_part);
233     }
234
235     // Write projects.
236     if (count($projects) > 0) {
237       fwrite($this->file, $this->indentation."  <projects>\n");
238       foreach ($projects as $project_item) {
239         $tasks_str = null;
240         if($project_item['tasks']){
241           $tasks = explode(',', $project_item['tasks']);
242           $tasks_mapped = array();
243           foreach ($tasks as $item)
244             $tasks_mapped[] = $this->taskMap[$item];
245           $tasks_str = implode(',', $tasks_mapped);
246         }
247         $project_part = $this->indentation.'    '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
248         $project_part .= " name=\"".htmlspecialchars($project_item['name'])."\"";
249         $project_part .= " description=\"".htmlspecialchars($project_item['description'])."\"";
250         $project_part .= " tasks=\"".$tasks_str."\"";
251         $project_part .= " status=\"".$project_item['status']."\"";
252         $project_part .= "></project>\n";
253         fwrite($this->file, $project_part);
254       }
255       fwrite($this->file, $this->indentation."  </projects>\n");
256       unset($projects);
257       unset($project_part);
258     }
259
260     // Write clients.
261     if (count($clients) > 0) {
262       fwrite($this->file, $this->indentation."  <clients>\n");
263       foreach ($clients as $client_item) {
264         if($client_item['projects']){
265           $projects_db = explode(',', $client_item['projects']);
266           $projects_mapped = array();
267           foreach ($projects_db as $item)
268             $projects_mapped[] = $this->projectMap[$item];
269           $projects_str = implode(',', $projects_mapped);
270         }
271         $client_part = $this->indentation.'    '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
272         $client_part .= " name=\"".htmlspecialchars($client_item['name'])."\"";
273         $client_part .= " address=\"".htmlspecialchars($client_item['address'])."\"";
274         $client_part .= " tax=\"".$client_item['tax']."\"";
275         $client_part .= " projects=\"".$projects_str."\"";
276         $client_part .= " status=\"".$client_item['status']."\"";
277         $client_part .= "></client>\n";
278         fwrite($this->file, $client_part);
279       }
280       fwrite($this->file, $this->indentation."  </clients>\n");
281       unset($clients);
282       unset($client_part);
283     }
284
285     // Write users.
286     if (count($users) > 0) {
287       fwrite($this->file, $this->indentation."  <users>\n");
288       foreach ($users as $user_item) {
289         $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
290         $user_part = $this->indentation.'    '."<user id=\"".$this->userMap[$user_item['id']]."\"";
291         $user_part .= " name=\"".htmlspecialchars($user_item['name'])."\"";
292         $user_part .= " login=\"".htmlspecialchars($user_item['login'])."\"";
293         $user_part .= " password=\"".$user_item['password']."\"";
294         $user_part .= " role_id=\"".$role_id."\"";
295         $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
296         $user_part .= " rate=\"".$user_item['rate']."\"";
297         $user_part .= " quota_percent=\"".$user_item['quota_percent']."\"";
298         $user_part .= " email=\"".$user_item['email']."\"";
299         $user_part .= " status=\"".$user_item['status']."\"";
300         $user_part .= "></user>\n";
301         fwrite($this->file, $user_part);
302       }
303       fwrite($this->file, $this->indentation."  </users>\n");
304       unset($users);
305       unset($user_part);
306     }
307
308     // Write user to project binds.
309     $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
310     if (count($user_binds) > 0) {
311       fwrite($this->file, $this->indentation."  <user_project_binds>\n");
312       foreach ($user_binds as $bind) {
313         $user_id = $this->userMap[$bind['user_id']];
314         $project_id = $this->projectMap[$bind['project_id']];
315         $bind_part = $this->indentation.'    '."<user_project_bind user_id=\"".$user_id."\"";
316         $bind_part .= " project_id=\"".$project_id."\"";
317         $bind_part .= " rate=\"".$bind['rate']."\"";
318         $bind_part .= " status=\"".$bind['status']."\"";
319         $bind_part .= "></user_project_bind>\n";
320         fwrite($this->file, $bind_part);
321       }
322       fwrite($this->file, $this->indentation."  </user_project_binds>\n");
323       unset($user_binds);
324       unset($bind_part);
325     }
326
327     // Write timesheets.
328     if (count($timesheets) > 0) {
329       fwrite($this->file, $this->indentation."  <timesheets>\n");
330       foreach ($timesheets as $timesheet_item) {
331         $timesheet_part = $this->indentation.'    '."<timesheet id=\"".$this->timesheetMap[$timesheet_item['id']]."\"";
332         $timesheet_part .= " user_id=\"".$this->userMap[$timesheet_item['user_id']]."\"";
333         $timesheet_part .= " client_id=\"".$this->clientMap[$timesheet_item['client_id']]."\"";
334         $timesheet_part .= " name=\"".htmlspecialchars($timesheet_item['name'])."\"";
335         $timesheet_part .= " submit_status=\"".$timesheet_item['submit_status']."\"";
336         $timesheet_part .= " submitter_comment=\"".htmlspecialchars($timesheet_item['submitter_name'])."\"";
337         $timesheet_part .= " approval_status=\"".$timesheet_item['approval_status']."\"";
338         $timesheet_part .= " manager_comment=\"".htmlspecialchars($timesheet_item['manager_comment'])."\"";
339         $timesheet_part .= " status=\"".$timesheet_item['status']."\"";
340         $timesheet_part .= "></timesheet>\n";
341         fwrite($this->file, $timesheet_part);
342       }
343       fwrite($this->file, $this->indentation."  </timesheets>\n");
344       unset($timesheets);
345       unset($timesheet_part);
346     }
347
348     // Write invoices.
349     if (count($invoices) > 0) {
350       fwrite($this->file, $this->indentation."  <invoices>\n");
351       foreach ($invoices as $invoice_item) {
352         $invoice_part = $this->indentation.'    '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
353         $invoice_part .= " name=\"".htmlspecialchars($invoice_item['name'])."\"";
354         $invoice_part .= " date=\"".$invoice_item['date']."\"";
355         $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
356         $invoice_part .= " status=\"".$invoice_item['status']."\"";
357         $invoice_part .= "></invoice>\n";
358         fwrite($this->file, $invoice_part);
359       }
360       fwrite($this->file, $this->indentation."  </invoices>\n");
361       unset($invoices);
362       unset($invoice_part);
363     }
364
365     // Write time log entries and build logMap at the same time.
366     // TODO: big data sets get us out of memory error.
367     // We need to optimize this by working on smaller result sets at a time.
368     // tt_log is one potentially large table, but so may be others.
369     // Refactor this during next round of work here.
370     $records = $this->getRecordsFromTable('tt_log');
371     if (count($records) > 0) {
372       fwrite($this->file, $this->indentation."  <log>\n");
373       $key = 0;
374       foreach ($records as $record) {
375         $key++;
376         $this->logMap[$record['id']] = $key;
377         $log_part = $this->indentation.'    '."<log_item id=\"$key\"";
378         $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
379         $log_part .= " date=\"".$record['date']."\"";
380         $log_part .= " start=\"".$record['start']."\"";
381         $log_part .= " duration=\"".$record['duration']."\"";
382         $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
383         $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
384         $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
385         $log_part .= " timesheet_id=\"".$this->timesheetMap[$record['timesheet_id']]."\"";
386         $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
387         $log_part .= " comment=\"".htmlspecialchars($record['comment'])."\"";
388         $log_part .= " billable=\"".$record['billable']."\"";
389         $log_part .= " approved=\"".$record['approved']."\"";
390         $log_part .= " paid=\"".$record['paid']."\"";
391         $log_part .= " status=\"".$record['status']."\"";
392         $log_part .= "></log_item>\n";
393         fwrite($this->file, $log_part);
394       }
395       fwrite($this->file, $this->indentation."  </log>\n");
396       unset($records);
397       unset($log_part);
398     }
399
400     // Write custom fields.
401     if (count($custom_fields) > 0) {
402       fwrite($this->file, $this->indentation."  <custom_fields>\n");
403       foreach ($custom_fields as $custom_field) {
404         $custom_field_part = $this->indentation.'    '."<custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\"";
405         $custom_field_part .= " type=\"".$custom_field['type']."\"";
406         $custom_field_part .= " label=\"".htmlspecialchars($custom_field['label'])."\"";
407         $custom_field_part .= " required=\"".$custom_field['required']."\"";
408         $custom_field_part .= " status=\"".$custom_field['status']."\"";
409         $custom_field_part .= "></custom_field>\n";
410         fwrite($this->file, $custom_field_part);
411       }
412       fwrite($this->file, $this->indentation."  </custom_fields>\n");
413       unset($custom_fields);
414       unset($custom_field_part);
415     }
416
417     // Write custom field options.
418     if (count($custom_field_options) > 0) {
419       fwrite($this->file, $this->indentation."  <custom_field_options>\n");
420       foreach ($custom_field_options as $option) {
421         $custom_field_option_part = $this->indentation.'    '."<custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\"";
422         $custom_field_option_part .= " field_id=\"".$this->customFieldMap[$option['field_id']]."\"";
423         $custom_field_option_part .= " value=\"".htmlspecialchars($option['value'])."\"";
424         $custom_field_option_part .= "></custom_field_option>\n";
425         fwrite($this->file, $custom_field_option_part);
426       }
427       fwrite($this->file, $this->indentation."  </custom_field_options>\n");
428       unset($custom_field_options);
429       unset($custom_field_option_part);
430     }
431
432     // Write custom field log.
433     $custom_field_log = ttTeamHelper::getCustomFieldLog($this->group_id);
434     if (count($custom_field_log) > 0) {
435       fwrite($this->file, $this->indentation."  <custom_field_log>\n");
436       foreach ($custom_field_log as $entry) {
437         $custom_field_log_part = $this->indentation.'    '."<custom_field_log_entry log_id=\"".$this->logMap[$entry['log_id']]."\"";
438         $custom_field_log_part .= " field_id=\"".$this->customFieldMap[$entry['field_id']]."\"";
439         $custom_field_log_part .= " option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\"";
440         $custom_field_log_part .= " value=\"".htmlspecialchars($entry['value'])."\"";
441         $custom_field_log_part .= " status=\"".$entry['status']."\"";
442         $custom_field_log_part .= "></custom_field_log_entry>\n";
443         fwrite($this->file, $custom_field_log_part);
444       }
445       fwrite($this->file, $this->indentation."  </custom_field_log>\n");
446       unset($custom_field_log);
447       unset($custom_field_log_part);
448     }
449
450     // Write expense items.
451     $expense_items = ttTeamHelper::getExpenseItems($this->group_id);
452     if (count($expense_items) > 0) {
453       fwrite($this->file, $this->indentation."  <expense_items>\n");
454       foreach ($expense_items as $expense_item) {
455         $expense_item_part = $this->indentation.'    '."<expense_item date=\"".$expense_item['date']."\"";
456         $expense_item_part .= " user_id=\"".$this->userMap[$expense_item['user_id']]."\"";
457         $expense_item_part .= " client_id=\"".$this->clientMap[$expense_item['client_id']]."\"";
458         $expense_item_part .= " project_id=\"".$this->projectMap[$expense_item['project_id']]."\"";
459         $expense_item_part .= " timesheet_id=\"".$this->timesheetMap[$expense_item['timesheet_id']]."\"";
460         $expense_item_part .= " name=\"".htmlspecialchars($expense_item['name'])."\"";
461         $expense_item_part .= " cost=\"".$expense_item['cost']."\"";
462         $expense_item_part .= " invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\"";
463         $expense_item_part .= " approved=\"".$expense_item['approved']."\"";
464         $expense_item_part .= " paid=\"".$expense_item['paid']."\"";
465         $expense_item_part .= " status=\"".$expense_item['status']."\"";
466         $expense_item_part .= "></expense_item>\n";
467         fwrite($this->file, $expense_item_part);
468       }
469       fwrite($this->file, $this->indentation."  </expense_items>\n");
470       unset($expense_items);
471       unset($expense_item_part);
472     }
473
474     // Write predefined expenses.
475     $predefined_expenses = $this->getRecordsFromTable('tt_predefined_expenses');
476     if (count($predefined_expenses) > 0) {
477       fwrite($this->file, $this->indentation."  <predefined_expenses>\n");
478       foreach ($predefined_expenses as $predefined_expense) {
479         $predefined_expense_part = $this->indentation.'    '."<predefined_expense name=\"".htmlspecialchars($predefined_expense['name'])."\"";
480         $predefined_expense_part .= " cost=\"".$predefined_expense['cost']."\"";
481         $predefined_expense_part .= "></predefined_expense>\n";
482         fwrite($this->file, $predefined_expense_part);
483       }
484       fwrite($this->file, $this->indentation."  </predefined_expenses>\n");
485       unset($predefined_expenses);
486       unset($predefined_expense_part);
487     }
488
489     // Write monthly quotas.
490     $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
491     if (count($quotas) > 0) {
492       fwrite($this->file, $this->indentation."  <monthly_quotas>\n");
493       foreach ($quotas as $quota) {
494         $quota_part = $this->indentation.'    '."<monthly_quota year=\"".$quota['year']."\"";
495         $quota_part .= " month=\"".$quota['month']."\"";
496         $quota_part .= " minutes=\"".$quota['minutes']."\"";
497         $quota_part .= "></monthly_quota>\n";
498         fwrite($this->file, $quota_part);
499       }
500       fwrite($this->file, $this->indentation."  </monthly_quotas>\n");
501       unset($quotas);
502       unset($quota_part);
503     }
504
505     // Write fav reports.
506     if (count($fav_reports) > 0) {
507       fwrite($this->file, $this->indentation."  <fav_reports>\n");
508       foreach ($fav_reports as $fav_report) {
509         $user_list = '';
510         if (strlen($fav_report['users']) > 0) {
511           $arr = explode(',', $fav_report['users']);
512           foreach ($arr as $k=>$v) {
513             if (array_key_exists($arr[$k], $this->userMap))
514               $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
515           }
516         }
517         $fav_report_part = $this->indentation.'    '."<fav_report id=\"".$this->favReportMap[$fav_report['id']]."\"";
518         $fav_report_part .= " user_id=\"".$this->userMap[$fav_report['user_id']]."\"";
519         $fav_report_part .= " name=\"".htmlspecialchars($fav_report['name'])."\"";
520         $fav_report_part .= " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"";
521         $fav_report_part .= " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"";
522         $fav_report_part .= " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"";
523         $fav_report_part .= " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"";
524         $fav_report_part .= " billable=\"".$fav_report['billable']."\"";
525         $fav_report_part .= " approved=\"".$fav_report['approved']."\"";
526         $fav_report_part .= " invoice=\"".$fav_report['invoice']."\"";
527         $fav_report_part .= " timesheet=\"".$fav_report['timesheet']."\"";
528         $fav_report_part .= " paid_status=\"".$fav_report['paid_status']."\"";
529         $fav_report_part .= " users=\"".$user_list."\"";
530         $fav_report_part .= " period=\"".$fav_report['period']."\"";
531         $fav_report_part .= " period_start=\"".$fav_report['period_start']."\"";
532         $fav_report_part .= " period_end=\"".$fav_report['period_end']."\"";
533         $fav_report_part .= " show_client=\"".$fav_report['show_client']."\"";
534         $fav_report_part .= " show_invoice=\"".$fav_report['show_invoice']."\"";
535         $fav_report_part .= " show_paid=\"".$fav_report['show_paid']."\"";
536         $fav_report_part .= " show_ip=\"".$fav_report['show_ip']."\"";
537         $fav_report_part .= " show_project=\"".$fav_report['show_project']."\"";
538         $fav_report_part .= " show_timesheet=\"".$fav_report['show_timesheet']."\"";
539         $fav_report_part .= " show_start=\"".$fav_report['show_start']."\"";
540         $fav_report_part .= " show_duration=\"".$fav_report['show_duration']."\"";
541         $fav_report_part .= " show_cost=\"".$fav_report['show_cost']."\"";
542         $fav_report_part .= " show_task=\"".$fav_report['show_task']."\"";
543         $fav_report_part .= " show_end=\"".$fav_report['show_end']."\"";
544         $fav_report_part .= " show_note=\"".$fav_report['show_note']."\"";
545         $fav_report_part .= " show_approved=\"".$fav_report['show_approved']."\"";
546         $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
547         $fav_report_part .= " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"";
548         $fav_report_part .= " show_work_units=\"".$fav_report['show_work_units']."\"";
549         $fav_report_part .= " group_by1=\"".$fav_report['group_by1']."\"";
550         $fav_report_part .= " group_by2=\"".$fav_report['group_by2']."\"";
551         $fav_report_part .= " group_by3=\"".$fav_report['group_by3']."\"";
552         $fav_report_part .= "></fav_report>\n";
553         fwrite($this->file, $fav_report_part);
554       }
555       fwrite($this->file, $this->indentation."  </fav_reports>\n");
556       unset($fav_reports);
557       unset($fav_report_part);
558     }
559
560     // Write notifications.
561     $notifications = $this->getRecordsFromTable('tt_cron');
562     if (count($notifications) > 0) {
563       fwrite($this->file, $this->indentation."  <notifications>\n");
564       foreach ($notifications as $notification) {
565         $notification_part = $this->indentation.'    '."<notification cron_spec=\"".$notification['cron_spec']."\"";
566         $notification_part .= " last=\"".$notification['last']."\"";
567         $notification_part .= " next=\"".$notification['next']."\"";
568         $notification_part .= " report_id=\"".$this->favReportMap[$notification['report_id']]."\"";
569         $notification_part .= " email=\"".htmlspecialchars($notification['email'])."\"";
570         $notification_part .= " cc=\"".htmlspecialchars($notification['cc'])."\"";
571         $notification_part .= " subject=\"".htmlspecialchars($notification['subject'])."\"";
572         $notification_part .= " report_condition=\"".htmlspecialchars($notification['report_condition'])."\"";
573         $notification_part .= " status=\"".$notification['status']."\"";
574         $notification_part .= "></notification>\n";
575         fwrite($this->file, $notification_part);
576       }
577       fwrite($this->file, $this->indentation."  </notifications>\n");
578       unset($notifications);
579       unset($notification_part);
580     }
581
582     // Write user config parameters.
583     $user_params = $this->getRecordsFromTable('tt_config');
584     if (count($user_params) > 0) {
585       fwrite($this->file, $this->indentation."  <user_params>\n");
586       foreach ($user_params as $user_param) {
587         $user_param_part = $this->indentation.'    '."<user_param user_id=\"".$this->userMap[$user_param['user_id']]."\"";
588         $user_param_part .= " param_name=\"".htmlspecialchars($user_param['param_name'])."\"";
589         $user_param_part .= " param_value=\"".htmlspecialchars($user_param['param_value'])."\"";
590         $user_param_part .= "></user_param>\n";
591         fwrite($this->file, $user_param_part);
592       }
593       fwrite($this->file, $this->indentation."  </user_params>\n");
594       unset($user_params);
595       unset($user_param_part);
596     }
597
598     // We are mostly done with writing this group data, destroy all maps.
599     unset($this->roleMap);
600     unset($this->userMap);
601     unset($this->taskMap);
602     unset($this->projectMap);
603     unset($this->clientMap);
604     unset($this->invoiceMap);
605     unset($this->logMap);
606     unset($this->customFieldMap);
607     unset($this->customFieldOptionMap);
608
609     // Call self recursively for all subgroups.
610     foreach ($this->subgroups as $subgroup) {
611       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
612       $subgroup_helper->writeData();
613     }
614     unset($this->subgroups);
615
616     fwrite($this->file, $this->indentation."</group>\n");
617   }
618 }