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 $projectMap  = array(); // Project ids.
 
  40   var $taskMap     = array(); // Task ids.
 
  41   var $clientMap   = array(); // Client ids.
 
  42   var $invoiceMap  = array(); // Invoice ids.
 
  43   var $customFieldMap       = array(); // Custom field ids.
 
  44   var $customFieldOptionMap = array(); // Custop field option ids.
 
  45   var $logMap      = array(); // Time log ids.
 
  47   // createDataFile creates a file with all data for a given team.
 
  48   function createDataFile($compress = false) {
 
  51     // Create a temporary file.
 
  52     $dirName = dirname(TEMPLATE_DIR . '_c/.');
 
  53     $tmp_file = tempnam($dirName, 'tt');
 
  55     // Open the file for writing.
 
  56     $file = fopen($tmp_file, 'wb');
 
  57     if (!$file) return false;
 
  59     // Write XML to the file.
 
  60     fwrite($file, "<?xml version=\"1.0\"?>\n");
 
  61     fwrite($file, "<pack>\n");
 
  64     fwrite($file, "<team currency=\"".$user->currency."\" decimal_mark=\"".$user->decimal_mark."\" lang=\"".$user->lang.
 
  65       "\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format."\" week_start=\"".$user->week_start.
 
  66       "\" tracking_mode=\"".$user->tracking_mode."\" project_required=\"".$user->project_required."\" task_required=\"".$user->task_required.
 
  67       "\" record_type=\"".$user->record_type."\" bcc_email=\"".$user->bcc_email.
 
  68       "\" plugins=\"".$user->plugins."\" lock_spec=\"".$user->lock_spec."\" workday_minutes=\"".$user->workday_minutes.
 
  69       "\" config=\"".$user->config.
 
  71     fwrite($file, "  <name><![CDATA[".$user->team."]]></name>\n");
 
  72     fwrite($file, "</team>\n");
 
  75     $users = ttTeamHelper::getAllUsers($user->team_id, true);
 
  76     foreach ($users as $key=>$user_item)
 
  77       $this->userMap[$user_item['id']] = $key + 1;
 
  79     // Prepare project map.
 
  80     $projects = ttTeamHelper::getAllProjects($user->team_id, true);
 
  81     foreach ($projects as $key=>$project_item)
 
  82       $this->projectMap[$project_item['id']] = $key + 1;
 
  85     $tasks = ttTeamHelper::getAllTasks($user->team_id, true);
 
  86     foreach ($tasks as $key=>$task_item)
 
  87       $this->taskMap[$task_item['id']] = $key + 1;
 
  89     // Prepare client map.
 
  90     $clients = ttTeamHelper::getAllClients($user->team_id, true);
 
  91     foreach ($clients as $key=>$client_item)
 
  92       $this->clientMap[$client_item['id']] = $key + 1;
 
  94     // Prepare invoice map.
 
  95     $invoices = ttTeamHelper::getAllInvoices();
 
  96     foreach ($invoices as $key=>$invoice_item)
 
  97       $this->invoiceMap[$invoice_item['id']] = $key + 1;
 
  99     // Prepare custom fields map.
 
 100     $custom_fields = ttTeamHelper::getAllCustomFields($user->team_id);
 
 101     foreach ($custom_fields as $key=>$custom_field)
 
 102       $this->customFieldMap[$custom_field['id']] = $key + 1;
 
 104     // Prepare custom field options map.
 
 105     $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($user->team_id);
 
 106     foreach ($custom_field_options as $key=>$option)
 
 107       $this->customFieldOptionMap[$option['id']] = $key + 1;
 
 110     fwrite($file, "<users>\n");
 
 111     foreach ($users as $user_item) {
 
 112       fwrite($file, "  <user id=\"".$this->userMap[$user_item['id']]."\" login=\"".htmlentities($user_item['login'])."\" password=\"".$user_item['password']."\" role=\"".$user_item['role']."\" client_id=\"".$this->clientMap[$user_item['client_id']]."\" rate=\"".$user_item['rate']."\" email=\"".$user_item['email']."\" status=\"".$user_item['status']."\">\n");
 
 113       fwrite($file, "    <name><![CDATA[".$user_item['name']."]]></name>\n");
 
 114       fwrite($file, "  </user>\n");
 
 116     fwrite($file, "</users>\n");
 
 119     fwrite($file, "<tasks>\n");
 
 120     foreach ($tasks as $task_item) {
 
 121       fwrite($file, "  <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
 
 122       fwrite($file, "    <name><![CDATA[".$task_item['name']."]]></name>\n");
 
 123       fwrite($file, "    <description><![CDATA[".$task_item['description']."]]></description>\n");
 
 124       fwrite($file, "  </task>\n");
 
 126     fwrite($file, "</tasks>\n");
 
 130     fwrite($file, "<projects>\n");
 
 131     foreach ($projects as $project_item) {
 
 132       if($project_item['tasks']){
 
 133         $tasks = explode(',', $project_item['tasks']);
 
 134         $tasks_mapped = array();
 
 135         foreach ($tasks as $item)
 
 136           $tasks_mapped[] = $this->taskMap[$item];
 
 137         $tasks_str = implode(',', $tasks_mapped);
 
 139       fwrite($file, "  <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
 
 140       fwrite($file, "    <name><![CDATA[".$project_item['name']."]]></name>\n");
 
 141       fwrite($file, "    <description><![CDATA[".$project_item['description']."]]></description>\n");
 
 142       fwrite($file, "  </project>\n");
 
 144     fwrite($file, "</projects>\n");
 
 147     // Write user to project binds.
 
 148     fwrite($file, "<user_project_binds>\n");
 
 149     $user_binds = ttTeamHelper::getUserToProjectBinds($user->team_id);
 
 150     foreach ($user_binds as $bind) {
 
 151       $user_id = $this->userMap[$bind['user_id']];
 
 152       $project_id = $this->projectMap[$bind['project_id']];
 
 153       fwrite($file, "  <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
 
 155     fwrite($file, "</user_project_binds>\n");
 
 159     fwrite($file, "<clients>\n");
 
 160     foreach ($clients as $client_item) {
 
 161       if($client_item['projects']){
 
 162         $projects = explode(',', $client_item['projects']);
 
 163         $projects_mapped = array();
 
 164         foreach ($projects as $item)
 
 165           $projects_mapped[] = $this->projectMap[$item];
 
 166         $projects_str = implode(',', $projects_mapped);
 
 168       fwrite($file, "  <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
 
 169       fwrite($file, "    <name><![CDATA[".$client_item['name']."]]></name>\n");
 
 170       fwrite($file, "    <address><![CDATA[".$client_item['address']."]]></address>\n");
 
 171       fwrite($file, "  </client>\n");
 
 173     fwrite($file, "</clients>\n");
 
 177     fwrite($file, "<invoices>\n");
 
 178     foreach ($invoices as $invoice_item) {
 
 179       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");
 
 180       fwrite($file, "    <name><![CDATA[".$invoice_item['name']."]]></name>\n");
 
 181       fwrite($file, "  </invoice>\n");
 
 183     fwrite($file, "</invoices>\n");
 
 186     // Write custom fields.
 
 187     fwrite($file, "<custom_fields>\n");
 
 188     foreach ($custom_fields as $custom_field) {
 
 189       fwrite($file, "  <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
 
 190       fwrite($file, "    <label><![CDATA[".$custom_field['label']."]]></label>\n");
 
 191       fwrite($file, "  </custom_field>\n");
 
 193     fwrite($file, "</custom_fields>\n");
 
 194     unset($custom_fields);
 
 196     // Write custom field options.
 
 197     fwrite($file, "<custom_field_options>\n");
 
 198     foreach ($custom_field_options as $option) {
 
 199       fwrite($file, "  <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
 
 200       fwrite($file, "    <value><![CDATA[".$option['value']."]]></value>\n");
 
 201       fwrite($file, "  </custom_field_option>\n");
 
 203     fwrite($file, "</custom_field_options>\n");
 
 204     unset($custom_field_options);
 
 206     // Write monthly quotas.
 
 207     $quotas = ttTeamHelper::getMonthlyQuotas($user->team_id);
 
 208     fwrite($file, "<monthly_quotas>\n");
 
 209     foreach ($quotas as $quota) {
 
 210       fwrite($file, "  <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" minutes=\"".$quota['minutes']."\"/>\n");
 
 212     fwrite($file, "</monthly_quotas>\n");
 
 214     // Write time log entries.
 
 215     fwrite($file, "<log>\n");
 
 217     foreach ($users as $user_item) {
 
 218       $records = ttTimeHelper::getAllRecords($user_item['id']);
 
 219       foreach ($records as $record) {
 
 221         $this->logMap[$record['id']] = $key;
 
 222         fwrite($file, "  <log_item id=\"$key\" timestamp=\"".$record['timestamp']."\" 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");
 
 223         fwrite($file, "    <comment><![CDATA[".$record['comment']."]]></comment>\n");
 
 224         fwrite($file, "  </log_item>\n");
 
 227     fwrite($file, "</log>\n");
 
 230     // Write custom field log.
 
 231     $custom_field_log = ttTeamHelper::getCustomFieldLog($user->team_id);
 
 232     fwrite($file, "<custom_field_log>\n");
 
 233     foreach ($custom_field_log as $entry) {
 
 234       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");
 
 235       fwrite($file, "    <value><![CDATA[".$entry['value']."]]></value>\n");
 
 236       fwrite($file, "  </custom_field_log_entry>\n");
 
 238     fwrite($file, "</custom_field_log>\n");
 
 239     unset($custom_field_log);
 
 241     // Write expense items.
 
 242     $expense_items = ttTeamHelper::getExpenseItems($user->team_id);
 
 243     fwrite($file, "<expense_items>\n");
 
 244     foreach ($expense_items as $expense_item) {
 
 245       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");
 
 246       fwrite($file, "    <name><![CDATA[".$expense_item['name']."]]></name>\n");
 
 247       fwrite($file, "  </expense_item>\n");
 
 249     fwrite($file, "</expense_items>\n");
 
 250     unset($expense_items);
 
 252     // Write fav reports.
 
 253     fwrite($file, "<fav_reports>\n");
 
 254     $fav_reports = ttTeamHelper::getFavReports($user->team_id);
 
 255     foreach ($fav_reports as $fav_report) {
 
 257       if (strlen($fav_report['users']) > 0) {
 
 258         $arr = explode(',', $fav_report['users']);
 
 259         foreach ($arr as $k=>$v) {
 
 260           if (array_key_exists($arr[$k], $this->userMap))
 
 261             $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
 
 264       fwrite($file, "\t<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
 
 265         " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
 
 266         " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
 
 267         " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
 
 268         " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
 
 269         " billable=\"".$fav_report['billable']."\"".
 
 270         " users=\"".$user_list."\"".
 
 271         " period=\"".$fav_report['period']."\"".
 
 272         " period_start=\"".$fav_report['period_start']."\"".
 
 273         " period_end=\"".$fav_report['period_end']."\"".
 
 274         " show_client=\"".$fav_report['show_client']."\"".
 
 275         " show_invoice=\"".$fav_report['show_invoice']."\"".
 
 276         " show_project=\"".$fav_report['show_project']."\"".
 
 277         " show_start=\"".$fav_report['show_start']."\"".
 
 278         " show_duration=\"".$fav_report['show_duration']."\"".
 
 279         " show_cost=\"".$fav_report['show_cost']."\"".
 
 280         " show_task=\"".$fav_report['show_task']."\"".
 
 281         " show_end=\"".$fav_report['show_end']."\"".
 
 282         " show_note=\"".$fav_report['show_note']."\"".
 
 283         " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
 
 284         " group_by=\"".$fav_report['group_by']."\"".
 
 285         " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
 
 286       fwrite($file, "\t\t<name><![CDATA[".$fav_report["name"]."]]></name>\n");
 
 287       fwrite($file, "\t</fav_report>\n");
 
 289     fwrite($file, "</fav_reports>\n");
 
 293     fwrite($file, "<roles>\n");
 
 294     $roles = ttTeamHelper::getAllRoles($user->team_id);
 
 295     foreach ($roles as $role) {
 
 296       fwrite($file, "\t<role rank=\"".$role['rank']."\"".
 
 297         " rights=\"".$role['rights']."\">\n");
 
 298       fwrite($file, "\t\t<name><![CDATA[".$role['name']."]]></name>\n");
 
 299       fwrite($file, "\t</role>\n");
 
 301     fwrite($file, "</roles>\n");
 
 306     $this->userMap = array();
 
 307     $this->projectMap = array();
 
 308     $this->taskMap = array();
 
 310     fwrite($file, "</pack>\n");
 
 314       $this->fileName = tempnam($dirName, 'tt');
 
 315       $this->compress($tmp_file, $this->fileName);
 
 318       $this->fileName = $tmp_file;
 
 323   // getFileName - returns file name.
 
 324   function getFileName() {
 
 325     return $this->fileName;
 
 328   // compress - compresses the content of the $in file into $out file.
 
 329   function compress($in, $out) {
 
 330     // Initial checks of file names and permissions.
 
 331     if (!file_exists($in) || !is_readable ($in))
 
 333     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
 
 336     $in_file = fopen($in, 'rb');
 
 338     if (function_exists('bzopen')) {
 
 339       if (!$out_file = bzopen($out, 'w'))
 
 342       while (!feof ($in_file)) {
 
 343         $buffer = fread($in_file, 4096);
 
 344         bzwrite($out_file, $buffer, 4096);