From 5210e7673369389701814dab3bf646b7a0d98437 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Sun, 4 Nov 2018 20:36:55 +0000 Subject: [PATCH] Starting to work on ttExportHelper2 class as part of subgroup support. --- WEB-INF/lib/ttExportHelper2.class.php | 420 ++++++++++++++++++++++++++ WEB-INF/templates/footer.tpl | 2 +- export.php | 7 +- 3 files changed, 427 insertions(+), 2 deletions(-) create mode 100644 WEB-INF/lib/ttExportHelper2.class.php diff --git a/WEB-INF/lib/ttExportHelper2.class.php b/WEB-INF/lib/ttExportHelper2.class.php new file mode 100644 index 00000000..a34c66fa --- /dev/null +++ b/WEB-INF/lib/ttExportHelper2.class.php @@ -0,0 +1,420 @@ +\n"); + fwrite($file, "\n"); + + // Write group info. + fwrite($file, "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, " group_name."]]>\n"); + fwrite($file, " allow_ip."]]>\n"); + fwrite($file, " password_complexity."]]>\n"); + fwrite($file, "\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($user->group_id, true); + foreach ($projects as $key=>$project_item) + $this->projectMap[$project_item['id']] = $key + 1; + + // Prepare task map. + $tasks = ttTeamHelper::getAllTasks($user->group_id, true); + foreach ($tasks as $key=>$task_item) + $this->taskMap[$task_item['id']] = $key + 1; + + // Prepare client map. + $clients = ttTeamHelper::getAllClients($user->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($user->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($user->group_id); + foreach ($custom_field_options as $key=>$option) + $this->customFieldOptionMap[$option['id']] = $key + 1; + + // Write roles. + fwrite($file, "\n"); + foreach ($roles as $role) { + fwrite($file, " roleMap[$role['id']]."\" rank=\"".$role['rank']."\"". + " rights=\"".$role['rights']."\" status=\"".$role['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($roles); + + // Write users. + fwrite($file, "\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, " 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, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + + // Write tasks. + fwrite($file, "\n"); + foreach ($tasks as $task_item) { + fwrite($file, " taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($tasks); + + // Write projects. + fwrite($file, "\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, " projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($projects); + + // Write user to project binds. + fwrite($file, "\n"); + $user_binds = ttTeamHelper::getUserToProjectBinds($user->group_id); + foreach ($user_binds as $bind) { + $user_id = $this->userMap[$bind['user_id']]; + $project_id = $this->projectMap[$bind['project_id']]; + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($user_binds); + + // Write clients. + fwrite($file, "\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, " clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, "
\n"); + fwrite($file, "
\n"); + } + fwrite($file, "
\n"); + unset($clients); + + // Write invoices. + fwrite($file, "\n"); + foreach ($invoices as $invoice_item) { + fwrite($file, " invoiceMap[$invoice_item['id']]."\" date=\"".$invoice_item['date']."\" client_id=\"".$this->clientMap[$invoice_item['client_id']]."\" status=\"".$invoice_item['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($invoices); + + // Write custom fields. + fwrite($file, "\n"); + foreach ($custom_fields as $custom_field) { + fwrite($file, " customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($custom_fields); + + // Write custom field options. + fwrite($file, "\n"); + foreach ($custom_field_options as $option) { + fwrite($file, " customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($custom_field_options); + + // Write monthly quotas. + $quotas = ttTeamHelper::getMonthlyQuotas($user->group_id); + fwrite($file, "\n"); + foreach ($quotas as $quota) { + fwrite($file, " \n"); + } + fwrite($file, "\n"); + + // Write time log entries. + fwrite($file, "\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, " 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, " \n"); + fwrite($file, " \n"); + } + } + fwrite($file, "\n"); + unset($records); + + // Write custom field log. + $custom_field_log = ttTeamHelper::getCustomFieldLog($user->group_id); + fwrite($file, "\n"); + foreach ($custom_field_log as $entry) { + fwrite($file, " logMap[$entry['log_id']]."\" field_id=\"".$this->customFieldMap[$entry['field_id']]."\" option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\" status=\"".$entry['status']."\">\n"); + fwrite($file, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($custom_field_log); + + // Write expense items. + $expense_items = ttTeamHelper::getExpenseItems($user->group_id); + fwrite($file, "\n"); + foreach ($expense_items as $expense_item) { + fwrite($file, " 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, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($expense_items); + + // Write fav reports. + fwrite($file, "\n"); + $fav_reports = ttTeamHelper::getFavReports($user->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, " 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, " \n"); + fwrite($file, " \n"); + } + fwrite($file, "\n"); + unset($fav_reports); + + // Cleanup. + unset($users); + $this->roleMap = array(); + $this->userMap = array(); + $this->projectMap = array(); + $this->taskMap = array(); + + fwrite($file, "
\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->group_id"; + $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->group_id 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; + } +} diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 5f015a50..45a12a65 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.07.4363 | Copyright © Anuko | +  Anuko Time Tracker 1.18.07.4364 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/export.php b/export.php index 5f7bd02d..b194adfb 100644 --- a/export.php +++ b/export.php @@ -28,6 +28,7 @@ require_once('initialize.php'); import('ttExportHelper'); +import('ttExportHelper2'); import('form.Form'); // Access check. @@ -56,7 +57,11 @@ if ($request->isPost()) { $mime_type = 'application/x-bzip2'; } - $exportHelper = new ttExportHelper(); + if (defined('SUBGROUP_DEBUG') && isTrue(SUBGROUP_DEBUG)) { + $exportHelper = new ttExportHelper2(); + } else { + $exportHelper = new ttExportHelper(); + } if ($exportHelper->createDataFile($compress)) { header('Pragma: public'); // This is needed for IE8 to download files over https. header('Content-Type: '.$mime_type); -- 2.20.1