2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 import('ttTeamHelper');
 
  30 import('ttTimeHelper');
 
  32 // ttExportHelper - this class is used to export team data to a file.
 
  33 class ttExportHelper {
 
  34   var $fileName    = null;    // Name of the file with data.
 
  36   // The following arrays are maps between entity ids in the file versus the database.
 
  37   // We write to the file sequentially (1,2,3...) while in the database the entities have different ids.
 
  38   var $userMap     = array(); // User ids.
 
  39   var $roleMap     = array(); // Role ids.
 
  40   var $projectMap  = array(); // Project ids.
 
  41   var $taskMap     = array(); // Task ids.
 
  42   var $clientMap   = array(); // Client ids.
 
  43   var $invoiceMap  = array(); // Invoice ids.
 
  44   var $customFieldMap       = array(); // Custom field ids.
 
  45   var $customFieldOptionMap = array(); // Custop field option ids.
 
  46   var $logMap      = array(); // Time log ids.
 
  48   // createDataFile creates a file with all data for a given team.
 
  49   function createDataFile($compress = false) {
 
  52     // Create a temporary file.
 
  53     $dirName = dirname(TEMPLATE_DIR . '_c/.');
 
  54     $tmp_file = tempnam($dirName, 'tt');
 
  56     // Open the file for writing.
 
  57     $file = fopen($tmp_file, 'wb');
 
  58     if (!$file) return false;
 
  60     // Write XML to the file.
 
  61     fwrite($file, "<?xml version=\"1.0\"?>\n");
 
  62     fwrite($file, "<pack>\n");
 
  65     fwrite($file, "<team currency=\"".$user->currency."\" decimal_mark=\"".$user->decimal_mark."\" lang=\"".$user->lang.
 
  66       "\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format."\" week_start=\"".$user->week_start.
 
  67       "\" tracking_mode=\"".$user->tracking_mode."\" project_required=\"".$user->project_required."\" task_required=\"".$user->task_required.
 
  68       "\" record_type=\"".$user->record_type."\" bcc_email=\"".$user->bcc_email.
 
  69       "\" plugins=\"".$user->plugins."\" lock_spec=\"".$user->lock_spec."\" workday_minutes=\"".$user->workday_minutes.
 
  70       "\" config=\"".$user->config.
 
  72     fwrite($file, "  <name><![CDATA[".$user->team."]]></name>\n");
 
  73     fwrite($file, "</team>\n");
 
  76     $roles = $this->getRoles();
 
  77     foreach ($roles as $key=>$role_item)
 
  78       $this->roleMap[$role_item['id']] = $key + 1;
 
  81     $users = $this->getUsers();
 
  82     foreach ($users as $key=>$user_item)
 
  83       $this->userMap[$user_item['id']] = $key + 1;
 
  85     // Prepare project map.
 
  86     $projects = ttTeamHelper::getAllProjects($user->group_id, true);
 
  87     foreach ($projects as $key=>$project_item)
 
  88       $this->projectMap[$project_item['id']] = $key + 1;
 
  91     $tasks = ttTeamHelper::getAllTasks($user->group_id, true);
 
  92     foreach ($tasks as $key=>$task_item)
 
  93       $this->taskMap[$task_item['id']] = $key + 1;
 
  95     // Prepare client map.
 
  96     $clients = ttTeamHelper::getAllClients($user->group_id, true);
 
  97     foreach ($clients as $key=>$client_item)
 
  98       $this->clientMap[$client_item['id']] = $key + 1;
 
 100     // Prepare invoice map.
 
 101     $invoices = ttTeamHelper::getAllInvoices();
 
 102     foreach ($invoices as $key=>$invoice_item)
 
 103       $this->invoiceMap[$invoice_item['id']] = $key + 1;
 
 105     // Prepare custom fields map.
 
 106     $custom_fields = ttTeamHelper::getAllCustomFields($user->group_id);
 
 107     foreach ($custom_fields as $key=>$custom_field)
 
 108       $this->customFieldMap[$custom_field['id']] = $key + 1;
 
 110     // Prepare custom field options map.
 
 111     $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($user->group_id);
 
 112     foreach ($custom_field_options as $key=>$option)
 
 113       $this->customFieldOptionMap[$option['id']] = $key + 1;
 
 116     fwrite($file, "<roles>\n");
 
 117     foreach ($roles as $role) {
 
 118       fwrite($file, "  <role id=\"".$this->roleMap[$role['id']]."\" rank=\"".$role['rank']."\"".
 
 119         " rights=\"".$role['rights']."\" status=\"".$role['status']."\">\n");
 
 120       fwrite($file, "    <name><![CDATA[".$role['name']."]]></name>\n");
 
 121       fwrite($file, "  </role>\n");
 
 123     fwrite($file, "</roles>\n");
 
 127     fwrite($file, "<users>\n");
 
 128     foreach ($users as $user_item) {
 
 129       $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
 
 130       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");
 
 131       fwrite($file, "    <name><![CDATA[".$user_item['name']."]]></name>\n");
 
 132       fwrite($file, "  </user>\n");
 
 134     fwrite($file, "</users>\n");
 
 137     fwrite($file, "<tasks>\n");
 
 138     foreach ($tasks as $task_item) {
 
 139       fwrite($file, "  <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
 
 140       fwrite($file, "    <name><![CDATA[".$task_item['name']."]]></name>\n");
 
 141       fwrite($file, "    <description><![CDATA[".$task_item['description']."]]></description>\n");
 
 142       fwrite($file, "  </task>\n");
 
 144     fwrite($file, "</tasks>\n");
 
 148     fwrite($file, "<projects>\n");
 
 149     foreach ($projects as $project_item) {
 
 150       if($project_item['tasks']){
 
 151         $tasks = explode(',', $project_item['tasks']);
 
 152         $tasks_mapped = array();
 
 153         foreach ($tasks as $item)
 
 154           $tasks_mapped[] = $this->taskMap[$item];
 
 155         $tasks_str = implode(',', $tasks_mapped);
 
 157       fwrite($file, "  <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
 
 158       fwrite($file, "    <name><![CDATA[".$project_item['name']."]]></name>\n");
 
 159       fwrite($file, "    <description><![CDATA[".$project_item['description']."]]></description>\n");
 
 160       fwrite($file, "  </project>\n");
 
 162     fwrite($file, "</projects>\n");
 
 165     // Write user to project binds.
 
 166     fwrite($file, "<user_project_binds>\n");
 
 167     $user_binds = ttTeamHelper::getUserToProjectBinds($user->group_id);
 
 168     foreach ($user_binds as $bind) {
 
 169       $user_id = $this->userMap[$bind['user_id']];
 
 170       $project_id = $this->projectMap[$bind['project_id']];
 
 171       fwrite($file, "  <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
 
 173     fwrite($file, "</user_project_binds>\n");
 
 177     fwrite($file, "<clients>\n");
 
 178     foreach ($clients as $client_item) {
 
 179       if($client_item['projects']){
 
 180         $projects = explode(',', $client_item['projects']);
 
 181         $projects_mapped = array();
 
 182         foreach ($projects as $item)
 
 183           $projects_mapped[] = $this->projectMap[$item];
 
 184         $projects_str = implode(',', $projects_mapped);
 
 186       fwrite($file, "  <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
 
 187       fwrite($file, "    <name><![CDATA[".$client_item['name']."]]></name>\n");
 
 188       fwrite($file, "    <address><![CDATA[".$client_item['address']."]]></address>\n");
 
 189       fwrite($file, "  </client>\n");
 
 191     fwrite($file, "</clients>\n");
 
 195     fwrite($file, "<invoices>\n");
 
 196     foreach ($invoices as $invoice_item) {
 
 197       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");
 
 198       fwrite($file, "    <name><![CDATA[".$invoice_item['name']."]]></name>\n");
 
 199       fwrite($file, "  </invoice>\n");
 
 201     fwrite($file, "</invoices>\n");
 
 204     // Write custom fields.
 
 205     fwrite($file, "<custom_fields>\n");
 
 206     foreach ($custom_fields as $custom_field) {
 
 207       fwrite($file, "  <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
 
 208       fwrite($file, "    <label><![CDATA[".$custom_field['label']."]]></label>\n");
 
 209       fwrite($file, "  </custom_field>\n");
 
 211     fwrite($file, "</custom_fields>\n");
 
 212     unset($custom_fields);
 
 214     // Write custom field options.
 
 215     fwrite($file, "<custom_field_options>\n");
 
 216     foreach ($custom_field_options as $option) {
 
 217       fwrite($file, "  <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
 
 218       fwrite($file, "    <value><![CDATA[".$option['value']."]]></value>\n");
 
 219       fwrite($file, "  </custom_field_option>\n");
 
 221     fwrite($file, "</custom_field_options>\n");
 
 222     unset($custom_field_options);
 
 224     // Write monthly quotas.
 
 225     $quotas = ttTeamHelper::getMonthlyQuotas($user->group_id);
 
 226     fwrite($file, "<monthly_quotas>\n");
 
 227     foreach ($quotas as $quota) {
 
 228       fwrite($file, "  <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" minutes=\"".$quota['minutes']."\"/>\n");
 
 230     fwrite($file, "</monthly_quotas>\n");
 
 232     // Write time log entries.
 
 233     fwrite($file, "<log>\n");
 
 235     foreach ($users as $user_item) {
 
 236       $records = ttTimeHelper::getAllRecords($user_item['id']);
 
 237       foreach ($records as $record) {
 
 239         $this->logMap[$record['id']] = $key;
 
 240         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");
 
 241         fwrite($file, "    <comment><![CDATA[".$record['comment']."]]></comment>\n");
 
 242         fwrite($file, "  </log_item>\n");
 
 245     fwrite($file, "</log>\n");
 
 248     // Write custom field log.
 
 249     $custom_field_log = ttTeamHelper::getCustomFieldLog($user->group_id);
 
 250     fwrite($file, "<custom_field_log>\n");
 
 251     foreach ($custom_field_log as $entry) {
 
 252       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");
 
 253       fwrite($file, "    <value><![CDATA[".$entry['value']."]]></value>\n");
 
 254       fwrite($file, "  </custom_field_log_entry>\n");
 
 256     fwrite($file, "</custom_field_log>\n");
 
 257     unset($custom_field_log);
 
 259     // Write expense items.
 
 260     $expense_items = ttTeamHelper::getExpenseItems($user->group_id);
 
 261     fwrite($file, "<expense_items>\n");
 
 262     foreach ($expense_items as $expense_item) {
 
 263       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");
 
 264       fwrite($file, "    <name><![CDATA[".$expense_item['name']."]]></name>\n");
 
 265       fwrite($file, "  </expense_item>\n");
 
 267     fwrite($file, "</expense_items>\n");
 
 268     unset($expense_items);
 
 270     // Write fav reports.
 
 271     fwrite($file, "<fav_reports>\n");
 
 272     $fav_reports = ttTeamHelper::getFavReports($user->group_id);
 
 273     foreach ($fav_reports as $fav_report) {
 
 275       if (strlen($fav_report['users']) > 0) {
 
 276         $arr = explode(',', $fav_report['users']);
 
 277         foreach ($arr as $k=>$v) {
 
 278           if (array_key_exists($arr[$k], $this->userMap))
 
 279             $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
 
 282       fwrite($file, "  <fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
 
 283         " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
 
 284         " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
 
 285         " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
 
 286         " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
 
 287         " billable=\"".$fav_report['billable']."\"".
 
 288         " users=\"".$user_list."\"".
 
 289         " period=\"".$fav_report['period']."\"".
 
 290         " period_start=\"".$fav_report['period_start']."\"".
 
 291         " period_end=\"".$fav_report['period_end']."\"".
 
 292         " show_client=\"".$fav_report['show_client']."\"".
 
 293         " show_invoice=\"".$fav_report['show_invoice']."\"".
 
 294         " show_paid=\"".$fav_report['show_paid']."\"".
 
 295         " show_ip=\"".$fav_report['show_ip']."\"".
 
 296         " show_project=\"".$fav_report['show_project']."\"".
 
 297         " show_start=\"".$fav_report['show_start']."\"".
 
 298         " show_duration=\"".$fav_report['show_duration']."\"".
 
 299         " show_cost=\"".$fav_report['show_cost']."\"".
 
 300         " show_task=\"".$fav_report['show_task']."\"".
 
 301         " show_end=\"".$fav_report['show_end']."\"".
 
 302         " show_note=\"".$fav_report['show_note']."\"".
 
 303         " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
 
 304         " group_by=\"".$fav_report['group_by']."\"".
 
 305         " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
 
 306       fwrite($file, "    <name><![CDATA[".$fav_report["name"]."]]></name>\n");
 
 307       fwrite($file, "  </fav_report>\n");
 
 309     fwrite($file, "</fav_reports>\n");
 
 314     $this->roleMap = array();
 
 315     $this->userMap = array();
 
 316     $this->projectMap = array();
 
 317     $this->taskMap = array();
 
 319     fwrite($file, "</pack>\n");
 
 323       $this->fileName = tempnam($dirName, 'tt');
 
 324       $this->compress($tmp_file, $this->fileName);
 
 327       $this->fileName = $tmp_file;
 
 332   // getFileName - returns file name.
 
 333   function getFileName() {
 
 334     return $this->fileName;
 
 337   // compress - compresses the content of the $in file into $out file.
 
 338   function compress($in, $out) {
 
 339     // Initial checks of file names and permissions.
 
 340     if (!file_exists($in) || !is_readable ($in))
 
 342     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
 
 345     $in_file = fopen($in, 'rb');
 
 347     if (function_exists('bzopen')) {
 
 348       if (!$out_file = bzopen($out, 'w'))
 
 351       while (!feof ($in_file)) {
 
 352         $buffer = fread($in_file, 4096);
 
 353         bzwrite($out_file, $buffer, 4096);
 
 362    * Note about the utility functions below.
 
 363    * We have roughly 4 groups of operations:
 
 364    *   1) Regular system usage for tracking time, etc.
 
 365    *   2) Registration process - used infrequently.
 
 366    *   3) Admin usage - used infrequently.
 
 367    *   4) Export - used infrequently.
 
 369    * It is tempting to have a generic function to get things done for
 
 370    * all situations. However, as registration, export and admin access are one-off
 
 371    * operations, while regular system usage is daily and must be efficient,
 
 372    * the current approach is to have SEPARATE functions for each mode.
 
 374    * This is because each mode requires a slightly different approach,
 
 375    * and we don't want to over-complicate things.
 
 378   // getRoles - obtains all roles defined for team.
 
 379   function getRoles() {
 
 381     $mdb2 = getConnection();
 
 384     $sql = "select * from tt_roles where group_id = $user->group_id";
 
 385     $res = $mdb2->query($sql);
 
 387     if (!is_a($res, 'PEAR_Error')) {
 
 388       while ($val = $res->fetchRow()) {
 
 396   // The getUsers obtains all users in team for the purpose of export.
 
 397   function getUsers() {
 
 399     $mdb2 = getConnection();
 
 401     $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.
 
 402     $res = $mdb2->query($sql);
 
 404     if (!is_a($res, 'PEAR_Error')) {
 
 405       while ($val = $res->fetchRow()) {