Work in progress on roles. Implemented role delete.
[timetracker.git] / WEB-INF / lib / ttTaskHelper.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 // Class ttTaskHelper is used to help with task related operations.
30 class ttTaskHelper {
31
32   // get - gets details of a task identified by its id.
33   static function get($id)
34   {
35     global $user;
36  
37     $mdb2 = getConnection();
38
39     $sql = "select id, name, description, status from tt_tasks
40       where id = $id and team_id = $user->team_id and (status = 0 or status = 1)";
41     $res = $mdb2->query($sql);
42
43     if (!is_a($res, 'PEAR_Error')) {
44       $val = $res->fetchRow();
45           if ($val['id'] != '') {
46         return $val;
47       } else
48         return false;
49     }
50     return false;
51   }
52
53   // getAssignedProjects - returns an array of projects associatied with a task.
54   static function getAssignedProjects($task_id)
55   {
56         global $user;
57         
58     $result = array();
59     $mdb2 = getConnection();
60     
61     // Do a query with inner join to get assigned projects.
62     $sql = "select p.id, p.name from tt_projects p
63       inner join tt_project_task_binds ptb on (ptb.project_id = p.id and ptb.task_id = $task_id)
64       where p.team_id = $user->team_id and p.status = 1 order by p.name";
65     $res = $mdb2->query($sql);
66     if (!is_a($res, 'PEAR_Error')) {
67       while ($val = $res->fetchRow()) {
68         $result[] = $val;
69       }
70     }
71     return $result;
72   }
73   
74   // The getTaskByName looks up a task by name.
75   static function getTaskByName($task_name) {
76         
77     $mdb2 = getConnection();
78     global $user;
79
80     $sql = "select id from tt_tasks where team_id = $user->team_id and name = ".
81       $mdb2->quote($task_name)." and (status = 1 or status = 0)";
82         $res = $mdb2->query($sql);
83
84         if (!is_a($res, 'PEAR_Error')) {
85       $val = $res->fetchRow();
86           if ($val['id']) {
87         return $val;
88       }
89     }
90     return false;
91   }
92  
93   // delete - deletes things associated with a task and marks the task as deleted. 
94   static function delete($task_id) {
95     global $user;
96             
97     $mdb2 = getConnection();
98
99     // Delete project binds to this task from tt_project_task_binds table.
100     $sql = "delete from tt_project_task_binds where task_id = $task_id";
101     $affected = $mdb2->exec($sql);
102     if (is_a($affected, 'PEAR_Error'))
103       return false;
104         
105     // Delete project binds to this task from the tasks field in tt_projects table.
106     // Get projects where tasks is not NULL.
107     $sql = "select id, tasks from tt_projects where team_id = $user->team_id and tasks is not NULL";
108     $res = $mdb2->query($sql);
109     if (is_a($res, 'PEAR_Error'))
110       return false;
111     while ($val = $res->fetchRow()) {
112       $project_id = $val['id'];
113       $tasks = explode(',', $val['tasks']);
114       
115       if (in_array($task_id, $tasks)) {
116         // Remove task from array.
117         unset($tasks[array_search($task_id, $tasks)]);
118         $comma_separated = implode(',', $tasks); // This is a new comma-separated list of associated task ids.
119       
120         // Re-bind the project to tasks.
121         $sql = "update tt_projects set tasks = ".$mdb2->quote($comma_separated)." where id = $project_id";
122         $affected = $mdb2->exec($sql);
123         if (is_a($affected, 'PEAR_Error'))
124           return false;
125       }
126     }
127
128     // Mark the task as deleted.
129     $sql = "update tt_tasks set status = NULL where id = $task_id";
130     $affected = $mdb2->exec($sql);
131     return (!is_a($affected, 'PEAR_Error'));
132   }
133   
134  // insert function inserts a new task into database.
135   static function insert($fields)
136   {
137     $mdb2 = getConnection();
138
139     $team_id = (int) $fields['team_id'];
140     $name = $fields['name'];
141     $description = $fields['description'];
142     $projects = $fields['projects'];
143     $status = $fields['status'];
144         
145     $sql = "insert into tt_tasks (team_id, name, description, status)
146       values ($team_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
147     $affected = $mdb2->exec($sql);
148     $last_id = 0;
149     if (is_a($affected, 'PEAR_Error'))
150       return false;
151
152     $sql = "select last_insert_id() as last_insert_id";
153     $res = $mdb2->query($sql);
154     $val = $res->fetchRow();
155     $last_id = $val['last_insert_id'];
156     
157     if (is_array($projects)) {
158       foreach ($projects as $p_id) {
159         // Insert task binds into tt_project_task_binds table.
160         $sql = "insert into tt_project_task_binds (project_id, task_id) values($p_id, $last_id)";
161         $affected = $mdb2->exec($sql);
162                 if (is_a($affected, 'PEAR_Error'))
163                   return false;
164
165                 // Add task bind to the tasks field of the tt_projects table.
166         $sql = "select tasks from tt_projects where id = $p_id";
167         $res = $mdb2->query($sql);
168         if (is_a($res, 'PEAR_Error'))
169           return false;
170
171         $val = $res->fetchRow();
172         $task_ids = $val['tasks'];
173         if ($task_ids) {
174                   $task_ids .= ",$last_id";
175                   $task_ids = ttTaskHelper::sort($task_ids);
176                 } else
177                   $task_ids = $last_id;
178
179         $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $p_id";
180         $affected = $mdb2->exec($sql);
181         if (is_a($affected, 'PEAR_Error'))
182           return false;
183           }
184     }
185     return $last_id;
186   }
187  
188  // update function updates a task in database.
189   static function update($fields)
190   {
191     global $user;
192             
193     $mdb2 = getConnection();
194
195     $task_id = (int)$fields['task_id'];
196     $name = $fields['name'];
197     $description = $fields['description'];
198     $status = $fields['status'];
199     $projects = $fields['projects'];
200
201     $sql = "update tt_tasks set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
202       ", status = $status where id = $task_id";
203     $affected = $mdb2->exec($sql);
204     if (is_a($affected, 'PEAR_Error'))
205       die($affected->getMessage());
206         
207     // Insert task binds into tt_project_task_binds table.
208     $sql = "delete from tt_project_task_binds where task_id = $task_id";
209     $affected = $mdb2->exec($sql);
210     if (is_a($affected, 'PEAR_Error'))
211       die($affected->getMessage());
212     if (count($projects) > 0)
213       foreach ($projects as $p_id) {
214         $sql = "insert into tt_project_task_binds (project_id, task_id) values($p_id, $task_id)";
215         $affected = $mdb2->exec($sql);
216         if (is_a($affected, 'PEAR_Error'))
217           die($affected->getMessage());
218       }
219       
220     // Handle task binds in the tasks field of the tt_projects table.
221     // We need to either delete or insert task id in all affected projects.
222     
223     // Get all not deleted projects for team.
224     $sql = "select id, tasks from tt_projects where team_id = $user->team_id and status is not NULL";
225     $res = $mdb2->query($sql);
226     if (is_a($res, 'PEAR_Error'))
227       die($res->getMessage());
228     
229     // Iterate through projects.
230     while ($val = $res->fetchRow()) {
231       $project_id = $val['id'];
232       $task_ids = $val['tasks'];
233       $task_arr = explode(',', $task_ids);
234       
235       if (is_array($projects) && in_array($project_id, $projects)) {
236         // Task needs to be available for this project.
237         if (!in_array($task_id, $task_arr)) {
238           if ($task_ids) {
239             $task_ids .= ",$task_id";
240             $task_ids = ttTaskHelper::sort($task_ids);
241                   } else
242                         $task_ids = $task_id;
243
244                  $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $project_id";
245                  $affected = $mdb2->exec($sql);
246                  if (is_a($affected, 'PEAR_Error'))
247            die($affected->getMessage());
248                 }
249       } else {
250         // Task needs to be removed from this project.
251         if (in_array($task_id, $task_arr)) {
252           // Remove task from array.
253           unset($task_arr[array_search($task_id, $task_arr)]);
254           $comma_separated = implode(",", $task_arr); // This is a comma-separated list of associated task ids.
255       
256           // Re-bind the project to tasks.
257           $sql = "update tt_projects set tasks = ".$mdb2->quote($comma_separated)." where id = $project_id";
258           $affected = $mdb2->exec($sql);
259           if (is_a($affected, 'PEAR_Error'))
260             die($affected->getMessage());
261         }
262       }
263     }
264     return true;
265   }
266
267   // sort function sorts task ids passed as comma-separated list by their name.
268   static function sort($comma_separated) {
269         // We can't sort an empty string.
270         if (!$comma_separated)
271           return $comma_separated;
272                   
273     $mdb2 = getConnection();
274       
275         $sql = "select id, name from tt_tasks where id in ($comma_separated)";
276     $res = $mdb2->query($sql);
277     if (is_a($res, 'PEAR_Error'))
278       die ($res->getMessage());
279     
280     $task_arr = array();
281     while ($val = $res->fetchRow()) {
282       $task_arr[] = array('id'=>$val['id'],'name'=>$val['name']);
283     }
284     $task_arr = mu_sort($task_arr, 'name');
285     $task_ids = array();
286     for($i = 0; $i < count($task_arr); $i++) {
287           $task_ids[] = $task_arr[$i]['id'];
288     }
289         $result = implode(',', $task_ids);
290     return $result;
291   }
292 }