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();
 
  39     $sql = "select id, name, description, status from tt_tasks
 
  40       where id = $id and group_id = $user->group_id and (status = 0 or status = 1)";
 
  41     $res = $mdb2->query($sql);
 
  43     if (!is_a($res, 'PEAR_Error')) {
 
  44       $val = $res->fetchRow();
 
  45           if ($val['id'] != '') {
 
  53   // getAssignedProjects - returns an array of projects associatied with a task.
 
  54   static function getAssignedProjects($task_id)
 
  59     $mdb2 = getConnection();
 
  61     // Do a query with inner join to get assigned projects.
 
  62     $sql = "select p.id, p.name from tt_projects p
 
  63       inner join tt_project_task_binds ptb on (ptb.project_id = p.id and ptb.task_id = $task_id)
 
  64       where p.group_id = $user->group_id and p.status = 1 order by p.name";
 
  65     $res = $mdb2->query($sql);
 
  66     if (!is_a($res, 'PEAR_Error')) {
 
  67       while ($val = $res->fetchRow()) {
 
  74   // The getTaskByName looks up a task by name.
 
  75   static function getTaskByName($task_name) {
 
  77     $mdb2 = getConnection();
 
  80     $sql = "select id from tt_tasks where group_id = $user->group_id and name = ".
 
  81       $mdb2->quote($task_name)." and (status = 1 or status = 0)";
 
  82       $res = $mdb2->query($sql);
 
  84       if (!is_a($res, 'PEAR_Error')) {
 
  85       $val = $res->fetchRow();
 
  92   // delete - deletes things associated with a task and marks the task as deleted. 
 
  93   static function delete($task_id) {
 
  96     $mdb2 = getConnection();
 
  98     // Delete project binds to this task from tt_project_task_binds table.
 
  99     $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 100     $affected = $mdb2->exec($sql);
 
 101     if (is_a($affected, 'PEAR_Error'))
 
 104     // Delete project binds to this task from the tasks field in tt_projects table.
 
 105     // Get projects where tasks is not NULL.
 
 106     $sql = "select id, tasks from tt_projects where group_id = $user->group_id and tasks is not NULL";
 
 107     $res = $mdb2->query($sql);
 
 108     if (is_a($res, 'PEAR_Error'))
 
 110     while ($val = $res->fetchRow()) {
 
 111       $project_id = $val['id'];
 
 112       $tasks = explode(',', $val['tasks']);
 
 114       if (in_array($task_id, $tasks)) {
 
 115         // Remove task from array.
 
 116         unset($tasks[array_search($task_id, $tasks)]);
 
 117         $comma_separated = implode(',', $tasks); // This is a new comma-separated list of associated task ids.
 
 119         // Re-bind the project to tasks.
 
 120         $sql = "update tt_projects set tasks = ".$mdb2->quote($comma_separated)." where id = $project_id";
 
 121         $affected = $mdb2->exec($sql);
 
 122         if (is_a($affected, 'PEAR_Error'))
 
 127     // Mark the task as deleted.
 
 128     $sql = "update tt_tasks set status = NULL where id = $task_id";
 
 129     $affected = $mdb2->exec($sql);
 
 130     return (!is_a($affected, 'PEAR_Error'));
 
 133  // insert function inserts a new task into database.
 
 134   static function insert($fields)
 
 136     $mdb2 = getConnection();
 
 138     $group_id = (int) $fields['group_id'];
 
 139     $org_id = (int) $fields['org_id'];
 
 140     $name = $fields['name'];
 
 141     $description = $fields['description'];
 
 142     $projects = $fields['projects'];
 
 143     $status = $fields['status'];
 
 145     $sql = "insert into tt_tasks (group_id, org_id, name, description, status)
 
 146       values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($status).")";
 
 147     $affected = $mdb2->exec($sql);
 
 149     if (is_a($affected, 'PEAR_Error'))
 
 152     $sql = "select last_insert_id() as last_insert_id";
 
 153     $res = $mdb2->query($sql);
 
 154     $val = $res->fetchRow();
 
 155     $last_id = $val['last_insert_id'];
 
 157     if (is_array($projects)) {
 
 158       foreach ($projects as $p_id) {
 
 159         // Insert task binds into tt_project_task_binds table.
 
 160         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
 
 161           " values($p_id, $last_id, $group_id, $org_id)";
 
 162         $affected = $mdb2->exec($sql);
 
 163         if (is_a($affected, 'PEAR_Error'))
 
 166         // Add task bind to the tasks field of the tt_projects table.
 
 167         $sql = "select tasks from tt_projects where id = $p_id";
 
 168         $res = $mdb2->query($sql);
 
 169         if (is_a($res, 'PEAR_Error'))
 
 172         $val = $res->fetchRow();
 
 173         $task_ids = $val['tasks'];
 
 175           $task_ids .= ",$last_id";
 
 176           $task_ids = ttTaskHelper::sort($task_ids);
 
 178           $task_ids = $last_id;
 
 180         $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $p_id";
 
 181         $affected = $mdb2->exec($sql);
 
 182         if (is_a($affected, 'PEAR_Error'))
 
 189   // update function updates a task in the database.
 
 190   static function update($fields)
 
 193     $mdb2 = getConnection();
 
 195     $group_id = $user->getActiveGroup();
 
 196     $org_id = $user->org_id;
 
 197     $task_id = (int)$fields['task_id'];
 
 198     $name = $fields['name'];
 
 199     $description = $fields['description'];
 
 200     $status = $fields['status'];
 
 201     $projects = $fields['projects'];
 
 203     $sql = "update tt_tasks set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
 
 204       ", status = $status where id = $task_id and group_id = $group_id";
 
 205     $affected = $mdb2->exec($sql);
 
 206     if (is_a($affected, 'PEAR_Error'))
 
 207       die($affected->getMessage());
 
 209     // Insert task binds into tt_project_task_binds table.
 
 210     $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 211     $affected = $mdb2->exec($sql);
 
 212     if (is_a($affected, 'PEAR_Error'))
 
 213       die($affected->getMessage());
 
 214     if (count($projects) > 0)
 
 215       foreach ($projects as $p_id) {
 
 216         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
 
 217           " values($p_id, $task_id, $group_id, $org_id)";
 
 218         $affected = $mdb2->exec($sql);
 
 219         if (is_a($affected, 'PEAR_Error'))
 
 220           die($affected->getMessage());
 
 223     // Handle task binds in the tasks field of the tt_projects table.
 
 224     // We need to either delete or insert task id in all affected projects.
 
 226     // Get all not deleted projects for group.
 
 227     $sql = "select id, tasks from tt_projects where group_id = $group_id and status is not NULL";
 
 228     $res = $mdb2->query($sql);
 
 229     if (is_a($res, 'PEAR_Error'))
 
 230       die($res->getMessage());
 
 232     // Iterate through projects.
 
 233     while ($val = $res->fetchRow()) {
 
 234       $project_id = $val['id'];
 
 235       $task_ids = $val['tasks'];
 
 236       $task_arr = explode(',', $task_ids);
 
 238       if (is_array($projects) && in_array($project_id, $projects)) {
 
 239         // Task needs to be available for this project.
 
 240         if (!in_array($task_id, $task_arr)) {
 
 242             $task_ids .= ",$task_id";
 
 243             $task_ids = ttTaskHelper::sort($task_ids);
 
 245           $task_ids = $task_id;
 
 247         $sql = "update tt_projects set tasks = ".$mdb2->quote($task_ids)." where id = $project_id";
 
 248         $affected = $mdb2->exec($sql);
 
 249         if (is_a($affected, 'PEAR_Error'))
 
 250           die($affected->getMessage());
 
 253         // Task needs to be removed from this project.
 
 254         if (in_array($task_id, $task_arr)) {
 
 255           // Remove task from array.
 
 256           unset($task_arr[array_search($task_id, $task_arr)]);
 
 257           $comma_separated = implode(",", $task_arr); // This is a comma-separated list of associated task ids.
 
 259           // Re-bind the project to tasks.
 
 260           $sql = "update tt_projects set tasks = ".$mdb2->quote($comma_separated)." where id = $project_id";
 
 261           $affected = $mdb2->exec($sql);
 
 262           if (is_a($affected, 'PEAR_Error'))
 
 263             die($affected->getMessage());
 
 270   // sort function sorts task ids passed as comma-separated list by their name.
 
 271   static function sort($comma_separated) {
 
 272         // We can't sort an empty string.
 
 273         if (!$comma_separated)
 
 274           return $comma_separated;
 
 276     $mdb2 = getConnection();
 
 278         $sql = "select id, name from tt_tasks where id in ($comma_separated)";
 
 279     $res = $mdb2->query($sql);
 
 280     if (is_a($res, 'PEAR_Error'))
 
 281       die ($res->getMessage());
 
 284     while ($val = $res->fetchRow()) {
 
 285       $task_arr[] = array('id'=>$val['id'],'name'=>$val['name']);
 
 287     $task_arr = mu_sort($task_arr, 'name');
 
 289     for($i = 0; $i < count($task_arr); $i++) {
 
 290           $task_ids[] = $task_arr[$i]['id'];
 
 292         $result = implode(',', $task_ids);