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