From caa00015e80223639f563ccfd7eb321e53c6d470 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Thu, 28 Jun 2012 00:11:46 -0400
Subject: [PATCH] Remove LEFT JOIN to time zone table in order to support
 upgrading from 1.6* to 1.7 + Fix bug on getDepts

---
 include/class.staff.php | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/include/class.staff.php b/include/class.staff.php
index 2f5726d01..0563d5017 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -37,13 +37,11 @@ class Staff {
         if(!$var && !($var=$this->getId()))
             return false;
 
-        $sql='SELECT staff.*, grp.*, tz.offset as tz_offset '
-            .' ,TIME_TO_SEC(TIMEDIFF(NOW(),IFNULL(staff.passwdreset,staff.created))) as passwd_change_sec '
+        $sql='SELECT staff.*, staff.created as added, grp.* '
             .' FROM '.STAFF_TABLE.' staff '
-            .' LEFT JOIN '.GROUP_TABLE.' grp ON(grp.group_id=staff.group_id) '
-            .' LEFT JOIN '.TIMEZONE_TABLE.' tz ON(tz.id=staff.timezone_id) ';
+            .' LEFT JOIN '.GROUP_TABLE.' grp ON(grp.group_id=staff.group_id) ';
 
-        $sql.=sprintf('WHERE %s=%s',is_numeric($var)?'staff_id':'username',db_input($var));
+        $sql.=sprintf(' WHERE %s=%s',is_numeric($var)?'staff_id':'username',db_input($var));
 
         if(!($res=db_query($sql)) || !db_num_rows($res))
             return NULL;
@@ -51,11 +49,20 @@ class Staff {
         
         $this->ht=db_fetch_array($res);
         $this->id  = $this->ht['staff_id'];
-        $this->teams =$this->ht['teams']=$this->getTeams();
-
-        $this->teams=array();
+        $this->teams = $this->ht['teams']= array();
         $this->stats=array();
 
+        //WE have to patch info here to support upgrading from old versions.
+        if(($time=strtotime($this->ht['passwdreset']?$this->ht['passwdreset']:$this->ht['added'])))
+            $this->ht['passwd_change'] = time()-$time; //XXX: check timezone issues.
+
+        /* TODO: Add timezone class
+        if($this->ht['timezone_id'])
+            $this->ht['tz_offset'] = Timezone::getOffsetById($this->ht['timezone_id']);
+        elseif($this->ht['timezone_offset'])
+            $this->ht['tz_offset'] = $this->ht['timezone_offset'];
+        */
+
         return ($this->id);
     }
 
@@ -96,7 +103,7 @@ class Staff {
     function isPasswdResetDue() {
         global $cfg;
         return ($cfg && $cfg->getPasswdResetPeriod() 
-                    && $this->ht['passwd_change_sec']>($cfg->getPasswdResetPeriod()*30*24*60*60));
+                    && $this->ht['passwd_change']>($cfg->getPasswdResetPeriod()*30*24*60*60));
     }
 
     function isPasswdChangeDue() {
@@ -169,7 +176,7 @@ class Staff {
 
     function getDepts() {
         //Departments the user is allowed to access...based on the group they belong to + user's dept.
-        return array_filter(array_unique(array_merge(explode(',', $this->ht['dept_access']), array($this->dept_id)))); //Neptune help us
+        return array_filter(array_unique(array_merge(explode(',', $this->ht['dept_access']), array($this->getDeptId())))); //Neptune help us
     }
 
     function getDepartments() {
-- 
GitLab