A bit more refactoring for subgroups.
[timetracker.git] / WEB-INF / lib / ttProjectHelper.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 import('ttTeamHelper');
30 import('ttUserHelper');
31 import('ttGroupHelper');
32
33 // Class ttProjectHelper is used to help with project related tasks.
34 class ttProjectHelper {
35         
36   // getAssignedProjects - returns an array of assigned projects.
37   static function getAssignedProjects($user_id)
38   {
39     global $user;
40         
41     $result = array();
42     $mdb2 = getConnection();
43     
44     // Do a query with inner join to get assigned projects.
45     $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p".
46       " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
47       " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
48     $res = $mdb2->query($sql);
49     if (!is_a($res, 'PEAR_Error')) {
50       while ($val = $res->fetchRow()) {
51         $result[] = $val;
52       }
53     }
54     return $result;
55   }
56
57   // getRates - returns an array of project rates for user, including deassigned and deactivated projects.
58   static function getRates($user_id)
59   {
60     global $user;
61         
62     $result = array();
63     $mdb2 = getConnection();
64     
65     $sql = "select p.id, upb.rate from tt_projects p".
66       " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id)".
67       " where group_id = ".$user->getGroup();
68     $res = $mdb2->query($sql);
69     if (!is_a($res, 'PEAR_Error')) {
70       while ($val = $res->fetchRow()) {
71         $val['rate'] = str_replace('.', $user->decimal_mark, $val['rate']);
72         $result[] = $val;
73       }
74     }
75     return $result;
76   }
77   
78   // getProjects - returns an array of active and inactive projects in group.
79   static function getProjects()
80   {
81     global $user;
82
83     $result = array();
84     $mdb2 = getConnection();
85     
86     $sql = "select id, name, tasks from tt_projects".
87       " where group_id = ".$user->getGroup()." and (status = 0 or status = 1) order by name";
88         
89     $res = $mdb2->query($sql);
90     if (!is_a($res, 'PEAR_Error')) {
91       while ($val = $res->fetchRow()) {
92         $result[] = $val;
93       }
94     }
95     return $result;
96   }
97
98   // getProjectsForClient - returns an array of active and inactive projects in a group for a client.
99   static function getProjectsForClient()
100   {
101     global $user;
102
103     $result = array();
104     $mdb2 = getConnection();
105
106     $sql = "select p.id, p.name, p.tasks from tt_projects p".
107       " inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)".
108       " where p.group_id = ".$user->getGroup()." and (p.status = 0 or p.status = 1)".
109       " order by p.name";
110
111     $res = $mdb2->query($sql);
112     if (!is_a($res, 'PEAR_Error')) {
113       while ($val = $res->fetchRow()) {
114         $result[] = $val;
115       }
116     }
117     return $result;
118   }
119   
120   
121   // get - gets details of the project identified by its id. 
122   static function get($id)
123   {
124     global $user;
125  
126     $mdb2 = getConnection();
127
128     $sql = "select id, name, description, status, tasks from tt_projects where id = $id and group_id = ".
129             $user->getGroup()." and (status = 0 or status = 1)";
130     $res = $mdb2->query($sql);
131     if (!is_a($res, 'PEAR_Error')) {
132       $val = $res->fetchRow();
133           if ($val && $val['id'])
134         return $val;
135     }
136     return false;
137   }
138   
139   // The getProjectByName looks up a project by name.
140   static function getProjectByName($name) {
141         
142     $mdb2 = getConnection();
143     global $user;
144
145     $sql = "select id from tt_projects where group_id = ".
146       $user->getGroup()." and name = ".$mdb2->quote($name).
147       " and (status = 1 or status = 0)";
148     $res = $mdb2->query($sql);
149     if (!is_a($res, 'PEAR_Error')) {
150       $val = $res->fetchRow();
151       if ($val && $val['id'])
152         return $val;
153     }
154     return false;
155   }
156   
157   
158   // delete - deletes things associated with a project and marks the project as deleted. 
159   static function delete($id) {
160     global $user;
161     $mdb2 = getConnection();
162
163     $group_id = $user->getGroup();
164     $org_id = $user->org_id;
165
166     // Start with project itself. Reason: if the passed in project_id is bogus,
167     // we'll fail right here and don't damage any other data.
168
169     // Mark project as deleted and remove associated tasks.
170     $sql = "update tt_projects set status = NULL, tasks = NULL where id = $id and group_id = $group_id and org_id = $org_id";
171     $affected = $mdb2->exec($sql);
172     if (is_a($affected, 'PEAR_Error') || 0 == $affected)
173       return false; // An error ocurred, or 0 rows updated.
174
175     // Delete user binds to this project.
176     $sql = "delete from tt_user_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
177     $affected = $mdb2->exec($sql);
178     if (is_a($affected, 'PEAR_Error'))
179       return false;
180
181     // Delete task binds to this project.
182     $sql = "delete from tt_project_task_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
183     $affected = $mdb2->exec($sql);
184     if (is_a($affected, 'PEAR_Error'))
185       return false;
186
187     // Delete client binds to this project.
188     $sql = "delete from tt_client_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
189     $affected = $mdb2->exec($sql);
190     if (is_a($affected, 'PEAR_Error'))
191       return false;
192
193     return true;
194   }
195   
196   // insert function inserts a new project into database.
197   static function insert($fields)
198   {
199     global $user;
200     $mdb2 = getConnection();
201
202     $group_id = $user->getGroup();
203     $org_id = $user->org_id;
204
205     $name = $fields['name'];
206     $description = $fields['description'];
207     $users = $fields['users'];
208     $tasks = $fields['tasks'];
209     $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
210     $status = $fields['status'];
211     
212     $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
213       values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
214     $affected = $mdb2->exec($sql);
215     if (is_a($affected, 'PEAR_Error'))
216       return false;
217
218     $last_id = $mdb2->lastInsertID('tt_projects', 'id');
219
220     // Bind the project to users.
221     $active_users = ttGroupHelper::getActiveUsers(array('getAllFields'=>true));
222     foreach ($active_users as $u) {
223       if(in_array($u['id'], $users)) {
224         $sql = "insert into tt_user_project_binds (project_id, user_id, group_id, org_id, status, rate) values(
225           $last_id, ".$u['id'].", $group_id, $org_id, 1, ".$u['rate'].")";
226         $affected = $mdb2->exec($sql);
227         if (is_a($affected, 'PEAR_Error'))
228           return false;
229       }
230     }
231
232     // Bind the project to tasks in tt_project_task_binds table.
233     $all_tasks = ttTeamHelper::getAllTasks($group_id);
234     foreach ($all_tasks as $task) {
235       if(in_array($task['id'], $tasks)) {
236         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
237           " values($last_id, ".$task['id'].", $group_id, $org_id)";
238         $affected = $mdb2->exec($sql);
239         if (is_a($affected, 'PEAR_Error'))
240           return false;
241       }
242     }
243
244     return $last_id;
245   } 
246
247   // update function - updates the project in database.
248   static function update($fields) {
249     global $user;
250     $mdb2 = getConnection();
251
252     $group_id = $user->getGroup();
253     $org_id = $user->org_id;
254     $project_id = $fields['id']; // Project we are updating.
255     $name = $fields['name']; // Project name.
256     $description = $fields['description']; // Project description.
257     $users_to_bind = $fields['users']; // Users to bind with project.
258     $tasks_to_bind = $fields['tasks']; // Tasks to bind with project.
259     $status = $fields['status']; // Project status.
260     
261     // Update records in tt_user_project_binds table.
262     $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
263     $all_users = array();
264     $users_to_update = array();
265     $res2 = $mdb2->query($sql);
266     while ($row = $res2->fetchRow()) {
267       if(!in_array($row['user_id'], $users_to_bind)) { 
268         // Delete tt_user_project_binds record (safely).
269         ttUserHelper::deleteBind($row['user_id'], $project_id);
270       } elseif (!$row['status']) {
271         // If we are here, status of the bind is not active. Memorize such users to update their bind status.
272         $users_to_update[] = $row['user_id'];  // Users we need to update in tt_user_project_binds.
273       }
274       $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
275     }
276     // Insert records.
277     $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
278     if(count($users_to_add) > 0) {
279       $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
280       $res = $mdb2->query($sql);
281       while ($row = $res->fetchRow()) {
282         $user_rate[$row['id']] = $row['rate'];
283       }
284       foreach ($users_to_add as $id) {
285         $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
286           " values($id, $project_id, $group_id, $org_id, ".$user_rate[$id].", 1)";
287         $affected = $mdb2->exec($sql);
288         if (is_a($affected, 'PEAR_Error'))
289           return false;
290       }
291     }
292     // Update status (to active) in existing tt_user_project_binds records.
293     if ($users_to_update) {
294       $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
295       $affected = $mdb2->exec($sql);
296       if (is_a($affected, 'PEAR_Error'))
297         return false;
298     }
299     // End of updating tt_user_project_binds table.
300
301     // Update records in tt_project_task_binds table.
302     // Obtain existing task binds.
303     $existing_task_binds = array();
304     $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
305     $res = $mdb2->query($sql);
306     while ($val = $res->fetchRow()) {
307       $existing_task_binds[] = $val['task_id'];
308     }
309     // Determine which task binds to delete and which ones to add.
310     $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
311     $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
312     foreach ($task_binds_to_delete as $task_id) {
313       $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
314       $affected = $mdb2->exec($sql);
315       if (is_a($affected, 'PEAR_Error'))
316         return false;
317     }    
318     foreach ($task_binds_to_add as $task_id) {
319       $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
320         " values($project_id, $task_id, $group_id, $org_id)";
321       $affected = $mdb2->exec($sql);
322       if (is_a($affected, 'PEAR_Error'))
323         return false;
324     }
325     // End of updating tt_project_task_binds table.
326     
327     // Update project name, description, tasks and status in tt_projects table.
328     $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
329     $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
330            ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getGroup();
331     $affected = $mdb2->exec($sql);
332     return (!is_a($affected, 'PEAR_Error'));
333   }
334   
335   /*
336   // getAssignedUsers - returns an array of user ids assigned to a project.
337   static function getAssignedUsers($project_id)
338   {
339         global $user;
340         
341     $result = array();
342     $mdb2 = getConnection();
343     
344     // Do a query with inner join to get assigned users.
345     $sql = "select id, name from tt_users u
346       inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
347     $res = $mdb2->query($sql);
348     if (!is_a($res, 'PEAR_Error')) {
349       while ($val = $res->fetchRow()) {
350         $result[] = $val;
351       }
352     }
353     return $result;
354   }*/
355 }