]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttOrgImportHelper.class.php
Added database schema attribute to export files.
[timetracker.git] / WEB-INF / lib / ttOrgImportHelper.class.php
index 17827986c682b96e08bd5302d0a1a1b669eb6c64..ada232862258b604190e69c7d335d43ec3c6dffd 100644 (file)
@@ -33,12 +33,15 @@ import('ttProjectHelper');
 import('ttClientHelper');
 import('ttInvoiceHelper');
 import('ttCustomFieldHelper');
+import('ttExpenseHelper');
+import('ttFavReportHelper');
 
 // ttOrgImportHelper - this class is a future replacement for ttImportHelper.
 // Currently, it is work in progress.
 // When done, it should handle import of complex groups consisting of other groups.
 class ttOrgImportHelper {
   var $errors               = null; // Errors go here. Set in constructor by reference.
+  var $schema_version       = null; // Database schema version from XML file we import from.
   var $conflicting_entities = null; // A comma-separated list of entity names we cannot import.
   var $canImport      = true;    // False if we cannot import data due to a conflict such as login collision.
   var $firstPass      = true;    // True during first pass through the file.
@@ -71,6 +74,18 @@ class ttOrgImportHelper {
 
     // First pass. We only check user logins for potential collisions with existing.
     if ($this->firstPass) {
+      if ($name == 'ORG' && $this->canImport) {
+         if ($attrs['SCHEMA'] == null) {
+           // We need (database) schema attribute to be available for import to work.
+           // Old Time Tracker export files don't have this.
+           // Current import code does not work with old format because we had to
+           // restructure data in export files for subgroup support.
+           $this->canImport = false;
+           $this->errors->add($i18n->get('error.format'));
+           return;
+         }
+      }
+
       if ($name == 'USER' && $this->canImport) {
         $login = $attrs['LOGIN'];
         if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) {
@@ -370,6 +385,72 @@ class ttOrgImportHelper {
           $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'));
+      }
+
+      if ($name == 'MONTHLY_QUOTA') {
+        if (!$this->insertMonthlyQuota($this->current_group_id,
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          $attrs['YEAR'],
+          $attrs['MONTH'],
+          $attrs['MINUTES'])) {
+          $this->errors->add($i18n->get('error.db'));
+        }
+      }
+
+      if ($name == 'FAV_REPORT') {
+        $user_list = '';
+        if (strlen($attrs['USERS']) > 0) {
+          $arr = explode(',', $attrs['USERS']);
+          foreach ($arr as $v)
+            $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
+        }
+        $fav_report_id = ttFavReportHelper::insertReport(array(
+          'name' => $attrs['NAME'],
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
+          'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
+          'billable' => $attrs['BILLABLE'],
+          'users' => $user_list,
+          'period' => $attrs['PERIOD'],
+          'from' => $attrs['PERIOD_START'],
+          'to' => $attrs['PERIOD_END'],
+          'chclient' => (int) $attrs['SHOW_CLIENT'],
+          'chinvoice' => (int) $attrs['SHOW_INVOICE'],
+          'chpaid' => (int) $attrs['SHOW_PAID'],
+          'chip' => (int) $attrs['SHOW_IP'],
+          'chproject' => (int) $attrs['SHOW_PROJECT'],
+          'chstart' => (int) $attrs['SHOW_START'],
+          'chduration' => (int) $attrs['SHOW_DURATION'],
+          'chcost' => (int) $attrs['SHOW_COST'],
+          'chtask' => (int) $attrs['SHOW_TASK'],
+          'chfinish' => (int) $attrs['SHOW_END'],
+          'chnote' => (int) $attrs['SHOW_NOTE'],
+          'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
+          'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
+          'group_by1' => $attrs['GROUP_BY1'],
+          'group_by2' => $attrs['GROUP_BY2'],
+          'group_by3' => $attrs['GROUP_BY3'],
+          'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
+         if (!$fav_report_id) $this->errors->add($i18n->get('error.db'));
+      }
     }
   }
 
@@ -530,4 +611,12 @@ class ttOrgImportHelper {
     $group_id = $mdb2->lastInsertID('tt_groups', 'id');
     return $group_id;
   }
+
+  // insertMonthlyQuota - a helper function to insert a monthly quota.
+  private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
+    $mdb2 = getConnection();
+    $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
+    $affected = $mdb2->exec($sql);
+    return (!is_a($affected, 'PEAR_Error'));
+  }
 }