+
+ // isUserValid determines if a user is valid for on behalf work.
+ function isUserValid($user_id) {
+ if ($user_id == $this->id)
+ return true;
+ return ($this->getUserDetails($user_id) != null);
+ }
+
+ // isGroupValid determines if a group is valid for user.
+ function isGroupValid($group_id) {
+ if ($group_id == $this->group_id)
+ return true;
+ else
+ return $this->isSubgroupValid($group_id);
+ }
+
+ // isSubgroupValid determines if a subgroup is valid for user.
+ // A subgroup is valid if:
+ // - user can manage_subgroups;
+ // - subgroup is either a direct child of user group, or "on the path"
+ // to it (grand-child, etc.).
+ function isSubgroupValid($subgroup_id) {
+ if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
+
+ $current_group_id = $subgroup_id;
+ while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
+ if ($parent_group_id == $this->group_id) {
+ return true; // Found it.
+ }
+ $current_group_id = $parent_group_id;
+ }
+ return false;
+ }
+
+ // getMaxRankForGroup determines effective user rank for a user in a given group.
+ // For home group it is the existing user rank (as per role) minus 1.
+ // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
+ function getMaxRankForGroup($group_id) {
+
+ $max_rank = 0; // Start safely.
+ if ($this->group_id == $group_id) {
+ $max_rank = $this->rank - 1;
+ return $max_rank;
+ }
+
+ if ($this->isSubgroupValid($group_id))
+ $max_rank = MAX_RANK;
+
+ return $max_rank;
+ }
+
+ // getUserPartForHeader constructs a string for user to display on pages header.
+ // It changes with "on behalf" attributes for both user and group.
+ function getUserPartForHeader() {
+ global $i18n;
+ if (!$this->id) return null;
+
+ $user_part = htmlspecialchars($this->name);
+ $user_part .= ' - '.htmlspecialchars($this->role_name);
+ if ($this->behalf_id) {
+ $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
+ }
+ if ($this->behalf_group_id) {
+ $user_part .= ', <span class="onBehalf">'.htmlspecialchars($this->behalf_group_name).'</span>';
+ } else {
+ if ($this->group_name) // Note: we did not require group names in the past.
+ $user_part .= ', '.$this->group_name;
+ }
+ return $user_part;
+ }
+
+ // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
+ function setOnBehalfGroup($group_id) {
+
+ // Unset things first.
+ $this->behalf_group_id = null;
+ $this->behalf_group_name = null;
+ $this->behalf_id = null;
+ $this->behalf_name = null;
+ unset($this->behalfGroup);
+ unset($_SESSION['behalf_group_id']);
+ unset($_SESSION['behalf_group_name']);
+ 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;
+
+ // No need to set if group is our home group.
+ if ($group_id == $this->group_id) return;
+
+ // No need to set if subgroup is not valid.
+ if (!$this->isSubgroupValid($group_id)) return;
+
+ // We are good to set on behalf group.
+ $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
+ $_SESSION['behalf_group_id'] = $group_id;
+ $_SESSION['behalf_group_name'] = $onBehalfGroupName;
+ $this->behalf_group_id = $group_id;
+ $this->behalf_group_name = $onBehalfGroupName;
+
+ $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
+
+ // Adjust on behalf user to first found user in subgroup.
+ $this->adjustBehalfId();
+ return;
+ }
+
+ // setOnBehalfUser sets on behalf user both the object and the session.
+ function setOnBehalfUser($user_id) {
+
+ // Unset things first.
+ $this->behalf_id = null;
+ $this->behalf_name = null;
+ unset($this->behalfUser);
+ unset($_SESSION['behalf_id']);
+ unset($_SESSION['behalf_name']);
+
+ // No need to set if user is us.
+ if ($user_id == $this->id) return;
+
+ // No need to set if user id is not valid.
+ if (!$this->isUserValid($user_id)) return;
+
+ // We are good to set on behalf user.
+ $onBehalfUserName = ttUserHelper::getUserName($user_id);
+ $_SESSION['behalf_id'] = $user_id;
+ $_SESSION['behalf_name'] = $onBehalfUserName;
+ $this->behalf_id = $user_id;
+ $this->behalf_name = $onBehalfUserName;
+
+ $this->behalfUser = new ttBehalfUser($this->behalf_id, $this->org_id);
+ 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;
+ }