]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttGroupHelper.class.php
Some more work in progress on group editor.
[timetracker.git] / WEB-INF / lib / ttGroupHelper.class.php
index d28c30670e8eb26c07d91d055f79c977ac51da3e..41c03114e371425629e41f8f02cc8bc80b55c6c5 100644 (file)
 // This is a planned replacement for ttTeamHelper as we move forward with subgroups.
 class ttGroupHelper {
 
-  // The getTopGroups function returns an array of all active top groups on the server.
-  static function getTopGroups() {
-    $result = array();
+  // The getGroupName function returns group name.
+  static function getGroupName($group_id) {
     $mdb2 = getConnection();
 
-    $sql =  "select id, name, created, lang from tt_groups".
-            " where status = 1 and org_id is NULL or org_id = id order by id desc";
+    $sql = "select name from tt_groups where id = $group_id and (status = 1 or status = 0)";
     $res = $mdb2->query($sql);
-    $result = array();
+
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      return $val['name'];
+    }
+    return false;
+  }
+
+  // The getParentGroup determines a parent group for a given group.
+  static function getParentGroup($group_id) {
+    global $user;
+
+    $mdb2 = getConnection();
+
+    $sql = "select parent_id from tt_groups where id = $group_id and org_id = $user->org_id and status = 1";
+    $res = $mdb2->query($sql);
+
     if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $val['date'] = substr($val['created'], 0, 10); // Strip the time.
-        $result[] = $val;
-      }
-      return $result;
+      $val = $res->fetchRow();
+      return $val['parent_id'];
     }
     return false;
   }
+
+  // The getSubgroupByName obtain an immediate subgroup by name if one exists.
+  static function getSubgroupByName($name) {
+    global $user;
+
+    $mdb2 = getConnection();
+    $parent_id = $user->getActiveGroup();
+    $org_id = $user->org_id;
+
+    $sql = "select id from tt_groups where parent_id = $parent_id and org_id = $org_id".
+      " and name = ".$mdb2->quote($name)." and status is not null";
+    $res = $mdb2->query($sql);
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val && $val['id'])
+        return $val;
+    }
+    return false;
+  }
+
+  // The insertSubgroup inserts a new subgroup in database.
+  static function insertSubgroup($fields) {
+    global $user;
+
+    $mdb2 = getConnection();
+    $parent_id = $user->getActiveGroup();
+    $org_id = $user->org_id;
+
+    // TODO: inherit all attributes from the parent group, if not supplied.
+    $name = $fields['name'];
+    $description = $fields['description'];
+
+    $created = 'now()';
+    $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
+
+    $sql = "insert into tt_groups (parent_id, org_id, name, description, created, created_ip)".
+      " values($parent_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", $created, $created_ip)";
+    $affected = $mdb2->exec($sql);
+    return (!is_a($affected, 'PEAR_Error'));
+    // TODO: design subgroup roles carefully. Perhaps roles are not to be touched in subgroups at all.
+  }
 }