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 group 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 group.
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, "<group 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->group."]]></name>\n");
73 fwrite($file, " <allow_ip><![CDATA[".$user->allow_ip."]]></allow_ip>\n");
74 fwrite($file, " <password_complexity><![CDATA[".$user->password_complexity."]]></password_complexity>\n");
75 fwrite($file, "</group>\n");
78 $roles = $this->getRoles();
79 foreach ($roles as $key=>$role_item)
80 $this->roleMap[$role_item['id']] = $key + 1;
83 $users = $this->getUsers();
84 foreach ($users as $key=>$user_item)
85 $this->userMap[$user_item['id']] = $key + 1;
87 // Prepare project map.
88 $projects = ttTeamHelper::getAllProjects($user->group_id, true);
89 foreach ($projects as $key=>$project_item)
90 $this->projectMap[$project_item['id']] = $key + 1;
93 $tasks = ttTeamHelper::getAllTasks($user->group_id, true);
94 foreach ($tasks as $key=>$task_item)
95 $this->taskMap[$task_item['id']] = $key + 1;
97 // Prepare client map.
98 $clients = ttTeamHelper::getAllClients($user->group_id, true);
99 foreach ($clients as $key=>$client_item)
100 $this->clientMap[$client_item['id']] = $key + 1;
102 // Prepare invoice map.
103 $invoices = ttTeamHelper::getAllInvoices();
104 foreach ($invoices as $key=>$invoice_item)
105 $this->invoiceMap[$invoice_item['id']] = $key + 1;
107 // Prepare custom fields map.
108 $custom_fields = ttTeamHelper::getAllCustomFields($user->group_id);
109 foreach ($custom_fields as $key=>$custom_field)
110 $this->customFieldMap[$custom_field['id']] = $key + 1;
112 // Prepare custom field options map.
113 $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($user->group_id);
114 foreach ($custom_field_options as $key=>$option)
115 $this->customFieldOptionMap[$option['id']] = $key + 1;
118 fwrite($file, "<roles>\n");
119 foreach ($roles as $role) {
120 fwrite($file, " <role id=\"".$this->roleMap[$role['id']]."\" rank=\"".$role['rank']."\"".
121 " rights=\"".$role['rights']."\" status=\"".$role['status']."\">\n");
122 fwrite($file, " <name><![CDATA[".$role['name']."]]></name>\n");
123 fwrite($file, " </role>\n");
125 fwrite($file, "</roles>\n");
129 fwrite($file, "<users>\n");
130 foreach ($users as $user_item) {
131 $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
132 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");
133 fwrite($file, " <name><![CDATA[".$user_item['name']."]]></name>\n");
134 fwrite($file, " </user>\n");
136 fwrite($file, "</users>\n");
139 fwrite($file, "<tasks>\n");
140 foreach ($tasks as $task_item) {
141 fwrite($file, " <task id=\"".$this->taskMap[$task_item['id']]."\" status=\"".$task_item['status']."\">\n");
142 fwrite($file, " <name><![CDATA[".$task_item['name']."]]></name>\n");
143 fwrite($file, " <description><![CDATA[".$task_item['description']."]]></description>\n");
144 fwrite($file, " </task>\n");
146 fwrite($file, "</tasks>\n");
150 fwrite($file, "<projects>\n");
151 foreach ($projects as $project_item) {
152 if($project_item['tasks']){
153 $tasks = explode(',', $project_item['tasks']);
154 $tasks_mapped = array();
155 foreach ($tasks as $item)
156 $tasks_mapped[] = $this->taskMap[$item];
157 $tasks_str = implode(',', $tasks_mapped);
159 fwrite($file, " <project id=\"".$this->projectMap[$project_item['id']]."\" tasks=\"".$tasks_str."\" status=\"".$project_item['status']."\">\n");
160 fwrite($file, " <name><![CDATA[".$project_item['name']."]]></name>\n");
161 fwrite($file, " <description><![CDATA[".$project_item['description']."]]></description>\n");
162 fwrite($file, " </project>\n");
164 fwrite($file, "</projects>\n");
167 // Write user to project binds.
168 fwrite($file, "<user_project_binds>\n");
169 $user_binds = ttTeamHelper::getUserToProjectBinds($user->group_id);
170 foreach ($user_binds as $bind) {
171 $user_id = $this->userMap[$bind['user_id']];
172 $project_id = $this->projectMap[$bind['project_id']];
173 fwrite($file, " <user_project_bind user_id=\"{$user_id}\" project_id=\"{$project_id}\" rate=\"".$bind['rate']."\" status=\"".$bind['status']."\"/>\n");
175 fwrite($file, "</user_project_binds>\n");
179 fwrite($file, "<clients>\n");
180 foreach ($clients as $client_item) {
181 if($client_item['projects']){
182 $projects = explode(',', $client_item['projects']);
183 $projects_mapped = array();
184 foreach ($projects as $item)
185 $projects_mapped[] = $this->projectMap[$item];
186 $projects_str = implode(',', $projects_mapped);
188 fwrite($file, " <client id=\"".$this->clientMap[$client_item['id']]."\" tax=\"".$client_item['tax']."\" projects=\"".$projects_str."\" status=\"".$client_item['status']."\">\n");
189 fwrite($file, " <name><![CDATA[".$client_item['name']."]]></name>\n");
190 fwrite($file, " <address><![CDATA[".$client_item['address']."]]></address>\n");
191 fwrite($file, " </client>\n");
193 fwrite($file, "</clients>\n");
197 fwrite($file, "<invoices>\n");
198 foreach ($invoices as $invoice_item) {
199 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");
200 fwrite($file, " <name><![CDATA[".$invoice_item['name']."]]></name>\n");
201 fwrite($file, " </invoice>\n");
203 fwrite($file, "</invoices>\n");
206 // Write custom fields.
207 fwrite($file, "<custom_fields>\n");
208 foreach ($custom_fields as $custom_field) {
209 fwrite($file, " <custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\" type=\"".$custom_field['type']."\" required=\"".$custom_field['required']."\" status=\"".$custom_field['status']."\">\n");
210 fwrite($file, " <label><![CDATA[".$custom_field['label']."]]></label>\n");
211 fwrite($file, " </custom_field>\n");
213 fwrite($file, "</custom_fields>\n");
214 unset($custom_fields);
216 // Write custom field options.
217 fwrite($file, "<custom_field_options>\n");
218 foreach ($custom_field_options as $option) {
219 fwrite($file, " <custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\" field_id=\"".$this->customFieldMap[$option['field_id']]."\">\n");
220 fwrite($file, " <value><![CDATA[".$option['value']."]]></value>\n");
221 fwrite($file, " </custom_field_option>\n");
223 fwrite($file, "</custom_field_options>\n");
224 unset($custom_field_options);
226 // Write monthly quotas.
227 $quotas = ttTeamHelper::getMonthlyQuotas($user->group_id);
228 fwrite($file, "<monthly_quotas>\n");
229 foreach ($quotas as $quota) {
230 fwrite($file, " <monthly_quota year=\"".$quota['year']."\" month=\"".$quota['month']."\" minutes=\"".$quota['minutes']."\"/>\n");
232 fwrite($file, "</monthly_quotas>\n");
234 // Write time log entries.
235 fwrite($file, "<log>\n");
237 foreach ($users as $user_item) {
238 $records = ttTimeHelper::getAllRecords($user_item['id']);
239 foreach ($records as $record) {
241 $this->logMap[$record['id']] = $key;
242 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");
243 fwrite($file, " <comment><![CDATA[".$record['comment']."]]></comment>\n");
244 fwrite($file, " </log_item>\n");
247 fwrite($file, "</log>\n");
250 // Write custom field log.
251 $custom_field_log = ttTeamHelper::getCustomFieldLog($user->group_id);
252 fwrite($file, "<custom_field_log>\n");
253 foreach ($custom_field_log as $entry) {
254 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");
255 fwrite($file, " <value><![CDATA[".$entry['value']."]]></value>\n");
256 fwrite($file, " </custom_field_log_entry>\n");
258 fwrite($file, "</custom_field_log>\n");
259 unset($custom_field_log);
261 // Write expense items.
262 $expense_items = ttTeamHelper::getExpenseItems($user->group_id);
263 fwrite($file, "<expense_items>\n");
264 foreach ($expense_items as $expense_item) {
265 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");
266 fwrite($file, " <name><![CDATA[".$expense_item['name']."]]></name>\n");
267 fwrite($file, " </expense_item>\n");
269 fwrite($file, "</expense_items>\n");
270 unset($expense_items);
272 // Write fav reports.
273 fwrite($file, "<fav_reports>\n");
274 $fav_reports = ttTeamHelper::getFavReports($user->group_id);
275 foreach ($fav_reports as $fav_report) {
277 if (strlen($fav_report['users']) > 0) {
278 $arr = explode(',', $fav_report['users']);
279 foreach ($arr as $k=>$v) {
280 if (array_key_exists($arr[$k], $this->userMap))
281 $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
284 fwrite($file, " <fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"".
285 " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"".
286 " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"".
287 " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"".
288 " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"".
289 " billable=\"".$fav_report['billable']."\"".
290 " users=\"".$user_list."\"".
291 " period=\"".$fav_report['period']."\"".
292 " period_start=\"".$fav_report['period_start']."\"".
293 " period_end=\"".$fav_report['period_end']."\"".
294 " show_client=\"".$fav_report['show_client']."\"".
295 " show_invoice=\"".$fav_report['show_invoice']."\"".
296 " show_paid=\"".$fav_report['show_paid']."\"".
297 " show_ip=\"".$fav_report['show_ip']."\"".
298 " show_project=\"".$fav_report['show_project']."\"".
299 " show_start=\"".$fav_report['show_start']."\"".
300 " show_duration=\"".$fav_report['show_duration']."\"".
301 " show_cost=\"".$fav_report['show_cost']."\"".
302 " show_task=\"".$fav_report['show_task']."\"".
303 " show_end=\"".$fav_report['show_end']."\"".
304 " show_note=\"".$fav_report['show_note']."\"".
305 " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"".
306 " show_work_units=\"".$fav_report['show_work_units']."\"".
307 " group_by=\"".$fav_report['group_by']."\"".
308 " show_totals_only=\"".$fav_report['show_totals_only']."\">\n");
309 fwrite($file, " <name><![CDATA[".$fav_report["name"]."]]></name>\n");
310 fwrite($file, " </fav_report>\n");
312 fwrite($file, "</fav_reports>\n");
317 $this->roleMap = array();
318 $this->userMap = array();
319 $this->projectMap = array();
320 $this->taskMap = array();
322 fwrite($file, "</pack>\n");
326 $this->fileName = tempnam($dirName, 'tt');
327 $this->compress($tmp_file, $this->fileName);
330 $this->fileName = $tmp_file;
335 // getFileName - returns file name.
336 function getFileName() {
337 return $this->fileName;
340 // compress - compresses the content of the $in file into $out file.
341 function compress($in, $out) {
342 // Initial checks of file names and permissions.
343 if (!file_exists($in) || !is_readable ($in))
345 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
348 $in_file = fopen($in, 'rb');
350 if (function_exists('bzopen')) {
351 if (!$out_file = bzopen($out, 'w'))
354 while (!feof ($in_file)) {
355 $buffer = fread($in_file, 4096);
356 bzwrite($out_file, $buffer, 4096);
365 * Note about the utility functions below.
366 * We have roughly 4 groups of operations:
367 * 1) Regular system usage for tracking time, etc.
368 * 2) Registration process - used infrequently.
369 * 3) Admin usage - used infrequently.
370 * 4) Export - used infrequently.
372 * It is tempting to have a generic function to get things done for
373 * all situations. However, as registration, export and admin access are one-off
374 * operations, while regular system usage is daily and must be efficient,
375 * the current approach is to have SEPARATE functions for each mode.
377 * This is because each mode requires a slightly different approach,
378 * and we don't want to over-complicate things.
381 // getRoles - obtains all roles defined for group.
382 function getRoles() {
384 $mdb2 = getConnection();
387 $sql = "select * from tt_roles where group_id = $user->group_id";
388 $res = $mdb2->query($sql);
390 if (!is_a($res, 'PEAR_Error')) {
391 while ($val = $res->fetchRow()) {
399 // The getUsers obtains all users in group for the purpose of export.
400 function getUsers() {
402 $mdb2 = getConnection();
404 $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.
405 $res = $mdb2->query($sql);
407 if (!is_a($res, 'PEAR_Error')) {
408 while ($val = $res->fetchRow()) {