X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/7af7d601282b0ea3138d05c300163e18e121ec32..b0cd6a547ce6a16f0f26667016efce5b66211e54:/WEB-INF/lib/ttOrgImportHelper.class.php diff --git a/WEB-INF/lib/ttOrgImportHelper.class.php b/WEB-INF/lib/ttOrgImportHelper.class.php index c70d2580..6519ffae 100644 --- a/WEB-INF/lib/ttOrgImportHelper.class.php +++ b/WEB-INF/lib/ttOrgImportHelper.class.php @@ -27,109 +27,207 @@ // +----------------------------------------------------------------------+ import('ttUserHelper'); -import('ttProjectHelper'); +import('ttRoleHelper'); import('ttTaskHelper'); -import('ttInvoiceHelper'); -import('ttTimeHelper'); +import('ttProjectHelper'); import('ttClientHelper'); -import('ttCustomFieldHelper'); -import('ttFavReportHelper'); -import('ttExpenseHelper'); -import('ttRoleHelper'); // 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 $currentElement = array(); // Current element of the XML file we are parsing. - var $currentTag = ''; // XML tag of the current element. - - var $canImport = true; // False if we cannot import data due to a login collision. + var $errors = null; // Errors go here. Set in constructor by reference. + 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. - var $org_id = null; // Organization id (same as top group_id). - var $current_parent_group_id = null; // Current parent group id as we parse the file. + 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. - 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(); // 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(); + var $currentGroupUserMap = 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. function startElement($parser, $name, $attrs) { -/* - if ($name == 'GROUP' - || $name == 'USER') { - $this->currentElement = $attrs; - } - $this->currentTag = $name; -*/ + global $i18n; + // First pass. We only check user logins for potential collisions with existing. if ($this->firstPass) { if ($name == 'USER' && $this->canImport) { - if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($attrs['LOGIN'])) { - // We have a login collision, cannot import any data. - $this->canImport = false; + $login = $attrs['LOGIN']; + if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) { + // We have a login collision. Append colliding login to a list of things we cannot import. + $this->conflicting_entities .= ($this->conflicting_entities ? ", $login" : $login); } } - //$this->currentTag = ''; } // 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 through the XML file and can import data. + // We are in second pass and can import data. if ($name == 'GROUP') { // Create a new group. - $group_id = $this->createGroup(array( + $this->current_group_id = $this->createGroup(array( 'parent_id' => $this->current_parent_group_id, 'org_id' => $this->org_id, - 'name' => $this->currentElement['NAME'], - 'currency' => $this->currentElement['CURRENCY'], - 'lang' => $this->currentElement['LANG'])); - // We only have 3 properties in export at the moment, while work is ongoing... + 'name' => $attrs['NAME'], + 'currency' => $attrs['CURRENCY'], + 'lang' => $attrs['LANG'])); + // We only have 3 properties at the moment, while work is ongoing... // Special handling for top group. - if (!$this->org_id) { - $this->org_id = $group_id; - $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_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. - $this->current_parent_group_id = $group_id; + // 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. - // Add new role to $this->currentGroupRoles and a mapping to $this->currentGroupRoleMap. - $this->currentGroupRoles[$this->currentElement['ID']] = $this->currentElement; - $this->currentElement = array(); + // 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, + 'name' => $attrs['NAME'], + 'description' => $attrs['DESCRIPTION'], + 'rank' => $attrs['RANK'], + 'rights' => $attrs['RIGHTS'], + 'status' => $attrs['STATUS'])); + 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')); + } + + if ($name == 'CLIENTS') { + // If we get here, we have to recycle $currentGroupClientMap. + unset($this->currentGroupClientMap); + $this->currentGroupClientMap = array(); + // Client map is reconstructed after processing elements in XML. See below. + } + + if ($name == 'CLIENT') { + // We get here when processing 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 elements in XML. See below. + } + + if ($name == 'USER') { + // We get here when processing 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'], // TODO: check if we need to decode all such things from htmlentities back. Refactor from the beginning if necessary. + 'login' => $attrs['LOGIN'], + 'password' => $attrs['PASSWORD'], + 'rate' => $attrs['RATE'], + 'email' => $attrs['EMAIL'], + 'status' => $attrs['STATUS']), false); + // TODO: what about created_by and other audit info? + if ($user_id) { + // Add a mapping. + $this->currentGroupUserMap[$attrs['ID']] = $user_id; + } else $this->errors->add($i18n->get('error.db')); } } } @@ -181,15 +279,17 @@ 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))); - } - if (!$this->canImport) { - $this->errors->add($i18n->get('error.user_exists')); - break; + $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) { + $this->canImport = false; + $this->errors->add($i18n->get('error.user_exists')); + $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_entities)); + } + $this->firstPass = false; // We are done with 1st pass. xml_parser_free($parser); if ($file) fclose($file); @@ -197,6 +297,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(); @@ -207,9 +308,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); @@ -246,7 +347,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)'; @@ -282,7 +383,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;