+ // 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_group_id, // Note: after insert current_group_id changes.
+ 'org_id' => $this->org_id,
+ 'name' => $attrs['NAME'],
+ 'description' => $attrs['DESCRIPTION'],
+ '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);
+ }
+ // Add self to parent stack.
+ array_push($this->parents, $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.
+ if ($attrs['TASKS']) {
+ $tasks = explode(',', $attrs['TASKS']);
+ foreach ($tasks as $id)
+ $mapped_tasks[] = $this->currentGroupTaskMap[$id];
+ }
+
+ $project_id = $this->insertProject(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.
+ if ($attrs['PROJECTS']) {
+ $projects = explode(',', $attrs['PROJECTS']);
+ foreach ($projects as $id)
+ $mapped_projects[] = $this->currentGroupProjectMap[$id];
+ }
+
+ $client_id = $this->insertClient(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;
+ }
+
+ if ($name == 'LOG_ITEM') {
+ // We get here when processing <log_item> tags for the current group.
+ $log_item_id = ttTimeHelper::insert(array(
+ 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'date' => $attrs['DATE'],
+ 'start' => $attrs['START'],
+ 'finish' => $attrs['FINISH'],
+ 'duration' => $attrs['DURATION'],
+ 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+ 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+ 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
+ 'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+ 'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
+ 'billable' => $attrs['BILLABLE'],
+ 'paid' => $attrs['PAID'],
+ 'status' => $attrs['STATUS']));
+ if ($log_item_id) {
+ // Add a mapping.
+ $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'CUSTOM_FIELDS') {
+ // If we get here, we have to recycle $currentGroupCustomFieldMap.
+ unset($this->currentGroupCustomFieldMap);
+ $this->currentGroupCustomFieldMap = array();
+ // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'CUSTOM_FIELD') {
+ // We get here when processing <custom_field> tags for the current group.
+ $custom_field_id = $this->insertCustomField(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'type' => $attrs['TYPE'],
+ 'label' => $attrs['LABEL'],
+ 'required' => $attrs['REQUIRED'],
+ 'status' => $attrs['STATUS']));
+ if ($custom_field_id) {
+ // Add a mapping.
+ $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'CUSTOM_FIELD_OPTIONS') {
+ // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
+ unset($this->currentGroupCustomFieldOptionMap);
+ $this->currentGroupCustomFieldOptionMap = array();
+ // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'CUSTOM_FIELD_OPTION') {
+ // We get here when processing <custom_field_option> tags for the current group.
+ $custom_field_option_id = $this->insertCustomFieldOption(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+ 'value' => $attrs['VALUE']));
+ if ($custom_field_option_id) {
+ // Add a mapping.
+ $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
+ // We get here when processing <custom_field_log_entry> tags for the current group.
+ if (!$this->insertCustomFieldLogEntry(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
+ 'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+ 'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
+ 'value' => $attrs['VALUE'],
+ 'status' => $attrs['STATUS']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }
+
+ if ($name == 'EXPENSE_ITEM') {
+ // We get here when processing <expense_item> tags for the current group.
+ $expense_item_id = $this->insertExpense(array(
+ 'date' => $attrs['DATE'],
+ 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+ 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+ 'name' => $attrs['NAME'],
+ 'cost' => $attrs['COST'],
+ 'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+ 'paid' => $attrs['PAID'],
+ 'status' => $attrs['STATUS']));
+ if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'PREDEFINED_EXPENSE') {
+ if (!$this->insertPredefinedExpense(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'name' => $attrs['NAME'],
+ 'cost' => $attrs['COST']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }
+
+ if ($name == 'MONTHLY_QUOTA') {
+ if (!$this->insertMonthlyQuota(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'year' => $attrs['YEAR'],
+ 'month' => $attrs['MONTH'],
+ 'minutes' => $attrs['MINUTES']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }
+
+ if ($name == 'FAV_REPORTS') {
+ // If we get here, we have to recycle $currentGroupFavReportMap.
+ unset($this->currentGroupFavReportMap);
+ $this->currentGroupFavReportMap = array();
+ // Favorite report map is reconstructed after processing <fav_report> elements in XML. See below.
+ return;
+ }
+
+ if ($name == 'FAV_REPORT') {
+ $user_list = '';
+ if (strlen($attrs['USERS']) > 0) {
+ $arr = explode(',', $attrs['USERS']);
+ foreach ($arr as $v)
+ $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
+ }
+ $fav_report_id = $this->insertFavReport(array(
+ 'name' => $attrs['NAME'],
+ 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+ 'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
+ 'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+ 'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
+ 'billable' => $attrs['BILLABLE'],
+ 'users' => $user_list,
+ 'period' => $attrs['PERIOD'],
+ 'from' => $attrs['PERIOD_START'],
+ 'to' => $attrs['PERIOD_END'],
+ 'chclient' => (int) $attrs['SHOW_CLIENT'],
+ 'chinvoice' => (int) $attrs['SHOW_INVOICE'],
+ 'chpaid' => (int) $attrs['SHOW_PAID'],
+ 'chip' => (int) $attrs['SHOW_IP'],
+ 'chproject' => (int) $attrs['SHOW_PROJECT'],
+ 'chstart' => (int) $attrs['SHOW_START'],
+ 'chduration' => (int) $attrs['SHOW_DURATION'],
+ 'chcost' => (int) $attrs['SHOW_COST'],
+ 'chtask' => (int) $attrs['SHOW_TASK'],
+ 'chfinish' => (int) $attrs['SHOW_END'],
+ 'chnote' => (int) $attrs['SHOW_NOTE'],
+ 'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
+ 'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
+ 'group_by1' => $attrs['GROUP_BY1'],
+ 'group_by2' => $attrs['GROUP_BY2'],
+ 'group_by3' => $attrs['GROUP_BY3'],
+ 'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
+ if ($fav_report_id) {
+ // Add a mapping.
+ $this->currentGroupFavReportMap[$attrs['ID']] = $fav_report_id;
+ } else $this->errors->add($i18n->get('error.db'));
+ return;
+ }
+
+ if ($name == 'NOTIFICATION') {
+ if (!$this->insertNotification(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'cron_spec' => $attrs['CRON_SPEC'],
+ 'last' => $attrs['LAST'],
+ 'next' => $attrs['NEXT'],
+ 'report_id' => $this->currentGroupFavReportMap[$attrs['REPORT_ID']],
+ 'email' => $attrs['EMAIL'],
+ 'cc' => $attrs['CC'],
+ 'subject' => $attrs['SUBJECT'],
+ 'report_condition' => $attrs['REPORT_CONDITION'],
+ 'status' => $attrs['STATUS']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }
+
+ if ($name == 'USER_PARAM') {
+ if (!$this->insertUserParam(array(
+ 'group_id' => $this->current_group_id,
+ 'org_id' => $this->org_id,
+ 'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+ 'param_name' => $attrs['PARAM_NAME'],
+ 'param_value' => $attrs['PARAM_VALUE']))) {
+ $this->errors->add($i18n->get('error.db'));
+ }
+ return;
+ }