Adjusted header.tpl for Work plugin.
[timetracker.git] / WEB-INF / lib / ttClientHelper.class.php
index 42eb128..7105595 100644 (file)
@@ -63,7 +63,7 @@ class ttClientHelper {
 
     $result = array();
 
-    $sql = "select id, name from tt_clients where group_id = $group_id and org_id = $org_id and (status = 0 or status = 1) order by upper(name)";
+    $sql = "select id, name, projects from tt_clients 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()) {
@@ -270,14 +270,17 @@ class ttClientHelper {
   static function getAssignedProjects($client_id)
   {
     global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
 
     $result = array();
-    $mdb2 = getConnection();
 
     // Do a query with inner join to get assigned projects.
     $sql = "select p.id, p.name from tt_projects p".
       " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)".
-      " where p.group_id = ".$user->getGroup()." 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 p.name";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       while ($val = $res->fetchRow()) {
@@ -291,15 +294,19 @@ class ttClientHelper {
   static function getClientsForUser()
   {
     global $user;
+    $mdb2 = getConnection();
+
     $user_id = $user->getUser();
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
 
     $result = array();
-    $mdb2 = getConnection();
 
-    $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
-      inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
-      inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
-      where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
+    $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb".
+      " inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)".
+      " inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)".
+      " where upb.user_id = $user_id and upb.group_id = $group_id and upb.org_id = $org_id".
+      " and upb.status = 1 order by upper(c.name)";
 
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
@@ -309,4 +316,51 @@ class ttClientHelper {
     }
     return $result;
   }
+
+  // deleteProject - deletes a project from the projects field it tt_clients table
+  // for all clients in a group.
+  static function deleteProject($project_id) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    $sql = "select id from tt_clients".
+      " where projects like '%$project_id%'".
+      " and group_id = $group_id and org_id = $org_id";
+    $res = $mdb2->query($sql);
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        if (!ttClientHelper::deleteProjectFromClient($project_id, $val['id']))
+          return false;
+      }
+    }
+    return true;
+  }
+
+  // deleteProjectFromClient - deletes a project from the projects field in tt_clients table
+  // for a single client in a group.
+  static function deleteProjectFromClient($project_id, $client_id) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    $sql = "select projects from tt_clients".
+      " where id = $client_id and group_id = $group_id and org_id = $org_id";
+    $res = $mdb2->query($sql);
+    if (is_a($res, 'PEAR_Error')) return false;
+    $val = $res->fetchRow();
+    $projects = explode(',', $val['projects']);
+    if (($key = array_search($project_id, $projects)) !== false) {
+      unset($projects[$key]);
+    }
+    $comma_separated = implode(',', $projects);
+    $sql = "update tt_clients set projects = ".$mdb2->quote($comma_separated).
+      " where id = $client_id and group_id = $group_id and org_id = $org_id";
+    $affected = $mdb2->exec($sql);
+    return (!is_a($affected, 'PEAR_Error'));
+  }
 }