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');
 
  32 // Class ttProjectHelper is used to help with project related tasks.
 
  33 class ttProjectHelper {
 
  35   // getAssignedProjects - returns an array of assigned projects.
 
  36   static function getAssignedProjects($user_id)
 
  41     $mdb2 = getConnection();
 
  43     // Do a query with inner join to get assigned projects.
 
  44     $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p".
 
  45       " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
 
  46       " where p.group_id = ".$user->getActiveGroup()." and p.status = 1 order by p.name";
 
  47     $res = $mdb2->query($sql);
 
  48     if (!is_a($res, 'PEAR_Error')) {
 
  49       while ($val = $res->fetchRow()) {
 
  56   // getRates - returns an array of project rates for user, including deassigned and deactivated projects.
 
  57   static function getRates($user_id)
 
  62     $mdb2 = getConnection();
 
  64     $sql = "select p.id, upb.rate from tt_projects p".
 
  65       " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id)".
 
  66       " where group_id = ".$user->getActiveGroup();
 
  67     $res = $mdb2->query($sql);
 
  68     if (!is_a($res, 'PEAR_Error')) {
 
  69       while ($val = $res->fetchRow()) {
 
  70         $val['rate'] = str_replace('.', $user->decimal_mark, $val['rate']);
 
  77   // getProjects - returns an array of active and inactive projects in group.
 
  78   static function getProjects()
 
  83     $mdb2 = getConnection();
 
  85     $sql = "select id, name, tasks from tt_projects".
 
  86       " where group_id = ".$user->getActiveGroup()." and (status = 0 or status = 1) order by name";
 
  88     $res = $mdb2->query($sql);
 
  89     if (!is_a($res, 'PEAR_Error')) {
 
  90       while ($val = $res->fetchRow()) {
 
  97   // getProjectsForClient - returns an array of active and inactive projects in a group for a client.
 
  98   static function getProjectsForClient()
 
 103     $mdb2 = getConnection();
 
 105     $sql = "select p.id, p.name, p.tasks from tt_projects p".
 
 106       " inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)".
 
 107       " where p.group_id = ".$user->getActiveGroup()." and (p.status = 0 or p.status = 1)".
 
 110     $res = $mdb2->query($sql);
 
 111     if (!is_a($res, 'PEAR_Error')) {
 
 112       while ($val = $res->fetchRow()) {
 
 120   // get - gets details of the project identified by its id. 
 
 121   static function get($id)
 
 125     $mdb2 = getConnection();
 
 127     $sql = "select id, name, description, status, tasks from tt_projects where id = $id and group_id = ".
 
 128             $user->getActiveGroup()." and (status = 0 or status = 1)";
 
 129     $res = $mdb2->query($sql);
 
 130     if (!is_a($res, 'PEAR_Error')) {
 
 131       $val = $res->fetchRow();
 
 132           if ($val && $val['id'])
 
 138   // The getProjectByName looks up a project by name.
 
 139   static function getProjectByName($name) {
 
 141     $mdb2 = getConnection();
 
 144     $sql = "select id from tt_projects where group_id = ".
 
 145       $user->getActiveGroup()." and name = ".$mdb2->quote($name).
 
 146       " and (status = 1 or status = 0)";
 
 147     $res = $mdb2->query($sql);
 
 148     if (!is_a($res, 'PEAR_Error')) {
 
 149       $val = $res->fetchRow();
 
 150       if ($val && $val['id'])
 
 157   // delete - deletes things associated with a project and marks the project as deleted. 
 
 158   static function delete($id) {
 
 160     $mdb2 = getConnection();
 
 162     // Start with project itself. Reason: if the passed in project_id is bogus,
 
 163     // we'll fail right here and don't damage any other data.
 
 165     // Mark project as deleted and remove associated tasks.
 
 166     $sql = "update tt_projects set status = NULL, tasks = NULL where id = $id and group_id = ".$user->getActiveGroup();
 
 167     $affected = $mdb2->exec($sql);
 
 168     if (is_a($affected, 'PEAR_Error') || 0 == $affected)
 
 169       return false; // An error ocurred, or 0 rows updated.
 
 171     // Delete user binds to this project.
 
 172     $sql = "delete from tt_user_project_binds where project_id = $id";
 
 173     $affected = $mdb2->exec($sql);
 
 174     if (is_a($affected, 'PEAR_Error'))
 
 177     // Delete task binds to this project.
 
 178     $sql = "delete from tt_project_task_binds where project_id = $id";
 
 179     $affected = $mdb2->exec($sql);
 
 180     if (is_a($affected, 'PEAR_Error'))
 
 186   // insert function inserts a new project into database.
 
 187   static function insert($fields)
 
 189     $mdb2 = getConnection();
 
 191     $group_id = (int) $fields['group_id'];
 
 192     $org_id = (int) $fields['org_id'];
 
 194     $name = $fields['name'];
 
 195     $description = $fields['description'];
 
 196     $users = $fields['users'];
 
 197     $tasks = $fields['tasks'];
 
 198     $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
 
 199     $status = $fields['status'];
 
 201     $sql = "insert into tt_projects (group_id, org_id, name, description, tasks, status)
 
 202       values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
 
 203     $affected = $mdb2->exec($sql);
 
 204     if (is_a($affected, 'PEAR_Error'))
 
 208     $sql = "select last_insert_id() as last_insert_id";
 
 209     $res = $mdb2->query($sql);
 
 210     $val = $res->fetchRow();
 
 211     $last_id = $val['last_insert_id'];
 
 213     // Bind the project to users.
 
 214     $active_users = ttTeamHelper::getActiveUsers(array('getAllFields'=>true));
 
 215     foreach ($active_users as $u) {
 
 216       if(in_array($u['id'], $users)) {
 
 217         $sql = "insert into tt_user_project_binds (project_id, user_id, status, rate) values(
 
 218           $last_id, ".$u['id'].", 1, ".$u['rate'].")";
 
 219         $affected = $mdb2->exec($sql);
 
 220         if (is_a($affected, 'PEAR_Error'))
 
 225     // Bind the project to tasks in tt_project_task_binds table.
 
 226     $all_tasks = ttTeamHelper::getAllTasks($group_id);
 
 227     foreach ($all_tasks as $task) {
 
 228       if(in_array($task['id'], $tasks)) {
 
 229         $sql = "insert into tt_project_task_binds (project_id, task_id) values($last_id, ".$task['id'].")";
 
 230         $affected = $mdb2->exec($sql);
 
 231         if (is_a($affected, 'PEAR_Error'))
 
 239   // update function - updates the project in database.
 
 240   static function update($fields) {
 
 242     $mdb2 = getConnection();
 
 244     $project_id = $fields['id']; // Project we are updating.
 
 245     $name = $fields['name']; // Project name.
 
 246     $description = $fields['description']; // Project description.
 
 247     $users_to_bind = $fields['users']; // Users to bind with project.
 
 248     $tasks_to_bind = $fields['tasks']; // Tasks to bind with project.
 
 249     $status = $fields['status']; // Project status.
 
 251     // Update records in tt_user_project_binds table.
 
 252     $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
 
 253     $all_users = array();
 
 254     $users_to_update = array();
 
 255     $res2 = $mdb2->query($sql);
 
 256     while ($row = $res2->fetchRow()) {
 
 257       if(!in_array($row['user_id'], $users_to_bind)) { 
 
 258         // Delete tt_user_project_binds record (safely).
 
 259         ttUserHelper::deleteBind($row['user_id'], $project_id);
 
 260       } elseif (!$row['status']) {
 
 261         // If we are here, status of the bind is not active. Memorize such users to update their bind status.
 
 262         $users_to_update[] = $row['user_id'];  // Users we need to update in tt_user_project_binds.
 
 264       $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
 
 267     $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
 
 268     if(count($users_to_add) > 0) {
 
 269       $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
 
 270       $res = $mdb2->query($sql);
 
 271       while ($row = $res->fetchRow()) {
 
 272         $user_rate[$row['id']] = $row['rate'];
 
 274       foreach ($users_to_add as $id) {
 
 275         $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status) values($id, $project_id, ".$user_rate[$id].", 1)";
 
 276         $affected = $mdb2->exec($sql);
 
 277         if (is_a($affected, 'PEAR_Error'))
 
 281     // Update status (to active) in existing tt_user_project_binds records.
 
 282     if ($users_to_update) {
 
 283       $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
 
 284       $affected = $mdb2->exec($sql);
 
 285       if (is_a($affected, 'PEAR_Error'))
 
 288     // End of updating tt_user_project_binds table.
 
 290     // Update records in tt_project_task_binds table.
 
 291     // Obtain existing task binds.
 
 292     $existing_task_binds = array();
 
 293     $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
 
 294     $res = $mdb2->query($sql);
 
 295     while ($val = $res->fetchRow()) {
 
 296       $existing_task_binds[] = $val['task_id'];
 
 298     // Determine which task binds to delete and which ones to add.
 
 299     $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
 
 300     $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
 
 301     foreach ($task_binds_to_delete as $task_id) {
 
 302       $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
 
 303       $affected = $mdb2->exec($sql);
 
 304       if (is_a($affected, 'PEAR_Error'))
 
 307     foreach ($task_binds_to_add as $task_id) {
 
 308       $sql = "insert into tt_project_task_binds (project_id, task_id) values($project_id, $task_id)";
 
 309       $affected = $mdb2->exec($sql);
 
 310       if (is_a($affected, 'PEAR_Error'))
 
 313     // End of updating tt_project_task_binds table.
 
 315     // Update project name, description, tasks and status in tt_projects table.
 
 316     $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
 
 317     $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
 
 318            ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getActiveGroup();
 
 319     $affected = $mdb2->exec($sql);
 
 320     return (!is_a($affected, 'PEAR_Error'));
 
 324   // getAssignedUsers - returns an array of user ids assigned to a project.
 
 325   static function getAssignedUsers($project_id)
 
 330     $mdb2 = getConnection();
 
 332     // Do a query with inner join to get assigned users.
 
 333     $sql = "select id, name from tt_users u
 
 334       inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
 
 335     $res = $mdb2->query($sql);
 
 336     if (!is_a($res, 'PEAR_Error')) {
 
 337       while ($val = $res->fetchRow()) {