X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2FttOrgImportHelper.class.php;h=251cb3ea6a2a398ad160683dce94d7d5d80cba9d;hb=a3d07d9fe0c01738c69c6ea37220f3feb4395912;hp=34933411444f542b1d392995c7ecb612840b3bb5;hpb=a2f024d2184505a889e51d92337d9f40e392a1be;p=timetracker.git diff --git a/WEB-INF/lib/ttOrgImportHelper.class.php b/WEB-INF/lib/ttOrgImportHelper.class.php index 34933411..251cb3ea 100644 --- a/WEB-INF/lib/ttOrgImportHelper.class.php +++ b/WEB-INF/lib/ttOrgImportHelper.class.php @@ -26,13 +26,12 @@ // | https://www.anuko.com/time_tracker/credits.htm // +----------------------------------------------------------------------+ -import('ttUserHelper'); - // ttOrgImportHelper class is used to import organization data from an XML file // prepared by ttOrgExportHelper and consisting of nested groups with their info. 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 $num_users = 0; // A number of active and inactive users we are importing. var $conflicting_logins = null; // A comma-separated list of logins 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. @@ -83,7 +82,8 @@ class ttOrgImportHelper { // In first pass we check user logins for potential collisions with existing. if ($name == 'USER' && $this->canImport) { $login = $attrs['LOGIN']; - if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) { + if ('' != $attrs['STATUS']) $this->num_users++; + if ('' != $attrs['STATUS'] && $this->loginExists($login)) { // We have a login collision. Append colliding login to a list of things we cannot import. $this->conflicting_logins .= ($this->conflicting_logins ? ", $login" : $login); // The above is printed in error message with all found colliding logins. @@ -240,7 +240,7 @@ class ttOrgImportHelper { $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( + $user_id = $this->insertUser(array( 'group_id' => $this->current_group_id, 'org_id' => $this->org_id, 'role_id' => $role_id, @@ -249,6 +249,7 @@ class ttOrgImportHelper { 'login' => $attrs['LOGIN'], 'password' => $attrs['PASSWORD'], 'rate' => $attrs['RATE'], + 'quota_percent' => $attrs['QUOTA_PERCENT'], 'email' => $attrs['EMAIL'], 'status' => $attrs['STATUS']), false); if ($user_id) { @@ -261,7 +262,7 @@ class ttOrgImportHelper { } if ($name == 'USER_PROJECT_BIND') { - if (!ttUserHelper::insertBind(array( + if (!$this->insertUserProjectBind(array( 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']], 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']], 'group_id' => $this->current_group_id, @@ -495,12 +496,33 @@ class ttOrgImportHelper { } } - // importXml - uncompresses the file, reads and parses its content. During parsing, - // startElement, endElement, and dataElement functions are called as many times as necessary. - // Actual import occurs in the endElement handler. + // importXml - uncompresses the file, reads and parses its content. + // It goes through the file 2 times. + // + // During 1st pass, it determines whether we can import data. + // In 1st pass, startElement function is called as many times as necessary. + // + // Actual import occurs during 2nd pass. + // In 2nd pass, startElement and endElement are called many times. + // We only use endElement to finish current group processing. + // + // The above allows us to export/import complex orgs with nested groups, + // while by design all data are in attributes of the elements (no CDATA). + // + // There is currently at least one problem with keeping all data in attributes: + // a vertical tab character 0xB anywhere breaks parsing, making import impossible. + // See https://github.com/sparklemotion/nokogiri/issues/1581 - looks like + // an XML standard thing. Apparently, other invalid characters break parsing too. + // This problem needs to be addressed at some point but how exactly without + // complicating export-import too much with CDATA and dataElement processing? function importXml() { global $i18n; + if (!$_FILES['xmlfile']['name']) { + $this->errors->add($i18n->get('error.upload')); + return; // There is nothing to do if we don't have a file. + } + // Do we have a compressed file? $compressed = false; $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1); @@ -549,15 +571,19 @@ class ttOrgImportHelper { $this->errors->add($i18n->get('error.user_exists')); $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins)); } + if (!ttUserHelper::canAdd($this->num_users)) { + $this->canImport = false; + $this->errors->add($i18n->get('error.user_count')); + } $this->firstPass = false; // We are done with 1st pass. xml_parser_free($parser); if ($file) fclose($file); - if (!$this->canImport) { + if ($this->errors->yes()) { + // Remove the file and exit if we have errors. 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(); @@ -654,6 +680,7 @@ class ttOrgImportHelper { // insertMonthlyQuota - a helper function to insert a monthly quota. private function insertMonthlyQuota($fields) { $mdb2 = getConnection(); + $group_id = (int) $fields['group_id']; $org_id = (int) $fields['org_id']; $year = (int) $fields['year']; @@ -669,6 +696,7 @@ class ttOrgImportHelper { // insertPredefinedExpense - a helper function to insert a predefined expense. private function insertPredefinedExpense($fields) { $mdb2 = getConnection(); + $group_id = (int) $fields['group_id']; $org_id = (int) $fields['org_id']; $name = $mdb2->quote($fields['name']); @@ -729,6 +757,56 @@ class ttOrgImportHelper { return $last_id; } + // insertUserProjectBind - inserts a user to project bind into tt_user_project_binds table. + private function insertUserProjectBind($fields) { + $mdb2 = getConnection(); + + $group_id = (int) $fields['group_id']; + $org_id = (int) $fields['org_id']; + $user_id = (int) $fields['user_id']; + $project_id = (int) $fields['project_id']; + $rate = $mdb2->quote($fields['rate']); + $status = $mdb2->quote($fields['status']); + + $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)". + " values($user_id, $project_id, $group_id, $org_id, $rate, $status)"; + $affected = $mdb2->exec($sql); + return (!is_a($affected, 'PEAR_Error')); + } + + // insertUser - inserts a user into database. + private function insertUser($fields) { + global $user; + $mdb2 = getConnection(); + + $group_id = (int) $fields['group_id']; + $org_id = (int) $fields['org_id']; + + $columns = '(login, password, name, group_id, org_id, role_id, client_id, rate, quota_percent, email, created, created_ip, created_by, status)'; + + $values = 'values ('; + $values .= $mdb2->quote($fields['login']); + $values .= ', '.$mdb2->quote($fields['password']); + $values .= ', '.$mdb2->quote($fields['name']); + $values .= ', '.$group_id; + $values .= ', '.$org_id; + $values .= ', '.(int)$fields['role_id']; + $values .= ', '.$mdb2->quote($fields['client_id']); + $values .= ', '.$mdb2->quote($fields['rate']); + $values .= ', '.$mdb2->quote($fields['quota_percent']); + $values .= ', '.$mdb2->quote($fields['email']); + $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id; + $values .= ', '.$mdb2->quote($fields['status']); + $values .= ')'; + + $sql = "insert into tt_users $columns $values"; + $affected = $mdb2->exec($sql); + if (is_a($affected, 'PEAR_Error')) return false; + + $last_id = $mdb2->lastInsertID('tt_users', 'id'); + return $last_id; + } + // insertProject - a helper function to insert a project as well as project to task binds. private function insertProject($fields) { @@ -736,7 +814,6 @@ class ttOrgImportHelper { $group_id = (int) $fields['group_id']; $org_id = (int) $fields['org_id']; - $name = $fields['name']; $description = $fields['description']; $tasks = $fields['tasks']; @@ -1042,4 +1119,18 @@ class ttOrgImportHelper { } return false; } + + // The loginExists function detrmines if a login already exists. + private function loginExists($login) { + $mdb2 = getConnection(); + + $sql = "select id from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)"; + $res = $mdb2->query($sql); + if (!is_a($res, 'PEAR_Error')) { + if ($val = $res->fetchRow()) { + return true; + } + } + return false; + } }