Adjusted export-import to work with entity type in custom fields.
[timetracker.git] / WEB-INF / lib / ttGroupExportHelper.class.php
index 4ce8eed..cd38de9 100644 (file)
@@ -42,6 +42,7 @@ class ttGroupExportHelper {
   var $taskMap    = array();
   var $projectMap = array();
   var $clientMap  = array();
+  var $timesheetMap = array();
   var $invoiceMap = array();
   var $logMap     = array();
   var $customFieldMap = array();
@@ -122,7 +123,8 @@ class ttGroupExportHelper {
 
     // Write group info.
     $group = $this->getGroupAttrs();
-    $group_part = "<group name=\"".htmlspecialchars($group['name'])."\"";
+    $group_part = "<group group_key=\"".htmlspecialchars($group['group_key'])."\"";
+    $group_part .= " name=\"".htmlspecialchars($group['name'])."\"";
     $group_part .= " description=\"".htmlspecialchars($group['description'])."\"";
     $group_part .= " currency=\"".htmlspecialchars($group['currency'])."\"";
     $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
@@ -174,6 +176,11 @@ class ttGroupExportHelper {
     foreach ($clients as $key=>$client_item)
       $this->clientMap[$client_item['id']] = $key + 1;
 
+    // Prepare timesheet map.
+    $timesheets = $this->getRecordsFromTable('tt_timesheets');
+    foreach ($timesheets as $key=>$timesheet_item)
+      $this->timesheetMap[$timesheet_item['id']] = $key + 1;
+
     // Prepare invoice map.
     $invoices = ttTeamHelper::getAllInvoices();
     foreach ($invoices as $key=>$invoice_item)
@@ -288,6 +295,7 @@ class ttGroupExportHelper {
         $user_part .= " role_id=\"".$role_id."\"";
         $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
         $user_part .= " rate=\"".$user_item['rate']."\"";
+        $user_part .= " quota_percent=\"".$user_item['quota_percent']."\"";
         $user_part .= " email=\"".$user_item['email']."\"";
         $user_part .= " status=\"".$user_item['status']."\"";
         $user_part .= "></user>\n";
@@ -317,6 +325,30 @@ class ttGroupExportHelper {
       unset($bind_part);
     }
 
+    // Write timesheets.
+    if (count($timesheets) > 0) {
+      fwrite($this->file, $this->indentation."  <timesheets>\n");
+      foreach ($timesheets as $timesheet_item) {
+        $timesheet_part = $this->indentation.'    '."<timesheet id=\"".$this->timesheetMap[$timesheet_item['id']]."\"";
+        $timesheet_part .= " user_id=\"".$this->userMap[$timesheet_item['user_id']]."\"";
+        $timesheet_part .= " client_id=\"".$this->clientMap[$timesheet_item['client_id']]."\"";
+        $timesheet_part .= " project_id=\"".$this->projectMap[$timesheet_item['project_id']]."\"";
+        $timesheet_part .= " name=\"".htmlspecialchars($timesheet_item['name'])."\"";
+        $timesheet_part .= " comment=\"".htmlspecialchars($timesheet_item['comment'])."\"";
+        $timesheet_part .= " start_date=\"".$timesheet_item['start_date']."\"";
+        $timesheet_part .= " end_date=\"".$timesheet_item['end_date']."\"";
+        $timesheet_part .= " submit_status=\"".$timesheet_item['submit_status']."\"";
+        $timesheet_part .= " approve_status=\"".$timesheet_item['approve_status']."\"";
+        $timesheet_part .= " approve_comment=\"".htmlspecialchars($timesheet_item['approve_comment'])."\"";
+        $timesheet_part .= " status=\"".$timesheet_item['status']."\"";
+        $timesheet_part .= "></timesheet>\n";
+        fwrite($this->file, $timesheet_part);
+      }
+      fwrite($this->file, $this->indentation."  </timesheets>\n");
+      unset($timesheets);
+      unset($timesheet_part);
+    }
+
     // Write invoices.
     if (count($invoices) > 0) {
       fwrite($this->file, $this->indentation."  <invoices>\n");
@@ -335,6 +367,10 @@ class ttGroupExportHelper {
     }
 
     // Write time log entries and build logMap at the same time.
+    // TODO: big data sets get us out of memory error.
+    // We need to optimize this by working on smaller result sets at a time.
+    // tt_log is one potentially large table, but so may be others.
+    // Refactor this during next round of work here.
     $records = $this->getRecordsFromTable('tt_log');
     if (count($records) > 0) {
       fwrite($this->file, $this->indentation."  <log>\n");
@@ -350,9 +386,11 @@ class ttGroupExportHelper {
         $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
         $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
         $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
+        $log_part .= " timesheet_id=\"".$this->timesheetMap[$record['timesheet_id']]."\"";
         $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
-        $log_part .= " comment=\"".htmlspecialchars($record['comment'])."\"";
+        $log_part .= " comment=\"".$this->encodeLineBreaks($record['comment'])."\"";
         $log_part .= " billable=\"".$record['billable']."\"";
+        $log_part .= " approved=\"".$record['approved']."\"";
         $log_part .= " paid=\"".$record['paid']."\"";
         $log_part .= " status=\"".$record['status']."\"";
         $log_part .= "></log_item>\n";
@@ -363,43 +401,12 @@ class ttGroupExportHelper {
       unset($log_part);
     }
 
-    /*
-    fwrite($this->file, $this->indentation."  <log>\n");
-    $key = 0;
-    foreach ($this->userMap as $key => $value) {
-      $user_id = $key;
-      $records = ttTimeHelper::getAllRecords($user_id);
-      foreach ($records as $record) {
-        $key++;
-        $this->logMap[$record['id']] = $key;
-        $log_part = $this->indentation.'    '."<log_item id=\"$key\"";
-        $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
-        $log_part .= " date=\"".$record['date']."\"";
-        $log_part .= " start=\"".$record['start']."\"";
-        $log_part .= " finish=\"".$record['finish']."\"";
-        $log_part .= " duration=\"".($record['start']?"":$record['duration'])."\"";
-        $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
-        $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
-        $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
-        $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
-        $log_part .= " comment=\"".htmlspecialchars($record['comment'])."\"";
-        $log_part .= " billable=\"".$record['billable']."\"";
-        $log_part .= " paid=\"".$record['paid']."\"";
-        $log_part .= " status=\"".$record['status']."\"";
-        $log_part .= "></log_item>\n";
-        fwrite($this->file, $log_part);
-      }
-    }
-    fwrite($this->file, $this->indentation."  </log>\n");
-    unset($records);
-    unset($log_part);
-    */
-
     // Write custom fields.
     if (count($custom_fields) > 0) {
       fwrite($this->file, $this->indentation."  <custom_fields>\n");
       foreach ($custom_fields as $custom_field) {
         $custom_field_part = $this->indentation.'    '."<custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\"";
+        $custom_field_part .= " entity_type=\"".$custom_field['entity_type']."\"";
         $custom_field_part .= " type=\"".$custom_field['type']."\"";
         $custom_field_part .= " label=\"".htmlspecialchars($custom_field['label'])."\"";
         $custom_field_part .= " required=\"".$custom_field['required']."\"";
@@ -454,9 +461,10 @@ class ttGroupExportHelper {
         $expense_item_part .= " user_id=\"".$this->userMap[$expense_item['user_id']]."\"";
         $expense_item_part .= " client_id=\"".$this->clientMap[$expense_item['client_id']]."\"";
         $expense_item_part .= " project_id=\"".$this->projectMap[$expense_item['project_id']]."\"";
-        $expense_item_part .= " name=\"".htmlspecialchars($expense_item['name'])."\"";
+        $expense_item_part .= " name=\"".$this->encodeLineBreaks($expense_item['name'])."\"";
         $expense_item_part .= " cost=\"".$expense_item['cost']."\"";
         $expense_item_part .= " invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\"";
+        $expense_item_part .= " approved=\"".$expense_item['approved']."\"";
         $expense_item_part .= " paid=\"".$expense_item['paid']."\"";
         $expense_item_part .= " status=\"".$expense_item['status']."\"";
         $expense_item_part .= "></expense_item>\n";
@@ -482,6 +490,23 @@ class ttGroupExportHelper {
       unset($predefined_expense_part);
     }
 
+    // Write templates.
+    $templates = $this->getRecordsFromTable('tt_templates');
+    if (count($templates) > 0) {
+      fwrite($this->file, $this->indentation."  <templates>\n");
+      foreach ($templates as $template) {
+        $template_part = $this->indentation.'    '."<template name=\"".htmlspecialchars($template['name'])."\"";
+        $template_part .= " description=\"".htmlspecialchars($template['description'])."\"";
+        $template_part .= " content=\"".$this->encodeLineBreaks($template['content'])."\"";
+        $template_part .= " status=\"".$template['status']."\"";
+        $template_part .= "></template>\n";
+        fwrite($this->file, $template_part);
+      }
+      fwrite($this->file, $this->indentation."  </templates>\n");
+      unset($templates);
+      unset($template_part);
+    }
+
     // Write monthly quotas.
     $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
     if (count($quotas) > 0) {
@@ -518,6 +543,10 @@ class ttGroupExportHelper {
         $fav_report_part .= " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"";
         $fav_report_part .= " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"";
         $fav_report_part .= " billable=\"".$fav_report['billable']."\"";
+        $fav_report_part .= " approved=\"".$fav_report['approved']."\"";
+        $fav_report_part .= " invoice=\"".$fav_report['invoice']."\"";
+        $fav_report_part .= " timesheet=\"".$fav_report['timesheet']."\"";
+        $fav_report_part .= " paid_status=\"".$fav_report['paid_status']."\"";
         $fav_report_part .= " users=\"".$user_list."\"";
         $fav_report_part .= " period=\"".$fav_report['period']."\"";
         $fav_report_part .= " period_start=\"".$fav_report['period_start']."\"";
@@ -527,18 +556,20 @@ class ttGroupExportHelper {
         $fav_report_part .= " show_paid=\"".$fav_report['show_paid']."\"";
         $fav_report_part .= " show_ip=\"".$fav_report['show_ip']."\"";
         $fav_report_part .= " show_project=\"".$fav_report['show_project']."\"";
+        $fav_report_part .= " show_timesheet=\"".$fav_report['show_timesheet']."\"";
         $fav_report_part .= " show_start=\"".$fav_report['show_start']."\"";
         $fav_report_part .= " show_duration=\"".$fav_report['show_duration']."\"";
         $fav_report_part .= " show_cost=\"".$fav_report['show_cost']."\"";
         $fav_report_part .= " show_task=\"".$fav_report['show_task']."\"";
         $fav_report_part .= " show_end=\"".$fav_report['show_end']."\"";
         $fav_report_part .= " show_note=\"".$fav_report['show_note']."\"";
+        $fav_report_part .= " show_approved=\"".$fav_report['show_approved']."\"";
+        $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
         $fav_report_part .= " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"";
         $fav_report_part .= " show_work_units=\"".$fav_report['show_work_units']."\"";
         $fav_report_part .= " group_by1=\"".$fav_report['group_by1']."\"";
         $fav_report_part .= " group_by2=\"".$fav_report['group_by2']."\"";
         $fav_report_part .= " group_by3=\"".$fav_report['group_by3']."\"";
-        $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
         $fav_report_part .= "></fav_report>\n";
         fwrite($this->file, $fav_report_part);
       }
@@ -605,4 +636,18 @@ class ttGroupExportHelper {
 
     fwrite($this->file, $this->indentation."</group>\n");
   }
+
+  // encodeLineBreaks encodes line breaks with an escape sequence.
+  // We do this, because our strings are attribute values inside XML tags.
+  //
+  // If we don't, we lose line breaks after importing data because
+  // XML parser converts line breaks into a single white character.
+  //
+  // TODO: investigate whether we need to encode \t, etc.
+  private function encodeLineBreaks($source) {
+    $result = htmlspecialchars($source);
+    $result = str_replace ("\n", '&#10;', $result);
+    $result = str_replace ("\r", '&#13;', $result);
+    return $result;
+  }
 }