Improved new export a bit by adding tasks to output.
[timetracker.git] / WEB-INF / lib / ttGroupExportHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
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.
31 //
32 // Currently, it is work in progress.
33 // When done, it should handle export of organizations containing multiple groups.
34 class ttGroupExportHelper {
35
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.
40
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(); // User ids.
44   var $roleMap   = array(); // Role ids.
45   var $taskMap   = array(); // Task ids.
46   var $clientMap = array(); // Client ids.
47
48   // Constructor.
49   function __construct($group_id, $file, $indentation) {
50     global $user;
51
52     $this->group_id = $group_id;
53     $this->file = $file;
54     $this->indentation = $indentation;
55
56     // Build a list of subgroups.
57     $mdb2 = getConnection();
58     $sql =  "select id from tt_groups".
59             " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
60     $res = $mdb2->query($sql);
61     if (!is_a($res, 'PEAR_Error')) {
62       while ($val = $res->fetchRow()) {
63         $this->subgroups[] = $val;
64       }
65     }
66   }
67
68   // getGroupData obtains group attributes for export.
69   function getGroupData() {
70     global $user;
71     $mdb2 = getConnection();
72
73     $sql =  "select name, currency, lang from tt_groups".
74             " where status = 1 and id = $this->group_id and org_id = $user->org_id";
75     $res = $mdb2->query($sql);
76     if (!is_a($res, 'PEAR_Error')) {
77       $val = $res->fetchRow();
78     }
79     return $val;
80   }
81
82   // The getUsers obtains all users in group for the purpose of export.
83   function getUsers() {
84     global $user;
85     $mdb2 = getConnection();
86
87     $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
88       " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
89     $res = $mdb2->query($sql);
90     $result = array();
91     if (!is_a($res, 'PEAR_Error')) {
92       while ($val = $res->fetchRow()) {
93         $result[] = $val;
94       }
95       return $result;
96     }
97     return false;
98   }
99
100   // getRoles - obtains all roles defined for group.
101   function getRoles() {
102     global $user;
103     $mdb2 = getConnection();
104
105     $result = array();
106     $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
107     $res = $mdb2->query($sql);
108     $result = array();
109     if (!is_a($res, 'PEAR_Error')) {
110       while ($val = $res->fetchRow()) {
111         $result[] = $val;
112       }
113       return $result;
114     }
115     return false;
116   }
117
118   // getTasks - obtains all tasks defined for group.
119   function getTasks() {
120     global $user;
121     $mdb2 = getConnection();
122
123     $result = array();
124     $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
125     $res = $mdb2->query($sql);
126     $result = array();
127     if (!is_a($res, 'PEAR_Error')) {
128       while ($val = $res->fetchRow()) {
129         $result[] = $val;
130       }
131       return $result;
132     }
133     return false;
134   }
135
136   // writeData writes group data into file.
137   function writeData() {
138
139     // Write group info.
140     $group = $this->getGroupData();
141     $group_part = "<group name=\"".htmlentities($group['name'])."\"";
142     $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
143     $group_part .= " lang=\"".$group['lang']."\"";
144     // TODO: add other group attributes here.
145     $group_part .= ">\n";
146
147     // Write group info.
148     fwrite($this->file, $this->indentation.$group_part);
149
150     // Prepare user map.
151     $users = $this->getUsers();
152     foreach ($users as $key=>$user_item)
153       $this->userMap[$user_item['id']] = $key + 1;
154
155     // Prepare role map.
156     $roles = $this->getRoles();
157     foreach ($roles as $key=>$role_item)
158       $this->roleMap[$role_item['id']] = $key + 1;
159
160     // Prepare task map.
161     $tasks = $this->getTasks();
162     foreach ($tasks as $key=>$task_item)
163       $this->taskMap[$task_item['id']] = $key + 1;
164
165     // Prepare client map.
166     $clients = ttTeamHelper::getAllClients($this->group_id, true);
167     foreach ($clients as $key=>$client_item)
168       $this->clientMap[$client_item['id']] = $key + 1;
169
170     // Write roles.
171     fwrite($this->file, $this->indentation."  <roles>\n");
172     foreach ($roles as $role) {
173       $role_part = $this->indentation.'    '."<role id=\"".$this->roleMap[$role['id']]."\"";
174       $role_part .= " name=\"".htmlentities($role['name'])."\"";
175       $role_part .= " description=\"".htmlentities($role['description'])."\"";
176       $role_part .= " rank=\"".$role['rank']."\"";
177       $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
178       $role_part .= " status=\"".$role['status']."\"";
179       $role_part .= "></role>\n";
180       fwrite($this->file, $role_part);
181     }
182     fwrite($this->file, $this->indentation."  </roles>\n");
183
184     // Write tasks.
185     fwrite($this->file, $this->indentation."  <tasks>\n");
186     foreach ($tasks as $task) {
187       $task_part = $this->indentation.'    '."<task id=\"".$this->taskMap[$task['id']]."\"";
188       $task_part .= " name=\"".htmlentities($task['name'])."\"";
189       $task_part .= " description=\"".htmlentities($task['description'])."\"";
190       $task_part .= " status=\"".$task['status']."\"";
191       $task_part .= "></task>\n";
192       fwrite($this->file, $task_part);
193     }
194     fwrite($this->file, $this->indentation."  </tasks>\n");
195
196     // Write users.
197     fwrite($this->file, $this->indentation."  <users>\n");
198     foreach ($users as $user_item) {
199       $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
200       $user_part = $this->indentation.'    '."<user id=\"".$this->userMap[$user_item['id']]."\"";
201       $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
202       $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
203       $user_part .= " password=\"".$user_item['password']."\"";
204       $user_part .= " role_id=\"".$role_id."\"";
205       $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
206       $user_part .= " rate=\"".$user_item['rate']."\"";
207       $user_part .= " email=\"".$user_item['email']."\"";
208       $user_part .= " status=\"".$user_item['status']."\"";
209       $user_part .= "></user>\n";
210       fwrite($this->file, $user_part);
211     }
212     fwrite($this->file, $this->indentation."  </users>\n");
213
214     // Call self recursively for all subgroups.
215     foreach ($this->subgroups as $subgroup) {
216       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
217       $subgroup_helper->writeData();
218     }
219
220     fwrite($this->file, $this->indentation."</group>\n");
221   }
222 }