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(); // User ids.
44 var $roleMap = array(); // Role ids.
45 var $taskMap = array(); // Task ids.
46 var $projectMap = array(); // Project ids.
47 var $clientMap = array(); // Client ids.
50 function __construct($group_id, $file, $indentation) {
53 $this->group_id = $group_id;
55 $this->indentation = $indentation;
57 // Build a list of subgroups.
58 $mdb2 = getConnection();
59 $sql = "select id from tt_groups".
60 " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
61 $res = $mdb2->query($sql);
62 if (!is_a($res, 'PEAR_Error')) {
63 while ($val = $res->fetchRow()) {
64 $this->subgroups[] = $val;
69 // getGroupData obtains group attributes for export.
70 function getGroupData() {
72 $mdb2 = getConnection();
74 $sql = "select * from tt_groups".
75 " where status = 1 and id = $this->group_id and org_id = $user->org_id";
76 $res = $mdb2->query($sql);
77 if (!is_a($res, 'PEAR_Error')) {
78 $val = $res->fetchRow();
83 // The getUsers obtains all users in group for the purpose of export.
86 $mdb2 = getConnection();
88 $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
89 " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
90 $res = $mdb2->query($sql);
92 if (!is_a($res, 'PEAR_Error')) {
93 while ($val = $res->fetchRow()) {
101 // getRoles - obtains all roles defined for group.
102 function getRoles() {
104 $mdb2 = getConnection();
107 $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
108 $res = $mdb2->query($sql);
110 if (!is_a($res, 'PEAR_Error')) {
111 while ($val = $res->fetchRow()) {
119 // getTasks - obtains all tasks defined for group.
120 function getTasks() {
122 $mdb2 = getConnection();
125 $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
126 $res = $mdb2->query($sql);
128 if (!is_a($res, 'PEAR_Error')) {
129 while ($val = $res->fetchRow()) {
137 // getProjects - obtains all projects defined for group.
138 function getProjects() {
140 $mdb2 = getConnection();
143 $sql = "select * from tt_projects where group_id = $this->group_id and org_id = $user->org_id";
144 $res = $mdb2->query($sql);
146 if (!is_a($res, 'PEAR_Error')) {
147 while ($val = $res->fetchRow()) {
155 // getClients - obtains all clients defined for group.
156 function getClients() {
158 $mdb2 = getConnection();
161 $sql = "select * from tt_clients where group_id = $this->group_id and org_id = $user->org_id";
162 $res = $mdb2->query($sql);
164 if (!is_a($res, 'PEAR_Error')) {
165 while ($val = $res->fetchRow()) {
173 // writeData writes group data into file.
174 function writeData() {
177 $group = $this->getGroupData();
178 $group_part = "<group name=\"".htmlentities($group['name'])."\"";
179 $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
180 $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
181 $group_part .= " lang=\"".$group['lang']."\"";
182 $group_part .= " date_format=\"".$group['date_format']."\"";
183 $group_part .= " time_format=\"".$group['time_format']."\"";
184 $group_part .= " week_start=\"".$group['week_start']."\"";
185 $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
186 $group_part .= " project_required=\"".$group['project_required']."\"";
187 $group_part .= " task_required=\"".$group['task_required']."\"";
188 $group_part .= " record_type=\"".$group['record_type']."\"";
189 $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
190 $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
191 $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
192 $group_part .= " plugins=\"".$group['plugins']."\"";
193 $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
194 $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
195 $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
196 $group_part .= " config=\"".$group['config']."\"";
197 $group_part .= ">\n";
200 fwrite($this->file, $this->indentation.$group_part);
203 $users = $this->getUsers();
204 foreach ($users as $key=>$user_item)
205 $this->userMap[$user_item['id']] = $key + 1;
208 $roles = $this->getRoles();
209 foreach ($roles as $key=>$role_item)
210 $this->roleMap[$role_item['id']] = $key + 1;
213 $tasks = $this->getTasks();
214 foreach ($tasks as $key=>$task_item)
215 $this->taskMap[$task_item['id']] = $key + 1;
217 // Prepare project map.
218 $projects = $this->getProjects();
219 foreach ($projects as $key=>$project_item)
220 $this->projectMap[$project_item['id']] = $key + 1;
222 // Prepare client map.
223 $clients = $this->getClients();
224 foreach ($clients as $key=>$client_item)
225 $this->clientMap[$client_item['id']] = $key + 1;
228 fwrite($this->file, $this->indentation." <roles>\n");
229 foreach ($roles as $role) {
230 $role_part = $this->indentation.' '."<role id=\"".$this->roleMap[$role['id']]."\"";
231 $role_part .= " name=\"".htmlentities($role['name'])."\"";
232 $role_part .= " description=\"".htmlentities($role['description'])."\"";
233 $role_part .= " rank=\"".$role['rank']."\"";
234 $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
235 $role_part .= " status=\"".$role['status']."\"";
236 $role_part .= "></role>\n";
237 fwrite($this->file, $role_part);
239 fwrite($this->file, $this->indentation." </roles>\n");
242 fwrite($this->file, $this->indentation." <tasks>\n");
243 foreach ($tasks as $task) {
244 $task_part = $this->indentation.' '."<task id=\"".$this->taskMap[$task['id']]."\"";
245 $task_part .= " name=\"".htmlentities($task['name'])."\"";
246 $task_part .= " description=\"".htmlentities($task['description'])."\"";
247 $task_part .= " status=\"".$task['status']."\"";
248 $task_part .= "></task>\n";
249 fwrite($this->file, $task_part);
251 fwrite($this->file, $this->indentation." </tasks>\n");
254 fwrite($this->file, $this->indentation." <projects>\n");
255 foreach ($projects as $project_item) {
256 if($project_item['tasks']){
257 $tasks = explode(',', $project_item['tasks']);
258 $tasks_mapped = array();
259 foreach ($tasks as $item)
260 $tasks_mapped[] = $this->taskMap[$item];
261 $tasks_str = implode(',', $tasks_mapped);
263 $project_part = $this->indentation.' '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
264 $project_part .= " name=\"".htmlentities($project_item['name'])."\"";
265 $project_part .= " description=\"".htmlentities($project_item['description'])."\"";
266 $project_part .= " tasks=\"".$tasks_str."\"";
267 $project_part .= " status=\"".$project_item['status']."\"";
268 $project_part .= "></project>\n";
269 fwrite($this->file, $project_part);
271 fwrite($this->file, $this->indentation." </projects>\n");
274 fwrite($this->file, $this->indentation." <clients>\n");
275 foreach ($clients as $client_item) {
276 if($client_item['projects']){
277 $projects_db = explode(',', $client_item['projects']);
278 $projects_mapped = array();
279 foreach ($projects_db as $item)
280 $projects_mapped[] = $this->projectMap[$item];
281 $projects_str = implode(',', $projects_mapped);
283 $client_part = $this->indentation.' '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
284 $client_part .= " name=\"".htmlentities($client_item['name'])."\"";
285 $client_part .= " address=\"".htmlentities($client_item['address'])."\"";
286 $client_part .= " tax=\"".$client_item['tax']."\"";
287 $client_part .= " projects=\"".$projects_str."\"";
288 $client_part .= " status=\"".$client_item['status']."\"";
289 $client_part .= "></client>\n";
290 fwrite($this->file, $client_part);
292 fwrite($this->file, $this->indentation." </clients>\n");
295 fwrite($this->file, $this->indentation." <users>\n");
296 foreach ($users as $user_item) {
297 $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
298 $user_part = $this->indentation.' '."<user id=\"".$this->userMap[$user_item['id']]."\"";
299 $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
300 $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
301 $user_part .= " password=\"".$user_item['password']."\"";
302 $user_part .= " role_id=\"".$role_id."\"";
303 $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
304 $user_part .= " rate=\"".$user_item['rate']."\"";
305 $user_part .= " email=\"".$user_item['email']."\"";
306 $user_part .= " status=\"".$user_item['status']."\"";
307 $user_part .= "></user>\n";
308 fwrite($this->file, $user_part);
310 fwrite($this->file, $this->indentation." </users>\n");
312 // Write user to project binds.
313 fwrite($this->file, $this->indentation." <user_project_binds>\n");
314 $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
315 foreach ($user_binds as $bind) {
316 $user_id = $this->userMap[$bind['user_id']];
317 $project_id = $this->projectMap[$bind['project_id']];
318 $bind_part = $this->indentation.' '."<user_project_bind user_id=\"".$user_id."\"";
319 $bind_part .= " project_id=\"".$project_id."\"";
320 $bind_part .= " rate=\"".$bind['rate']."\"";
321 $bind_part .= " status=\"".$bind['status']."\"";
322 $bind_part .= "></user_project_bind>\n";
323 fwrite($this->file, $bind_part);
325 fwrite($this->file, $this->indentation." </user_project_binds>\n");
327 // Call self recursively for all subgroups.
328 foreach ($this->subgroups as $subgroup) {
329 $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.' ');
330 $subgroup_helper->writeData();
333 fwrite($this->file, $this->indentation."</group>\n");