Adjusted export-import to work with entity type in custom fields.
[timetracker.git] / WEB-INF / lib / ttGroupExportHelper.class.php
index ac35cd8..cd38de9 100644 (file)
@@ -123,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']."\"";
@@ -333,8 +334,10 @@ class ttGroupExportHelper {
         $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 .= " submit_status=\"".$timesheet_item['submit_status']."\"";
         $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']."\"";
@@ -385,7 +388,7 @@ class ttGroupExportHelper {
         $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']."\"";
@@ -403,6 +406,7 @@ class ttGroupExportHelper {
       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']."\"";
@@ -457,8 +461,7 @@ 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 .= " timesheet_id=\"".$this->timesheetMap[$expense_item['timesheet_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']."\"";
@@ -487,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) {
@@ -616,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;
+  }
 }