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