+ // Second pass processing. We import data here, one tag at a time.
+ if (!$this->firstPass && $this->canImport && $this->errors->no()) {
+ $mdb2 = getConnection();
+
+ // We are in second pass and can import data.
+ if ($name == 'GROUP') {
+ // Create a new group.
+ $this->current_group_id = $this->createGroup(array(
+ 'parent_id' => $this->current_parent_group_id,
+ 'org_id' => $this->org_id,
+ 'name' => $attrs['NAME'],
+ 'currency' => $attrs['CURRENCY'],
+ 'decimal_mark' => $attrs['DECIMAL_MARK'],
+ 'lang' => $attrs['LANG'],
+ 'date_format' => $attrs['DATE_FORMAT'],
+ 'time_format' => $attrs['TIME_FORMAT'],
+ 'week_start' => $attrs['WEEK_START'],
+ 'tracking_mode' => $attrs['TRACKING_MODE'],
+ 'project_required' => $attrs['PROJECT_REQUIRED'],
+ 'task_required' => $attrs['TASK_REQUIRED'],
+ 'record_type' => $attrs['RECORD_TYPE'],
+ 'bcc_email' => $attrs['BCC_EMAIL'],
+ 'allow_ip' => $attrs['ALLOW_IP'],
+ 'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
+ 'plugins' => $attrs['PLUGINS'],
+ 'lock_spec' => $attrs['LOCK_SPEC'],
+ 'workday_minutes' => $attrs['WORKDAY_MINUTES'],
+ 'custom_logo' => $attrs['CUSTOM_LOGO'],
+ 'config' => $attrs['CONFIG']));
+
+ // Special handling for top group.
+ 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);
+ }
+ // Set parent group to create subgroups with this group as parent at next entry here.
+ $this->current_parent_group_id = $this->current_group_id;
+ return;
+ }
+
+ if ($name == 'ROLES') {
+ // If we get here, we have to recycle $currentGroupRoleMap.
+ unset($this->currentGroupRoleMap);
+ $this->currentGroupRoleMap = array();
+ // Role map is reconstructed after processing <role> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'ROLE') {
+ // We get here when processing <role> 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'));
+ return;
+ }
+
+ 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.
+ return;
+ }
+
+ 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'));
+ return;
+ }
+
+ 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.
+ return;
+ }
+
+ 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'));
+ return;
+ }
+
+ 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.
+ return;
+ }
+
+ 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'));
+ return;
+ }
+
+ if ($name == 'USERS') {
+ // If we get here, we have to recycle $currentGroupUserMap.
+ unset($this->currentGroupUserMap);
+ $this->currentGroupUserMap = array();
+ // User map is reconstructed after processing <user> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'USER') {
+ // We get here when processing <user> 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'],
+ 'login' => $attrs['LOGIN'],
+ 'password' => $attrs['PASSWORD'],
+ 'rate' => $attrs['RATE'],
+ 'email' => $attrs['EMAIL'],
+ 'status' => $attrs['STATUS']), false);
+ if ($user_id) {
+ // Add a mapping.
+ $this->currentGroupUserMap[$attrs['ID']] = $user_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'USER_PROJECT_BIND') {
+ if (!ttUserHelper::insertBind(array(
+ 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+ 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'rate' => $attrs['RATE'],
+ 'status' => $attrs['STATUS']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }
+
+ if ($name == 'INVOICES') {
+ // If we get here, we have to recycle $currentGroupInvoiceMap.
+ unset($this->currentGroupInvoiceMap);
+ $this->currentGroupInvoiceMap = array();
+ // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'INVOICE') {
+ // We get here when processing <invoice> tags for the current group.
+ $invoice_id = ttInvoiceHelper::insert(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'name' => $attrs['NAME'],
+ 'date' => $attrs['DATE'],
+ 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+ 'status' => $attrs['STATUS']));
+ if ($invoice_id) {
+ // Add a mapping.
+ $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'LOG') {
+ // If we get here, we have to recycle $currentGroupLogMap.
+ unset($this->currentGroupLogMap);
+ $this->currentGroupLogMap = array();
+ // Log map is reconstructed after processing <log_item> elements in XML. See below.
+ return;
+ }