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