Improved new export-import by adding time log.
[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   var $invoiceMap = array(); // Invoice ids.
49   var $logMap     = array(); // Time log ids.
50
51   // Constructor.
52   function __construct($group_id, $file, $indentation) {
53     global $user;
54
55     $this->group_id = $group_id;
56     $this->file = $file;
57     $this->indentation = $indentation;
58
59     // Build a list of subgroups.
60     $mdb2 = getConnection();
61     $sql =  "select id from tt_groups".
62             " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
63     $res = $mdb2->query($sql);
64     if (!is_a($res, 'PEAR_Error')) {
65       while ($val = $res->fetchRow()) {
66         $this->subgroups[] = $val;
67       }
68     }
69   }
70
71   // getGroupData obtains group attributes for export.
72   function getGroupData() {
73     global $user;
74     $mdb2 = getConnection();
75
76     $sql =  "select * from tt_groups".
77             " where status = 1 and id = $this->group_id and org_id = $user->org_id";
78     $res = $mdb2->query($sql);
79     if (!is_a($res, 'PEAR_Error')) {
80       $val = $res->fetchRow();
81     }
82     return $val;
83   }
84
85   // The getUsers obtains all users in group for the purpose of export.
86   function getUsers() {
87     global $user;
88     $mdb2 = getConnection();
89
90     $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
91       " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
92     $res = $mdb2->query($sql);
93     $result = array();
94     if (!is_a($res, 'PEAR_Error')) {
95       while ($val = $res->fetchRow()) {
96         $result[] = $val;
97       }
98       return $result;
99     }
100     return false;
101   }
102
103   // getRoles - obtains all roles defined for group.
104   function getRoles() {
105     global $user;
106     $mdb2 = getConnection();
107
108     $result = array();
109     $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
110     $res = $mdb2->query($sql);
111     $result = array();
112     if (!is_a($res, 'PEAR_Error')) {
113       while ($val = $res->fetchRow()) {
114         $result[] = $val;
115       }
116       return $result;
117     }
118     return false;
119   }
120
121   // getTasks - obtains all tasks defined for group.
122   function getTasks() {
123     global $user;
124     $mdb2 = getConnection();
125
126     $result = array();
127     $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
128     $res = $mdb2->query($sql);
129     $result = array();
130     if (!is_a($res, 'PEAR_Error')) {
131       while ($val = $res->fetchRow()) {
132         $result[] = $val;
133       }
134       return $result;
135     }
136     return false;
137   }
138
139   // getProjects - obtains all projects defined for group.
140   function getProjects() {
141     global $user;
142     $mdb2 = getConnection();
143
144     $result = array();
145     $sql = "select * from tt_projects where group_id = $this->group_id and org_id = $user->org_id";
146     $res = $mdb2->query($sql);
147     $result = array();
148     if (!is_a($res, 'PEAR_Error')) {
149       while ($val = $res->fetchRow()) {
150         $result[] = $val;
151       }
152       return $result;
153     }
154     return false;
155   }
156
157   // getClients - obtains all clients defined for group.
158   function getClients() {
159     global $user;
160     $mdb2 = getConnection();
161
162     $result = array();
163     $sql = "select * from tt_clients where group_id = $this->group_id and org_id = $user->org_id";
164     $res = $mdb2->query($sql);
165     $result = array();
166     if (!is_a($res, 'PEAR_Error')) {
167       while ($val = $res->fetchRow()) {
168         $result[] = $val;
169       }
170       return $result;
171     }
172     return false;
173   }
174
175   // writeData writes group data into file.
176   function writeData() {
177
178     // Write group info.
179     $group = $this->getGroupData();
180     $group_part = "<group name=\"".htmlentities($group['name'])."\"";
181     $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
182     $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
183     $group_part .= " lang=\"".$group['lang']."\"";
184     $group_part .= " date_format=\"".$group['date_format']."\"";
185     $group_part .= " time_format=\"".$group['time_format']."\"";
186     $group_part .= " week_start=\"".$group['week_start']."\"";
187     $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
188     $group_part .= " project_required=\"".$group['project_required']."\"";
189     $group_part .= " task_required=\"".$group['task_required']."\"";
190     $group_part .= " record_type=\"".$group['record_type']."\"";
191     $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
192     $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
193     $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
194     $group_part .= " plugins=\"".$group['plugins']."\"";
195     $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
196     $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
197     $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
198     $group_part .= " config=\"".$group['config']."\"";
199     $group_part .= ">\n";
200
201     // Write group info.
202     fwrite($this->file, $this->indentation.$group_part);
203
204     // Prepare user map.
205     $users = $this->getUsers();
206     foreach ($users as $key=>$user_item)
207       $this->userMap[$user_item['id']] = $key + 1;
208
209     // Prepare role map.
210     $roles = $this->getRoles();
211     foreach ($roles as $key=>$role_item)
212       $this->roleMap[$role_item['id']] = $key + 1;
213
214     // Prepare task map.
215     $tasks = $this->getTasks();
216     foreach ($tasks as $key=>$task_item)
217       $this->taskMap[$task_item['id']] = $key + 1;
218
219     // Prepare project map.
220     $projects = $this->getProjects();
221     foreach ($projects as $key=>$project_item)
222       $this->projectMap[$project_item['id']] = $key + 1;
223
224     // Prepare client map.
225     $clients = $this->getClients();
226     foreach ($clients as $key=>$client_item)
227       $this->clientMap[$client_item['id']] = $key + 1;
228
229     // Prepare invoice map.
230     $invoices = ttTeamHelper::getAllInvoices();
231     foreach ($invoices as $key=>$invoice_item)
232       $this->invoiceMap[$invoice_item['id']] = $key + 1;
233
234     // Write roles.
235     fwrite($this->file, $this->indentation."  <roles>\n");
236     foreach ($roles as $role) {
237       $role_part = $this->indentation.'    '."<role id=\"".$this->roleMap[$role['id']]."\"";
238       $role_part .= " name=\"".htmlentities($role['name'])."\"";
239       $role_part .= " description=\"".htmlentities($role['description'])."\"";
240       $role_part .= " rank=\"".$role['rank']."\"";
241       $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
242       $role_part .= " status=\"".$role['status']."\"";
243       $role_part .= "></role>\n";
244       fwrite($this->file, $role_part);
245     }
246     fwrite($this->file, $this->indentation."  </roles>\n");
247
248     // Write tasks.
249     fwrite($this->file, $this->indentation."  <tasks>\n");
250     foreach ($tasks as $task) {
251       $task_part = $this->indentation.'    '."<task id=\"".$this->taskMap[$task['id']]."\"";
252       $task_part .= " name=\"".htmlentities($task['name'])."\"";
253       $task_part .= " description=\"".htmlentities($task['description'])."\"";
254       $task_part .= " status=\"".$task['status']."\"";
255       $task_part .= "></task>\n";
256       fwrite($this->file, $task_part);
257     }
258     fwrite($this->file, $this->indentation."  </tasks>\n");
259
260     // Write projects.
261     fwrite($this->file, $this->indentation."  <projects>\n");
262     foreach ($projects as $project_item) {
263       if($project_item['tasks']){
264         $tasks = explode(',', $project_item['tasks']);
265         $tasks_mapped = array();
266         foreach ($tasks as $item)
267           $tasks_mapped[] = $this->taskMap[$item];
268         $tasks_str = implode(',', $tasks_mapped);
269       }
270       $project_part = $this->indentation.'    '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
271       $project_part .= " name=\"".htmlentities($project_item['name'])."\"";
272       $project_part .= " description=\"".htmlentities($project_item['description'])."\"";
273       $project_part .= " tasks=\"".$tasks_str."\"";
274       $project_part .= " status=\"".$project_item['status']."\"";
275       $project_part .= "></project>\n";
276       fwrite($this->file, $project_part);
277     }
278     fwrite($this->file, $this->indentation."  </projects>\n");
279
280     // Write clients.
281     fwrite($this->file, $this->indentation."  <clients>\n");
282     foreach ($clients as $client_item) {
283       if($client_item['projects']){
284         $projects_db = explode(',', $client_item['projects']);
285         $projects_mapped = array();
286         foreach ($projects_db as $item)
287           $projects_mapped[] = $this->projectMap[$item];
288         $projects_str = implode(',', $projects_mapped);
289       }
290       $client_part = $this->indentation.'    '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
291       $client_part .= " name=\"".htmlentities($client_item['name'])."\"";
292       $client_part .= " address=\"".htmlentities($client_item['address'])."\"";
293       $client_part .= " tax=\"".$client_item['tax']."\"";
294       $client_part .= " projects=\"".$projects_str."\"";
295       $client_part .= " status=\"".$client_item['status']."\"";
296       $client_part .= "></client>\n";
297       fwrite($this->file, $client_part);
298     }
299     fwrite($this->file, $this->indentation."  </clients>\n");
300
301     // Write users.
302     fwrite($this->file, $this->indentation."  <users>\n");
303     foreach ($users as $user_item) {
304       $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
305       $user_part = $this->indentation.'    '."<user id=\"".$this->userMap[$user_item['id']]."\"";
306       $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
307       $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
308       $user_part .= " password=\"".$user_item['password']."\"";
309       $user_part .= " role_id=\"".$role_id."\"";
310       $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
311       $user_part .= " rate=\"".$user_item['rate']."\"";
312       $user_part .= " email=\"".$user_item['email']."\"";
313       $user_part .= " status=\"".$user_item['status']."\"";
314       $user_part .= "></user>\n";
315       fwrite($this->file, $user_part);
316     }
317     fwrite($this->file, $this->indentation."  </users>\n");
318
319     // Write user to project binds.
320     fwrite($this->file, $this->indentation."  <user_project_binds>\n");
321     $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
322     foreach ($user_binds as $bind) {
323       $user_id = $this->userMap[$bind['user_id']];
324       $project_id = $this->projectMap[$bind['project_id']];
325       $bind_part = $this->indentation.'    '."<user_project_bind user_id=\"".$user_id."\"";
326       $bind_part .= " project_id=\"".$project_id."\"";
327       $bind_part .= " rate=\"".$bind['rate']."\"";
328       $bind_part .= " status=\"".$bind['status']."\"";
329       $bind_part .= "></user_project_bind>\n";
330       fwrite($this->file, $bind_part);
331     }
332     fwrite($this->file, $this->indentation."  </user_project_binds>\n");
333
334     // Write invoices.
335     fwrite($this->file, $this->indentation."  <invoices>\n");
336     foreach ($invoices as $invoice_item) {
337       $invoice_part = $this->indentation.'    '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
338       $invoice_part .= " name=\"".htmlentities($invoice_item['name'])."\"";
339       $invoice_part .= " date=\"".$invoice_item['date']."\"";
340       $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
341       $invoice_part .= " status=\"".$invoice_item['status']."\"";
342       $invoice_part .= "></invoice>\n";
343       fwrite($this->file, $invoice_part);
344     }
345     fwrite($this->file, $this->indentation."  </invoices>\n");
346
347     // Write time log entries and build logMap at the same time.
348     fwrite($this->file, $this->indentation."  <log>\n");
349     $key = 0;
350     foreach ($users as $user_item) {
351       $records = ttTimeHelper::getAllRecords($user_item['id']);
352       foreach ($records as $record) {
353         $key++;
354         $this->logMap[$record['id']] = $key;
355         $log_part = $this->indentation.'    '."<log_item id=\"$key\"";
356         $log_part .= " user_id=\"".$this->userMap[$record['user_id']]."\"";
357         $log_part .= " date=\"".$record['date']."\"";
358         $log_part .= " start=\"".$record['start']."\"";
359         $log_part .= " finish=\"".$record['finish']."\"";
360         $log_part .= " duration=\"".($record['start']?"":$record['duration'])."\"";
361         $log_part .= " client_id=\"".$this->clientMap[$record['client_id']]."\"";
362         $log_part .= " project_id=\"".$this->projectMap[$record['project_id']]."\"";
363         $log_part .= " task_id=\"".$this->taskMap[$record['task_id']]."\"";
364         $log_part .= " invoice_id=\"".$this->invoiceMap[$record['invoice_id']]."\"";
365         $log_part .= " comment=\"".htmlentities($record['comment'])."\"";
366         $log_part .= " billable=\"".$record['billable']."\"";
367         $log_part .= " paid=\"".$record['paid']."\"";
368         $log_part .= " status=\"".$record['status']."\"";
369         $log_part .= "></log_item>\n";
370         fwrite($this->file, $log_part);
371       }
372     }
373     fwrite($this->file, $this->indentation."  </log>\n");
374     unset($records);
375
376     // Call self recursively for all subgroups.
377     foreach ($this->subgroups as $subgroup) {
378       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
379       $subgroup_helper->writeData();
380     }
381
382     fwrite($this->file, $this->indentation."</group>\n");
383   }
384 }