]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttOrgImportHelper.class.php
Improved new import with integrating clients.
[timetracker.git] / WEB-INF / lib / ttOrgImportHelper.class.php
index 991424e610bfe597dd2b6be080d44aba170c4925..7c00f25436b4534b3a0e77eccd602aa23f37db58 100644 (file)
@@ -28,6 +28,9 @@
 
 import('ttUserHelper');
 import('ttRoleHelper');
+import('ttTaskHelper');
+import('ttProjectHelper');
+import('ttClientHelper');
 
 // ttOrgImportHelper - this class is a future replacement for ttImportHelper.
 // Currently, it is work in progress.
@@ -46,12 +49,10 @@ class ttOrgImportHelper {
   // var $currentGroupUsers = array(); // Array of arrays of user properties.
 
   // Entity maps for current group. They map XML ids with database ids.
-  var $currentGroupRoleMap = array(); // Maps role ids from XML to their database ids.
-  //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 $currentGroupRoleMap    = array();
+  var $currentGroupTaskMap    = array();
+  var $currentGroupProjectMap = array();
+  var $currentGroupClientMap  = array();
 
   // Constructor.
   function __construct(&$errors) {
@@ -120,6 +121,84 @@ class ttOrgImportHelper {
           $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
         } else $this->errors->add($i18n->get('error.db'));
       }
+
+      if ($name == 'TASKS') {
+        // If we get here, we have to recycle $currentGroupTaskMap.
+        unset($this->currentGroupTaskMap);
+        $this->currentGroupTaskMap = array();
+        // Task map is reconstructed after processing <task> elements in XML. See below.
+      }
+
+      if ($name == 'TASK') {
+        // We get here when processing <task> tags for the current group.
+        $task_id = ttTaskHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'description' => $attrs['DESCRIPTION'],
+          'status' => $attrs['STATUS']));
+        if ($task_id) {
+          // Add a mapping.
+          $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'PROJECTS') {
+        // If we get here, we have to recycle $currentGroupProjectMap.
+        unset($this->currentGroupProjectMap);
+        $this->currentGroupProjectMap = array();
+        // Project map is reconstructed after processing <project> elements in XML. See below.
+      }
+
+      if ($name == 'PROJECT') {
+        // We get here when processing <project> tags for the current group.
+
+        // Prepare a list of task ids.
+        $tasks = explode(',', $attrs['TASKS']);
+        foreach ($tasks as $id)
+          $mapped_tasks[] = $this->currentGroupTaskMap[$id];
+
+        $project_id = ttProjectHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'description' => $attrs['DESCRIPTION'],
+          'tasks' => $mapped_tasks,
+          'status' => $attrs['STATUS']));
+        if ($project_id) {
+          // Add a mapping.
+          $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'));
+      }
     }
   }