Fixed sorting of projects in dropdown on the reports.php page.
[timetracker.git] / WEB-INF / lib / ttProjectHelper.class.php
index 455076f..399cba2 100644 (file)
@@ -26,9 +26,9 @@
 // | https://www.anuko.com/time_tracker/credits.htm
 // +----------------------------------------------------------------------+
 
-import('ttTeamHelper');
 import('ttUserHelper');
 import('ttGroupHelper');
+import('ttClientHelper');
 
 // Class ttProjectHelper is used to help with project related tasks.
 class ttProjectHelper {
@@ -45,7 +45,7 @@ class ttProjectHelper {
     // Do a query with inner join to get assigned projects.
     $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p".
       " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
-      " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by p.name";
+      " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by upper(p.name)";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       while ($val = $res->fetchRow()) {
@@ -87,7 +87,7 @@ class ttProjectHelper {
 
     $result = array();
     $sql = "select id, name, tasks from tt_projects".
-      " where group_id = $group_id and org_id = $org_id and (status = 0 or status = 1) order by name";
+      " where group_id = $group_id and org_id = $org_id and (status = 0 or status = 1) order by upper(name)";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       while ($val = $res->fetchRow()) {
@@ -109,7 +109,7 @@ class ttProjectHelper {
     $sql = "select p.id, p.name, p.tasks from tt_projects p".
       " inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)".
       " where p.group_id = $group_id and p.org_id = $org_id and (p.status = 0 or p.status = 1)".
-      " order by p.name";
+      " order by upper(p.name)";
 
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
@@ -165,6 +165,15 @@ class ttProjectHelper {
     global $user;
     $mdb2 = getConnection();
 
+    // Delete associated files.
+    if ($user->isPluginEnabled('at')) {
+      import('ttFileHelper');
+      global $err;
+      $fileHelper = new ttFileHelper($err);
+      if (!$fileHelper->deleteEntityFiles($id, 'project'))
+        return false;
+    }
+
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
@@ -195,7 +204,9 @@ class ttProjectHelper {
     if (is_a($affected, 'PEAR_Error'))
       return false;
 
-    return true;
+    // Finally, delete the project from the projects field in tt_clients table.
+    $result = ttClientHelper::deleteProject($id);
+    return $result;
   }
   
   // insert function inserts a new project into database.
@@ -235,8 +246,8 @@ class ttProjectHelper {
     }
 
     // Bind the project to tasks in tt_project_task_binds table.
-    $all_tasks = ttTeamHelper::getAllTasks($group_id);
-    foreach ($all_tasks as $task) {
+    $active_tasks = ttGroupHelper::getActiveTasks();
+    foreach ($active_tasks as $task) {
       if(in_array($task['id'], $tasks)) {
         $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
           " values($last_id, ".$task['id'].", $group_id, $org_id)";
@@ -265,7 +276,10 @@ class ttProjectHelper {
     $status = $fields['status']; // Project status.
     
     // Update records in tt_user_project_binds table.
-    $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
+    // This logic is complicated because these binds have rates associated with them.
+    // We try to recover old rates if a bind existed before and is now reactivated.
+    $sql = "select user_id, status from tt_user_project_binds".
+      " where project_id = $project_id and group_id = $group_id and org_id = $org_id";
     $all_users = array();
     $users_to_update = array();
     $res2 = $mdb2->query($sql);
@@ -274,7 +288,7 @@ class ttProjectHelper {
        // Delete tt_user_project_binds record (safely).
        ttUserHelper::deleteBind($row['user_id'], $project_id);
       } elseif (!$row['status']) {
-       // If we are here, status of the bind is not active. Memorize such users to update their bind status.
+        // If we are here, status of the bind is not active. Memorize such users to activate their bind status.
         $users_to_update[] = $row['user_id'];  // Users we need to update in tt_user_project_binds.
       }
       $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
@@ -282,7 +296,8 @@ class ttProjectHelper {
     // Insert records.
     $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
     if(count($users_to_add) > 0) {
-      $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
+      $sql = "select id, rate from tt_users".
+        " where id in (".join(', ', $users_to_add).") and group_id = $group_id and org_id = $org_id";
       $res = $mdb2->query($sql);
       while ($row = $res->fetchRow()) {
         $user_rate[$row['id']] = $row['rate'];
@@ -297,31 +312,22 @@ class ttProjectHelper {
     }
     // Update status (to active) in existing tt_user_project_binds records.
     if ($users_to_update) {
-      $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
+      $sql = "update tt_user_project_binds set status = 1".
+        " where project_id = $project_id and user_id in (".join(', ', $users_to_update).") and group_id = $group_id and org_id = $org_id";
       $affected = $mdb2->exec($sql);
       if (is_a($affected, 'PEAR_Error'))
         return false;
     }
     // End of updating tt_user_project_binds table.
 
-    // Update records in tt_project_task_binds table.
-    // Obtain existing task binds.
-    $existing_task_binds = array();
-    $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
-    $res = $mdb2->query($sql);
-    while ($val = $res->fetchRow()) {
-      $existing_task_binds[] = $val['task_id'];
-    }
-    // Determine which task binds to delete and which ones to add.
-    $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
-    $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
-    foreach ($task_binds_to_delete as $task_id) {
-      $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
-      $affected = $mdb2->exec($sql);
-      if (is_a($affected, 'PEAR_Error'))
-        return false;
-    }    
-    foreach ($task_binds_to_add as $task_id) {
+    // Update records in tt_project_task_binds table by deleting and inserting.
+    $sql = "delete from tt_project_task_binds".
+      " where project_id = $project_id and group_id = $group_id and org_id = $org_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error'))
+      return false;
+
+    foreach ($tasks_to_bind as $task_id) {
       $sql = "insert into tt_project_task_binds (project_id, task_id, group_id, org_id)".
         " values($project_id, $task_id, $group_id, $org_id)";
       $affected = $mdb2->exec($sql);
@@ -333,29 +339,28 @@ class ttProjectHelper {
     // Update project name, description, tasks and status in tt_projects table.
     $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
     $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
-           ", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id and group_id = ".$user->getGroup();
+      ", tasks = ".$mdb2->quote($comma_separated).", status = ".$mdb2->quote($status).
+      " where id = $project_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
     return (!is_a($affected, 'PEAR_Error'));
   }
-  
-  /*
+
   // getAssignedUsers - returns an array of user ids assigned to a project.
-  static function getAssignedUsers($project_id)
-  {
-       global $user;
-       
-    $result = array();
+  static function getAssignedUsers($project_id) {
+    global $user;
     $mdb2 = getConnection();
-    
-    // Do a query with inner join to get assigned users.
-    $sql = "select id, name from tt_users u
-      inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    $result = array();
+    $sql = "select user_id from tt_user_project_binds".
+      " where project_id = $project_id and group_id = $group_id and org_id = $org_id and status = 1";
     $res = $mdb2->query($sql);
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
+    if (is_a($res, 'PEAR_Error')) return false;
+    while ($row = $res->fetchRow()) {
+      $result[] = $row['user_id'];
     }
     return $result;
-  }*/
+  }
 }