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.
42 function __construct($group_id, $file, $indentation) {
45 $this->group_id = $group_id;
47 $this->indentation = $indentation;
49 // Build a list of subgroups.
50 $mdb2 = getConnection();
51 $sql = "select id from tt_groups".
52 " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
53 $res = $mdb2->query($sql);
54 if (!is_a($res, 'PEAR_Error')) {
55 while ($val = $res->fetchRow()) {
56 $this->subgroups[] = $val;
61 // getGroupData obtains group attributes for export.
62 function getGroupData() {
65 $mdb2 = getConnection();
66 $sql = "select name, currency, lang from tt_groups".
67 " where status = 1 and id = $this->group_id and org_id = $user->org_id";
68 $res = $mdb2->query($sql);
69 if (!is_a($res, 'PEAR_Error')) {
70 $val = $res->fetchRow();
75 // writeData writes group data into file.
76 function writeData() {
79 $group = $this->getGroupData();
80 $group_part = "<group name=\"".htmlentities($group['name'])."\"";
81 $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
82 $group_part .= " lang=\"".$group['lang']."\"";
83 // TODO: add other group attributes here.
87 fwrite($this->file, $this->indentation.$group_part);
89 // Call self recursively for all subgroups.
90 foreach ($this->subgroups as $subgroup) {
91 $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.' ');
92 $subgroup_helper->writeData();
95 fwrite($this->file, $this->indentation."</group>\n");