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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttTeamHelper');
30 import('ttUserHelper');
31 import('ttGroupHelper');
33 // Class ttProjectHelper is used to help with project related tasks.
34 class ttProjectHelper {
36 // getAssignedProjects - returns an array of assigned projects.
37 static function getAssignedProjects($user_id)
42 $mdb2 = getConnection();
44 // Do a query with inner join to get assigned projects.
45 $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p".
46 " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
47 " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
48 $res = $mdb2->query($sql);
49 if (!is_a($res, 'PEAR_Error')) {
50 while ($val = $res->fetchRow()) {
57 // getRates - returns an array of project rates for user, including deassigned and deactivated projects.
58 static function getRates($user_id)
63 $mdb2 = getConnection();
65 $sql = "select p.id, upb.rate from tt_projects p".
66 " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id)".
67 " where group_id = ".$user->getGroup();
68 $res = $mdb2->query($sql);
69 if (!is_a($res, 'PEAR_Error')) {
70 while ($val = $res->fetchRow()) {
71 $val['rate'] = str_replace('.', $user->decimal_mark, $val['rate']);
78 // getProjects - returns an array of active and inactive projects in group.
79 static function getProjects()
84 $mdb2 = getConnection();
86 $sql = "select id, name, tasks from tt_projects".
87 " where group_id = ".$user->getGroup()." and (status = 0 or status = 1) order by name";
89 $res = $mdb2->query($sql);
90 if (!is_a($res, 'PEAR_Error')) {
91 while ($val = $res->fetchRow()) {
98 // getProjectsForClient - returns an array of active and inactive projects in a group for a client.
99 static function getProjectsForClient()
104 $mdb2 = getConnection();
106 $sql = "select p.id, p.name, p.tasks from tt_projects p".
107 " inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)".
108 " where p.group_id = ".$user->getGroup()." and (p.status = 0 or p.status = 1)".
111 $res = $mdb2->query($sql);
112 if (!is_a($res, 'PEAR_Error')) {
113 while ($val = $res->fetchRow()) {
121 // get - gets details of the project identified by its id.
122 static function get($id)
126 $mdb2 = getConnection();
128 $sql = "select id, name, description, status, tasks from tt_projects where id = $id and group_id = ".
129 $user->getGroup()." and (status = 0 or status = 1)";
130 $res = $mdb2->query($sql);
131 if (!is_a($res, 'PEAR_Error')) {
132 $val = $res->fetchRow();
133 if ($val && $val['id'])
139 // The getProjectByName looks up a project by name.
140 static function getProjectByName($name) {
142 $mdb2 = getConnection();
145 $sql = "select id from tt_projects where group_id = ".
146 $user->getGroup()." and name = ".$mdb2->quote($name).
147 " and (status = 1 or status = 0)";
148 $res = $mdb2->query($sql);
149 if (!is_a($res, 'PEAR_Error')) {
150 $val = $res->fetchRow();
151 if ($val && $val['id'])
158 // delete - deletes things associated with a project and marks the project as deleted.
159 static function delete($id) {
161 $mdb2 = getConnection();
163 $group_id = $user->getGroup();
164 $org_id = $user->org_id;
166 // Start with project itself. Reason: if the passed in project_id is bogus,
167 // we'll fail right here and don't damage any other data.
169 // Mark project as deleted and remove associated tasks.
170 $sql = "update tt_projects set status = NULL, tasks = NULL where id = $id and group_id = $group_id and org_id = $org_id";
171 $affected = $mdb2->exec($sql);
172 if (is_a($affected, 'PEAR_Error') || 0 == $affected)
173 return false; // An error ocurred, or 0 rows updated.
175 // Delete user binds to this project.
176 $sql = "delete from tt_user_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
177 $affected = $mdb2->exec($sql);
178 if (is_a($affected, 'PEAR_Error'))
181 // Delete task binds to this project.
182 $sql = "delete from tt_project_task_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
183 $affected = $mdb2->exec($sql);
184 if (is_a($affected, 'PEAR_Error'))
187 // Delete client binds to this project.
188 $sql = "delete from tt_client_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
189 $affected = $mdb2->exec($sql);
190 if (is_a($affected, 'PEAR_Error'))
196 // insert function inserts a new project into database.
197 static function insert($fields)
199 $mdb2 = getConnection();
201 $group_id = (int) $fields['group_id'];
202 $org_id = (int) $fields['org_id'];
204 $name = $fields['name'];
205 $description = $fields['description'];
206 $users = $fields['users'];
207 $tasks = $fields['tasks'];
208 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
209 $status = $fields['status'];
211 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
212 values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
213 $affected = $mdb2->exec($sql);
214 if (is_a($affected, 'PEAR_Error'))
217 $last_id = $mdb2->lastInsertID('tt_projects', 'id');
219 // Bind the project to users.
220 $active_users = ttGroupHelper::getActiveUsers(array('getAllFields'=>true));
221 foreach ($active_users as $u) {
222 if(in_array($u['id'], $users)) {
223 $sql = "insert into tt_user_project_binds (project_id, user_id, group_id, org_id, status, rate) values(
224 $last_id, ".$u['id'].", $group_id, $org_id, 1, ".$u['rate'].")";
225 $affected = $mdb2->exec($sql);
226 if (is_a($affected, 'PEAR_Error'))
231 // Bind the project to tasks in tt_project_task_binds table.
232 $all_tasks = ttTeamHelper::getAllTasks($group_id);
233 foreach ($all_tasks as $task) {
234 if(in_array($task['id'], $tasks)) {
235 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
236 " values($last_id, ".$task['id'].", $group_id, $org_id)";
237 $affected = $mdb2->exec($sql);
238 if (is_a($affected, 'PEAR_Error'))
246 // update function - updates the project in database.
247 static function update($fields) {
249 $mdb2 = getConnection();
251 $group_id = $user->getGroup();
252 $org_id = $user->org_id;
253 $project_id = $fields['id']; // Project we are updating.
254 $name = $fields['name']; // Project name.
255 $description = $fields['description']; // Project description.
256 $users_to_bind = $fields['users']; // Users to bind with project.
257 $tasks_to_bind = $fields['tasks']; // Tasks to bind with project.
258 $status = $fields['status']; // Project status.
260 // Update records in tt_user_project_binds table.
261 $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
262 $all_users = array();
263 $users_to_update = array();
264 $res2 = $mdb2->query($sql);
265 while ($row = $res2->fetchRow()) {
266 if(!in_array($row['user_id'], $users_to_bind)) {
267 // Delete tt_user_project_binds record (safely).
268 ttUserHelper::deleteBind($row['user_id'], $project_id);
269 } elseif (!$row['status']) {
270 // If we are here, status of the bind is not active. Memorize such users to update their bind status.
271 $users_to_update[] = $row['user_id']; // Users we need to update in tt_user_project_binds.
273 $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
276 $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
277 if(count($users_to_add) > 0) {
278 $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
279 $res = $mdb2->query($sql);
280 while ($row = $res->fetchRow()) {
281 $user_rate[$row['id']] = $row['rate'];
283 foreach ($users_to_add as $id) {
284 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
285 " values($id, $project_id, $group_id, $org_id, ".$user_rate[$id].", 1)";
286 $affected = $mdb2->exec($sql);
287 if (is_a($affected, 'PEAR_Error'))
291 // Update status (to active) in existing tt_user_project_binds records.
292 if ($users_to_update) {
293 $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
294 $affected = $mdb2->exec($sql);
295 if (is_a($affected, 'PEAR_Error'))
298 // End of updating tt_user_project_binds table.
300 // Update records in tt_project_task_binds table.
301 // Obtain existing task binds.
302 $existing_task_binds = array();
303 $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
304 $res = $mdb2->query($sql);
305 while ($val = $res->fetchRow()) {
306 $existing_task_binds[] = $val['task_id'];
308 // Determine which task binds to delete and which ones to add.
309 $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
310 $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
311 foreach ($task_binds_to_delete as $task_id) {
312 $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
313 $affected = $mdb2->exec($sql);
314 if (is_a($affected, 'PEAR_Error'))
317 foreach ($task_binds_to_add as $task_id) {
318 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
319 " values($project_id, $task_id, $group_id, $org_id)";
320 $affected = $mdb2->exec($sql);
321 if (is_a($affected, 'PEAR_Error'))
324 // End of updating tt_project_task_binds table.
326 // Update project name, description, tasks and status in tt_projects table.
327 $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
328 $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
329 ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getGroup();
330 $affected = $mdb2->exec($sql);
331 return (!is_a($affected, 'PEAR_Error'));
335 // getAssignedUsers - returns an array of user ids assigned to a project.
336 static function getAssignedUsers($project_id)
341 $mdb2 = getConnection();
343 // Do a query with inner join to get assigned users.
344 $sql = "select id, name from tt_users u
345 inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
346 $res = $mdb2->query($sql);
347 if (!is_a($res, 'PEAR_Error')) {
348 while ($val = $res->fetchRow()) {