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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  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 {
 
  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.
 
  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();
 
  53   function __construct($group_id, $file, $indentation) {
 
  56     $this->group_id = $group_id;
 
  58     $this->indentation = $indentation;
 
  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;
 
  72   // getGroupAttrs obtains group attributes for export.
 
  73   private function getGroupAttrs() {
 
  75     $mdb2 = getConnection();
 
  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();
 
  86   // The getUsers obtains all users in group for the purpose of export.
 
  87   private function getUsers() {
 
  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);
 
  94     if (!is_a($res, 'PEAR_Error')) {
 
  95       while ($val = $res->fetchRow()) {
 
 103   // getRecordsFromTable - obtains all fields from a given table for a group.
 
 104   function getRecordsFromTable($table_name) {
 
 106     $mdb2 = getConnection();
 
 109     $sql = "select * from $table_name where group_id = $this->group_id and org_id = $user->org_id";
 
 110     $res = $mdb2->query($sql);
 
 112     if (!is_a($res, 'PEAR_Error')) {
 
 113       while ($val = $res->fetchRow()) {
 
 121   // writeData writes group data into file.
 
 122   function writeData() {
 
 125     $group = $this->getGroupAttrs();
 
 126     $group_part = "<group group_key=\"".htmlspecialchars($group['group_key'])."\"";
 
 127     $group_part .= " name=\"".htmlspecialchars($group['name'])."\"";
 
 128     $group_part .= " description=\"".htmlspecialchars($group['description'])."\"";
 
 129     $group_part .= " currency=\"".htmlspecialchars($group['currency'])."\"";
 
 130     $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
 
 131     $group_part .= " lang=\"".$group['lang']."\"";
 
 132     $group_part .= " date_format=\"".$group['date_format']."\"";
 
 133     $group_part .= " time_format=\"".$group['time_format']."\"";
 
 134     $group_part .= " week_start=\"".$group['week_start']."\"";
 
 135     $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
 
 136     $group_part .= " project_required=\"".$group['project_required']."\"";
 
 137     $group_part .= " task_required=\"".$group['task_required']."\"";
 
 138     $group_part .= " record_type=\"".$group['record_type']."\"";
 
 139     $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
 
 140     $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
 
 141     $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
 
 142     $group_part .= " plugins=\"".$group['plugins']."\"";
 
 143     $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
 
 144     $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
 
 145     $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
 
 146     $group_part .= " config=\"".$group['config']."\"";
 
 147     $group_part .= ">\n";
 
 150     fwrite($this->file, $this->indentation.$group_part);
 
 155     $users = $this->getUsers();
 
 156     foreach ($users as $key=>$user_item)
 
 157       $this->userMap[$user_item['id']] = $key + 1;
 
 160     $roles = $this->getRecordsFromTable('tt_roles');
 
 161     foreach ($roles as $key=>$role_item)
 
 162       $this->roleMap[$role_item['id']] = $key + 1;
 
 165     $tasks = $this->getRecordsFromTable('tt_tasks');
 
 166     foreach ($tasks as $key=>$task_item)
 
 167       $this->taskMap[$task_item['id']] = $key + 1;
 
 169     // Prepare project map.
 
 170     $projects = $this->getRecordsFromTable('tt_projects');
 
 171     foreach ($projects as $key=>$project_item)
 
 172       $this->projectMap[$project_item['id']] = $key + 1;
 
 174     // Prepare client map.
 
 175     $clients = $this->getRecordsFromTable('tt_clients');
 
 176     foreach ($clients as $key=>$client_item)
 
 177       $this->clientMap[$client_item['id']] = $key + 1;
 
 179     // Prepare timesheet map.
 
 180     $timesheets = $this->getRecordsFromTable('tt_timesheets');
 
 181     foreach ($timesheets as $key=>$timesheet_item)
 
 182       $this->timesheetMap[$timesheet_item['id']] = $key + 1;
 
 184     // Prepare invoice map.
 
 185     $invoices = ttTeamHelper::getAllInvoices();
 
 186     foreach ($invoices as $key=>$invoice_item)
 
 187       $this->invoiceMap[$invoice_item['id']] = $key + 1;
 
 189     // Prepare custom fields map.
 
 190     $custom_fields = ttTeamHelper::getAllCustomFields($this->group_id);
 
 191     foreach ($custom_fields as $key=>$custom_field)
 
 192       $this->customFieldMap[$custom_field['id']] = $key + 1;
 
 194     // Prepare custom field options map.
 
 195     $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($this->group_id);
 
 196     foreach ($custom_field_options as $key=>$option)
 
 197       $this->customFieldOptionMap[$option['id']] = $key + 1;
 
 199     // Prepare favorite report map.
 
 200     $fav_reports = $this->getRecordsFromTable('tt_fav_reports');
 
 201     foreach ($fav_reports as $key=>$fav_report)
 
 202       $this->favReportMap[$fav_report['id']] = $key + 1;
 
 205     fwrite($this->file, $this->indentation."  <roles>\n");
 
 206     foreach ($roles as $role) {
 
 207       $role_part = $this->indentation.'    '."<role id=\"".$this->roleMap[$role['id']]."\"";
 
 208       $role_part .= " name=\"".htmlspecialchars($role['name'])."\"";
 
 209       $role_part .= " description=\"".htmlspecialchars($role['description'])."\"";
 
 210       $role_part .= " rank=\"".$role['rank']."\"";
 
 211       $role_part .= " rights=\"".htmlspecialchars($role['rights'])."\"";
 
 212       $role_part .= " status=\"".$role['status']."\"";
 
 213       $role_part .= "></role>\n";
 
 214       fwrite($this->file, $role_part);
 
 216     fwrite($this->file, $this->indentation."  </roles>\n");
 
 221     if (count($tasks) > 0) {
 
 222       fwrite($this->file, $this->indentation."  <tasks>\n");
 
 223       foreach ($tasks as $task) {
 
 224         $task_part = $this->indentation.'    '."<task id=\"".$this->taskMap[$task['id']]."\"";
 
 225         $task_part .= " name=\"".htmlspecialchars($task['name'])."\"";
 
 226         $task_part .= " description=\"".htmlspecialchars($task['description'])."\"";
 
 227         $task_part .= " status=\"".$task['status']."\"";
 
 228         $task_part .= "></task>\n";
 
 229         fwrite($this->file, $task_part);
 
 231       fwrite($this->file, $this->indentation."  </tasks>\n");
 
 237     if (count($projects) > 0) {
 
 238       fwrite($this->file, $this->indentation."  <projects>\n");
 
 239       foreach ($projects as $project_item) {
 
 241         if($project_item['tasks']){
 
 242           $tasks = explode(',', $project_item['tasks']);
 
 243           $tasks_mapped = array();
 
 244           foreach ($tasks as $item)
 
 245             $tasks_mapped[] = $this->taskMap[$item];
 
 246           $tasks_str = implode(',', $tasks_mapped);
 
 248         $project_part = $this->indentation.'    '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
 
 249         $project_part .= " name=\"".htmlspecialchars($project_item['name'])."\"";
 
 250         $project_part .= " description=\"".htmlspecialchars($project_item['description'])."\"";
 
 251         $project_part .= " tasks=\"".$tasks_str."\"";
 
 252         $project_part .= " status=\"".$project_item['status']."\"";
 
 253         $project_part .= "></project>\n";
 
 254         fwrite($this->file, $project_part);
 
 256       fwrite($this->file, $this->indentation."  </projects>\n");
 
 258       unset($project_part);
 
 262     if (count($clients) > 0) {
 
 263       fwrite($this->file, $this->indentation."  <clients>\n");
 
 264       foreach ($clients as $client_item) {
 
 265         if($client_item['projects']){
 
 266           $projects_db = explode(',', $client_item['projects']);
 
 267           $projects_mapped = array();
 
 268           foreach ($projects_db as $item)
 
 269             $projects_mapped[] = $this->projectMap[$item];
 
 270           $projects_str = implode(',', $projects_mapped);
 
 272         $client_part = $this->indentation.'    '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
 
 273         $client_part .= " name=\"".htmlspecialchars($client_item['name'])."\"";
 
 274         $client_part .= " address=\"".htmlspecialchars($client_item['address'])."\"";
 
 275         $client_part .= " tax=\"".$client_item['tax']."\"";
 
 276         $client_part .= " projects=\"".$projects_str."\"";
 
 277         $client_part .= " status=\"".$client_item['status']."\"";
 
 278         $client_part .= "></client>\n";
 
 279         fwrite($this->file, $client_part);
 
 281       fwrite($this->file, $this->indentation."  </clients>\n");
 
 287     if (count($users) > 0) {
 
 288       fwrite($this->file, $this->indentation."  <users>\n");
 
 289       foreach ($users as $user_item) {
 
 290         $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
 
 291         $user_part = $this->indentation.'    '."<user id=\"".$this->userMap[$user_item['id']]."\"";
 
 292         $user_part .= " name=\"".htmlspecialchars($user_item['name'])."\"";
 
 293         $user_part .= " login=\"".htmlspecialchars($user_item['login'])."\"";
 
 294         $user_part .= " password=\"".$user_item['password']."\"";
 
 295         $user_part .= " role_id=\"".$role_id."\"";
 
 296         $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
 
 297         $user_part .= " rate=\"".$user_item['rate']."\"";
 
 298         $user_part .= " quota_percent=\"".$user_item['quota_percent']."\"";
 
 299         $user_part .= " email=\"".$user_item['email']."\"";
 
 300         $user_part .= " status=\"".$user_item['status']."\"";
 
 301         $user_part .= "></user>\n";
 
 302         fwrite($this->file, $user_part);
 
 304       fwrite($this->file, $this->indentation."  </users>\n");
 
 309     // Write user to project binds.
 
 310     $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
 
 311     if (count($user_binds) > 0) {
 
 312       fwrite($this->file, $this->indentation."  <user_project_binds>\n");
 
 313       foreach ($user_binds as $bind) {
 
 314         $user_id = $this->userMap[$bind['user_id']];
 
 315         $project_id = $this->projectMap[$bind['project_id']];
 
 316         $bind_part = $this->indentation.'    '."<user_project_bind user_id=\"".$user_id."\"";
 
 317         $bind_part .= " project_id=\"".$project_id."\"";
 
 318         $bind_part .= " rate=\"".$bind['rate']."\"";
 
 319         $bind_part .= " status=\"".$bind['status']."\"";
 
 320         $bind_part .= "></user_project_bind>\n";
 
 321         fwrite($this->file, $bind_part);
 
 323       fwrite($this->file, $this->indentation."  </user_project_binds>\n");
 
 329     if (count($timesheets) > 0) {
 
 330       fwrite($this->file, $this->indentation."  <timesheets>\n");
 
 331       foreach ($timesheets as $timesheet_item) {
 
 332         $timesheet_part = $this->indentation.'    '."<timesheet id=\"".$this->timesheetMap[$timesheet_item['id']]."\"";
 
 333         $timesheet_part .= " user_id=\"".$this->userMap[$timesheet_item['user_id']]."\"";
 
 334         $timesheet_part .= " client_id=\"".$this->clientMap[$timesheet_item['client_id']]."\"";
 
 335         $timesheet_part .= " project_id=\"".$this->projectMap[$timesheet_item['project_id']]."\"";
 
 336         $timesheet_part .= " name=\"".htmlspecialchars($timesheet_item['name'])."\"";
 
 337         $timesheet_part .= " comment=\"".htmlspecialchars($timesheet_item['comment'])."\"";
 
 338         $timesheet_part .= " start_date=\"".$timesheet_item['start_date']."\"";
 
 339         $timesheet_part .= " end_date=\"".$timesheet_item['end_date']."\"";
 
 340         $timesheet_part .= " submit_status=\"".$timesheet_item['submit_status']."\"";
 
 341         $timesheet_part .= " approve_status=\"".$timesheet_item['approve_status']."\"";
 
 342         $timesheet_part .= " approve_comment=\"".htmlspecialchars($timesheet_item['approve_comment'])."\"";
 
 343         $timesheet_part .= " status=\"".$timesheet_item['status']."\"";
 
 344         $timesheet_part .= "></timesheet>\n";
 
 345         fwrite($this->file, $timesheet_part);
 
 347       fwrite($this->file, $this->indentation."  </timesheets>\n");
 
 349       unset($timesheet_part);
 
 353     if (count($invoices) > 0) {
 
 354       fwrite($this->file, $this->indentation."  <invoices>\n");
 
 355       foreach ($invoices as $invoice_item) {
 
 356         $invoice_part = $this->indentation.'    '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
 
 357         $invoice_part .= " name=\"".htmlspecialchars($invoice_item['name'])."\"";
 
 358         $invoice_part .= " date=\"".$invoice_item['date']."\"";
 
 359         $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
 
 360         $invoice_part .= " status=\"".$invoice_item['status']."\"";
 
 361         $invoice_part .= "></invoice>\n";
 
 362         fwrite($this->file, $invoice_part);
 
 364       fwrite($this->file, $this->indentation."  </invoices>\n");
 
 366       unset($invoice_part);
 
 369     // Write time log entries and build logMap at the same time.
 
 370     // TODO: big data sets get us out of memory error.
 
 371     // We need to optimize this by working on smaller result sets at a time.
 
 372     // tt_log is one potentially large table, but so may be others.
 
 373     // Refactor this during next round of work here.
 
 374     $records = $this->getRecordsFromTable('tt_log');
 
 375     if (count($records) > 0) {
 
 376       fwrite($this->file, $this->indentation."  <log>\n");
 
 378       foreach ($records as $record) {
 
 380         $this->logMap[$record['id']] = $key;
 
 381         $log_part = $this->indentation.'    '."<log_item id=\"$key\"";
 
 382         $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
 
 383         $log_part .= " date=\"".$record['date']."\"";
 
 384         $log_part .= " start=\"".$record['start']."\"";
 
 385         $log_part .= " duration=\"".$record['duration']."\"";
 
 386         $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
 
 387         $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
 
 388         $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
 
 389         $log_part .= " timesheet_id=\"".$this->timesheetMap[$record['timesheet_id']]."\"";
 
 390         $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
 
 391         $log_part .= " comment=\"".$this->encodeLineBreaks($record['comment'])."\"";
 
 392         $log_part .= " billable=\"".$record['billable']."\"";
 
 393         $log_part .= " approved=\"".$record['approved']."\"";
 
 394         $log_part .= " paid=\"".$record['paid']."\"";
 
 395         $log_part .= " status=\"".$record['status']."\"";
 
 396         $log_part .= "></log_item>\n";
 
 397         fwrite($this->file, $log_part);
 
 399       fwrite($this->file, $this->indentation."  </log>\n");
 
 404     // Write custom fields.
 
 405     if (count($custom_fields) > 0) {
 
 406       fwrite($this->file, $this->indentation."  <custom_fields>\n");
 
 407       foreach ($custom_fields as $custom_field) {
 
 408         $custom_field_part = $this->indentation.'    '."<custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\"";
 
 409         $custom_field_part .= " type=\"".$custom_field['type']."\"";
 
 410         $custom_field_part .= " label=\"".htmlspecialchars($custom_field['label'])."\"";
 
 411         $custom_field_part .= " required=\"".$custom_field['required']."\"";
 
 412         $custom_field_part .= " status=\"".$custom_field['status']."\"";
 
 413         $custom_field_part .= "></custom_field>\n";
 
 414         fwrite($this->file, $custom_field_part);
 
 416       fwrite($this->file, $this->indentation."  </custom_fields>\n");
 
 417       unset($custom_fields);
 
 418       unset($custom_field_part);
 
 421     // Write custom field options.
 
 422     if (count($custom_field_options) > 0) {
 
 423       fwrite($this->file, $this->indentation."  <custom_field_options>\n");
 
 424       foreach ($custom_field_options as $option) {
 
 425         $custom_field_option_part = $this->indentation.'    '."<custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\"";
 
 426         $custom_field_option_part .= " field_id=\"".$this->customFieldMap[$option['field_id']]."\"";
 
 427         $custom_field_option_part .= " value=\"".htmlspecialchars($option['value'])."\"";
 
 428         $custom_field_option_part .= "></custom_field_option>\n";
 
 429         fwrite($this->file, $custom_field_option_part);
 
 431       fwrite($this->file, $this->indentation."  </custom_field_options>\n");
 
 432       unset($custom_field_options);
 
 433       unset($custom_field_option_part);
 
 436     // Write custom field log.
 
 437     $custom_field_log = ttTeamHelper::getCustomFieldLog($this->group_id);
 
 438     if (count($custom_field_log) > 0) {
 
 439       fwrite($this->file, $this->indentation."  <custom_field_log>\n");
 
 440       foreach ($custom_field_log as $entry) {
 
 441         $custom_field_log_part = $this->indentation.'    '."<custom_field_log_entry log_id=\"".$this->logMap[$entry['log_id']]."\"";
 
 442         $custom_field_log_part .= " field_id=\"".$this->customFieldMap[$entry['field_id']]."\"";
 
 443         $custom_field_log_part .= " option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\"";
 
 444         $custom_field_log_part .= " value=\"".htmlspecialchars($entry['value'])."\"";
 
 445         $custom_field_log_part .= " status=\"".$entry['status']."\"";
 
 446         $custom_field_log_part .= "></custom_field_log_entry>\n";
 
 447         fwrite($this->file, $custom_field_log_part);
 
 449       fwrite($this->file, $this->indentation."  </custom_field_log>\n");
 
 450       unset($custom_field_log);
 
 451       unset($custom_field_log_part);
 
 454     // Write expense items.
 
 455     $expense_items = ttTeamHelper::getExpenseItems($this->group_id);
 
 456     if (count($expense_items) > 0) {
 
 457       fwrite($this->file, $this->indentation."  <expense_items>\n");
 
 458       foreach ($expense_items as $expense_item) {
 
 459         $expense_item_part = $this->indentation.'    '."<expense_item date=\"".$expense_item['date']."\"";
 
 460         $expense_item_part .= " user_id=\"".$this->userMap[$expense_item['user_id']]."\"";
 
 461         $expense_item_part .= " client_id=\"".$this->clientMap[$expense_item['client_id']]."\"";
 
 462         $expense_item_part .= " project_id=\"".$this->projectMap[$expense_item['project_id']]."\"";
 
 463         $expense_item_part .= " name=\"".$this->encodeLineBreaks($expense_item['name'])."\"";
 
 464         $expense_item_part .= " cost=\"".$expense_item['cost']."\"";
 
 465         $expense_item_part .= " invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\"";
 
 466         $expense_item_part .= " approved=\"".$expense_item['approved']."\"";
 
 467         $expense_item_part .= " paid=\"".$expense_item['paid']."\"";
 
 468         $expense_item_part .= " status=\"".$expense_item['status']."\"";
 
 469         $expense_item_part .= "></expense_item>\n";
 
 470         fwrite($this->file, $expense_item_part);
 
 472       fwrite($this->file, $this->indentation."  </expense_items>\n");
 
 473       unset($expense_items);
 
 474       unset($expense_item_part);
 
 477     // Write predefined expenses.
 
 478     $predefined_expenses = $this->getRecordsFromTable('tt_predefined_expenses');
 
 479     if (count($predefined_expenses) > 0) {
 
 480       fwrite($this->file, $this->indentation."  <predefined_expenses>\n");
 
 481       foreach ($predefined_expenses as $predefined_expense) {
 
 482         $predefined_expense_part = $this->indentation.'    '."<predefined_expense name=\"".htmlspecialchars($predefined_expense['name'])."\"";
 
 483         $predefined_expense_part .= " cost=\"".$predefined_expense['cost']."\"";
 
 484         $predefined_expense_part .= "></predefined_expense>\n";
 
 485         fwrite($this->file, $predefined_expense_part);
 
 487       fwrite($this->file, $this->indentation."  </predefined_expenses>\n");
 
 488       unset($predefined_expenses);
 
 489       unset($predefined_expense_part);
 
 493     $templates = $this->getRecordsFromTable('tt_templates');
 
 494     if (count($templates) > 0) {
 
 495       fwrite($this->file, $this->indentation."  <templates>\n");
 
 496       foreach ($templates as $template) {
 
 497         $template_part = $this->indentation.'    '."<template name=\"".htmlspecialchars($template['name'])."\"";
 
 498         $template_part .= " description=\"".htmlspecialchars($template['description'])."\"";
 
 499         $template_part .= " content=\"".$this->encodeLineBreaks($template['content'])."\"";
 
 500         $template_part .= " status=\"".$template['status']."\"";
 
 501         $template_part .= "></template>\n";
 
 502         fwrite($this->file, $template_part);
 
 504       fwrite($this->file, $this->indentation."  </templates>\n");
 
 506       unset($template_part);
 
 509     // Write monthly quotas.
 
 510     $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
 
 511     if (count($quotas) > 0) {
 
 512       fwrite($this->file, $this->indentation."  <monthly_quotas>\n");
 
 513       foreach ($quotas as $quota) {
 
 514         $quota_part = $this->indentation.'    '."<monthly_quota year=\"".$quota['year']."\"";
 
 515         $quota_part .= " month=\"".$quota['month']."\"";
 
 516         $quota_part .= " minutes=\"".$quota['minutes']."\"";
 
 517         $quota_part .= "></monthly_quota>\n";
 
 518         fwrite($this->file, $quota_part);
 
 520       fwrite($this->file, $this->indentation."  </monthly_quotas>\n");
 
 525     // Write fav reports.
 
 526     if (count($fav_reports) > 0) {
 
 527       fwrite($this->file, $this->indentation."  <fav_reports>\n");
 
 528       foreach ($fav_reports as $fav_report) {
 
 530         if (strlen($fav_report['users']) > 0) {
 
 531           $arr = explode(',', $fav_report['users']);
 
 532           foreach ($arr as $k=>$v) {
 
 533             if (array_key_exists($arr[$k], $this->userMap))
 
 534               $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
 
 537         $fav_report_part = $this->indentation.'    '."<fav_report id=\"".$this->favReportMap[$fav_report['id']]."\"";
 
 538         $fav_report_part .= " user_id=\"".$this->userMap[$fav_report['user_id']]."\"";
 
 539         $fav_report_part .= " name=\"".htmlspecialchars($fav_report['name'])."\"";
 
 540         $fav_report_part .= " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"";
 
 541         $fav_report_part .= " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"";
 
 542         $fav_report_part .= " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"";
 
 543         $fav_report_part .= " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"";
 
 544         $fav_report_part .= " billable=\"".$fav_report['billable']."\"";
 
 545         $fav_report_part .= " approved=\"".$fav_report['approved']."\"";
 
 546         $fav_report_part .= " invoice=\"".$fav_report['invoice']."\"";
 
 547         $fav_report_part .= " timesheet=\"".$fav_report['timesheet']."\"";
 
 548         $fav_report_part .= " paid_status=\"".$fav_report['paid_status']."\"";
 
 549         $fav_report_part .= " users=\"".$user_list."\"";
 
 550         $fav_report_part .= " period=\"".$fav_report['period']."\"";
 
 551         $fav_report_part .= " period_start=\"".$fav_report['period_start']."\"";
 
 552         $fav_report_part .= " period_end=\"".$fav_report['period_end']."\"";
 
 553         $fav_report_part .= " show_client=\"".$fav_report['show_client']."\"";
 
 554         $fav_report_part .= " show_invoice=\"".$fav_report['show_invoice']."\"";
 
 555         $fav_report_part .= " show_paid=\"".$fav_report['show_paid']."\"";
 
 556         $fav_report_part .= " show_ip=\"".$fav_report['show_ip']."\"";
 
 557         $fav_report_part .= " show_project=\"".$fav_report['show_project']."\"";
 
 558         $fav_report_part .= " show_timesheet=\"".$fav_report['show_timesheet']."\"";
 
 559         $fav_report_part .= " show_start=\"".$fav_report['show_start']."\"";
 
 560         $fav_report_part .= " show_duration=\"".$fav_report['show_duration']."\"";
 
 561         $fav_report_part .= " show_cost=\"".$fav_report['show_cost']."\"";
 
 562         $fav_report_part .= " show_task=\"".$fav_report['show_task']."\"";
 
 563         $fav_report_part .= " show_end=\"".$fav_report['show_end']."\"";
 
 564         $fav_report_part .= " show_note=\"".$fav_report['show_note']."\"";
 
 565         $fav_report_part .= " show_approved=\"".$fav_report['show_approved']."\"";
 
 566         $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
 
 567         $fav_report_part .= " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"";
 
 568         $fav_report_part .= " show_work_units=\"".$fav_report['show_work_units']."\"";
 
 569         $fav_report_part .= " group_by1=\"".$fav_report['group_by1']."\"";
 
 570         $fav_report_part .= " group_by2=\"".$fav_report['group_by2']."\"";
 
 571         $fav_report_part .= " group_by3=\"".$fav_report['group_by3']."\"";
 
 572         $fav_report_part .= "></fav_report>\n";
 
 573         fwrite($this->file, $fav_report_part);
 
 575       fwrite($this->file, $this->indentation."  </fav_reports>\n");
 
 577       unset($fav_report_part);
 
 580     // Write notifications.
 
 581     $notifications = $this->getRecordsFromTable('tt_cron');
 
 582     if (count($notifications) > 0) {
 
 583       fwrite($this->file, $this->indentation."  <notifications>\n");
 
 584       foreach ($notifications as $notification) {
 
 585         $notification_part = $this->indentation.'    '."<notification cron_spec=\"".$notification['cron_spec']."\"";
 
 586         $notification_part .= " last=\"".$notification['last']."\"";
 
 587         $notification_part .= " next=\"".$notification['next']."\"";
 
 588         $notification_part .= " report_id=\"".$this->favReportMap[$notification['report_id']]."\"";
 
 589         $notification_part .= " email=\"".htmlspecialchars($notification['email'])."\"";
 
 590         $notification_part .= " cc=\"".htmlspecialchars($notification['cc'])."\"";
 
 591         $notification_part .= " subject=\"".htmlspecialchars($notification['subject'])."\"";
 
 592         $notification_part .= " report_condition=\"".htmlspecialchars($notification['report_condition'])."\"";
 
 593         $notification_part .= " status=\"".$notification['status']."\"";
 
 594         $notification_part .= "></notification>\n";
 
 595         fwrite($this->file, $notification_part);
 
 597       fwrite($this->file, $this->indentation."  </notifications>\n");
 
 598       unset($notifications);
 
 599       unset($notification_part);
 
 602     // Write user config parameters.
 
 603     $user_params = $this->getRecordsFromTable('tt_config');
 
 604     if (count($user_params) > 0) {
 
 605       fwrite($this->file, $this->indentation."  <user_params>\n");
 
 606       foreach ($user_params as $user_param) {
 
 607         $user_param_part = $this->indentation.'    '."<user_param user_id=\"".$this->userMap[$user_param['user_id']]."\"";
 
 608         $user_param_part .= " param_name=\"".htmlspecialchars($user_param['param_name'])."\"";
 
 609         $user_param_part .= " param_value=\"".htmlspecialchars($user_param['param_value'])."\"";
 
 610         $user_param_part .= "></user_param>\n";
 
 611         fwrite($this->file, $user_param_part);
 
 613       fwrite($this->file, $this->indentation."  </user_params>\n");
 
 615       unset($user_param_part);
 
 618     // We are mostly done with writing this group data, destroy all maps.
 
 619     unset($this->roleMap);
 
 620     unset($this->userMap);
 
 621     unset($this->taskMap);
 
 622     unset($this->projectMap);
 
 623     unset($this->clientMap);
 
 624     unset($this->invoiceMap);
 
 625     unset($this->logMap);
 
 626     unset($this->customFieldMap);
 
 627     unset($this->customFieldOptionMap);
 
 629     // Call self recursively for all subgroups.
 
 630     foreach ($this->subgroups as $subgroup) {
 
 631       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
 
 632       $subgroup_helper->writeData();
 
 634     unset($this->subgroups);
 
 636     fwrite($this->file, $this->indentation."</group>\n");
 
 639   // encodeLineBreaks encodes line breaks with an escape sequence.
 
 640   // We do this, because our strings are attribute values inside XML tags.
 
 642   // If we don't, we lose line breaks after importing data because
 
 643   // XML parser converts line breaks into a single white character.
 
 645   // TODO: investigate whether we need to encode \t, etc.
 
 646   private function encodeLineBreaks($source) {
 
 647     $result = htmlspecialchars($source);
 
 648     $result = str_replace ("\n", '
', $result);
 
 649     $result = str_replace ("\r", '
', $result);