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) {
39 $mdb2 = getConnection();
41 $group_id = $user->getGroup();
42 $org_id = $user->org_id;
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 p.name";
49 $res = $mdb2->query($sql);
50 if (!is_a($res, 'PEAR_Error')) {
51 while ($val = $res->fetchRow()) {
58 // getRates - returns an array of project rates for user, including deassigned and deactivated projects.
59 static function getRates($user_id) {
61 $mdb2 = getConnection();
63 $group_id = $user->getGroup();
64 $org_id = $user->org_id;
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']);
80 // getProjects - returns an array of active and inactive projects in group.
81 static function getProjects() {
83 $mdb2 = getConnection();
85 $group_id = $user->getGroup();
86 $org_id = $user->org_id;
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 name";
91 $res = $mdb2->query($sql);
92 if (!is_a($res, 'PEAR_Error')) {
93 while ($val = $res->fetchRow()) {
100 // getProjectsForClient - returns an array of active and inactive projects in a group for a client.
101 static function getProjectsForClient() {
103 $mdb2 = getConnection();
105 $group_id = $user->getGroup();
106 $org_id = $user->org_id;
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)".
114 $res = $mdb2->query($sql);
115 if (!is_a($res, 'PEAR_Error')) {
116 while ($val = $res->fetchRow()) {
123 // get - gets details of the project identified by its id.
124 static function get($id) {
126 $mdb2 = getConnection();
128 $group_id = $user->getGroup();
129 $org_id = $user->org_id;
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'])
142 // The getProjectByName looks up a project by name.
143 static function getProjectByName($name) {
145 $mdb2 = getConnection();
147 $group_id = $user->getGroup();
148 $org_id = $user->org_id;
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'])
163 // delete - deletes things associated with a project and marks the project as deleted.
164 static function delete($id) {
166 $mdb2 = getConnection();
168 $group_id = $user->getGroup();
169 $org_id = $user->org_id;
171 // Start with project itself. Reason: if the passed in project_id is bogus,
172 // we'll fail right here and don't damage any other data.
174 // Mark project as deleted and remove associated tasks.
175 $sql = "update tt_projects set status = NULL, tasks = NULL where id = $id and group_id = $group_id and org_id = $org_id";
176 $affected = $mdb2->exec($sql);
177 if (is_a($affected, 'PEAR_Error') || 0 == $affected)
178 return false; // An error ocurred, or 0 rows updated.
180 // Delete user binds to this project.
181 $sql = "delete from tt_user_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
182 $affected = $mdb2->exec($sql);
183 if (is_a($affected, 'PEAR_Error'))
186 // Delete task binds to this project.
187 $sql = "delete from tt_project_task_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
188 $affected = $mdb2->exec($sql);
189 if (is_a($affected, 'PEAR_Error'))
192 // Delete client binds to this project.
193 $sql = "delete from tt_client_project_binds where project_id = $id and group_id = $group_id and org_id = $org_id";
194 $affected = $mdb2->exec($sql);
195 if (is_a($affected, 'PEAR_Error'))
201 // insert function inserts a new project into database.
202 static function insert($fields)
205 $mdb2 = getConnection();
207 $group_id = $user->getGroup();
208 $org_id = $user->org_id;
210 $name = $fields['name'];
211 $description = $fields['description'];
212 $users = $fields['users'];
213 $tasks = $fields['tasks'];
214 $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
215 $status = $fields['status'];
217 $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)".
218 " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
219 $affected = $mdb2->exec($sql);
220 if (is_a($affected, 'PEAR_Error'))
223 $last_id = $mdb2->lastInsertID('tt_projects', 'id');
225 // Bind the project to users.
226 $active_users = ttGroupHelper::getActiveUsers(array('getAllFields'=>true));
227 foreach ($active_users as $u) {
228 if(in_array($u['id'], $users)) {
229 $sql = "insert into tt_user_project_binds (project_id, user_id, group_id, org_id, status, rate) values(
230 $last_id, ".$u['id'].", $group_id, $org_id, 1, ".$u['rate'].")";
231 $affected = $mdb2->exec($sql);
232 if (is_a($affected, 'PEAR_Error'))
237 // Bind the project to tasks in tt_project_task_binds table.
238 $all_tasks = ttTeamHelper::getAllTasks($group_id);
239 foreach ($all_tasks as $task) {
240 if(in_array($task['id'], $tasks)) {
241 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
242 " values($last_id, ".$task['id'].", $group_id, $org_id)";
243 $affected = $mdb2->exec($sql);
244 if (is_a($affected, 'PEAR_Error'))
252 // update function - updates the project in database.
253 static function update($fields) {
255 $mdb2 = getConnection();
257 $group_id = $user->getGroup();
258 $org_id = $user->org_id;
260 $project_id = $fields['id']; // Project we are updating.
261 $name = $fields['name']; // Project name.
262 $description = $fields['description']; // Project description.
263 $users_to_bind = $fields['users']; // Users to bind with project.
264 $tasks_to_bind = $fields['tasks']; // Tasks to bind with project.
265 $status = $fields['status']; // Project status.
267 // Update records in tt_user_project_binds table.
268 $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
269 $all_users = array();
270 $users_to_update = array();
271 $res2 = $mdb2->query($sql);
272 while ($row = $res2->fetchRow()) {
273 if(!in_array($row['user_id'], $users_to_bind)) {
274 // Delete tt_user_project_binds record (safely).
275 ttUserHelper::deleteBind($row['user_id'], $project_id);
276 } elseif (!$row['status']) {
277 // If we are here, status of the bind is not active. Memorize such users to update their bind status.
278 $users_to_update[] = $row['user_id']; // Users we need to update in tt_user_project_binds.
280 $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
283 $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
284 if(count($users_to_add) > 0) {
285 $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
286 $res = $mdb2->query($sql);
287 while ($row = $res->fetchRow()) {
288 $user_rate[$row['id']] = $row['rate'];
290 foreach ($users_to_add as $id) {
291 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
292 " values($id, $project_id, $group_id, $org_id, ".$user_rate[$id].", 1)";
293 $affected = $mdb2->exec($sql);
294 if (is_a($affected, 'PEAR_Error'))
298 // Update status (to active) in existing tt_user_project_binds records.
299 if ($users_to_update) {
300 $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
301 $affected = $mdb2->exec($sql);
302 if (is_a($affected, 'PEAR_Error'))
305 // End of updating tt_user_project_binds table.
307 // Update records in tt_project_task_binds table.
308 // Obtain existing task binds.
309 $existing_task_binds = array();
310 $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
311 $res = $mdb2->query($sql);
312 while ($val = $res->fetchRow()) {
313 $existing_task_binds[] = $val['task_id'];
315 // Determine which task binds to delete and which ones to add.
316 $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
317 $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
318 foreach ($task_binds_to_delete as $task_id) {
319 $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
320 $affected = $mdb2->exec($sql);
321 if (is_a($affected, 'PEAR_Error'))
324 foreach ($task_binds_to_add as $task_id) {
325 $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
326 " values($project_id, $task_id, $group_id, $org_id)";
327 $affected = $mdb2->exec($sql);
328 if (is_a($affected, 'PEAR_Error'))
331 // End of updating tt_project_task_binds table.
333 // Update project name, description, tasks and status in tt_projects table.
334 $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
335 $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
336 ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getGroup();
337 $affected = $mdb2->exec($sql);
338 return (!is_a($affected, 'PEAR_Error'));
342 // getAssignedUsers - returns an array of user ids assigned to a project.
343 static function getAssignedUsers($project_id)
348 $mdb2 = getConnection();
350 // Do a query with inner join to get assigned users.
351 $sql = "select id, name from tt_users u
352 inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
353 $res = $mdb2->query($sql);
354 if (!is_a($res, 'PEAR_Error')) {
355 while ($val = $res->fetchRow()) {