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."\" record_type=\"".$user->record_type."\">\n");
68 fwrite($file, " <name><![CDATA[".$user->team."]]></name>\n");
69 fwrite($file, " <address><![CDATA[".$user->address."]]></address>\n");
70 fwrite($file, "</team>\n");
73 $users = ttTeamHelper::getAllUsers($user->team_id, true);
74 foreach ($users as $key=>$user_item)
75 $this->userMap[$user_item['id']] = $key + 1;
77 // Prepare project map.
78 $projects = ttTeamHelper::getAllProjects($user->team_id, true);
79 foreach ($projects as $key=>$project_item)
80 $this->projectMap[$project_item['id']] = $key + 1;
83 $tasks = ttTeamHelper::getAllTasks($user->team_id, true);
84 foreach ($tasks as $key=>$task_item)
85 $this->taskMap[$task_item['id']] = $key + 1;
87 // Prepare client map.
88 $clients = ttTeamHelper::getAllClients($user->team_id, true);
89 foreach ($clients as $key=>$client_item)
90 $this->clientMap[$client_item['id']] = $key + 1;
92 // Prepare invoice map.
93 $invoices = ttTeamHelper::getAllInvoices();
94 foreach ($invoices as $key=>$invoice_item)
95 $this->invoiceMap[$invoice_item['id']] = $key + 1;
97 // Prepare custom fields map.
98 $custom_fields = ttTeamHelper::getAllCustomFields($user->team_id);
99 foreach ($custom_fields as $key=>$custom_field)
100 $this->customFieldMap[$custom_field['id']] = $key + 1;
102 // Prepare custom field options map.
103 $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($user->team_id);
104 foreach ($custom_field_options as $key=>$option)
105 $this->customFieldOptionMap[$option['id']] = $key + 1;
108 fwrite($file, "<users>\n");
109 foreach ($users as $user_item) {
110 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");
111 fwrite($file, " <name><![CDATA[".$user_item['name']."]]></name>\n");
112 fwrite($file, " </user>\n");
114 fwrite($file, "</users>\n");
117 fwrite($file, "<tasks>\n");
118 foreach ($tasks as $task_item) {
119 fwrite($file, " <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
120 fwrite($file, " <name><![CDATA[".$task_item['name']."]]></name>\n");
121 fwrite($file, " <description><![CDATA[".$task_item['description']."]]></description>\n");
122 fwrite($file, " </task>\n");
124 fwrite($file, "</tasks>\n");
128 fwrite($file, "<projects>\n");
129 foreach ($projects as $project_item) {
130 if($project_item['tasks']){
131 $tasks = explode(',', $project_item['tasks']);
132 $tasks_mapped = array();
133 foreach ($tasks as $item)
134 $tasks_mapped[] = $this->taskMap[$item];
135 $tasks_str = implode(',', $tasks_mapped);
137 fwrite($file, " <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
138 fwrite($file, " <name><![CDATA[".$project_item['name']."]]></name>\n");
139 fwrite($file, " <description><![CDATA[".$project_item['description']."]]></description>\n");
140 fwrite($file, " </project>\n");
142 fwrite($file, "</projects>\n");
145 // Write user to project binds.
146 fwrite($file, "<user_project_binds>\n");
147 $user_binds = ttTeamHelper::getUserToProjectBinds($user->team_id);
148 foreach ($user_binds as $bind) {
149 $user_id = $this->userMap[$bind['user_id']];
150 $project_id = $this->projectMap[$bind['project_id']];
151 fwrite($file, " <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
153 fwrite($file, "</user_project_binds>\n");
157 fwrite($file, "<clients>\n");
158 foreach ($clients as $client_item) {
159 if($client_item['projects']){
160 $projects = explode(',', $client_item['projects']);
161 $projects_mapped = array();
162 foreach ($projects as $item)
163 $projects_mapped[] = $this->projectMap[$item];
164 $projects_str = implode(',', $projects_mapped);
166 fwrite($file, " <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
167 fwrite($file, " <name><![CDATA[".$client_item['name']."]]></name>\n");
168 fwrite($file, " <address><![CDATA[".$client_item['address']."]]></address>\n");
169 fwrite($file, " </client>\n");
171 fwrite($file, "</clients>\n");
175 fwrite($file, "<invoices>\n");
176 foreach ($invoices as $invoice_item) {
177 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");
178 fwrite($file, " <name><![CDATA[".$invoice_item['name']."]]></name>\n");
179 fwrite($file, " </invoice>\n");
181 fwrite($file, "</invoices>\n");
184 // Write custom fields.
185 fwrite($file, "<custom_fields>\n");
186 foreach ($custom_fields as $custom_field) {
187 fwrite($file, " <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
188 fwrite($file, " <label><![CDATA[".$custom_field['label']."]]></label>\n");
189 fwrite($file, " </custom_field>\n");
191 fwrite($file, "</custom_fields>\n");
192 unset($custom_fields);
194 // Write custom field options.
195 fwrite($file, "<custom_field_options>\n");
196 foreach ($custom_field_options as $option) {
197 fwrite($file, " <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
198 fwrite($file, " <value><![CDATA[".$option['value']."]]></value>\n");
199 fwrite($file, " </custom_field_option>\n");
201 fwrite($file, "</custom_field_options>\n");
202 unset($custom_field_options);
204 // Write monthly quotas.
205 $quotas = ttTeamHelper::getMonthlyQuotas($user->team_id);
206 fwrite($file, "<monthly_quotas>\n");
207 foreach ($quotas as $quota) {
208 fwrite($file, " <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" quota=\"".$quota['quota']."\"/>\n");
210 fwrite($file, "</monthly_quotas>\n");
212 // Write time log entries.
213 fwrite($file, "<log>\n");
215 foreach ($users as $user_item) {
216 $records = ttTimeHelper::getAllRecords($user_item['id']);
217 foreach ($records as $record) {
219 $this->logMap[$record['id']] = $key;
220 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");
221 fwrite($file, " <comment><![CDATA[".$record['comment']."]]></comment>\n");
222 fwrite($file, " </log_item>\n");
225 fwrite($file, "</log>\n");
228 // Write custom field log.
229 $custom_field_log = ttTeamHelper::getCustomFieldLog($user->team_id);
230 fwrite($file, "<custom_field_log>\n");
231 foreach ($custom_field_log as $entry) {
232 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");
233 fwrite($file, " <value><![CDATA[".$entry['value']."]]></value>\n");
234 fwrite($file, " </custom_field_log_entry>\n");
236 fwrite($file, "</custom_field_log>\n");
237 unset($custom_field_log);
239 // Write expense items.
240 $expense_items = ttTeamHelper::getExpenseItems($user->team_id);
241 fwrite($file, "<expense_items>\n");
242 foreach ($expense_items as $expense_item) {
243 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");
244 fwrite($file, " <name><![CDATA[".$expense_item['name']."]]></name>\n");
245 fwrite($file, " </expense_item>\n");
247 fwrite($file, "</expense_items>\n");
248 unset($expense_items);
250 // Write fav reports.
251 fwrite($file, "<fav_reports>\n");
252 $fav_reports = ttTeamHelper::getFavReports($user->team_id);
253 foreach ($fav_reports as $fav_report) {
255 if (strlen($fav_report['users']) > 0) {
256 $arr = explode(',', $fav_report['users']);
257 foreach ($arr as $k=>$v) {
258 if (array_key_exists($arr[$k], $this->userMap))
259 $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
262 fwrite($file, "\t<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
263 " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
264 " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
265 " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
266 " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
267 " billable=\"".$fav_report['billable']."\"".
268 " users=\"".$user_list."\"".
269 " period=\"".$fav_report['period']."\"".
270 " period_start=\"".$fav_report['period_start']."\"".
271 " period_end=\"".$fav_report['period_end']."\"".
272 " show_client=\"".$fav_report['show_client']."\"".
273 " show_invoice=\"".$fav_report['show_invoice']."\"".
274 " show_project=\"".$fav_report['show_project']."\"".
275 " show_start=\"".$fav_report['show_start']."\"".
276 " show_duration=\"".$fav_report['show_duration']."\"".
277 " show_cost=\"".$fav_report['show_cost']."\"".
278 " show_task=\"".$fav_report['show_task']."\"".
279 " show_end=\"".$fav_report['show_end']."\"".
280 " show_note=\"".$fav_report['show_note']."\"".
281 " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
282 " group_by=\"".$fav_report['group_by']."\"".
283 " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
284 fwrite($file, "\t\t<name><![CDATA[".$fav_report["name"]."]]></name>\n");
285 fwrite($file, "\t</fav_report>\n");
287 fwrite($file, "</fav_reports>\n");
292 $this->userMap = array();
293 $this->projectMap = array();
294 $this->taskMap = array();
296 fwrite($file, "</pack>\n");
300 $this->fileName = tempnam($dirName, 'tt');
301 $this->compress($tmp_file, $this->fileName);
304 $this->fileName = $tmp_file;
309 // getFileName - returns file name.
310 function getFileName() {
311 return $this->fileName;
314 // compress - compresses the content of the $in file into $out file.
315 function compress($in, $out) {
316 // Initial checks of file names and permissions.
317 if (!file_exists($in) || !is_readable ($in))
319 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
322 $in_file = fopen($in, 'rb');
324 if (function_exists('bzopen')) {
325 if (!$out_file = bzopen($out, 'w'))
328 while (!feof ($in_file)) {
329 $buffer = fread($in_file, 4096);
330 bzwrite($out_file, $buffer, 4096);