]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttOrgImportHelper.class.php
Integrated expense items in new export-import.
[timetracker.git] / WEB-INF / lib / ttOrgImportHelper.class.php
index f8dbc523b20b1283cbcac058c16b39eed5f6c93d..89428c4c9cd6a5732aff20e360e52319bf12e9da 100644 (file)
@@ -30,6 +30,10 @@ import('ttUserHelper');
 import('ttRoleHelper');
 import('ttTaskHelper');
 import('ttProjectHelper');
+import('ttClientHelper');
+import('ttInvoiceHelper');
+import('ttCustomFieldHelper');
+import('ttExpenseHelper');
 
 // ttOrgImportHelper - this class is a future replacement for ttImportHelper.
 // Currently, it is work in progress.
@@ -43,23 +47,23 @@ class ttOrgImportHelper {
   var $current_group_id        = null; // Current group id during parsing.
   var $current_parent_group_id = null; // Current parent group id during parsing.
                                        // Set when we create a new group.
-  // Entities for current group. -- Looks like they are not needed as we insert right away...
-  // var $currentGroupRoles = array(); // Array of arrays of role properties.
-  // var $currentGroupUsers = array(); // Array of arrays of user properties.
+  var $top_role_id    = 0;       // Top role id.
 
   // Entity maps for current group. They map XML ids with database ids.
   var $currentGroupRoleMap    = array();
   var $currentGroupTaskMap    = array();
   var $currentGroupProjectMap = array();
-  //var $userMap       = array(); // User ids.
-  //var $projectMap    = array(); // Project ids.
-  //var $taskMap       = array(); // Task ids.
-  //var $clientMap     = array(); // Client ids.
-  //var $invoiceMap    = array(); // Invoice ids.
+  var $currentGroupClientMap  = array();
+  var $currentGroupUserMap    = array();
+  var $currentGroupInvoiceMap = array();
+  var $currentGroupLogMap     = array();
+  var $currentGroupCustomFieldMap = array();
+  var $currentGroupCustomFieldOptionMap = array();
 
   // Constructor.
   function __construct(&$errors) {
     $this->errors = &$errors;
+    $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
   }
 
   // startElement - callback handler for opening tag of an XML element in the file.
@@ -89,8 +93,23 @@ class ttOrgImportHelper {
           'org_id' => $this->org_id,
           'name' => $attrs['NAME'],
           'currency' => $attrs['CURRENCY'],
-          'lang' => $attrs['LANG']));
-        // We only have 3 properties at the moment, while work is ongoing...
+          'decimal_mark' => $attrs['DECIMAL_MARK'],
+          'lang' => $attrs['LANG'],
+          'date_format' => $attrs['DATE_FORMAT'],
+          'time_format' => $attrs['TIME_FORMAT'],
+          'week_start' => $attrs['WEEK_START'],
+          'tracking_mode' => $attrs['TRACKING_MODE'],
+          'project_required' => $attrs['PROJECT_REQUIRED'],
+          'task_required' => $attrs['TASK_REQUIRED'],
+          'record_type' => $attrs['RECORD_TYPE'],
+          'bcc_email' => $attrs['BCC_EMAIL'],
+          'allow_ip' => $attrs['ALLOW_IP'],
+          'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
+          'plugins' => $attrs['PLUGINS'],
+          'lock_spec' => $attrs['LOCK_SPEC'],
+          'workday_minutes' => $attrs['WORKDAY_MINUTES'],
+          'custom_logo' => $attrs['CUSTOM_LOGO'],
+          'config' => $attrs['CONFIG']));
 
         // Special handling for top group.
         if (!$this->org_id && $this->current_group_id) {
@@ -173,6 +192,202 @@ class ttOrgImportHelper {
           $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
         } else $this->errors->add($i18n->get('error.db'));
       }
+
+      if ($name == 'CLIENTS') {
+        // If we get here, we have to recycle $currentGroupClientMap.
+        unset($this->currentGroupClientMap);
+        $this->currentGroupClientMap = array();
+        // Client map is reconstructed after processing <client> elements in XML. See below.
+      }
+
+      if ($name == 'CLIENT') {
+        // We get here when processing <client> tags for the current group.
+
+        // Prepare a list of project ids.
+        $projects = explode(',', $attrs['PROJECTS']);
+        foreach ($projects as $id)
+          $mapped_projects[] = $this->currentGroupProjectMap[$id];
+
+        $client_id = ttClientHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'address' => $attrs['ADDRESS'],
+          'tax' => $attrs['TAX'],
+          'projects' => $mapped_projects,
+          'status' => $attrs['STATUS']));
+        if ($client_id) {
+          // Add a mapping.
+          $this->currentGroupClientMap[$attrs['ID']] = $client_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'USERS') {
+        // If we get here, we have to recycle $currentGroupUserMap.
+        unset($this->currentGroupUserMap);
+        $this->currentGroupUserMap = array();
+        // User map is reconstructed after processing <user> elements in XML. See below.
+      }
+
+      if ($name == 'USER') {
+        // We get here when processing <user> tags for the current group.
+
+        $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id :  $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
+
+        $user_id = ttUserHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'role_id' => $role_id,
+          'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'name' => $attrs['NAME'],
+          'login' => $attrs['LOGIN'],
+          'password' => $attrs['PASSWORD'],
+          'rate' => $attrs['RATE'],
+          'email' => $attrs['EMAIL'],
+          'status' => $attrs['STATUS']), false);
+        if ($user_id) {
+          // Add a mapping.
+          $this->currentGroupUserMap[$attrs['ID']] = $user_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'USER_PROJECT_BIND') {
+        if (!ttUserHelper::insertBind(array(
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'rate' => $attrs['RATE'],
+          'status' => $attrs['STATUS']))) {
+          $this->errors->add($i18n->get('error.db'));
+        }
+      }
+
+      if ($name == 'INVOICES') {
+        // If we get here, we have to recycle $currentGroupInvoiceMap.
+        unset($this->currentGroupInvoiceMap);
+        $this->currentGroupInvoiceMap = array();
+        // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
+      }
+
+      if ($name == 'INVOICE') {
+        // We get here when processing <invoice> tags for the current group.
+        $invoice_id = ttInvoiceHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'date' => $attrs['DATE'],
+          'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'status' => $attrs['STATUS']));
+        if ($invoice_id) {
+          // Add a mapping.
+          $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'LOG') {
+        // If we get here, we have to recycle $currentGroupLogMap.
+        unset($this->currentGroupLogMap);
+        $this->currentGroupLogMap = array();
+        // Log map is reconstructed after processing <log_item> elements in XML. See below.
+      }
+
+      if ($name == 'LOG_ITEM') {
+        // We get here when processing <log_item> tags for the current group.
+        $log_item_id = ttTimeHelper::insert(array(
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'date' => $attrs['DATE'],
+          'start' => $attrs['START'],
+          'finish' => $attrs['FINISH'],
+          'duration' => $attrs['DURATION'],
+          'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
+          'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+          'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
+          'billable' => $attrs['BILLABLE'],
+          'paid' => $attrs['PAID'],
+          'status' => $attrs['STATUS']));
+        if ($log_item_id) {
+          // Add a mapping.
+          $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELDS') {
+        // If we get here, we have to recycle $currentGroupCustomFieldMap.
+        unset($this->currentGroupCustomFieldMap);
+        $this->currentGroupCustomFieldMap = array();
+        // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
+      }
+
+      if ($name == 'CUSTOM_FIELD') {
+        // We get here when processing <custom_field> tags for the current group.
+        $custom_field_id = ttCustomFieldHelper::insertField(array(
+          'group_id' => $this->current_group_id,
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'type' => $attrs['TYPE'],
+          'label' => $attrs['LABEL'],
+          'required' => $attrs['REQUIRED'],
+          'status' => $attrs['STATUS']));
+        if ($custom_field_id) {
+          // Add a mapping.
+          $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELD_OPTIONS') {
+        // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
+        unset($this->currentGroupCustomFieldOptionMap);
+        $this->currentGroupCustomFieldOptionMap = array();
+        // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
+      }
+
+      if ($name == 'CUSTOM_FIELD_OPTION') {
+        // We get here when processing <custom_field_option> tags for the current group.
+        $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
+          // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+          'value' => $attrs['VALUE']));
+        if ($custom_field_option_id) {
+          // Add a mapping.
+          $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
+        // We get here when processing <custom_field_log_entry> tags for the current group.
+        if (!ttCustomFieldHelper::insertLogEntry(array(
+          // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
+          'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+          'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
+          'value' => $attrs['VALUE'],
+          'status' => $attrs['STATUS']))) {
+          $this->errors->add($i18n->get('error.db'));
+        }
+      }
+
+      if ($name == 'EXPENSE_ITEM') {
+        // We get here when processing <expense_item> tags for the current group.
+        $expense_item_id = ttExpenseHelper::insert(array(
+          'date' => $attrs['DATE'],
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'group_id' => $this->current_group_id,
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'name' => $attrs['NAME'],
+          'cost' => $attrs['COST'],
+          'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+          'paid' => $attrs['PAID'],
+          'status' => $attrs['STATUS']));
+        if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
+      }
     }
   }
 
@@ -290,24 +505,22 @@ class ttOrgImportHelper {
 
   // createGroup function creates a new group.
   private function createGroup($fields) {
-
+    global $user;
     global $i18n;
     $mdb2 = getConnection();
 
-    $columns = '(parent_id, org_id, name, currency, lang)';
-
-//    $columns = '(name, currency, decimal_mark, lang, date_format, time_format, week_start, tracking_mode'.
-//      ', project_required, task_required, record_type, bcc_email, allow_ip, password_complexity, plugins'.
-//      ', lock_spec, workday_minutes, config, created, created_ip, created_by)';
+    $columns = '(parent_id, org_id, name, currency, decimal_mark, lang, date_format, time_format'.
+      ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
+      ', allow_ip, password_complexity, plugins, lock_spec'.
+      ', workday_minutes, config, created, created_ip, created_by)';
 
     $values = ' values (';
     $values .= $mdb2->quote($fields['parent_id']);
     $values .= ', '.$mdb2->quote($fields['org_id']);
     $values .= ', '.$mdb2->quote(trim($fields['name']));
     $values .= ', '.$mdb2->quote(trim($fields['currency']));
-    //$values .= ', '.$mdb2->quote($fields['decimal_mark']);
+    $values .= ', '.$mdb2->quote($fields['decimal_mark']);
     $values .= ', '.$mdb2->quote($fields['lang']);
-/*
     $values .= ', '.$mdb2->quote($fields['date_format']);
     $values .= ', '.$mdb2->quote($fields['time_format']);
     $values .= ', '.(int)$fields['week_start'];
@@ -322,7 +535,7 @@ class ttOrgImportHelper {
     $values .= ', '.$mdb2->quote($fields['lock_spec']);
     $values .= ', '.(int)$fields['workday_minutes'];
     $values .= ', '.$mdb2->quote($fields['config']);
-    $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id); */
+    $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
     $values .= ')';
 
     $sql = 'insert into tt_groups '.$columns.$values;