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 // ttGroupExportHelper - this class is used to write data for a single group
30 // to a file. When group contains other groups, it reuses itself recursively.
32 // Currently, it is work in progress.
33 // When done, it should handle export of organizations containing multiple groups.
34 class ttGroupExportHelper {
36 var $group_id = null; // Group we are exporting.
37 var $file = null; // File to write to.
38 var $indentation = null; // A string consisting of a number of spaces.
39 var $subgroups = array(); // Immediate subgroups.
41 // The following arrays are maps between entity ids in the file versus the database.
42 // We write to the file sequentially (1,2,3...) while in the database the entities have different ids.
43 var $userMap = array();
44 var $roleMap = array();
45 var $taskMap = array();
46 var $projectMap = array();
47 var $clientMap = array();
48 var $invoiceMap = array();
49 var $logMap = array();
50 var $customFieldMap = array();
51 var $customFieldOptionMap = array();
54 function __construct($group_id, $file, $indentation) {
57 $this->group_id = $group_id;
59 $this->indentation = $indentation;
61 // Build a list of subgroups.
62 $mdb2 = getConnection();
63 $sql = "select id from tt_groups".
64 " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
65 $res = $mdb2->query($sql);
66 if (!is_a($res, 'PEAR_Error')) {
67 while ($val = $res->fetchRow()) {
68 $this->subgroups[] = $val;
73 // getGroupData obtains group attributes for export.
74 function getGroupData() {
76 $mdb2 = getConnection();
78 $sql = "select * from tt_groups".
79 " where status = 1 and id = $this->group_id and org_id = $user->org_id";
80 $res = $mdb2->query($sql);
81 if (!is_a($res, 'PEAR_Error')) {
82 $val = $res->fetchRow();
87 // The getUsers obtains all users in group for the purpose of export.
90 $mdb2 = getConnection();
92 $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
93 " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
94 $res = $mdb2->query($sql);
96 if (!is_a($res, 'PEAR_Error')) {
97 while ($val = $res->fetchRow()) {
105 // getRoles - obtains all roles defined for group.
106 function getRoles() {
108 $mdb2 = getConnection();
111 $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
112 $res = $mdb2->query($sql);
114 if (!is_a($res, 'PEAR_Error')) {
115 while ($val = $res->fetchRow()) {
123 // getTasks - obtains all tasks defined for group.
124 function getTasks() {
126 $mdb2 = getConnection();
129 $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
130 $res = $mdb2->query($sql);
132 if (!is_a($res, 'PEAR_Error')) {
133 while ($val = $res->fetchRow()) {
141 // getProjects - obtains all projects defined for group.
142 function getProjects() {
144 $mdb2 = getConnection();
147 $sql = "select * from tt_projects where group_id = $this->group_id and org_id = $user->org_id";
148 $res = $mdb2->query($sql);
150 if (!is_a($res, 'PEAR_Error')) {
151 while ($val = $res->fetchRow()) {
159 // getClients - obtains all clients defined for group.
160 function getClients() {
162 $mdb2 = getConnection();
165 $sql = "select * from tt_clients where group_id = $this->group_id and org_id = $user->org_id";
166 $res = $mdb2->query($sql);
168 if (!is_a($res, 'PEAR_Error')) {
169 while ($val = $res->fetchRow()) {
177 // writeData writes group data into file.
178 function writeData() {
181 $group = $this->getGroupData();
182 $group_part = "<group name=\"".htmlspecialchars($group['name'])."\"";
183 $group_part .= " currency=\"".htmlspecialchars($group['currency'])."\"";
184 $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
185 $group_part .= " lang=\"".$group['lang']."\"";
186 $group_part .= " date_format=\"".$group['date_format']."\"";
187 $group_part .= " time_format=\"".$group['time_format']."\"";
188 $group_part .= " week_start=\"".$group['week_start']."\"";
189 $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
190 $group_part .= " project_required=\"".$group['project_required']."\"";
191 $group_part .= " task_required=\"".$group['task_required']."\"";
192 $group_part .= " record_type=\"".$group['record_type']."\"";
193 $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
194 $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
195 $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
196 $group_part .= " plugins=\"".$group['plugins']."\"";
197 $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
198 $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
199 $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
200 $group_part .= " config=\"".$group['config']."\"";
201 $group_part .= ">\n";
204 fwrite($this->file, $this->indentation.$group_part);
209 $users = $this->getUsers();
210 foreach ($users as $key=>$user_item)
211 $this->userMap[$user_item['id']] = $key + 1;
214 $roles = $this->getRoles();
215 foreach ($roles as $key=>$role_item)
216 $this->roleMap[$role_item['id']] = $key + 1;
219 $tasks = $this->getTasks();
220 foreach ($tasks as $key=>$task_item)
221 $this->taskMap[$task_item['id']] = $key + 1;
223 // Prepare project map.
224 $projects = $this->getProjects();
225 foreach ($projects as $key=>$project_item)
226 $this->projectMap[$project_item['id']] = $key + 1;
228 // Prepare client map.
229 $clients = $this->getClients();
230 foreach ($clients as $key=>$client_item)
231 $this->clientMap[$client_item['id']] = $key + 1;
233 // Prepare invoice map.
234 $invoices = ttTeamHelper::getAllInvoices();
235 foreach ($invoices as $key=>$invoice_item)
236 $this->invoiceMap[$invoice_item['id']] = $key + 1;
238 // Prepare custom fields map.
239 $custom_fields = ttTeamHelper::getAllCustomFields($this->group_id);
240 foreach ($custom_fields as $key=>$custom_field)
241 $this->customFieldMap[$custom_field['id']] = $key + 1;
243 // Prepare custom field options map.
244 $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($this->group_id);
245 foreach ($custom_field_options as $key=>$option)
246 $this->customFieldOptionMap[$option['id']] = $key + 1;
249 fwrite($this->file, $this->indentation." <roles>\n");
250 foreach ($roles as $role) {
251 $role_part = $this->indentation.' '."<role id=\"".$this->roleMap[$role['id']]."\"";
252 $role_part .= " name=\"".htmlspecialchars($role['name'])."\"";
253 $role_part .= " description=\"".htmlspecialchars($role['description'])."\"";
254 $role_part .= " rank=\"".$role['rank']."\"";
255 $role_part .= " rights=\"".htmlspecialchars($role['rights'])."\"";
256 $role_part .= " status=\"".$role['status']."\"";
257 $role_part .= "></role>\n";
258 fwrite($this->file, $role_part);
260 fwrite($this->file, $this->indentation." </roles>\n");
265 fwrite($this->file, $this->indentation." <tasks>\n");
266 foreach ($tasks as $task) {
267 $task_part = $this->indentation.' '."<task id=\"".$this->taskMap[$task['id']]."\"";
268 $task_part .= " name=\"".htmlspecialchars($task['name'])."\"";
269 $task_part .= " description=\"".htmlspecialchars($task['description'])."\"";
270 $task_part .= " status=\"".$task['status']."\"";
271 $task_part .= "></task>\n";
272 fwrite($this->file, $task_part);
274 fwrite($this->file, $this->indentation." </tasks>\n");
279 fwrite($this->file, $this->indentation." <projects>\n");
280 foreach ($projects as $project_item) {
281 if($project_item['tasks']){
282 $tasks = explode(',', $project_item['tasks']);
283 $tasks_mapped = array();
284 foreach ($tasks as $item)
285 $tasks_mapped[] = $this->taskMap[$item];
286 $tasks_str = implode(',', $tasks_mapped);
288 $project_part = $this->indentation.' '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
289 $project_part .= " name=\"".htmlspecialchars($project_item['name'])."\"";
290 $project_part .= " description=\"".htmlspecialchars($project_item['description'])."\"";
291 $project_part .= " tasks=\"".$tasks_str."\"";
292 $project_part .= " status=\"".$project_item['status']."\"";
293 $project_part .= "></project>\n";
294 fwrite($this->file, $project_part);
296 fwrite($this->file, $this->indentation." </projects>\n");
298 unset($project_part);
301 fwrite($this->file, $this->indentation." <clients>\n");
302 foreach ($clients as $client_item) {
303 if($client_item['projects']){
304 $projects_db = explode(',', $client_item['projects']);
305 $projects_mapped = array();
306 foreach ($projects_db as $item)
307 $projects_mapped[] = $this->projectMap[$item];
308 $projects_str = implode(',', $projects_mapped);
310 $client_part = $this->indentation.' '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
311 $client_part .= " name=\"".htmlspecialchars($client_item['name'])."\"";
312 $client_part .= " address=\"".htmlspecialchars($client_item['address'])."\"";
313 $client_part .= " tax=\"".$client_item['tax']."\"";
314 $client_part .= " projects=\"".$projects_str."\"";
315 $client_part .= " status=\"".$client_item['status']."\"";
316 $client_part .= "></client>\n";
317 fwrite($this->file, $client_part);
319 fwrite($this->file, $this->indentation." </clients>\n");
324 fwrite($this->file, $this->indentation." <users>\n");
325 foreach ($users as $user_item) {
326 $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
327 $user_part = $this->indentation.' '."<user id=\"".$this->userMap[$user_item['id']]."\"";
328 $user_part .= " name=\"".htmlspecialchars($user_item['name'])."\"";
329 $user_part .= " login=\"".htmlspecialchars($user_item['login'])."\"";
330 $user_part .= " password=\"".$user_item['password']."\"";
331 $user_part .= " role_id=\"".$role_id."\"";
332 $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
333 $user_part .= " rate=\"".$user_item['rate']."\"";
334 $user_part .= " email=\"".$user_item['email']."\"";
335 $user_part .= " status=\"".$user_item['status']."\"";
336 $user_part .= "></user>\n";
337 fwrite($this->file, $user_part);
339 fwrite($this->file, $this->indentation." </users>\n");
343 // Write user to project binds.
344 fwrite($this->file, $this->indentation." <user_project_binds>\n");
345 $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
346 foreach ($user_binds as $bind) {
347 $user_id = $this->userMap[$bind['user_id']];
348 $project_id = $this->projectMap[$bind['project_id']];
349 $bind_part = $this->indentation.' '."<user_project_bind user_id=\"".$user_id."\"";
350 $bind_part .= " project_id=\"".$project_id."\"";
351 $bind_part .= " rate=\"".$bind['rate']."\"";
352 $bind_part .= " status=\"".$bind['status']."\"";
353 $bind_part .= "></user_project_bind>\n";
354 fwrite($this->file, $bind_part);
356 fwrite($this->file, $this->indentation." </user_project_binds>\n");
361 fwrite($this->file, $this->indentation." <invoices>\n");
362 foreach ($invoices as $invoice_item) {
363 $invoice_part = $this->indentation.' '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
364 $invoice_part .= " name=\"".htmlspecialchars($invoice_item['name'])."\"";
365 $invoice_part .= " date=\"".$invoice_item['date']."\"";
366 $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
367 $invoice_part .= " status=\"".$invoice_item['status']."\"";
368 $invoice_part .= "></invoice>\n";
369 fwrite($this->file, $invoice_part);
371 fwrite($this->file, $this->indentation." </invoices>\n");
373 unset($invoice_part);
375 // Write time log entries and build logMap at the same time.
376 fwrite($this->file, $this->indentation." <log>\n");
378 foreach ($this->userMap as $key => $value) {
380 $records = ttTimeHelper::getAllRecords($user_id);
381 foreach ($records as $record) {
383 $this->logMap[$record['id']] = $key;
384 $log_part = $this->indentation.' '."<log_item id=\"$key\"";
385 $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
386 $log_part .= " date=\"".$record['date']."\"";
387 $log_part .= " start=\"".$record['start']."\"";
388 $log_part .= " finish=\"".$record['finish']."\"";
389 $log_part .= " duration=\"".($record['start']?"":$record['duration'])."\"";
390 $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
391 $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
392 $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
393 $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
394 $log_part .= " comment=\"".htmlspecialchars($record['comment'])."\"";
395 $log_part .= " billable=\"".$record['billable']."\"";
396 $log_part .= " paid=\"".$record['paid']."\"";
397 $log_part .= " status=\"".$record['status']."\"";
398 $log_part .= "></log_item>\n";
399 fwrite($this->file, $log_part);
402 fwrite($this->file, $this->indentation." </log>\n");
406 // Write custom fields.
407 fwrite($this->file, $this->indentation." <custom_fields>\n");
408 foreach ($custom_fields as $custom_field) {
409 $custom_field_part = $this->indentation.' '."<custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\"";
410 $custom_field_part .= " type=\"".$custom_field['type']."\"";
411 $custom_field_part .= " label=\"".htmlspecialchars($custom_field['label'])."\"";
412 $custom_field_part .= " required=\"".$custom_field['required']."\"";
413 $custom_field_part .= " status=\"".$custom_field['status']."\"";
414 $custom_field_part .= "></custom_field>\n";
415 fwrite($this->file, $custom_field_part);
417 fwrite($this->file, $this->indentation." </custom_fields>\n");
418 unset($custom_fields);
419 unset($custom_field_part);
421 // Write custom field options.
422 fwrite($this->file, $this->indentation." <custom_field_options>\n");
423 foreach ($custom_field_options as $option) {
424 $custom_field_option_part = $this->indentation.' '."<custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\"";
425 $custom_field_option_part .= " field_id=\"".$this->customFieldMap[$option['field_id']]."\"";
426 $custom_field_option_part .= " value=\"".htmlspecialchars($option['value'])."\"";
427 $custom_field_option_part .= "></custom_field_option>\n";
428 fwrite($this->file, $custom_field_option_part);
430 fwrite($this->file, $this->indentation." </custom_field_options>\n");
431 unset($custom_field_options);
432 unset($custom_field_option_part);
434 // Write custom field log.
435 $custom_field_log = ttTeamHelper::getCustomFieldLog($this->group_id);
436 fwrite($this->file, $this->indentation." <custom_field_log>\n");
437 foreach ($custom_field_log as $entry) {
438 $custom_field_log_part = $this->indentation.' '."<custom_field_log_entry log_id=\"".$this->logMap[$entry['log_id']]."\"";
439 $custom_field_log_part .= " field_id=\"".$this->customFieldMap[$entry['field_id']]."\"";
440 $custom_field_log_part .= " option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\"";
441 $custom_field_log_part .= " value=\"".htmlspecialchars($entry['value'])."\"";
442 $custom_field_log_part .= " status=\"".$entry['status']."\"";
443 $custom_field_log_part .= "></custom_field_log_entry>\n";
444 fwrite($this->file, $custom_field_log_part);
446 fwrite($this->file, $this->indentation." </custom_field_log>\n");
447 unset($custom_field_log);
448 unset($custom_field_log_part);
450 // Write expense items.
451 $expense_items = ttTeamHelper::getExpenseItems($this->group_id);
452 fwrite($this->file, $this->indentation." <expense_items>\n");
453 foreach ($expense_items as $expense_item) {
454 $expense_item_part = $this->indentation.' '."<expense_item date=\"".$expense_item['date']."\"";
455 $expense_item_part .= " user_id=\"".$this->userMap[$expense_item['user_id']]."\"";
456 $expense_item_part .= " client_id=\"".$this->clientMap[$expense_item['client_id']]."\"";
457 $expense_item_part .= " project_id=\"".$this->projectMap[$expense_item['project_id']]."\"";
458 $expense_item_part .= " name=\"".htmlspecialchars($expense_item['name'])."\"";
459 $expense_item_part .= " cost=\"".$expense_item['cost']."\"";
460 $expense_item_part .= " invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\"";
461 $expense_item_part .= " paid=\"".$expense_item['paid']."\"";
462 $expense_item_part .= " status=\"".$expense_item['status']."\"";
463 $expense_item_part .= "></expense_item>\n";
464 fwrite($this->file, $expense_item_part);
466 fwrite($this->file, $this->indentation." </expense_items>\n");
467 unset($expense_items);
468 unset($expense_item_part);
470 // Write monthly quotas.
471 $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
472 fwrite($this->file, $this->indentation." <monthly_quotas>\n");
473 foreach ($quotas as $quota) {
474 $quota_part = $this->indentation.' '."<monthly_quota year=\"".$quota['year']."\"";
475 $quota_part .= " month=\"".$quota['month']."\"";
476 $quota_part .= " minutes=\"".$quota['minutes']."\"";
477 $quota_part .= "></monthly_quota>\n";
478 fwrite($this->file, $quota_part);
480 fwrite($this->file, $this->indentation." </monthly_quotas>\n");
484 // Write fav reports.
485 $fav_reports = ttTeamHelper::getFavReports($this->group_id);
486 fwrite($this->file, $this->indentation." <fav_reports>\n");
487 foreach ($fav_reports as $fav_report) {
489 if (strlen($fav_report['users']) > 0) {
490 $arr = explode(',', $fav_report['users']);
491 foreach ($arr as $k=>$v) {
492 if (array_key_exists($arr[$k], $this->userMap))
493 $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
496 $fav_report_part = $this->indentation.' '."<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"";
497 $fav_report_part .= " name=\"".htmlspecialchars($fav_report['name'])."\"";
498 $fav_report_part .= " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"";
499 $fav_report_part .= " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"";
500 $fav_report_part .= " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"";
501 $fav_report_part .= " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"";
502 $fav_report_part .= " billable=\"".$fav_report['billable']."\"";
503 $fav_report_part .= " users=\"".$user_list."\"";
504 $fav_report_part .= " period=\"".$fav_report['period']."\"";
505 $fav_report_part .= " period_start=\"".$fav_report['period_start']."\"";
506 $fav_report_part .= " period_end=\"".$fav_report['period_end']."\"";
507 $fav_report_part .= " show_client=\"".$fav_report['show_client']."\"";
508 $fav_report_part .= " show_invoice=\"".$fav_report['show_invoice']."\"";
509 $fav_report_part .= " show_paid=\"".$fav_report['show_paid']."\"";
510 $fav_report_part .= " show_ip=\"".$fav_report['show_ip']."\"";
511 $fav_report_part .= " show_project=\"".$fav_report['show_project']."\"";
512 $fav_report_part .= " show_start=\"".$fav_report['show_start']."\"";
513 $fav_report_part .= " show_duration=\"".$fav_report['show_duration']."\"";
514 $fav_report_part .= " show_cost=\"".$fav_report['show_cost']."\"";
515 $fav_report_part .= " show_task=\"".$fav_report['show_task']."\"";
516 $fav_report_part .= " show_end=\"".$fav_report['show_end']."\"";
517 $fav_report_part .= " show_note=\"".$fav_report['show_note']."\"";
518 $fav_report_part .= " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"";
519 $fav_report_part .= " show_work_units=\"".$fav_report['show_work_units']."\"";
520 $fav_report_part .= " group_by1=\"".$fav_report['group_by1']."\"";
521 $fav_report_part .= " group_by2=\"".$fav_report['group_by2']."\"";
522 $fav_report_part .= " group_by3=\"".$fav_report['group_by3']."\"";
523 $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
524 $fav_report_part .= "></fav_report>\n";
525 fwrite($this->file, $fav_report_part);
527 fwrite($this->file, $this->indentation." </fav_reports>\n");
529 unset($fav_report_part);
531 // We are mostly done with writing this group data, destroy all maps.
532 unset($this->roleMap);
533 unset($this->userMap);
534 unset($this->taskMap);
535 unset($this->projectMap);
536 unset($this->clientMap);
537 unset($this->invoiceMap);
538 unset($this->logMap);
539 unset($this->customFieldMap);
540 unset($this->customFieldOptionMap);
542 // Call self recursively for all subgroups.
543 foreach ($this->subgroups as $subgroup) {
544 $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.' ');
545 $subgroup_helper->writeData();
547 unset($this->subgroups);
549 fwrite($this->file, $this->indentation."</group>\n");