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