Improved new export to include roles 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 $clientMap = array(); // Client ids.
46
47   // Constructor.
48   function __construct($group_id, $file, $indentation) {
49     global $user;
50
51     $this->group_id = $group_id;
52     $this->file = $file;
53     $this->indentation = $indentation;
54
55     // Build a list of subgroups.
56     $mdb2 = getConnection();
57     $sql =  "select id from tt_groups".
58             " where status = 1 and parent_id = $this->group_id and org_id = $user->org_id";
59     $res = $mdb2->query($sql);
60     if (!is_a($res, 'PEAR_Error')) {
61       while ($val = $res->fetchRow()) {
62         $this->subgroups[] = $val;
63       }
64     }
65   }
66
67   // getGroupData obtains group attributes for export.
68   function getGroupData() {
69     global $user;
70     $mdb2 = getConnection();
71
72     $sql =  "select name, currency, lang from tt_groups".
73             " where status = 1 and id = $this->group_id and org_id = $user->org_id";
74     $res = $mdb2->query($sql);
75     if (!is_a($res, 'PEAR_Error')) {
76       $val = $res->fetchRow();
77     }
78     return $val;
79   }
80
81   // The getUsers obtains all users in group for the purpose of export.
82   function getUsers() {
83     global $user;
84     $mdb2 = getConnection();
85
86     $sql = "select u.*, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id)".
87       " where u.group_id = $this->group_id and u.org_id = $user->org_id order by upper(u.name)"; // Note: deleted users are included.
88     $res = $mdb2->query($sql);
89     $result = array();
90     if (!is_a($res, 'PEAR_Error')) {
91       while ($val = $res->fetchRow()) {
92         $result[] = $val;
93       }
94       return $result;
95     }
96     return false;
97   }
98
99   // getRoles - obtains all roles defined for group.
100   function getRoles() {
101     global $user;
102     $mdb2 = getConnection();
103
104     $result = array();
105     $sql = "select * from tt_roles where group_id = $this->group_id and org_id = $user->org_id";
106     $res = $mdb2->query($sql);
107     $result = array();
108     if (!is_a($res, 'PEAR_Error')) {
109       while ($val = $res->fetchRow()) {
110         $result[] = $val;
111       }
112       return $result;
113     }
114     return false;
115   }
116
117   // writeData writes group data into file.
118   function writeData() {
119
120     // Write group info.
121     $group = $this->getGroupData();
122     $group_part = "<group name=\"".htmlentities($group['name'])."\"";
123     $group_part .= " currency=\"".htmlentities($group['currency'])."\"";
124     $group_part .= " lang=\"".$group['lang']."\"";
125     // TODO: add other group attributes here.
126     $group_part .= ">\n";
127
128     // Write group info.
129     fwrite($this->file, $this->indentation.$group_part);
130
131     // Prepare user map.
132     $users = $this->getUsers();
133     foreach ($users as $key=>$user_item)
134       $this->userMap[$user_item['id']] = $key + 1;
135
136     // Prepare role map.
137     $roles = $this->getRoles();
138     foreach ($roles as $key=>$role_item)
139       $this->roleMap[$role_item['id']] = $key + 1;
140
141     // Prepare client map.
142     $clients = ttTeamHelper::getAllClients($this->group_id, true);
143     foreach ($clients as $key=>$client_item)
144       $this->clientMap[$client_item['id']] = $key + 1;
145
146     // Write roles.
147     fwrite($this->file, $this->indentation."<roles>\n");
148     foreach ($roles as $role) {
149       $role_part = $this->indentation.'  '."<role id=\"".$this->roleMap[$role['id']]."\"";
150       $role_part .= " name=\"".htmlentities($role['name'])."\"";
151       $role_part .= " description=\"".htmlentities($role['description'])."\"";
152       $role_part .= " rank=\"".$role['rank']."\"";
153       $role_part .= " rights=\"".htmlentities($role['rights'])."\"";
154       $role_part .= " status=\"".$role['status']."\"";
155       $role_part .= "></role>\n";
156       fwrite($this->file, $role_part);
157     }
158     fwrite($this->file, $this->indentation."</roles>\n");
159
160     // Write users.
161     fwrite($this->file, $this->indentation."<users>\n");
162     foreach ($users as $user_item) {
163       $role_id = $user_item['rank'] == 512 ? 0 : $this->roleMap[$user_item['role_id']]; // Special role_id 0 (not null) for top manager.
164       $user_part = $this->indentation.'  '."<user id=\"".$this->userMap[$user_item['id']]."\"";
165       $user_part .= " name=\"".htmlentities($user_item['name'])."\"";
166       $user_part .= " login=\"".htmlentities($user_item['login'])."\"";
167       $user_part .= " password=\"".$user_item['password']."\"";
168       $user_part .= " role_id=\"".$role_id."\"";
169       $user_part .= " client_id=\"".$this->clientMap[$user_item['client_id']]."\"";
170       $user_part .= " rate=\"".$user_item['rate']."\"";
171       $user_part .= " email=\"".$user_item['email']."\"";
172       $user_part .= " status=\"".$user_item['status']."\"";
173       $user_part .= "></user>\n";
174       fwrite($this->file, $user_part);
175     }
176     fwrite($this->file, $this->indentation."</users>\n");
177
178     // Call self recursively for all subgroups.
179     foreach ($this->subgroups as $subgroup) {
180       $subgroup_helper = new ttGroupExportHelper($subgroup['id'], $this->file, $this->indentation.'  ');
181       $subgroup_helper->writeData();
182     }
183
184     fwrite($this->file, $this->indentation."</group>\n");
185   }
186 }