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