X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/bf2399f1315130312cc513cdd4efbcffaeb617d0..6bbd7742c4daa1b3d16e14cfb7f64c2b651a5362:/WEB-INF/lib/ttOrgImportHelper.class.php diff --git a/WEB-INF/lib/ttOrgImportHelper.class.php b/WEB-INF/lib/ttOrgImportHelper.class.php index 015579c0..f8dbc523 100644 --- a/WEB-INF/lib/ttOrgImportHelper.class.php +++ b/WEB-INF/lib/ttOrgImportHelper.class.php @@ -28,6 +28,8 @@ import('ttUserHelper'); import('ttRoleHelper'); +import('ttTaskHelper'); +import('ttProjectHelper'); // ttOrgImportHelper - this class is a future replacement for ttImportHelper. // Currently, it is work in progress. @@ -46,7 +48,9 @@ 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 $currentGroupRoleMap = array(); + var $currentGroupTaskMap = array(); + var $currentGroupProjectMap = array(); //var $userMap = array(); // User ids. //var $projectMap = array(); // Project ids. //var $taskMap = array(); // Task ids. @@ -60,6 +64,7 @@ class ttOrgImportHelper { // startElement - callback handler for opening tag of an XML element in the file. function startElement($parser, $name, $attrs) { + global $i18n; // First pass. We only check user logins for potential collisions with existing. if ($this->firstPass) { @@ -73,7 +78,7 @@ class ttOrgImportHelper { } // Second pass processing. We import data here, one tag at a time. - if (!$this->firstPass && $this->canImport) { + if (!$this->firstPass && $this->canImport && $this->errors->no()) { $mdb2 = getConnection(); // We are in second pass and can import data. @@ -88,28 +93,24 @@ class ttOrgImportHelper { // We only have 3 properties at the moment, while work is ongoing... // Special handling for top group. - if (!$this->org_id) { + if (!$this->org_id && $this->current_group_id) { $this->org_id = $this->current_group_id; $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id"; $affected = $mdb2->exec($sql); - // TODO: design a better error handling approach for the entire import process. } - // Set current parent group. + // Set parent group to create subgroups with this group as parent at next entry here. $this->current_parent_group_id = $this->current_group_id; } if ($name == 'ROLES') { - // If we get here, we have to recycle both $currentGroupRoles and $currentGroupRoleMap. - unset($this->currentGroupRoles); + // If we get here, we have to recycle $currentGroupRoleMap. unset($this->currentGroupRoleMap); - $this->currentGroupRoles = array(); $this->currentGroupRoleMap = array(); - // Both arrays are now empty. - // They will get reconstructed after processing of elements in XML. See below. + // Role map is reconstructed after processing elements in XML. See below. } if ($name == 'ROLE') { - // We get here when processing a tag for the current group. + // We get here when processing tags for the current group. $role_id = ttRoleHelper::insert(array( 'group_id' => $this->current_group_id, 'org_id' => $this->org_id, @@ -118,8 +119,59 @@ class ttOrgImportHelper { 'rank' => $attrs['RANK'], 'rights' => $attrs['RIGHTS'], 'status' => $attrs['STATUS'])); - // Add a mapping. - $this->currentGroupRoleMap[$attrs['ID']] = $role_id; + if ($role_id) { + // Add a mapping. + $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 elements in XML. See below. + } + + if ($name == 'TASK') { + // We get here when processing 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 elements in XML. See below. + } + + if ($name == 'PROJECT') { + // We get here when processing 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')); } } } @@ -171,9 +223,9 @@ class ttOrgImportHelper { $file = fopen($filename, 'r'); while ($data = fread($file, 4096)) { if (!xml_parse($parser, $data, feof($file))) { - $this->errors->add(sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($parser)), - xml_get_current_line_number($parser))); + $this->errors->add(sprintf($i18n->get('error.xml'), + xml_get_current_line_number($parser), + xml_error_string(xml_get_error_code($parser)))); } } if ($this->conflicting_entities) { @@ -189,6 +241,7 @@ class ttOrgImportHelper { unlink($filename); return; } + if ($this->errors->yes()) return; // Exit if we have errors. // Now we can do a second pass, where real work is done. $parser = xml_parser_create(); @@ -199,9 +252,9 @@ class ttOrgImportHelper { $file = fopen($filename, 'r'); while ($data = fread($file, 4096)) { if (!xml_parse($parser, $data, feof($file))) { - $this->errors->add(sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($parser)), - xml_get_current_line_number($parser))); + $this->errors->add(sprintf($i18n->get('error.xml'), + xml_get_current_line_number($parser), + xml_error_string(xml_get_error_code($parser)))); } } xml_parser_free($parser); @@ -238,7 +291,7 @@ 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)'; @@ -274,7 +327,10 @@ class ttOrgImportHelper { $sql = 'insert into tt_groups '.$columns.$values; $affected = $mdb2->exec($sql); - if (is_a($affected, 'PEAR_Error')) return false; + if (is_a($affected, 'PEAR_Error')) { + $this->errors->add($i18n->get('error.db')); + return false; + } $group_id = $mdb2->lastInsertID('tt_groups', 'id'); return $group_id;