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