Excluded clients from getActiveUsers call.
[timetracker.git] / WEB-INF / lib / ttUser.class.php
index 548d70e..c00bd9f 100644 (file)
@@ -29,6 +29,8 @@
 import('ttConfigHelper');
 import('ttGroupHelper');
 import('ttGroup');
+import('form.Form');
+import('form.ActionForm');
 
 class ttUser {
   var $login = null;            // User login.
@@ -111,9 +113,9 @@ class ttUser {
       $this->role_id = $val['role_id'];
       $this->role_name = $val['role_name'];
       $this->rights = explode(',', $val['rights']);
-      $this->is_client = !in_array('track_own_time', $this->rights);
       $this->rank = $val['rank'];
       $this->client_id = $val['client_id'];
+      $this->is_client = $this->client_id && !in_array('track_own_time', $this->rights);
       $this->email = $val['email'];
       $this->lang = $val['lang'];
       $this->decimal_mark = $val['decimal_mark'];
@@ -209,6 +211,16 @@ class ttUser {
     return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins);
   }
 
+  // getLockSpec returns lock specification for active group.
+  function getLockSpec() {
+    return ($this->behalfGroup ? $this->behalfGroup->lock_spec : $this->lock_spec);
+  }
+
+  // getWorkdayMinutes returns workday_minutes for active group.
+  function getWorkdayMinutes() {
+    return ($this->behalfGroup ? $this->behalfGroup->workday_minutes : $this->workday_minutes);
+  }
+
   // getConfig returns config string for active group.
   function getConfig() {
     return ($this->behalfGroup ? $this->behalfGroup->config : $this->config);
@@ -245,9 +257,9 @@ class ttUser {
     $result = array();
     $mdb2 = getConnection();
 
+    $user_id = $this->getUser();
     $group_id = $this->getGroup();
     $org_id = $this->org_id;
-    $user_id = $this->getUser();
 
     // Do a query with inner join to get assigned projects.
     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p".
@@ -329,7 +341,7 @@ class ttUser {
     if (!$this->isPluginEnabled('lk'))
       return false; // Locking feature is disabled.
 
-    if (!$this->lock_spec)
+    if (!$this->getLockSpec())
       return false; // There is no lock specification.
 
     if (!$this->behalf_id && $this->can('override_own_date_lock'))
@@ -342,7 +354,7 @@ class ttUser {
     require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
 
     // Calculate the last occurrence of a lock.
-    $last = tdCron::getLastOccurrence($this->lock_spec, time());
+    $last = tdCron::getLastOccurrence($this->getLockSpec(), time());
     $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
     if ($date->before($lockdate))
       return true;
@@ -589,15 +601,10 @@ class ttUser {
 
   // updateGroup updates group information with new data.
   function updateGroup($fields) {
-    if (!($this->can('manage_basic_settings') ||
-      $this->can('manage_advanced_settings') ||
-      $this->can('manage_features'))) return false;
-    // TODO: update the above for subgroup updates.
+    $mdb2 = getConnection();
 
     $group_id = $fields['group_id'];
     if ($group_id && !$this->isGroupValid($group_id)) return false;
-
-    $mdb2 = getConnection();
     if (!$group_id) $group_id = $this->getGroup();
 
     if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
@@ -782,6 +789,13 @@ class ttUser {
     unset($_SESSION['behalf_id']);
     unset($_SESSION['behalf_name']);
 
+    // Destroy report bean if it was set in session.
+    $form = new Form('dummyForm');
+    $bean = new ActionForm('reportBean', $form, $request);
+    if ($bean->isSaved()) {
+      $bean->destroyBean();
+    }
+
     // Do not do anything if we don't have rights.
     if (!$this->can('manage_subgroups')) return;
 
@@ -805,7 +819,7 @@ class ttUser {
     return;
   }
 
-   // setOnBehalfUser sets on behalf user both the object and the session.
+  // setOnBehalfUser sets on behalf user both the object and the session.
   function setOnBehalfUser($user_id) {
 
     // Unset things first.
@@ -828,4 +842,17 @@ class ttUser {
     $this->behalf_name = $onBehalfUserName;
     return;
   }
+
+  // The exists() function determines if an active user exists in context of a page.
+  // If we are working as self, true.
+  // If we are working in a subgroup with active users, true.
+  // If we are working in a subgroup without active users, false.
+  function exists() {
+    if (!$this->behalfGroup)
+      return true; // Working as self.
+    else if ($this->behalfGroup->active_users)
+      return true; // Subgroup has users.
+
+    return false;
+  }
 }