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.
48 var $invoiceMap = array(); // Invoice ids.
51 function __construct($group_id, $file, $indentation) {
54 $this->group_id = $group_id;
56 $this->indentation = $indentation;
58 // Build a list of subgroups.
59 $mdb2 = getConnection();
60 $sql = "select id from tt_groups".
61 " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
62 $res = $mdb2->query($sql);
63 if (!is_a($res, 'PEAR_Error')) {
64 while ($val = $res->fetchRow()) {
65 $this->subgroups[] = $val;
70 // getGroupData obtains group attributes for export.
71 function getGroupData() {
73 $mdb2 = getConnection();
75 $sql = "select * from tt_groups".
76 " where status = 1 and id = $this->group_id and org_id = $user->org_id";
77 $res = $mdb2->query($sql);
78 if (!is_a($res, 'PEAR_Error')) {
79 $val = $res->fetchRow();
84 // The getUsers obtains all users in group for the purpose of export.
87 $mdb2 = getConnection();
89 $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
90 " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
91 $res = $mdb2->query($sql);
93 if (!is_a($res, 'PEAR_Error')) {
94 while ($val = $res->fetchRow()) {
102 // getRoles - obtains all roles defined for group.
103 function getRoles() {
105 $mdb2 = getConnection();
108 $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
109 $res = $mdb2->query($sql);
111 if (!is_a($res, 'PEAR_Error')) {
112 while ($val = $res->fetchRow()) {
120 // getTasks - obtains all tasks defined for group.
121 function getTasks() {
123 $mdb2 = getConnection();
126 $sql = "select * from tt_tasks where group_id = $this->group_id and org_id = $user->org_id";
127 $res = $mdb2->query($sql);
129 if (!is_a($res, 'PEAR_Error')) {
130 while ($val = $res->fetchRow()) {
138 // getProjects - obtains all projects defined for group.
139 function getProjects() {
141 $mdb2 = getConnection();
144 $sql = "select * from tt_projects where group_id = $this->group_id and org_id = $user->org_id";
145 $res = $mdb2->query($sql);
147 if (!is_a($res, 'PEAR_Error')) {
148 while ($val = $res->fetchRow()) {
156 // getClients - obtains all clients defined for group.
157 function getClients() {
159 $mdb2 = getConnection();
162 $sql = "select * from tt_clients where group_id = $this->group_id and org_id = $user->org_id";
163 $res = $mdb2->query($sql);
165 if (!is_a($res, 'PEAR_Error')) {
166 while ($val = $res->fetchRow()) {
174 // writeData writes group data into file.
175 function writeData() {
178 $group = $this->getGroupData();
179 $group_part = "<group name=\"".htmlentities($group['name'])."\"";
180 $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
181 $group_part .= " decimal_mark=\"".$group['decimal_mark']."\"";
182 $group_part .= " lang=\"".$group['lang']."\"";
183 $group_part .= " date_format=\"".$group['date_format']."\"";
184 $group_part .= " time_format=\"".$group['time_format']."\"";
185 $group_part .= " week_start=\"".$group['week_start']."\"";
186 $group_part .= " tracking_mode=\"".$group['tracking_mode']."\"";
187 $group_part .= " project_required=\"".$group['project_required']."\"";
188 $group_part .= " task_required=\"".$group['task_required']."\"";
189 $group_part .= " record_type=\"".$group['record_type']."\"";
190 $group_part .= " bcc_email=\"".$group['bcc_email']."\"";
191 $group_part .= " allow_ip=\"".$group['allow_ip']."\"";
192 $group_part .= " password_complexity=\"".$group['password_complexity']."\"";
193 $group_part .= " plugins=\"".$group['plugins']."\"";
194 $group_part .= " lock_spec=\"".$group['lock_spec']."\"";
195 $group_part .= " workday_minutes=\"".$group['workday_minutes']."\"";
196 $group_part .= " custom_logo=\"".$group['custom_logo']."\"";
197 $group_part .= " config=\"".$group['config']."\"";
198 $group_part .= ">\n";
201 fwrite($this->file, $this->indentation.$group_part);
204 $users = $this->getUsers();
205 foreach ($users as $key=>$user_item)
206 $this->userMap[$user_item['id']] = $key + 1;
209 $roles = $this->getRoles();
210 foreach ($roles as $key=>$role_item)
211 $this->roleMap[$role_item['id']] = $key + 1;
214 $tasks = $this->getTasks();
215 foreach ($tasks as $key=>$task_item)
216 $this->taskMap[$task_item['id']] = $key + 1;
218 // Prepare project map.
219 $projects = $this->getProjects();
220 foreach ($projects as $key=>$project_item)
221 $this->projectMap[$project_item['id']] = $key + 1;
223 // Prepare client map.
224 $clients = $this->getClients();
225 foreach ($clients as $key=>$client_item)
226 $this->clientMap[$client_item['id']] = $key + 1;
228 // Prepare invoice map.
229 $invoices = ttTeamHelper::getAllInvoices();
230 foreach ($invoices as $key=>$invoice_item)
231 $this->invoiceMap[$invoice_item['id']] = $key + 1;
234 fwrite($this->file, $this->indentation." <roles>\n");
235 foreach ($roles as $role) {
236 $role_part = $this->indentation.' '."<role id=\"".$this->roleMap[$role['id']]."\"";
237 $role_part .= " name=\"".htmlentities($role['name'])."\"";
238 $role_part .= " description=\"".htmlentities($role['description'])."\"";
239 $role_part .= " rank=\"".$role['rank']."\"";
240 $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
241 $role_part .= " status=\"".$role['status']."\"";
242 $role_part .= "></role>\n";
243 fwrite($this->file, $role_part);
245 fwrite($this->file, $this->indentation." </roles>\n");
248 fwrite($this->file, $this->indentation." <tasks>\n");
249 foreach ($tasks as $task) {
250 $task_part = $this->indentation.' '."<task id=\"".$this->taskMap[$task['id']]."\"";
251 $task_part .= " name=\"".htmlentities($task['name'])."\"";
252 $task_part .= " description=\"".htmlentities($task['description'])."\"";
253 $task_part .= " status=\"".$task['status']."\"";
254 $task_part .= "></task>\n";
255 fwrite($this->file, $task_part);
257 fwrite($this->file, $this->indentation." </tasks>\n");
260 fwrite($this->file, $this->indentation." <projects>\n");
261 foreach ($projects as $project_item) {
262 if($project_item['tasks']){
263 $tasks = explode(',', $project_item['tasks']);
264 $tasks_mapped = array();
265 foreach ($tasks as $item)
266 $tasks_mapped[] = $this->taskMap[$item];
267 $tasks_str = implode(',', $tasks_mapped);
269 $project_part = $this->indentation.' '."<project id=\"".$this->projectMap[$project_item['id']]."\"";
270 $project_part .= " name=\"".htmlentities($project_item['name'])."\"";
271 $project_part .= " description=\"".htmlentities($project_item['description'])."\"";
272 $project_part .= " tasks=\"".$tasks_str."\"";
273 $project_part .= " status=\"".$project_item['status']."\"";
274 $project_part .= "></project>\n";
275 fwrite($this->file, $project_part);
277 fwrite($this->file, $this->indentation." </projects>\n");
280 fwrite($this->file, $this->indentation." <clients>\n");
281 foreach ($clients as $client_item) {
282 if($client_item['projects']){
283 $projects_db = explode(',', $client_item['projects']);
284 $projects_mapped = array();
285 foreach ($projects_db as $item)
286 $projects_mapped[] = $this->projectMap[$item];
287 $projects_str = implode(',', $projects_mapped);
289 $client_part = $this->indentation.' '."<client id=\"".$this->clientMap[$client_item['id']]."\"";
290 $client_part .= " name=\"".htmlentities($client_item['name'])."\"";
291 $client_part .= " address=\"".htmlentities($client_item['address'])."\"";
292 $client_part .= " tax=\"".$client_item['tax']."\"";
293 $client_part .= " projects=\"".$projects_str."\"";
294 $client_part .= " status=\"".$client_item['status']."\"";
295 $client_part .= "></client>\n";
296 fwrite($this->file, $client_part);
298 fwrite($this->file, $this->indentation." </clients>\n");
301 fwrite($this->file, $this->indentation." <users>\n");
302 foreach ($users as $user_item) {
303 $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
304 $user_part = $this->indentation.' '."<user id=\"".$this->userMap[$user_item['id']]."\"";
305 $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
306 $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
307 $user_part .= " password=\"".$user_item['password']."\"";
308 $user_part .= " role_id=\"".$role_id."\"";
309 $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
310 $user_part .= " rate=\"".$user_item['rate']."\"";
311 $user_part .= " email=\"".$user_item['email']."\"";
312 $user_part .= " status=\"".$user_item['status']."\"";
313 $user_part .= "></user>\n";
314 fwrite($this->file, $user_part);
316 fwrite($this->file, $this->indentation." </users>\n");
318 // Write user to project binds.
319 fwrite($this->file, $this->indentation." <user_project_binds>\n");
320 $user_binds = ttTeamHelper::getUserToProjectBinds($this->group_id);
321 foreach ($user_binds as $bind) {
322 $user_id = $this->userMap[$bind['user_id']];
323 $project_id = $this->projectMap[$bind['project_id']];
324 $bind_part = $this->indentation.' '."<user_project_bind user_id=\"".$user_id."\"";
325 $bind_part .= " project_id=\"".$project_id."\"";
326 $bind_part .= " rate=\"".$bind['rate']."\"";
327 $bind_part .= " status=\"".$bind['status']."\"";
328 $bind_part .= "></user_project_bind>\n";
329 fwrite($this->file, $bind_part);
331 fwrite($this->file, $this->indentation." </user_project_binds>\n");
334 fwrite($this->file, $this->indentation." <invoices>\n");
335 foreach ($invoices as $invoice_item) {
336 $invoice_part = $this->indentation.' '."<invoice id=\"".$this->invoiceMap[$invoice_item['id']]."\"";
337 $invoice_part .= " name=\"".htmlentities($invoice_item['name'])."\"";
338 $invoice_part .= " date=\"".$invoice_item['date']."\"";
339 $invoice_part .= " client_id=\"".$this->clientMap[$invoice_item['client_id']]."\"";
340 $invoice_part .= " status=\"".$invoice_item['status']."\"";
341 $invoice_part .= "></invoice>\n";
342 fwrite($this->file, $invoice_part);
344 fwrite($this->file, $this->indentation." </invoices>\n");
346 // Call self recursively for all subgroups.
347 foreach ($this->subgroups as $subgroup) {
348 $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.' ');
349 $subgroup_helper->writeData();
352 fwrite($this->file, $this->indentation."</group>\n");