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