Started to work on ttRegistrator class to encapsulate restration related tasks.
[timetracker.git] / WEB-INF / lib / ttExportHelper.class.php
index 61de42c..375de2f 100644 (file)
@@ -36,6 +36,7 @@ class ttExportHelper {
   // The following arrays are maps between entity ids in the file versus the database.
   // We write to the file sequentially (1,2,3...) while in the database the entities have different ids.
   var $userMap     = array(); // User ids.
+  var $roleMap     = array(); // Role ids.
   var $projectMap  = array(); // Project ids.
   var $taskMap     = array(); // Task ids.
   var $clientMap   = array(); // Client ids.
@@ -61,17 +62,23 @@ class ttExportHelper {
     fwrite($file, "<pack>\n");
 
     // Write team info.
-    fwrite($file, "<team currency=\"".$user->currency."\" lock_spec=\"".$user->lock_spec."\" lang=\"".$user->lang.
-      "\" decimal_mark=\"".$user->decimal_mark."\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format.
-      "\" week_start=\"".$user->week_start."\" workday_hours=\"".$user->workday_hours.
-      "\" plugins=\"".$user->plugins."\" tracking_mode=\"".$user->tracking_mode."\" task_required=\"".$user->task_required.
-      "\" record_type=\"".$user->record_type."\">\n");
+    fwrite($file, "<team currency=\"".$user->currency."\" decimal_mark=\"".$user->decimal_mark."\" lang=\"".$user->lang.
+      "\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format."\" week_start=\"".$user->week_start.
+      "\" tracking_mode=\"".$user->tracking_mode."\" project_required=\"".$user->project_required."\" task_required=\"".$user->task_required.
+      "\" record_type=\"".$user->record_type."\" bcc_email=\"".$user->bcc_email.
+      "\" plugins=\"".$user->plugins."\" lock_spec=\"".$user->lock_spec."\" workday_minutes=\"".$user->workday_minutes.
+      "\" config=\"".$user->config.
+      "\">\n");
     fwrite($file, "  <name><![CDATA[".$user->team."]]></name>\n");
-    fwrite($file, "  <address><![CDATA[".$user->address."]]></address>\n");
     fwrite($file, "</team>\n");
 
+    // Prepare role map.
+    $roles = $this->getRoles();
+    foreach ($roles as $key=>$role_item)
+      $this->roleMap[$role_item['id']] = $key + 1;
+
     // Prepare user map.
-    $users = ttTeamHelper::getAllUsers($user->team_id, true);
+    $users = $this->getUsers();
     foreach ($users as $key=>$user_item)
       $this->userMap[$user_item['id']] = $key + 1;
 
@@ -105,10 +112,22 @@ class ttExportHelper {
     foreach ($custom_field_options as $key=>$option)
       $this->customFieldOptionMap[$option['id']] = $key + 1;
 
+    // Write roles.
+    fwrite($file, "<roles>\n");
+    foreach ($roles as $role) {
+      fwrite($file, "  <role id=\"".$this->roleMap[$role['id']]."\" rank=\"".$role['rank']."\"".
+        " rights=\"".$role['rights']."\" status=\"".$role['status']."\">\n");
+      fwrite($file, "    <name><![CDATA[".$role['name']."]]></name>\n");
+      fwrite($file, "  </role>\n");
+    }
+    fwrite($file, "</roles>\n");
+    unset($roles);
+
     // Write users.
     fwrite($file, "<users>\n");
     foreach ($users as $user_item) {
-      fwrite($file, "  <user id=\"".$this->userMap[$user_item['id']]."\" login=\"".htmlentities($user_item['login'])."\" password=\"".$user_item['password']."\" role=\"".$user_item['role']."\" client_id=\"".$this->clientMap[$user_item['client_id']]."\" rate=\"".$user_item['rate']."\" email=\"".$user_item['email']."\" status=\"".$user_item['status']."\">\n");
+      $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
+      fwrite($file, "  <user id=\"".$this->userMap[$user_item['id']]."\" login=\"".htmlentities($user_item['login'])."\" password=\"".$user_item['password']."\" role_id=\"".$role_id."\" client_id=\"".$this->clientMap[$user_item['client_id']]."\" rate=\"".$user_item['rate']."\" email=\"".$user_item['email']."\" status=\"".$user_item['status']."\">\n");
       fwrite($file, "    <name><![CDATA[".$user_item['name']."]]></name>\n");
       fwrite($file, "  </user>\n");
     }
@@ -206,7 +225,7 @@ class ttExportHelper {
     $quotas = ttTeamHelper::getMonthlyQuotas($user->team_id);
     fwrite($file, "<monthly_quotas>\n");
     foreach ($quotas as $quota) {
-      fwrite($file, "  <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" quota=\"".$quota['quota']."\"/>\n");
+      fwrite($file, "  <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" minutes=\"".$quota['minutes']."\"/>\n");
     }
     fwrite($file, "</monthly_quotas>\n");
 
@@ -218,7 +237,7 @@ class ttExportHelper {
       foreach ($records as $record) {
         $key++;
         $this->logMap[$record['id']] = $key;
-        fwrite($file, "  <log_item id=\"$key\" timestamp=\"".$record['timestamp']."\" user_id=\"".$this->userMap[$record['user_id']]."\" date=\"".$record['date']."\" start=\"".$record['start']."\" finish=\"".$record['finish']."\" duration=\"".($record['start']?"":$record['duration'])."\" client_id=\"".$this->clientMap[$record['client_id']]."\" project_id=\"".$this->projectMap[$record['project_id']]."\" task_id=\"".$this->taskMap[$record['task_id']]."\" invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\" billable=\"".$record['billable']."\" status=\"".$record['status']."\">\n");
+        fwrite($file, "  <log_item id=\"$key\" user_id=\"".$this->userMap[$record['user_id']]."\" date=\"".$record['date']."\" start=\"".$record['start']."\" finish=\"".$record['finish']."\" duration=\"".($record['start']?"":$record['duration'])."\" client_id=\"".$this->clientMap[$record['client_id']]."\" project_id=\"".$this->projectMap[$record['project_id']]."\" task_id=\"".$this->taskMap[$record['task_id']]."\" invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\" billable=\"".$record['billable']."\" paid=\"".$record['paid']."\" status=\"".$record['status']."\">\n");
         fwrite($file, "    <comment><![CDATA[".$record['comment']."]]></comment>\n");
         fwrite($file, "  </log_item>\n");
       }
@@ -241,7 +260,7 @@ class ttExportHelper {
     $expense_items = ttTeamHelper::getExpenseItems($user->team_id);
     fwrite($file, "<expense_items>\n");
     foreach ($expense_items as $expense_item) {
-      fwrite($file, "  <expense_item date=\"".$expense_item['date']."\" user_id=\"".$this->userMap[$expense_item['user_id']]."\" client_id=\"".$this->clientMap[$expense_item['client_id']]."\" project_id=\"".$this->projectMap[$expense_item['project_id']]."\" cost=\"".$expense_item['cost']."\" invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\" status=\"".$expense_item['status']."\">\n");
+      fwrite($file, "  <expense_item date=\"".$expense_item['date']."\" user_id=\"".$this->userMap[$expense_item['user_id']]."\" client_id=\"".$this->clientMap[$expense_item['client_id']]."\" project_id=\"".$this->projectMap[$expense_item['project_id']]."\" cost=\"".$expense_item['cost']."\" invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\" paid=\"".$expense_item['paid']."\" status=\"".$expense_item['status']."\">\n");
       fwrite($file, "    <name><![CDATA[".$expense_item['name']."]]></name>\n");
       fwrite($file, "  </expense_item>\n");
     }
@@ -260,7 +279,7 @@ class ttExportHelper {
             $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
         }
       }
-      fwrite($file, "\t<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
+      fwrite($file, "  <fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
         " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
         " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
         " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
@@ -272,6 +291,8 @@ class ttExportHelper {
         " period_end=\"".$fav_report['period_end']."\"".
         " show_client=\"".$fav_report['show_client']."\"".
         " show_invoice=\"".$fav_report['show_invoice']."\"".
+        " show_paid=\"".$fav_report['show_paid']."\"".
+        " show_ip=\"".$fav_report['show_ip']."\"".
         " show_project=\"".$fav_report['show_project']."\"".
         " show_start=\"".$fav_report['show_start']."\"".
         " show_duration=\"".$fav_report['show_duration']."\"".
@@ -282,14 +303,15 @@ class ttExportHelper {
         " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
         " group_by=\"".$fav_report['group_by']."\"".
         " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
-      fwrite($file, "\t\t<name><![CDATA[".$fav_report["name"]."]]></name>\n");
-      fwrite($file, "\t</fav_report>\n");
+      fwrite($file, "    <name><![CDATA[".$fav_report["name"]."]]></name>\n");
+      fwrite($file, "  </fav_report>\n");
     }
     fwrite($file, "</fav_reports>\n");
     unset($fav_reports);
 
     // Cleanup.
     unset($users);
+    $this->roleMap = array();
     $this->userMap = array();
     $this->projectMap = array();
     $this->taskMap = array();
@@ -335,4 +357,56 @@ class ttExportHelper {
     fclose ($in_file);
     return true;
   }
+
+  /*
+   * Note about the utility functions below.
+   * We have roughly 4 groups of operations:
+   *   1) Regular system usage for tracking time, etc.
+   *   2) Registration process - used infrequently.
+   *   3) Admin usage - used infrequently.
+   *   4) Export - used infrequently.
+   *
+   * It is tempting to have a generic function to get things done for
+   * all situations. However, as registration, export and admin access are one-off
+   * operations, while regular system usage is daily and must be efficient,
+   * the current approach is to have SEPARATE functions for each mode.
+   *
+   * This is because each mode requires a slightly different approach,
+   * and we don't want to over-complicate things.
+   */
+
+  // getRoles - obtains all roles defined for team.
+  function getRoles() {
+    global $user;
+    $mdb2 = getConnection();
+
+    $result = array();
+    $sql = "select * from tt_roles where team_id = $user->team_id";
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $result[] = $val;
+      }
+      return $result;
+    }
+    return false;
+  }
+
+  // The getUsers obtains all users in team for the purpose of export.
+  function getUsers() {
+    global $user;
+    $mdb2 = getConnection();
+
+    $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.team_id = $user->team_id order by upper(u.name)"; // Note: deleted users are included.
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $result[] = $val;
+      }
+      return $result;
+    }
+    return false;
+  }
 }