Improved new export by adding clients in 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 $projectMap = array(); // Project ids.
47   var $clientMap  = array(); // Client ids.
48
49   // Constructor.
50   function __construct($group_id, $file, $indentation) {
51     global $user;
52
53     $this->group_id = $group_id;
54     $this->file = $file;
55     $this->indentation = $indentation;
56
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;
65       }
66     }
67   }
68
69   // getGroupData obtains group attributes for export.
70   function getGroupData() {
71     global $user;
72     $mdb2 = getConnection();
73
74     $sql =  "select name, currency, lang 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();
79     }
80     return $val;
81   }
82
83   // The getUsers obtains all users in group for the purpose of export.
84   function getUsers() {
85     global $user;
86     $mdb2 = getConnection();
87
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);
91     $result = array();
92     if (!is_a($res, 'PEAR_Error')) {
93       while ($val = $res->fetchRow()) {
94         $result[] = $val;
95       }
96       return $result;
97     }
98     return false;
99   }
100
101   // getRoles - obtains all roles defined for group.
102   function getRoles() {
103     global $user;
104     $mdb2 = getConnection();
105
106     $result = array();
107     $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
108     $res = $mdb2->query($sql);
109     $result = array();
110     if (!is_a($res, 'PEAR_Error')) {
111       while ($val = $res->fetchRow()) {
112         $result[] = $val;
113       }
114       return $result;
115     }
116     return false;
117   }
118
119   // getTasks - obtains all tasks defined for group.
120   function getTasks() {
121     global $user;
122     $mdb2 = getConnection();
123
124     $result = array();
125     $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
126     $res = $mdb2->query($sql);
127     $result = array();
128     if (!is_a($res, 'PEAR_Error')) {
129       while ($val = $res->fetchRow()) {
130         $result[] = $val;
131       }
132       return $result;
133     }
134     return false;
135   }
136
137   // getProjects - obtains all projects defined for group.
138   function getProjects() {
139     global $user;
140     $mdb2 = getConnection();
141
142     $result = array();
143     $sql = "select * from tt_projects where group_id = $this->group_id and org_id = $user->org_id";
144     $res = $mdb2->query($sql);
145     $result = array();
146     if (!is_a($res, 'PEAR_Error')) {
147       while ($val = $res->fetchRow()) {
148         $result[] = $val;
149       }
150       return $result;
151     }
152     return false;
153   }
154
155   // getClients - obtains all clients defined for group.
156   function getClients() {
157     global $user;
158     $mdb2 = getConnection();
159
160     $result = array();
161     $sql = "select * from tt_clients where group_id = $this->group_id and org_id = $user->org_id";
162     $res = $mdb2->query($sql);
163     $result = array();
164     if (!is_a($res, 'PEAR_Error')) {
165       while ($val = $res->fetchRow()) {
166         $result[] = $val;
167       }
168       return $result;
169     }
170     return false;
171   }
172
173   // writeData writes group data into file.
174   function writeData() {
175
176     // Write group info.
177     $group = $this->getGroupData();
178     $group_part = "<group name=\"".htmlentities($group['name'])."\"";
179     $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
180     $group_part .= " lang=\"".$group['lang']."\"";
181     // TODO: add other group attributes here.
182     $group_part .= ">\n";
183
184     // Write group info.
185     fwrite($this->file, $this->indentation.$group_part);
186
187     // Prepare user map.
188     $users = $this->getUsers();
189     foreach ($users as $key=>$user_item)
190       $this->userMap[$user_item['id']] = $key + 1;
191
192     // Prepare role map.
193     $roles = $this->getRoles();
194     foreach ($roles as $key=>$role_item)
195       $this->roleMap[$role_item['id']] = $key + 1;
196
197     // Prepare task map.
198     $tasks = $this->getTasks();
199     foreach ($tasks as $key=>$task_item)
200       $this->taskMap[$task_item['id']] = $key + 1;
201
202     // Prepare project map.
203     $projects = $this->getProjects();
204     foreach ($projects as $key=>$project_item)
205       $this->projectMap[$project_item['id']] = $key + 1;
206
207     // Prepare client map.
208     $clients = $this->getClients();
209     foreach ($clients as $key=>$client_item)
210       $this->clientMap[$client_item['id']] = $key + 1;
211
212     // Write roles.
213     fwrite($this->file, $this->indentation."  <roles>\n");
214     foreach ($roles as $role) {
215       $role_part = $this->indentation.'    '."<role id=\"".$this->roleMap[$role['id']]."\"";
216       $role_part .= " name=\"".htmlentities($role['name'])."\"";
217       $role_part .= " description=\"".htmlentities($role['description'])."\"";
218       $role_part .= " rank=\"".$role['rank']."\"";
219       $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
220       $role_part .= " status=\"".$role['status']."\"";
221       $role_part .= "></role>\n";
222       fwrite($this->file, $role_part);
223     }
224     fwrite($this->file, $this->indentation."  </roles>\n");
225
226     // Write tasks.
227     fwrite($this->file, $this->indentation."  <tasks>\n");
228     foreach ($tasks as $task) {
229       $task_part = $this->indentation.'    '."<task id=\"".$this->taskMap[$task['id']]."\"";
230       $task_part .= " name=\"".htmlentities($task['name'])."\"";
231       $task_part .= " description=\"".htmlentities($task['description'])."\"";
232       $task_part .= " status=\"".$task['status']."\"";
233       $task_part .= "></task>\n";
234       fwrite($this->file, $task_part);
235     }
236     fwrite($this->file, $this->indentation."  </tasks>\n");
237
238     // Write projects.
239     fwrite($this->file, $this->indentation."  <projects>\n");
240     foreach ($projects as $project_item) {
241       if($project_item['tasks']){
242         $tasks = explode(',', $project_item['tasks']);
243         $tasks_mapped = array();
244         foreach ($tasks as $item)
245           $tasks_mapped[] = $this->taskMap[$item];
246         $tasks_str = implode(',', $tasks_mapped);
247       }
248       $project_part = $this->indentation.'    '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
249       $project_part .= " name=\"".htmlentities($project_item['name'])."\"";
250       $project_part .= " description=\"".htmlentities($project_item['description'])."\"";
251       $project_part .= " tasks=\"".$tasks_str."\"";
252       $project_part .= " status=\"".$project_item['status']."\"";
253       $project_part .= "></project>\n";
254       fwrite($this->file, $project_part);
255     }
256     fwrite($this->file, $this->indentation."  </projects>\n");
257
258     // Write clients.
259     fwrite($this->file, $this->indentation."  <clients>\n");
260     foreach ($clients as $client_item) {
261       if($client_item['projects']){
262         $projects_db = explode(',', $client_item['projects']);
263         $projects_mapped = array();
264         foreach ($projects_db as $item)
265           $projects_mapped[] = $this->projectMap[$item];
266         $projects_str = implode(',', $projects_mapped);
267       }
268       $client_part = $this->indentation.'    '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
269       $client_part .= " name=\"".htmlentities($client_item['name'])."\"";
270       $client_part .= " address=\"".htmlentities($client_item['address'])."\"";
271       $client_part .= " tax=\"".$client_item['tax']."\"";
272       $client_part .= " projects=\"".$projects_str."\"";
273       $client_part .= " status=\"".$client_item['status']."\"";
274       $client_part .= "></client>\n";
275       fwrite($this->file, $client_part);
276     }
277     fwrite($this->file, $this->indentation."  </clients>\n");
278
279     // Write users.
280     fwrite($this->file, $this->indentation."  <users>\n");
281     foreach ($users as $user_item) {
282       $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
283       $user_part = $this->indentation.'    '."<user id=\"".$this->userMap[$user_item['id']]."\"";
284       $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
285       $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
286       $user_part .= " password=\"".$user_item['password']."\"";
287       $user_part .= " role_id=\"".$role_id."\"";
288       $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
289       $user_part .= " rate=\"".$user_item['rate']."\"";
290       $user_part .= " email=\"".$user_item['email']."\"";
291       $user_part .= " status=\"".$user_item['status']."\"";
292       $user_part .= "></user>\n";
293       fwrite($this->file, $user_part);
294     }
295     fwrite($this->file, $this->indentation."  </users>\n");
296
297     // Call self recursively for all subgroups.
298     foreach ($this->subgroups as $subgroup) {
299       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
300       $subgroup_helper->writeData();
301     }
302
303     fwrite($this->file, $this->indentation."</group>\n");
304   }
305 }