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