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."\" lock_spec=\"".$user->lock_spec."\" lang=\"".$user->lang.
 
  65       "\" decimal_mark=\"".$user->decimal_mark."\" date_format=\"".$user->date_format."\" time_format=\"".$user->time_format.
 
  66       "\" week_start=\"".$user->week_start."\" workday_hours=\"".$user->workday_hours.
 
  67       "\" plugins=\"".$user->plugins."\" tracking_mode=\"".$user->tracking_mode."\" task_required=\"".$user->task_required.
 
  68       "\" record_type=\"".$user->record_type."\">\n");
 
  69     fwrite($file, "  <name><![CDATA[".$user->team."]]></name>\n");
 
  70     fwrite($file, "  <address><![CDATA[".$user->address."]]></address>\n");
 
  71     fwrite($file, "</team>\n");
 
  74     $users = ttTeamHelper::getAllUsers($user->team_id, true);
 
  75     foreach ($users as $key=>$user_item)
 
  76       $this->userMap[$user_item['id']] = $key + 1;
 
  78     // Prepare project map.
 
  79     $projects = ttTeamHelper::getAllProjects($user->team_id, true);
 
  80     foreach ($projects as $key=>$project_item)
 
  81       $this->projectMap[$project_item['id']] = $key + 1;
 
  84     $tasks = ttTeamHelper::getAllTasks($user->team_id, true);
 
  85     foreach ($tasks as $key=>$task_item)
 
  86       $this->taskMap[$task_item['id']] = $key + 1;
 
  88     // Prepare client map.
 
  89     $clients = ttTeamHelper::getAllClients($user->team_id, true);
 
  90     foreach ($clients as $key=>$client_item)
 
  91       $this->clientMap[$client_item['id']] = $key + 1;
 
  93     // Prepare invoice map.
 
  94     $invoices = ttTeamHelper::getAllInvoices();
 
  95     foreach ($invoices as $key=>$invoice_item)
 
  96       $this->invoiceMap[$invoice_item['id']] = $key + 1;
 
  98     // Prepare custom fields map.
 
  99     $custom_fields = ttTeamHelper::getAllCustomFields($user->team_id);
 
 100     foreach ($custom_fields as $key=>$custom_field)
 
 101       $this->customFieldMap[$custom_field['id']] = $key + 1;
 
 103     // Prepare custom field options map.
 
 104     $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($user->team_id);
 
 105     foreach ($custom_field_options as $key=>$option)
 
 106       $this->customFieldOptionMap[$option['id']] = $key + 1;
 
 109     fwrite($file, "<users>\n");
 
 110     foreach ($users as $user_item) {
 
 111       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");
 
 112       fwrite($file, "    <name><![CDATA[".$user_item['name']."]]></name>\n");
 
 113       fwrite($file, "  </user>\n");
 
 115     fwrite($file, "</users>\n");
 
 118     fwrite($file, "<tasks>\n");
 
 119     foreach ($tasks as $task_item) {
 
 120       fwrite($file, "  <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
 
 121       fwrite($file, "    <name><![CDATA[".$task_item['name']."]]></name>\n");
 
 122       fwrite($file, "    <description><![CDATA[".$task_item['description']."]]></description>\n");
 
 123       fwrite($file, "  </task>\n");
 
 125     fwrite($file, "</tasks>\n");
 
 129     fwrite($file, "<projects>\n");
 
 130     foreach ($projects as $project_item) {
 
 131       if($project_item['tasks']){
 
 132         $tasks = explode(',', $project_item['tasks']);
 
 133         $tasks_mapped = array();
 
 134         foreach ($tasks as $item)
 
 135           $tasks_mapped[] = $this->taskMap[$item];
 
 136         $tasks_str = implode(',', $tasks_mapped);
 
 138       fwrite($file, "  <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
 
 139       fwrite($file, "    <name><![CDATA[".$project_item['name']."]]></name>\n");
 
 140       fwrite($file, "    <description><![CDATA[".$project_item['description']."]]></description>\n");
 
 141       fwrite($file, "  </project>\n");
 
 143     fwrite($file, "</projects>\n");
 
 146     // Write user to project binds.
 
 147     fwrite($file, "<user_project_binds>\n");
 
 148     $user_binds = ttTeamHelper::getUserToProjectBinds($user->team_id);
 
 149     foreach ($user_binds as $bind) {
 
 150       $user_id = $this->userMap[$bind['user_id']];
 
 151       $project_id = $this->projectMap[$bind['project_id']];
 
 152       fwrite($file, "  <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
 
 154     fwrite($file, "</user_project_binds>\n");
 
 158     fwrite($file, "<clients>\n");
 
 159     foreach ($clients as $client_item) {
 
 160       if($client_item['projects']){
 
 161         $projects = explode(',', $client_item['projects']);
 
 162         $projects_mapped = array();
 
 163         foreach ($projects as $item)
 
 164           $projects_mapped[] = $this->projectMap[$item];
 
 165         $projects_str = implode(',', $projects_mapped);
 
 167       fwrite($file, "  <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
 
 168       fwrite($file, "    <name><![CDATA[".$client_item['name']."]]></name>\n");
 
 169       fwrite($file, "    <address><![CDATA[".$client_item['address']."]]></address>\n");
 
 170       fwrite($file, "  </client>\n");
 
 172     fwrite($file, "</clients>\n");
 
 176     fwrite($file, "<invoices>\n");
 
 177     foreach ($invoices as $invoice_item) {
 
 178       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");
 
 179       fwrite($file, "    <name><![CDATA[".$invoice_item['name']."]]></name>\n");
 
 180       fwrite($file, "  </invoice>\n");
 
 182     fwrite($file, "</invoices>\n");
 
 185     // Write custom fields.
 
 186     fwrite($file, "<custom_fields>\n");
 
 187     foreach ($custom_fields as $custom_field) {
 
 188       fwrite($file, "  <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
 
 189       fwrite($file, "    <label><![CDATA[".$custom_field['label']."]]></label>\n");
 
 190       fwrite($file, "  </custom_field>\n");
 
 192     fwrite($file, "</custom_fields>\n");
 
 193     unset($custom_fields);
 
 195     // Write custom field options.
 
 196     fwrite($file, "<custom_field_options>\n");
 
 197     foreach ($custom_field_options as $option) {
 
 198       fwrite($file, "  <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
 
 199       fwrite($file, "    <value><![CDATA[".$option['value']."]]></value>\n");
 
 200       fwrite($file, "  </custom_field_option>\n");
 
 202     fwrite($file, "</custom_field_options>\n");
 
 203     unset($custom_field_options);
 
 205     // Write monthly quotas.
 
 206     $quotas = ttTeamHelper::getMonthlyQuotas($user->team_id);
 
 207     fwrite($file, "<monthly_quotas>\n");
 
 208     foreach ($quotas as $quota) {
 
 209       fwrite($file, "  <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" quota=\"".$quota['quota']."\"/>\n");
 
 211     fwrite($file, "</monthly_quotas>\n");
 
 213     // Write time log entries.
 
 214     fwrite($file, "<log>\n");
 
 216     foreach ($users as $user_item) {
 
 217       $records = ttTimeHelper::getAllRecords($user_item['id']);
 
 218       foreach ($records as $record) {
 
 220         $this->logMap[$record['id']] = $key;
 
 221         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']."\" status=\"".$record['status']."\">\n");
 
 222         fwrite($file, "    <comment><![CDATA[".$record['comment']."]]></comment>\n");
 
 223         fwrite($file, "  </log_item>\n");
 
 226     fwrite($file, "</log>\n");
 
 229     // Write custom field log.
 
 230     $custom_field_log = ttTeamHelper::getCustomFieldLog($user->team_id);
 
 231     fwrite($file, "<custom_field_log>\n");
 
 232     foreach ($custom_field_log as $entry) {
 
 233       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");
 
 234       fwrite($file, "    <value><![CDATA[".$entry['value']."]]></value>\n");
 
 235       fwrite($file, "  </custom_field_log_entry>\n");
 
 237     fwrite($file, "</custom_field_log>\n");
 
 238     unset($custom_field_log);
 
 240     // Write expense items.
 
 241     $expense_items = ttTeamHelper::getExpenseItems($user->team_id);
 
 242     fwrite($file, "<expense_items>\n");
 
 243     foreach ($expense_items as $expense_item) {
 
 244       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']]."\" status=\"".$expense_item['status']."\">\n");
 
 245       fwrite($file, "    <name><![CDATA[".$expense_item['name']."]]></name>\n");
 
 246       fwrite($file, "  </expense_item>\n");
 
 248     fwrite($file, "</expense_items>\n");
 
 249     unset($expense_items);
 
 251     // Write fav reports.
 
 252     fwrite($file, "<fav_reports>\n");
 
 253     $fav_reports = ttTeamHelper::getFavReports($user->team_id);
 
 254     foreach ($fav_reports as $fav_report) {
 
 256       if (strlen($fav_report['users']) > 0) {
 
 257         $arr = explode(',', $fav_report['users']);
 
 258         foreach ($arr as $k=>$v) {
 
 259           if (array_key_exists($arr[$k], $this->userMap))
 
 260             $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
 
 263       fwrite($file, "\t<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
 
 264         " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
 
 265         " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
 
 266         " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
 
 267         " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
 
 268         " billable=\"".$fav_report['billable']."\"".
 
 269         " users=\"".$user_list."\"".
 
 270         " period=\"".$fav_report['period']."\"".
 
 271         " period_start=\"".$fav_report['period_start']."\"".
 
 272         " period_end=\"".$fav_report['period_end']."\"".
 
 273         " show_client=\"".$fav_report['show_client']."\"".
 
 274         " show_invoice=\"".$fav_report['show_invoice']."\"".
 
 275         " show_project=\"".$fav_report['show_project']."\"".
 
 276         " show_start=\"".$fav_report['show_start']."\"".
 
 277         " show_duration=\"".$fav_report['show_duration']."\"".
 
 278         " show_cost=\"".$fav_report['show_cost']."\"".
 
 279         " show_task=\"".$fav_report['show_task']."\"".
 
 280         " show_end=\"".$fav_report['show_end']."\"".
 
 281         " show_note=\"".$fav_report['show_note']."\"".
 
 282         " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
 
 283         " group_by=\"".$fav_report['group_by']."\"".
 
 284         " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
 
 285       fwrite($file, "\t\t<name><![CDATA[".$fav_report["name"]."]]></name>\n");
 
 286       fwrite($file, "\t</fav_report>\n");
 
 288     fwrite($file, "</fav_reports>\n");
 
 293     $this->userMap = array();
 
 294     $this->projectMap = array();
 
 295     $this->taskMap = array();
 
 297     fwrite($file, "</pack>\n");
 
 301       $this->fileName = tempnam($dirName, 'tt');
 
 302       $this->compress($tmp_file, $this->fileName);
 
 305       $this->fileName = $tmp_file;
 
 310   // getFileName - returns file name.
 
 311   function getFileName() {
 
 312     return $this->fileName;
 
 315   // compress - compresses the content of the $in file into $out file.
 
 316   function compress($in, $out) {
 
 317     // Initial checks of file names and permissions.
 
 318     if (!file_exists($in) || !is_readable ($in))
 
 320     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
 
 323     $in_file = fopen($in, 'rb');
 
 325     if (function_exists('bzopen')) {
 
 326       if (!$out_file = bzopen($out, 'w'))
 
 329       while (!feof ($in_file)) {
 
 330         $buffer = fread($in_file, 4096);
 
 331         bzwrite($out_file, $buffer, 4096);