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 // getPredefinedExpenses - obtains all predefined expenses for group.
178 function getPredefinedExpenses() {
180 $mdb2 = getConnection();
183 $sql = "select * from tt_predefined_expenses where group_id = $this->group_id"; // TODO: add " and org_id = $user->org_id" when possible.
184 $res = $mdb2->query($sql);
186 if (!is_a($res, 'PEAR_Error')) {
187 while ($val = $res->fetchRow()) {
195 // writeData writes group data into file.
196 function writeData() {
199 $group = $this->getGroupData();
200 $group_part = "<group name=\"".htmlspecialchars($group['name'])."\"";
201 $group_part .= " currency=\"".htmlspecialchars($group['currency'])."\"";
202 $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
203 $group_part .= " lang=\"".$group['lang']."\"";
204 $group_part .= " date_format=\"".$group['date_format']."\"";
205 $group_part .= " time_format=\"".$group['time_format']."\"";
206 $group_part .= " week_start=\"".$group['week_start']."\"";
207 $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
208 $group_part .= " project_required=\"".$group['project_required']."\"";
209 $group_part .= " task_required=\"".$group['task_required']."\"";
210 $group_part .= " record_type=\"".$group['record_type']."\"";
211 $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
212 $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
213 $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
214 $group_part .= " plugins=\"".$group['plugins']."\"";
215 $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
216 $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
217 $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
218 $group_part .= " config=\"".$group['config']."\"";
219 $group_part .= ">\n";
222 fwrite($this->file, $this->indentation.$group_part);
227 $users = $this->getUsers();
228 foreach ($users as $key=>$user_item)
229 $this->userMap[$user_item['id']] = $key + 1;
232 $roles = $this->getRoles();
233 foreach ($roles as $key=>$role_item)
234 $this->roleMap[$role_item['id']] = $key + 1;
237 $tasks = $this->getTasks();
238 foreach ($tasks as $key=>$task_item)
239 $this->taskMap[$task_item['id']] = $key + 1;
241 // Prepare project map.
242 $projects = $this->getProjects();
243 foreach ($projects as $key=>$project_item)
244 $this->projectMap[$project_item['id']] = $key + 1;
246 // Prepare client map.
247 $clients = $this->getClients();
248 foreach ($clients as $key=>$client_item)
249 $this->clientMap[$client_item['id']] = $key + 1;
251 // Prepare invoice map.
252 $invoices = ttTeamHelper::getAllInvoices();
253 foreach ($invoices as $key=>$invoice_item)
254 $this->invoiceMap[$invoice_item['id']] = $key + 1;
256 // Prepare custom fields map.
257 $custom_fields = ttTeamHelper::getAllCustomFields($this->group_id);
258 foreach ($custom_fields as $key=>$custom_field)
259 $this->customFieldMap[$custom_field['id']] = $key + 1;
261 // Prepare custom field options map.
262 $custom_field_options = ttTeamHelper::getAllCustomFieldOptions($this->group_id);
263 foreach ($custom_field_options as $key=>$option)
264 $this->customFieldOptionMap[$option['id']] = $key + 1;
267 fwrite($this->file, $this->indentation." <roles>\n");
268 foreach ($roles as $role) {
269 $role_part = $this->indentation.' '."<role id=\"".$this->roleMap[$role['id']]."\"";
270 $role_part .= " name=\"".htmlspecialchars($role['name'])."\"";
271 $role_part .= " description=\"".htmlspecialchars($role['description'])."\"";
272 $role_part .= " rank=\"".$role['rank']."\"";
273 $role_part .= " rights=\"".htmlspecialchars($role['rights'])."\"";
274 $role_part .= " status=\"".$role['status']."\"";
275 $role_part .= "></role>\n";
276 fwrite($this->file, $role_part);
278 fwrite($this->file, $this->indentation." </roles>\n");
283 fwrite($this->file, $this->indentation." <tasks>\n");
284 foreach ($tasks as $task) {
285 $task_part = $this->indentation.' '."<task id=\"".$this->taskMap[$task['id']]."\"";
286 $task_part .= " name=\"".htmlspecialchars($task['name'])."\"";
287 $task_part .= " description=\"".htmlspecialchars($task['description'])."\"";
288 $task_part .= " status=\"".$task['status']."\"";
289 $task_part .= "></task>\n";
290 fwrite($this->file, $task_part);
292 fwrite($this->file, $this->indentation." </tasks>\n");
297 fwrite($this->file, $this->indentation." <projects>\n");
298 foreach ($projects as $project_item) {
299 if($project_item['tasks']){
300 $tasks = explode(',', $project_item['tasks']);
301 $tasks_mapped = array();
302 foreach ($tasks as $item)
303 $tasks_mapped[] = $this->taskMap[$item];
304 $tasks_str = implode(',', $tasks_mapped);
306 $project_part = $this->indentation.' '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
307 $project_part .= " name=\"".htmlspecialchars($project_item['name'])."\"";
308 $project_part .= " description=\"".htmlspecialchars($project_item['description'])."\"";
309 $project_part .= " tasks=\"".$tasks_str."\"";
310 $project_part .= " status=\"".$project_item['status']."\"";
311 $project_part .= "></project>\n";
312 fwrite($this->file, $project_part);
314 fwrite($this->file, $this->indentation." </projects>\n");
316 unset($project_part);
319 fwrite($this->file, $this->indentation." <clients>\n");
320 foreach ($clients as $client_item) {
321 if($client_item['projects']){
322 $projects_db = explode(',', $client_item['projects']);
323 $projects_mapped = array();
324 foreach ($projects_db as $item)
325 $projects_mapped[] = $this->projectMap[$item];
326 $projects_str = implode(',', $projects_mapped);
328 $client_part = $this->indentation.' '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
329 $client_part .= " name=\"".htmlspecialchars($client_item['name'])."\"";
330 $client_part .= " address=\"".htmlspecialchars($client_item['address'])."\"";
331 $client_part .= " tax=\"".$client_item['tax']."\"";
332 $client_part .= " projects=\"".$projects_str."\"";
333 $client_part .= " status=\"".$client_item['status']."\"";
334 $client_part .= "></client>\n";
335 fwrite($this->file, $client_part);
337 fwrite($this->file, $this->indentation." </clients>\n");
342 fwrite($this->file, $this->indentation." <users>\n");
343 foreach ($users as $user_item) {
344 $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
345 $user_part = $this->indentation.' '."<user id=\"".$this->userMap[$user_item['id']]."\"";
346 $user_part .= " name=\"".htmlspecialchars($user_item['name'])."\"";
347 $user_part .= " login=\"".htmlspecialchars($user_item['login'])."\"";
348 $user_part .= " password=\"".$user_item['password']."\"";
349 $user_part .= " role_id=\"".$role_id."\"";
350 $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
351 $user_part .= " rate=\"".$user_item['rate']."\"";
352 $user_part .= " email=\"".$user_item['email']."\"";
353 $user_part .= " status=\"".$user_item['status']."\"";
354 $user_part .= "></user>\n";
355 fwrite($this->file, $user_part);
357 fwrite($this->file, $this->indentation." </users>\n");
361 // Write user to project binds.
362 fwrite($this->file, $this->indentation." <user_project_binds>\n");
363 $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
364 foreach ($user_binds as $bind) {
365 $user_id = $this->userMap[$bind['user_id']];
366 $project_id = $this->projectMap[$bind['project_id']];
367 $bind_part = $this->indentation.' '."<user_project_bind user_id=\"".$user_id."\"";
368 $bind_part .= " project_id=\"".$project_id."\"";
369 $bind_part .= " rate=\"".$bind['rate']."\"";
370 $bind_part .= " status=\"".$bind['status']."\"";
371 $bind_part .= "></user_project_bind>\n";
372 fwrite($this->file, $bind_part);
374 fwrite($this->file, $this->indentation." </user_project_binds>\n");
379 fwrite($this->file, $this->indentation." <invoices>\n");
380 foreach ($invoices as $invoice_item) {
381 $invoice_part = $this->indentation.' '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
382 $invoice_part .= " name=\"".htmlspecialchars($invoice_item['name'])."\"";
383 $invoice_part .= " date=\"".$invoice_item['date']."\"";
384 $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
385 $invoice_part .= " status=\"".$invoice_item['status']."\"";
386 $invoice_part .= "></invoice>\n";
387 fwrite($this->file, $invoice_part);
389 fwrite($this->file, $this->indentation." </invoices>\n");
391 unset($invoice_part);
393 // Write time log entries and build logMap at the same time.
394 fwrite($this->file, $this->indentation." <log>\n");
396 foreach ($this->userMap as $key => $value) {
398 $records = ttTimeHelper::getAllRecords($user_id);
399 foreach ($records as $record) {
401 $this->logMap[$record['id']] = $key;
402 $log_part = $this->indentation.' '."<log_item id=\"$key\"";
403 $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
404 $log_part .= " date=\"".$record['date']."\"";
405 $log_part .= " start=\"".$record['start']."\"";
406 $log_part .= " finish=\"".$record['finish']."\"";
407 $log_part .= " duration=\"".($record['start']?"":$record['duration'])."\"";
408 $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
409 $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
410 $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
411 $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
412 $log_part .= " comment=\"".htmlspecialchars($record['comment'])."\"";
413 $log_part .= " billable=\"".$record['billable']."\"";
414 $log_part .= " paid=\"".$record['paid']."\"";
415 $log_part .= " status=\"".$record['status']."\"";
416 $log_part .= "></log_item>\n";
417 fwrite($this->file, $log_part);
420 fwrite($this->file, $this->indentation." </log>\n");
424 // Write custom fields.
425 fwrite($this->file, $this->indentation." <custom_fields>\n");
426 foreach ($custom_fields as $custom_field) {
427 $custom_field_part = $this->indentation.' '."<custom_field id=\"".$this->customFieldMap[$custom_field['id']]."\"";
428 $custom_field_part .= " type=\"".$custom_field['type']."\"";
429 $custom_field_part .= " label=\"".htmlspecialchars($custom_field['label'])."\"";
430 $custom_field_part .= " required=\"".$custom_field['required']."\"";
431 $custom_field_part .= " status=\"".$custom_field['status']."\"";
432 $custom_field_part .= "></custom_field>\n";
433 fwrite($this->file, $custom_field_part);
435 fwrite($this->file, $this->indentation." </custom_fields>\n");
436 unset($custom_fields);
437 unset($custom_field_part);
439 // Write custom field options.
440 fwrite($this->file, $this->indentation." <custom_field_options>\n");
441 foreach ($custom_field_options as $option) {
442 $custom_field_option_part = $this->indentation.' '."<custom_field_option id=\"".$this->customFieldOptionMap[$option['id']]."\"";
443 $custom_field_option_part .= " field_id=\"".$this->customFieldMap[$option['field_id']]."\"";
444 $custom_field_option_part .= " value=\"".htmlspecialchars($option['value'])."\"";
445 $custom_field_option_part .= "></custom_field_option>\n";
446 fwrite($this->file, $custom_field_option_part);
448 fwrite($this->file, $this->indentation." </custom_field_options>\n");
449 unset($custom_field_options);
450 unset($custom_field_option_part);
452 // Write custom field log.
453 $custom_field_log = ttTeamHelper::getCustomFieldLog($this->group_id);
454 fwrite($this->file, $this->indentation." <custom_field_log>\n");
455 foreach ($custom_field_log as $entry) {
456 $custom_field_log_part = $this->indentation.' '."<custom_field_log_entry log_id=\"".$this->logMap[$entry['log_id']]."\"";
457 $custom_field_log_part .= " field_id=\"".$this->customFieldMap[$entry['field_id']]."\"";
458 $custom_field_log_part .= " option_id=\"".$this->customFieldOptionMap[$entry['option_id']]."\"";
459 $custom_field_log_part .= " value=\"".htmlspecialchars($entry['value'])."\"";
460 $custom_field_log_part .= " status=\"".$entry['status']."\"";
461 $custom_field_log_part .= "></custom_field_log_entry>\n";
462 fwrite($this->file, $custom_field_log_part);
464 fwrite($this->file, $this->indentation." </custom_field_log>\n");
465 unset($custom_field_log);
466 unset($custom_field_log_part);
468 // Write expense items.
469 $expense_items = ttTeamHelper::getExpenseItems($this->group_id);
470 fwrite($this->file, $this->indentation." <expense_items>\n");
471 foreach ($expense_items as $expense_item) {
472 $expense_item_part = $this->indentation.' '."<expense_item date=\"".$expense_item['date']."\"";
473 $expense_item_part .= " user_id=\"".$this->userMap[$expense_item['user_id']]."\"";
474 $expense_item_part .= " client_id=\"".$this->clientMap[$expense_item['client_id']]."\"";
475 $expense_item_part .= " project_id=\"".$this->projectMap[$expense_item['project_id']]."\"";
476 $expense_item_part .= " name=\"".htmlspecialchars($expense_item['name'])."\"";
477 $expense_item_part .= " cost=\"".$expense_item['cost']."\"";
478 $expense_item_part .= " invoice_id=\"".$this->invoiceMap[$expense_item['invoice_id']]."\"";
479 $expense_item_part .= " paid=\"".$expense_item['paid']."\"";
480 $expense_item_part .= " status=\"".$expense_item['status']."\"";
481 $expense_item_part .= "></expense_item>\n";
482 fwrite($this->file, $expense_item_part);
484 fwrite($this->file, $this->indentation." </expense_items>\n");
485 unset($expense_items);
486 unset($expense_item_part);
488 // Write predefined expenses.
489 $predefined_expenses = $this->getPredefinedExpenses();
490 fwrite($this->file, $this->indentation." <predefined_expenses>\n");
491 foreach ($predefined_expenses as $predefined_expense) {
492 $predefined_expense_part = $this->indentation.' '."<predefined_expense name=\"".htmlspecialchars($predefined_expense['name'])."\"";
493 $predefined_expense_part .= " cost=\"".$predefined_expense['cost']."\"";
494 $predefined_expense_part .= "></predefined_expense>\n";
495 fwrite($this->file, $predefined_expense_part);
497 fwrite($this->file, $this->indentation." </predefined_expenses>\n");
498 unset($predefined_expenses);
499 unset($predefined_expense_part);
501 // Write monthly quotas.
502 $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
503 fwrite($this->file, $this->indentation." <monthly_quotas>\n");
504 foreach ($quotas as $quota) {
505 $quota_part = $this->indentation.' '."<monthly_quota year=\"".$quota['year']."\"";
506 $quota_part .= " month=\"".$quota['month']."\"";
507 $quota_part .= " minutes=\"".$quota['minutes']."\"";
508 $quota_part .= "></monthly_quota>\n";
509 fwrite($this->file, $quota_part);
511 fwrite($this->file, $this->indentation." </monthly_quotas>\n");
515 // Write fav reports.
516 $fav_reports = ttTeamHelper::getFavReports($this->group_id);
517 fwrite($this->file, $this->indentation." <fav_reports>\n");
518 foreach ($fav_reports as $fav_report) {
520 if (strlen($fav_report['users']) > 0) {
521 $arr = explode(',', $fav_report['users']);
522 foreach ($arr as $k=>$v) {
523 if (array_key_exists($arr[$k], $this->userMap))
524 $user_list .= (strlen($user_list) == 0? '' : ',').$this->userMap[$v];
527 $fav_report_part = $this->indentation.' '."<fav_report user_id=\"".$this->userMap[$fav_report['user_id']]."\"";
528 $fav_report_part .= " name=\"".htmlspecialchars($fav_report['name'])."\"";
529 $fav_report_part .= " client_id=\"".$this->clientMap[$fav_report['client_id']]."\"";
530 $fav_report_part .= " cf_1_option_id=\"".$this->customFieldOptionMap[$fav_report['cf_1_option_id']]."\"";
531 $fav_report_part .= " project_id=\"".$this->projectMap[$fav_report['project_id']]."\"";
532 $fav_report_part .= " task_id=\"".$this->taskMap[$fav_report['task_id']]."\"";
533 $fav_report_part .= " billable=\"".$fav_report['billable']."\"";
534 $fav_report_part .= " users=\"".$user_list."\"";
535 $fav_report_part .= " period=\"".$fav_report['period']."\"";
536 $fav_report_part .= " period_start=\"".$fav_report['period_start']."\"";
537 $fav_report_part .= " period_end=\"".$fav_report['period_end']."\"";
538 $fav_report_part .= " show_client=\"".$fav_report['show_client']."\"";
539 $fav_report_part .= " show_invoice=\"".$fav_report['show_invoice']."\"";
540 $fav_report_part .= " show_paid=\"".$fav_report['show_paid']."\"";
541 $fav_report_part .= " show_ip=\"".$fav_report['show_ip']."\"";
542 $fav_report_part .= " show_project=\"".$fav_report['show_project']."\"";
543 $fav_report_part .= " show_start=\"".$fav_report['show_start']."\"";
544 $fav_report_part .= " show_duration=\"".$fav_report['show_duration']."\"";
545 $fav_report_part .= " show_cost=\"".$fav_report['show_cost']."\"";
546 $fav_report_part .= " show_task=\"".$fav_report['show_task']."\"";
547 $fav_report_part .= " show_end=\"".$fav_report['show_end']."\"";
548 $fav_report_part .= " show_note=\"".$fav_report['show_note']."\"";
549 $fav_report_part .= " show_custom_field_1=\"".$fav_report['show_custom_field_1']."\"";
550 $fav_report_part .= " show_work_units=\"".$fav_report['show_work_units']."\"";
551 $fav_report_part .= " group_by1=\"".$fav_report['group_by1']."\"";
552 $fav_report_part .= " group_by2=\"".$fav_report['group_by2']."\"";
553 $fav_report_part .= " group_by3=\"".$fav_report['group_by3']."\"";
554 $fav_report_part .= " show_totals_only=\"".$fav_report['show_totals_only']."\"";
555 $fav_report_part .= "></fav_report>\n";
556 fwrite($this->file, $fav_report_part);
558 fwrite($this->file, $this->indentation." </fav_reports>\n");
560 unset($fav_report_part);
562 // We are mostly done with writing this group data, destroy all maps.
563 unset($this->roleMap);
564 unset($this->userMap);
565 unset($this->taskMap);
566 unset($this->projectMap);
567 unset($this->clientMap);
568 unset($this->invoiceMap);
569 unset($this->logMap);
570 unset($this->customFieldMap);
571 unset($this->customFieldOptionMap);
573 // Call self recursively for all subgroups.
574 foreach ($this->subgroups as $subgroup) {
575 $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.' ');
576 $subgroup_helper->writeData();
578 unset($this->subgroups);
580 fwrite($this->file, $this->indentation."</group>\n");