+++ /dev/null
-<?php
-// +----------------------------------------------------------------------+
-// | Anuko Time Tracker
-// +----------------------------------------------------------------------+
-// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
-// +----------------------------------------------------------------------+
-// | LIBERAL FREEWARE LICENSE: This source code document may be used
-// | by anyone for any purpose, and freely redistributed alone or in
-// | combination with other software, provided that the license is obeyed.
-// |
-// | There are only two ways to violate the license:
-// |
-// | 1. To redistribute this code in source form, with the copyright
-// | notice or license removed or altered. (Distributing in compiled
-// | forms without embedded copyright notices is permitted).
-// |
-// | 2. To redistribute modified versions of this code in *any* form
-// | that bears insufficient indications that the modifications are
-// | not the work of the original author(s).
-// |
-// | This license applies to this document only, not any other software
-// | that it may be combined with.
-// |
-// +----------------------------------------------------------------------+
-// | Contributors:
-// | https://www.anuko.com/time_tracker/credits.htm
-// +----------------------------------------------------------------------+
-
-import('ttTeamHelper');
-import('ttTimeHelper');
-
-// ttExportHelper - this class is used to export group data to a file.
-class ttExportHelper {
- var $fileName = null; // Name of the file with data.
-
- // The following arrays are maps between entity ids in the file versus the database.
- // We write to the file sequentially (1,2,3...) while in the database the entities have different ids.
- var $userMap = array(); // User ids.
- var $roleMap = array(); // Role ids.
- var $projectMap = array(); // Project ids.
- var $taskMap = array(); // Task ids.
- var $clientMap = array(); // Client ids.
- var $invoiceMap = array(); // Invoice ids.
- var $customFieldMap = array(); // Custom field ids.
- var $customFieldOptionMap = array(); // Custop field option ids.
- var $logMap = array(); // Time log ids.
-
- // createDataFile creates a file with all data for a given group.
- function createDataFile($compress = false) {
- global $user;
- $group_id = $user->getActiveGroup();
-
- // Create a temporary file.
- $dirName = dirname(TEMPLATE_DIR . '_c/.');
- $tmp_file = tempnam($dirName, 'tt');
-
- // Open the file for writing.
- $file = fopen($tmp_file, 'wb');
- if (!$file) return false;
-
- // Write XML to the file.
- fwrite($file, "<?xml version=\"1.0\"?>\n");
- fwrite($file, "<org>\n");
-
- // Write group info.
- fwrite($file, "<group currency=\"".$user->currency."\" decimal_mark=\"".$user->decimal_mark."\" lang=\"".$user->lang.
- "\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format."\" week_start=\"".$user->week_start.
- "\" tracking_mode=\"".$user->tracking_mode."\" project_required=\"".$user->project_required."\" task_required=\"".$user->task_required.
- "\" record_type=\"".$user->record_type."\" bcc_email=\"".$user->bcc_email.
- "\" plugins=\"".$user->plugins."\" lock_spec=\"".$user->lock_spec."\" workday_minutes=\"".$user->workday_minutes.
- "\" config=\"".$user->config.
- "\">\n");
- fwrite($file, " <name><![CDATA[".$user->group_name."]]></name>\n");
- fwrite($file, " <allow_ip><![CDATA[".$user->allow_ip."]]></allow_ip>\n");
- fwrite($file, " <password_complexity><![CDATA[".$user->password_complexity."]]></password_complexity>\n");
- fwrite($file, "</group>\n");
-
- // Prepare role map.
- $roles = $this->getRoles();
- foreach ($roles as $key=>$role_item)
- $this->roleMap[$role_item['id']] = $key + 1;
-
- // Prepare user map.
- $users = $this->getUsers();
- foreach ($users as $key=>$user_item)
- $this->userMap[$user_item['id']] = $key + 1;
-
- // Prepare project map.
- $projects = ttTeamHelper::getAllProjects($group_id, true);
- foreach ($projects as $key=>$project_item)
- $this->projectMap[$project_item['id']] = $key + 1;
-
- // Prepare task map.
- $tasks = ttTeamHelper::getAllTasks($group_id, true);
- foreach ($tasks as $key=>$task_item)
- $this->taskMap[$task_item['id']] = $key + 1;
-
- // Prepare client map.
- $clients = ttTeamHelper::getAllClients($group_id, true);
- foreach ($clients as $key=>$client_item)
- $this->clientMap[$client_item['id']] = $key + 1;
-
- // Prepare invoice map.
- $invoices = ttTeamHelper::getAllInvoices();
- foreach ($invoices as $key=>$invoice_item)
- $this->invoiceMap[$invoice_item['id']] = $key + 1;
-
- // Prepare custom fields map.
- $custom_fields = ttTeamHelper::getAllCustomFields($group_id);
- foreach ($custom_fields as $key=>$custom_field)
- $this->customFieldMap[$custom_field['id']] = $key + 1;
-
- // Prepare custom field options map.
- $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($group_id);
- foreach ($custom_field_options as $key=>$option)
- $this->customFieldOptionMap[$option['id']] = $key + 1;
-
- // Write roles.
- fwrite($file, "<roles>\n");
- foreach ($roles as $role) {
- fwrite($file, " <role id=\"".$this->roleMap[$role['id']]."\" rank=\"".$role['rank']."\"".
- " rights=\"".$role['rights']."\" status=\"".$role['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$role['name']."]]></name>\n");
- fwrite($file, " <description><![CDATA[".$role['description']."]]></description>\n");
- fwrite($file, " </role>\n");
- }
- fwrite($file, "</roles>\n");
- unset($roles);
-
- // Write users.
- fwrite($file, "<users>\n");
- foreach ($users as $user_item) {
- $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
- fwrite($file, " <user id=\"".$this->userMap[$user_item['id']]."\" login=\"".htmlentities($user_item['login'])."\" password=\"".$user_item['password']."\" role_id=\"".$role_id."\" client_id=\"".$this->clientMap[$user_item['client_id']]."\" rate=\"".$user_item['rate']."\" email=\"".$user_item['email']."\" status=\"".$user_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$user_item['name']."]]></name>\n");
- fwrite($file, " </user>\n");
- }
- fwrite($file, "</users>\n");
-
- // Write tasks.
- fwrite($file, "<tasks>\n");
- foreach ($tasks as $task_item) {
- fwrite($file, " <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$task_item['name']."]]></name>\n");
- fwrite($file, " <description><![CDATA[".$task_item['description']."]]></description>\n");
- fwrite($file, " </task>\n");
- }
- fwrite($file, "</tasks>\n");
- unset($tasks);
-
- // Write projects.
- fwrite($file, "<projects>\n");
- foreach ($projects as $project_item) {
- if($project_item['tasks']){
- $tasks = explode(',', $project_item['tasks']);
- $tasks_mapped = array();
- foreach ($tasks as $item)
- $tasks_mapped[] = $this->taskMap[$item];
- $tasks_str = implode(',', $tasks_mapped);
- }
- fwrite($file, " <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$project_item['name']."]]></name>\n");
- fwrite($file, " <description><![CDATA[".$project_item['description']."]]></description>\n");
- fwrite($file, " </project>\n");
- }
- fwrite($file, "</projects>\n");
- unset($projects);
-
- // Write user to project binds.
- fwrite($file, "<user_project_binds>\n");
- $user_binds = ttTeamHelper::getUserToProjectBinds($group_id);
- foreach ($user_binds as $bind) {
- $user_id = $this->userMap[$bind['user_id']];
- $project_id = $this->projectMap[$bind['project_id']];
- fwrite($file, " <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
- }
- fwrite($file, "</user_project_binds>\n");
- unset($user_binds);
-
- // Write clients.
- fwrite($file, "<clients>\n");
- foreach ($clients as $client_item) {
- if($client_item['projects']){
- $projects = explode(',', $client_item['projects']);
- $projects_mapped = array();
- foreach ($projects as $item)
- $projects_mapped[] = $this->projectMap[$item];
- $projects_str = implode(',', $projects_mapped);
- }
- fwrite($file, " <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$client_item['name']."]]></name>\n");
- fwrite($file, " <address><![CDATA[".$client_item['address']."]]></address>\n");
- fwrite($file, " </client>\n");
- }
- fwrite($file, "</clients>\n");
- unset($clients);
-
- // Write invoices.
- fwrite($file, "<invoices>\n");
- foreach ($invoices as $invoice_item) {
- fwrite($file, " <invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\" date=\"".$invoice_item['date']."\" client_id=\"".$this->clientMap[$invoice_item['client_id']]."\" status=\"".$invoice_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$invoice_item['name']."]]></name>\n");
- fwrite($file, " </invoice>\n");
- }
- fwrite($file, "</invoices>\n");
- unset($invoices);
-
- // Write custom fields.
- fwrite($file, "<custom_fields>\n");
- foreach ($custom_fields as $custom_field) {
- fwrite($file, " <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
- fwrite($file, " <label><![CDATA[".$custom_field['label']."]]></label>\n");
- fwrite($file, " </custom_field>\n");
- }
- fwrite($file, "</custom_fields>\n");
- unset($custom_fields);
-
- // Write custom field options.
- fwrite($file, "<custom_field_options>\n");
- foreach ($custom_field_options as $option) {
- fwrite($file, " <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
- fwrite($file, " <value><![CDATA[".$option['value']."]]></value>\n");
- fwrite($file, " </custom_field_option>\n");
- }
- fwrite($file, "</custom_field_options>\n");
- unset($custom_field_options);
-
- // Write monthly quotas.
- $quotas = ttTeamHelper::getMonthlyQuotas($group_id);
- fwrite($file, "<monthly_quotas>\n");
- foreach ($quotas as $quota) {
- fwrite($file, " <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" minutes=\"".$quota['minutes']."\"/>\n");
- }
- fwrite($file, "</monthly_quotas>\n");
-
- // Write time log entries.
- fwrite($file, "<log>\n");
- $key = 0;
- foreach ($users as $user_item) {
- $records = ttTimeHelper::getAllRecords($user_item['id']);
- foreach ($records as $record) {
- $key++;
- $this->logMap[$record['id']] = $key;
- fwrite($file, " <log_item id=\"$key\" user_id=\"".$this->userMap[$record['user_id']]."\" date=\"".$record['date']."\" start=\"".$record['start']."\" finish=\"".$record['finish']."\" duration=\"".($record['start']?"":$record['duration'])."\" client_id=\"".$this->clientMap[$record['client_id']]."\" project_id=\"".$this->projectMap[$record['project_id']]."\" task_id=\"".$this->taskMap[$record['task_id']]."\" invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\" billable=\"".$record['billable']."\" paid=\"".$record['paid']."\" status=\"".$record['status']."\">\n");
- fwrite($file, " <comment><![CDATA[".$record['comment']."]]></comment>\n");
- fwrite($file, " </log_item>\n");
- }
- }
- fwrite($file, "</log>\n");
- unset($records);
-
- // Write custom field log.
- $custom_field_log = ttTeamHelper::getCustomFieldLog($group_id);
- fwrite($file, "<custom_field_log>\n");
- foreach ($custom_field_log as $entry) {
- fwrite($file, " <custom_field_log_entry log_id=\"".$this->logMap[$entry['log_id']]."\" field_id=\"".$this->customFieldMap[$entry['field_id']]."\" option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\" status=\"".$entry['status']."\">\n");
- fwrite($file, " <value><![CDATA[".$entry['value']."]]></value>\n");
- fwrite($file, " </custom_field_log_entry>\n");
- }
- fwrite($file, "</custom_field_log>\n");
- unset($custom_field_log);
-
- // Write expense items.
- $expense_items = ttTeamHelper::getExpenseItems($group_id);
- fwrite($file, "<expense_items>\n");
- foreach ($expense_items as $expense_item) {
- fwrite($file, " <expense_item date=\"".$expense_item['date']."\" user_id=\"".$this->userMap[$expense_item['user_id']]."\" client_id=\"".$this->clientMap[$expense_item['client_id']]."\" project_id=\"".$this->projectMap[$expense_item['project_id']]."\" cost=\"".$expense_item['cost']."\" invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\" paid=\"".$expense_item['paid']."\" status=\"".$expense_item['status']."\">\n");
- fwrite($file, " <name><![CDATA[".$expense_item['name']."]]></name>\n");
- fwrite($file, " </expense_item>\n");
- }
- fwrite($file, "</expense_items>\n");
- unset($expense_items);
-
- // Write fav reports.
- fwrite($file, "<fav_reports>\n");
- $fav_reports = ttTeamHelper::getFavReports($group_id);
- foreach ($fav_reports as $fav_report) {
- $user_list = '';
- if (strlen($fav_report['users']) > 0) {
- $arr = explode(',', $fav_report['users']);
- foreach ($arr as $k=>$v) {
- if (array_key_exists($arr[$k], $this->userMap))
- $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
- }
- }
- fwrite($file, " <fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
- " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
- " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
- " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
- " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
- " billable=\"".$fav_report['billable']."\"".
- " users=\"".$user_list."\"".
- " period=\"".$fav_report['period']."\"".
- " period_start=\"".$fav_report['period_start']."\"".
- " period_end=\"".$fav_report['period_end']."\"".
- " show_client=\"".$fav_report['show_client']."\"".
- " show_invoice=\"".$fav_report['show_invoice']."\"".
- " show_paid=\"".$fav_report['show_paid']."\"".
- " show_ip=\"".$fav_report['show_ip']."\"".
- " show_project=\"".$fav_report['show_project']."\"".
- " show_start=\"".$fav_report['show_start']."\"".
- " show_duration=\"".$fav_report['show_duration']."\"".
- " show_cost=\"".$fav_report['show_cost']."\"".
- " show_task=\"".$fav_report['show_task']."\"".
- " show_end=\"".$fav_report['show_end']."\"".
- " show_note=\"".$fav_report['show_note']."\"".
- " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
- " show_work_units=\"".$fav_report['show_work_units']."\"".
- " group_by1=\"".$fav_report['group_by1']."\"".
- " group_by2=\"".$fav_report['group_by2']."\"".
- " group_by3=\"".$fav_report['group_by3']."\"".
- " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
- fwrite($file, " <name><![CDATA[".$fav_report["name"]."]]></name>\n");
- fwrite($file, " </fav_report>\n");
- }
- fwrite($file, "</fav_reports>\n");
- unset($fav_reports);
-
- // Cleanup.
- unset($users);
- $this->roleMap = array();
- $this->userMap = array();
- $this->projectMap = array();
- $this->taskMap = array();
-
- fwrite($file, "</org>\n");
- fclose($file);
-
- if ($compress) {
- $this->fileName = tempnam($dirName, 'tt');
- $this->compress($tmp_file, $this->fileName);
- unlink($tmp_file);
- } else
- $this->fileName = $tmp_file;
-
- return true;
- }
-
- // getFileName - returns file name.
- function getFileName() {
- return $this->fileName;
- }
-
- // compress - compresses the content of the $in file into $out file.
- function compress($in, $out) {
- // Initial checks of file names and permissions.
- if (!file_exists($in) || !is_readable ($in))
- return false;
- if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
- return false;
-
- $in_file = fopen($in, 'rb');
-
- if (function_exists('bzopen')) {
- if (!$out_file = bzopen($out, 'w'))
- return false;
-
- while (!feof ($in_file)) {
- $buffer = fread($in_file, 4096);
- bzwrite($out_file, $buffer, 4096);
- }
- bzclose($out_file);
- }
- fclose ($in_file);
- return true;
- }
-
- /*
- * Note about the utility functions below.
- * We have roughly 4 groups of operations:
- * 1) Regular system usage for tracking time, etc.
- * 2) Registration process - used infrequently.
- * 3) Admin usage - used infrequently.
- * 4) Export - used infrequently.
- *
- * It is tempting to have a generic function to get things done for
- * all situations. However, as registration, export and admin access are one-off
- * operations, while regular system usage is daily and must be efficient,
- * the current approach is to have SEPARATE functions for each mode.
- *
- * This is because each mode requires a slightly different approach,
- * and we don't want to over-complicate things.
- */
-
- // getRoles - obtains all roles defined for group.
- function getRoles() {
- global $user;
- $mdb2 = getConnection();
-
- $result = array();
- $sql = "select * from tt_roles where group_id = ".$user->getActiveGroup();
- $res = $mdb2->query($sql);
- $result = array();
- if (!is_a($res, 'PEAR_Error')) {
- while ($val = $res->fetchRow()) {
- $result[] = $val;
- }
- return $result;
- }
- return false;
- }
-
- // The getUsers obtains all users in group for the purpose of export.
- function getUsers() {
- global $user;
- $mdb2 = getConnection();
-
- $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = ".
- $user->getActiveGroup()." order by upper(u.name)"; // Note: deleted users are included.
- $res = $mdb2->query($sql);
- $result = array();
- if (!is_a($res, 'PEAR_Error')) {
- while ($val = $res->fetchRow()) {
- $result[] = $val;
- }
- return $result;
- }
- return false;
- }
-}
+++ /dev/null
-<?php
-// +----------------------------------------------------------------------+
-// | Anuko Time Tracker
-// +----------------------------------------------------------------------+
-// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
-// +----------------------------------------------------------------------+
-// | LIBERAL FREEWARE LICENSE: This source code document may be used
-// | by anyone for any purpose, and freely redistributed alone or in
-// | combination with other software, provided that the license is obeyed.
-// |
-// | There are only two ways to violate the license:
-// |
-// | 1. To redistribute this code in source form, with the copyright
-// | notice or license removed or altered. (Distributing in compiled
-// | forms without embedded copyright notices is permitted).
-// |
-// | 2. To redistribute modified versions of this code in *any* form
-// | that bears insufficient indications that the modifications are
-// | not the work of the original author(s).
-// |
-// | This license applies to this document only, not any other software
-// | that it may be combined with.
-// |
-// +----------------------------------------------------------------------+
-// | Contributors:
-// | https://www.anuko.com/time_tracker/credits.htm
-// +----------------------------------------------------------------------+
-
-import('ttTeamHelper');
-import('ttUserHelper');
-import('ttProjectHelper');
-import('ttTaskHelper');
-import('ttInvoiceHelper');
-import('ttTimeHelper');
-import('ttClientHelper');
-import('ttCustomFieldHelper');
-import('ttFavReportHelper');
-import('ttExpenseHelper');
-import('ttRoleHelper');
-
-// ttImportHelper - this class is used to import group data from a file.
-class ttImportHelper {
- 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 $groupData = array(); // Array of group data such as group name, etc.
- var $org_id = null; // New organization id we are importing. It is created during the import operation.
- var $group_id = null; // New group id we are importing. It is created during the import operation.
- var $roles = array(); // Array of arrays of role properties.
- var $users = array(); // Array of arrays of user properties.
- var $top_role_id = null; // Top manager role id on the new server.
-
- // The following arrays are maps between entity ids in the file versus the database.
- // In the file they are sequential (1,2,3...) while in the database the entities have different ids.
- var $roleMap = array(); // Role 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 $customFieldMap = array(); // Custom field ids.
- var $customFieldOptionMap = array(); // Custop field option ids.
- var $logMap = array(); // Time log ids.
-
- // Constructor.
- function __construct(&$errors) {
- $this->errors = &$errors;
- }
-
- // startElement - callback handler for opening tag of an XML element.
- // In this function we assign passed in attributes to currentElement.
- function startElement($parser, $name, $attrs) {
- if ($name == 'GROUP'
- || $name == 'USER'
- || $name == 'TASK'
- || $name == 'PROJECT'
- || $name == 'CLIENT'
- || $name == 'INVOICE'
- || $name == 'MONTHLY_QUOTA'
- || $name == 'LOG_ITEM'
- || $name == 'CUSTOM_FIELD'
- || $name == 'CUSTOM_FIELD_OPTION'
- || $name == 'CUSTOM_FIELD_LOG_ENTRY'
- || $name == 'INVOICE_HEADER'
- || $name == 'USER_PROJECT_BIND'
- || $name == 'EXPENSE_ITEM'
- || $name == 'FAV_REPORT'
- || $name == 'ROLE') {
- $this->currentElement = $attrs;
- }
- $this->currentTag = $name;
- }
-
- // endElement - callback handler for the closing tag of an XML element.
- // When we are here, currentElement is an array of the element attributes (as set in startElement).
- // Here we do the actual import of data into the database.
- function endElement($parser, $name) {
- if ($name == 'GROUP') {
- $this->groupData = $this->currentElement;
- // Now groupData is an array of group properties. We'll use it later to create a group.
- // Cannot create the group here. Need to determine whether logins collide with existing logins.
- $this->currentElement = array();
- }
- if ($name == 'ROLE') {
- $this->roles[$this->currentElement['ID']] = $this->currentElement;
- $this->currentElement = array();
- }
- if ($name == 'USER') {
- $this->users[$this->currentElement['ID']] = $this->currentElement;
- $this->currentElement = array();
- }
- if ($name == 'USERS') {
- foreach ($this->users as $user_item) {
- if (('' != $user_item['STATUS']) && ttUserHelper::getUserByLogin($user_item['LOGIN'])) {
- // We have a login collision, cannot import any data.
- $this->canImport = false;
- break;
- }
- }
-
- // Now we can create a group.
- if ($this->canImport) {
- $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
- $group_id = $this->createGroup(array(
- 'name' => $this->groupData['NAME'],
- 'currency' => $this->groupData['CURRENCY'],
- 'decimal_mark' => $this->groupData['DECIMAL_MARK'],
- 'lang' => $this->groupData['LANG'],
- 'date_format' => $this->groupData['DATE_FORMAT'],
- 'time_format' => $this->groupData['TIME_FORMAT'],
- 'week_start' => $this->groupData['WEEK_START'],
- 'tracking_mode' => $this->groupData['TRACKING_MODE'],
- 'project_required' => $this->groupData['PROJECT_REQUIRED'],
- 'task_required' => $this->groupData['TASK_REQUIRED'],
- 'record_type' => $this->groupData['RECORD_TYPE'],
- 'bcc_email' => $this->groupData['BCC_EMAIL'],
- 'allow_ip' => $this->groupData['ALLOW_IP'],
- 'password_complexity' => $this->groupData['PASSWORD_COMPLEXITY'],
- 'plugins' => $this->groupData['PLUGINS'],
- 'lock_spec' => $this->groupData['LOCK_SPEC'],
- 'workday_minutes' => $this->groupData['WORKDAY_MINUTES'],
- 'config' => $this->groupData['CONFIG']));
- if ($group_id) {
- $this->org_id = $group_id;
- $this->group_id = $group_id;
-
- // Create roles.
- foreach ($this->roles as $key=>$role_item) {
- $role_id = ttRoleHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'name' => $role_item['NAME'],
- 'description' => $role_item['DESCRIPTION'],
- 'rank' => $role_item['RANK'],
- 'rights' => $role_item['RIGHTS'],
- 'status' => $role_item['STATUS']));
- $this->roleMap[$role_item['ID']] = $role_id;
- }
-
- foreach ($this->users as $key=>$user_item) {
- $role_id = $user_item['ROLE_ID'] === '0' ? $this->top_role_id : $this->roleMap[$user_item['ROLE_ID']]; // 0 (not null) means top manager role.
- $user_id = ttUserHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'role_id' => $role_id,
- 'client_id' => $user_item['CLIENT_ID'], // Note: NOT mapped value, replaced in CLIENT handler.
- 'name' => $user_item['NAME'],
- 'login' => $user_item['LOGIN'],
- 'password' => $user_item['PASSWORD'],
- 'rate' => $user_item['RATE'],
- 'email' => $user_item['EMAIL'],
- 'status' => $user_item['STATUS']), false);
- $this->userMap[$key] = $user_id;
- }
- }
- }
- }
-
- if ($name == 'TASK' && $this->canImport) {
- $this->taskMap[$this->currentElement['ID']] =
- ttTaskHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'name' => $this->currentElement['NAME'],
- 'description' => $this->currentElement['DESCRIPTION'],
- 'status' => $this->currentElement['STATUS']));
- }
- if ($name == 'PROJECT' && $this->canImport) {
- // Prepare a list of task ids.
- $tasks = explode(',', $this->currentElement['TASKS']);
- foreach ($tasks as $id)
- $mapped_tasks[] = $this->taskMap[$id];
-
- // Add a new project.
- $this->projectMap[$this->currentElement['ID']] =
- ttProjectHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'name' => $this->currentElement['NAME'],
- 'description' => $this->currentElement['DESCRIPTION'],
- 'tasks' => $mapped_tasks,
- 'status' => $this->currentElement['STATUS']));
- }
- if ($name == 'USER_PROJECT_BIND' && $this->canImport) {
- ttUserHelper::insertBind(array(
- 'user_id' => $this->userMap[$this->currentElement['USER_ID']],
- 'project_id' => $this->projectMap[$this->currentElement['PROJECT_ID']],
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'rate' => $this->currentElement['RATE'],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'CLIENT' && $this->canImport) {
- // Prepare a list of project ids.
- if ($this->currentElement['PROJECTS']) {
- $projects = explode(',', $this->currentElement['PROJECTS']);
- foreach ($projects as $id)
- $mapped_projects[] = $this->projectMap[$id];
- }
-
- $this->clientMap[$this->currentElement['ID']] =
- ttClientHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'name' => $this->currentElement['NAME'],
- 'address' => $this->currentElement['ADDRESS'],
- 'tax' => $this->currentElement['TAX'],
- 'projects' => $mapped_projects,
- 'status' => $this->currentElement['STATUS']));
-
- // Update client_id for tt_users to a mapped value.
- // We did not do it during user insertion because clientMap was not ready then.
- if ($this->currentElement['ID'] != $this->clientMap[$this->currentElement['ID']])
- ttClientHelper::setMappedClient($this->group_id, $this->currentElement['ID'], $this->clientMap[$this->currentElement['ID']]);
- }
-
- if ($name == 'INVOICE' && $this->canImport) {
- $this->invoiceMap[$this->currentElement['ID']] =
- ttInvoiceHelper::insert(array(
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'name' => $this->currentElement['NAME'],
- 'date' => $this->currentElement['DATE'],
- 'client_id' => $this->clientMap[$this->currentElement['CLIENT_ID']],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'MONTHLY_QUOTA' && $this->canImport) {
- $this->insertMonthlyQuota($this->group_id, $this->currentElement['YEAR'], $this->currentElement['MONTH'], $this->currentElement['MINUTES']);
- }
-
- if ($name == 'LOG_ITEM' && $this->canImport) {
- $this->logMap[$this->currentElement['ID']] =
- ttTimeHelper::insert(array(
- 'user_id' => $this->userMap[$this->currentElement['USER_ID']],
- 'group_id' => $this->group_id,
- 'org_id' => $this->org_id,
- 'date' => $this->currentElement['DATE'],
- 'start' => $this->currentElement['START'],
- 'finish' => $this->currentElement['FINISH'],
- 'duration' => $this->currentElement['DURATION'],
- 'client' => $this->clientMap[$this->currentElement['CLIENT_ID']],
- 'project' => $this->projectMap[$this->currentElement['PROJECT_ID']],
- 'task' => $this->taskMap[$this->currentElement['TASK_ID']],
- 'invoice' => $this->invoiceMap[$this->currentElement['INVOICE_ID']],
- 'note' => (isset($this->currentElement['COMMENT']) ? $this->currentElement['COMMENT'] : ''),
- 'billable' => $this->currentElement['BILLABLE'],
- 'paid' => $this->currentElement['PAID'],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'CUSTOM_FIELD' && $this->canImport) {
- $this->customFieldMap[$this->currentElement['ID']] =
- ttCustomFieldHelper::insertField(array(
- 'group_id' => $this->group_id,
- 'type' => $this->currentElement['TYPE'],
- 'label' => $this->currentElement['LABEL'],
- 'required' => $this->currentElement['REQUIRED'],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'CUSTOM_FIELD_OPTION' && $this->canImport) {
- $this->customFieldOptionMap[$this->currentElement['ID']] =
- ttCustomFieldHelper::insertOption(array(
- 'field_id' => $this->customFieldMap[$this->currentElement['FIELD_ID']],
- 'value' => $this->currentElement['VALUE']));
- }
-
- if ($name == 'CUSTOM_FIELD_LOG_ENTRY' && $this->canImport) {
- ttCustomFieldHelper::insertLogEntry(array(
- 'log_id' => $this->logMap[$this->currentElement['LOG_ID']],
- 'field_id' => $this->customFieldMap[$this->currentElement['FIELD_ID']],
- 'option_id' => $this->customFieldOptionMap[$this->currentElement['OPTION_ID']],
- 'value' => $this->currentElement['VALUE'],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'EXPENSE_ITEM' && $this->canImport) {
- ttExpenseHelper::insert(array(
- 'date' => $this->currentElement['DATE'],
- 'user_id' => $this->userMap[$this->currentElement['USER_ID']],
- 'group_id' => $this->group_id,
- 'client_id' => $this->clientMap[$this->currentElement['CLIENT_ID']],
- 'project_id' => $this->projectMap[$this->currentElement['PROJECT_ID']],
- 'name' => $this->currentElement['NAME'],
- 'cost' => $this->currentElement['COST'],
- 'invoice_id' => $this->invoiceMap[$this->currentElement['INVOICE_ID']],
- 'paid' => $this->currentElement['PAID'],
- 'status' => $this->currentElement['STATUS']));
- }
-
- if ($name == 'FAV_REPORT' && $this->canImport) {
- $user_list = '';
- if (strlen($this->currentElement['USERS']) > 0) {
- $arr = explode(',', $this->currentElement['USERS']);
- foreach ($arr as $v)
- $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->userMap[$v];
- }
- ttFavReportHelper::insertReport(array(
- 'name' => $this->currentElement['NAME'],
- 'user_id' => $this->userMap[$this->currentElement['USER_ID']],
- 'client' => $this->clientMap[$this->currentElement['CLIENT_ID']],
- 'option' => $this->customFieldOptionMap[$this->currentElement['CF_1_OPTION_ID']],
- 'project' => $this->projectMap[$this->currentElement['PROJECT_ID']],
- 'task' => $this->taskMap[$this->currentElement['TASK_ID']],
- 'billable' => $this->currentElement['BILLABLE'],
- 'users' => $user_list,
- 'period' => $this->currentElement['PERIOD'],
- 'from' => $this->currentElement['PERIOD_START'],
- 'to' => $this->currentElement['PERIOD_END'],
- 'chclient' => (int) $this->currentElement['SHOW_CLIENT'],
- 'chinvoice' => (int) $this->currentElement['SHOW_INVOICE'],
- 'chpaid' => (int) $this->currentElement['SHOW_PAID'],
- 'chip' => (int) $this->currentElement['SHOW_IP'],
- 'chproject' => (int) $this->currentElement['SHOW_PROJECT'],
- 'chstart' => (int) $this->currentElement['SHOW_START'],
- 'chduration' => (int) $this->currentElement['SHOW_DURATION'],
- 'chcost' => (int) $this->currentElement['SHOW_COST'],
- 'chtask' => (int) $this->currentElement['SHOW_TASK'],
- 'chfinish' => (int) $this->currentElement['SHOW_END'],
- 'chnote' => (int) $this->currentElement['SHOW_NOTE'],
- 'chcf_1' => (int) $this->currentElement['SHOW_CUSTOM_FIELD_1'],
- 'chunits' => (int) $this->currentElement['SHOW_WORK_UNITS'],
- 'group_by1' => $this->currentElement['GROUP_BY1'],
- 'group_by2' => $this->currentElement['GROUP_BY2'],
- 'group_by3' => $this->currentElement['GROUP_BY3'],
- 'chtotalsonly' => (int) $this->currentElement['SHOW_TOTALS_ONLY']));
- }
- $this->currentTag = '';
- }
-
- // dataElement - callback handler for text data fragments. It builds up currentElement array with text pieces from XML.
- function dataElement($parser, $data) {
- if ($this->currentTag == 'NAME'
- || $this->currentTag == 'DESCRIPTION'
- || $this->currentTag == 'LABEL'
- || $this->currentTag == 'VALUE'
- || $this->currentTag == 'COMMENT'
- || $this->currentTag == 'ADDRESS'
- || $this->currentTag == 'ALLOW_IP'
- || $this->currentTag == 'PASSWORD_COMPLEXITY') {
- if (isset($this->currentElement[$this->currentTag]))
- $this->currentElement[$this->currentTag] .= trim($data);
- else
- $this->currentElement[$this->currentTag] = trim($data);
- }
- }
-
- // 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.
- function importXml() {
- global $i18n;
-
- // Do we have a compressed file?
- $compressed = false;
- $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
- if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
- $compressed = true;
- }
-
- // Create a temporary file.
- $dirName = dirname(TEMPLATE_DIR . '_c/.');
- $filename = tempnam($dirName, 'import_');
-
- // If the file is compressed - uncompress it.
- if ($compressed) {
- if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
- $this->errors->add($i18n->get('error.sys'));
- return;
- }
- unlink($_FILES['xmlfile']['tmp_name']);
- } else {
- if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
- $this->errors->add($i18n->get('error.upload'));
- return;
- }
- }
-
- // Initialize XML parser.
- $parser = xml_parser_create();
- xml_set_object($parser, $this);
- xml_set_element_handler($parser, 'startElement', 'endElement');
- xml_set_character_data_handler($parser, 'dataElement');
-
- // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called.
- $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;
- }
- }
- xml_parser_free($parser);
- if ($file) fclose($file);
- unlink($filename);
- }
-
- // uncompress - uncompresses the content of the $in file into the $out file.
- function uncompress($in, $out) {
- // Do we have the uncompress function?
- if (!function_exists('bzopen'))
- return false;
-
- // Initial checks of file names and permissions.
- if (!file_exists($in) || !is_readable ($in))
- return false;
- if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
- return false;
-
- if (!$out_file = fopen($out, 'wb'))
- return false;
- if (!$in_file = bzopen ($in, 'r'))
- return false;
-
- while (!feof($in_file)) {
- $buffer = bzread($in_file, 4096);
- fwrite($out_file, $buffer, 4096);
- }
- bzclose($in_file);
- fclose ($out_file);
- return true;
- }
-
- // createGroup function creates a new group.
- private function createGroup($fields) {
-
- global $user;
- $mdb2 = getConnection();
-
- $columns = '(name, currency, decimal_mark, lang, date_format, time_format, week_start, tracking_mode'.
- ', project_required, task_required, record_type, bcc_email, allow_ip, password_complexity, plugins'.
- ', lock_spec, workday_minutes, config, created, created_ip, created_by)';
-
- $values = ' values ('.$mdb2->quote(trim($fields['name']));
- $values .= ', '.$mdb2->quote(trim($fields['currency']));
- $values .= ', '.$mdb2->quote($fields['decimal_mark']);
- $values .= ', '.$mdb2->quote($fields['lang']);
- $values .= ', '.$mdb2->quote($fields['date_format']);
- $values .= ', '.$mdb2->quote($fields['time_format']);
- $values .= ', '.(int)$fields['week_start'];
- $values .= ', '.(int)$fields['tracking_mode'];
- $values .= ', '.(int)$fields['project_required'];
- $values .= ', '.(int)$fields['task_required'];
- $values .= ', '.(int)$fields['record_type'];
- $values .= ', '.$mdb2->quote($fields['bcc_email']);
- $values .= ', '.$mdb2->quote($fields['allow_ip']);
- $values .= ', '.$mdb2->quote($fields['password_complexity']);
- $values .= ', '.$mdb2->quote($fields['plugins']);
- $values .= ', '.$mdb2->quote($fields['lock_spec']);
- $values .= ', '.(int)$fields['workday_minutes'];
- $values .= ', '.$mdb2->quote($fields['config']);
- $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
- $values .= ')';
-
- $sql = 'insert into tt_groups '.$columns.$values;
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- $group_id = $mdb2->lastInsertID('tt_groups', 'id');
-
- // Update org_id with group_id.
- // NOTE: Both export and import need an additional effort to properly operate on subgroups.
- // Currently we are importing one group only, which becomes a top level group.
- $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- return $group_id;
- }
-
- // insertMonthlyQuota - a helper function to insert a monthly quota.
- private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
- $mdb2 = getConnection();
- $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
- $affected = $mdb2->exec($sql);
- return (!is_a($affected, 'PEAR_Error'));
- }
-}