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 // Class ttTaskHelper is used to help with task related operations.
 
  32   // get - gets details of a task identified by its id.
 
  33   static function get($id)
 
  37     $mdb2 = getConnection();
 
  38     $group_id = $user->getGroup();
 
  39     $org_id = $user->org_id;
 
  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);
 
  45     if (!is_a($res, 'PEAR_Error')) {
 
  46       $val = $res->fetchRow();
 
  47           if ($val['id'] != '') {
 
  55   // getAssignedProjects - returns an array of projects associatied with a task.
 
  56   static function getAssignedProjects($task_id)
 
  61     $mdb2 = getConnection();
 
  62     $group_id = $user->getGroup();
 
  63     $org_id = $user->org_id;
 
  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()) {
 
  78   // The getTaskByName looks up a task by name.
 
  79   static function getTaskByName($task_name) {
 
  81     $mdb2 = getConnection();
 
  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);
 
  88       if (!is_a($res, 'PEAR_Error')) {
 
  89       $val = $res->fetchRow();
 
  96   // delete - deletes things associated with a task and marks the task as deleted. 
 
  97   static function delete($task_id) {
 
 100     $mdb2 = getConnection();
 
 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'))
 
 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'))
 
 114     while ($val = $res->fetchRow()) {
 
 115       $project_id = $val['id'];
 
 116       $tasks = explode(',', $val['tasks']);
 
 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.
 
 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'))
 
 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'));
 
 137  // insert function inserts a new task into database.
 
 138   static function insert($fields)
 
 141     $mdb2 = getConnection();
 
 143     $group_id = $user->getGroup();
 
 144     $org_id = $user->org_id;
 
 145     $name = $fields['name'];
 
 146     $description = $fields['description'];
 
 147     $projects = $fields['projects'];
 
 148     $status = $fields['status'];
 
 150     $sql = "insert into tt_tasks (group_id, org_id, name, description, status)
 
 151       values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
 
 152     $affected = $mdb2->exec($sql);
 
 154     if (is_a($affected, 'PEAR_Error'))
 
 157     $last_id = $mdb2->lastInsertID('tt_tasks', 'id');
 
 159     if (is_array($projects)) {
 
 160       foreach ($projects as $p_id) {
 
 161         // Insert task binds into tt_project_task_binds table.
 
 162         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
 
 163           " values($p_id, $last_id, $group_id, $org_id)";
 
 164         $affected = $mdb2->exec($sql);
 
 165         if (is_a($affected, 'PEAR_Error'))
 
 168         // Add task bind to the tasks field of the tt_projects table.
 
 169         $sql = "select tasks from tt_projects where id = $p_id";
 
 170         $res = $mdb2->query($sql);
 
 171         if (is_a($res, 'PEAR_Error'))
 
 174         $val = $res->fetchRow();
 
 175         $task_ids = $val['tasks'];
 
 177           $task_ids .= ",$last_id";
 
 178           $task_ids = ttTaskHelper::sort($task_ids);
 
 180           $task_ids = $last_id;
 
 182         $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $p_id";
 
 183         $affected = $mdb2->exec($sql);
 
 184         if (is_a($affected, 'PEAR_Error'))
 
 191   // update function updates a task in the database.
 
 192   static function update($fields)
 
 195     $mdb2 = getConnection();
 
 197     $group_id = $user->getGroup();
 
 198     $org_id = $user->org_id;
 
 199     $task_id = (int)$fields['task_id'];
 
 200     $name = $fields['name'];
 
 201     $description = $fields['description'];
 
 202     $status = $fields['status'];
 
 203     $projects = $fields['projects'];
 
 205     $sql = "update tt_tasks set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
 
 206       ", status = $status where id = $task_id and group_id = $group_id";
 
 207     $affected = $mdb2->exec($sql);
 
 208     if (is_a($affected, 'PEAR_Error'))
 
 209       die($affected->getMessage());
 
 211     // Insert task binds into tt_project_task_binds table.
 
 212     $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 213     $affected = $mdb2->exec($sql);
 
 214     if (is_a($affected, 'PEAR_Error'))
 
 215       die($affected->getMessage());
 
 216     if (count($projects) > 0)
 
 217       foreach ($projects as $p_id) {
 
 218         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
 
 219           " values($p_id, $task_id, $group_id, $org_id)";
 
 220         $affected = $mdb2->exec($sql);
 
 221         if (is_a($affected, 'PEAR_Error'))
 
 222           die($affected->getMessage());
 
 225     // Handle task binds in the tasks field of the tt_projects table.
 
 226     // We need to either delete or insert task id in all affected projects.
 
 228     // Get all not deleted projects for group.
 
 229     $sql = "select id, tasks from tt_projects where group_id = $group_id and status is not NULL";
 
 230     $res = $mdb2->query($sql);
 
 231     if (is_a($res, 'PEAR_Error'))
 
 232       die($res->getMessage());
 
 234     // Iterate through projects.
 
 235     while ($val = $res->fetchRow()) {
 
 236       $project_id = $val['id'];
 
 237       $task_ids = $val['tasks'];
 
 238       $task_arr = explode(',', $task_ids);
 
 240       if (is_array($projects) && in_array($project_id, $projects)) {
 
 241         // Task needs to be available for this project.
 
 242         if (!in_array($task_id, $task_arr)) {
 
 244             $task_ids .= ",$task_id";
 
 245             $task_ids = ttTaskHelper::sort($task_ids);
 
 247           $task_ids = $task_id;
 
 249         $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $project_id";
 
 250         $affected = $mdb2->exec($sql);
 
 251         if (is_a($affected, 'PEAR_Error'))
 
 252           die($affected->getMessage());
 
 255         // Task needs to be removed from this project.
 
 256         if (in_array($task_id, $task_arr)) {
 
 257           // Remove task from array.
 
 258           unset($task_arr[array_search($task_id, $task_arr)]);
 
 259           $comma_separated = implode(",", $task_arr); // This is a comma-separated list of associated task ids.
 
 261           // Re-bind the project to tasks.
 
 262           $sql = "update tt_projects set tasks = ".$mdb2->quote($comma_separated)." where id = $project_id";
 
 263           $affected = $mdb2->exec($sql);
 
 264           if (is_a($affected, 'PEAR_Error'))
 
 265             die($affected->getMessage());
 
 272   // sort function sorts task ids passed as comma-separated list by their name.
 
 273   static function sort($comma_separated) {
 
 274     // We can't sort an empty string.
 
 275     if (!$comma_separated)
 
 276       return $comma_separated;
 
 278     $mdb2 = getConnection();
 
 280     $sql = "select id, name from tt_tasks where id in ($comma_separated)";
 
 281     $res = $mdb2->query($sql);
 
 282     if (is_a($res, 'PEAR_Error'))
 
 283       die ($res->getMessage());
 
 286     while ($val = $res->fetchRow()) {
 
 287       $task_arr[] = array('id'=>$val['id'],'name'=>$val['name']);
 
 289     $task_arr = mu_sort($task_arr, 'name');
 
 291     for($i = 0; $i < count($task_arr); $i++) {
 
 292           $task_ids[] = $task_arr[$i]['id'];
 
 294     $result = implode(',', $task_ids);