From dca09e4d8f32057895ba316e227545f3c63227c7 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 16 Apr 2015 12:05:46 -0500
Subject: [PATCH] Ensure consistent name format for agent dropdown

---
 include/class.dept.php             | 12 +++++++++++-
 include/class.staff.php            | 24 ++++++++++++++++++------
 include/class.user.php             | 10 ++++++++--
 include/staff/directory.inc.php    | 18 +++++++++++++++---
 include/staff/staffmembers.inc.php | 15 +++++++++++++--
 5 files changed, 65 insertions(+), 14 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index cea549872..9e6cf2146 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -124,6 +124,7 @@ class Dept {
     }
 
     function getMembers($criteria=null) {
+        global $cfg;
 
         if(!$this->members || $criteria) {
             $members = array();
@@ -144,7 +145,16 @@ class Dept {
                           AND s.isactive=1
                           AND s.onvacation=0 ) ';
 
-            $sql.=' ORDER BY s.lastname, s.firstname';
+            switch ($cfg->getDefaultNameFormat()) {
+            case 'last':
+            case 'lastfirst':
+            case 'legal':
+                $sql .= ' ORDER BY s.lastname, s.firstname';
+                break;
+
+            default:
+                $sql .= ' ORDER BY s.firstname, s.lastname';
+            }
 
             if(($res=db_query($sql)) && db_num_rows($res)) {
                 while(list($id)=db_fetch_row($res))
diff --git a/include/class.staff.php b/include/class.staff.php
index 367343272..e0525e63d 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -193,7 +193,7 @@ implements EmailContact {
     }
 
     function getName() {
-        return new PersonsName($this->ht['firstname'].' '.$this->ht['lastname']);
+        return new PersonsName(array('first' => $this->ht['firstname'], 'last' => $this->ht['lastname']));
     }
 
     function getFirstName() {
@@ -602,20 +602,32 @@ implements EmailContact {
 
     /**** Static functions ********/
     function getStaffMembers($availableonly=false) {
+        global $cfg;
 
-        $sql='SELECT s.staff_id, CONCAT_WS(" ", s.firstname, s.lastname) as name '
-            .' FROM '.STAFF_TABLE.' s ';
+        $sql = 'SELECT s.staff_id, s.firstname, s.lastname FROM '
+            .STAFF_TABLE.' s ';
 
         if($availableonly) {
             $sql.=' INNER JOIN '.GROUP_TABLE.' g ON(g.group_id=s.group_id AND g.group_enabled=1) '
                  .' WHERE s.isactive=1 AND s.onvacation=0';
         }
 
-        $sql.='  ORDER BY s.lastname, s.firstname';
+        switch ($cfg->getDefaultNameFormat()) {
+        case 'last':
+        case 'lastfirst':
+        case 'legal':
+            $sql .= ' ORDER BY s.lastname, s.firstname';
+            break;
+
+        default:
+            $sql .= ' ORDER BY s.firstname, s.lastname';
+        }
+
         $users=array();
         if(($res=db_query($sql)) && db_num_rows($res)) {
-            while(list($id, $name) = db_fetch_row($res))
-                $users[$id] = $name;
+            while(list($id, $fname, $lname) = db_fetch_row($res))
+                $users[$id] = new PersonsName(
+                    array('first' => $fname, 'last' => $lname));
         }
 
         return $users;
diff --git a/include/class.user.php b/include/class.user.php
index db6e7936d..635a6079d 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -623,8 +623,14 @@ class PersonsName {
         elseif($cfg)
             $this->format = $cfg->getDefaultNameFormat();
 
-        $this->parts = static::splitName($name);
-        $this->name = $name;
+        if (!is_array($name)) {
+            $this->parts = static::splitName($name);
+            $this->name = $name;
+        }
+        else {
+            $this->parts = $name;
+            $this->name = implode(' ', $name);
+        }
     }
 
     function getFirst() {
diff --git a/include/staff/directory.inc.php b/include/staff/directory.inc.php
index 622d4d371..15b717c14 100644
--- a/include/staff/directory.inc.php
+++ b/include/staff/directory.inc.php
@@ -1,7 +1,7 @@
 <?php
 if(!defined('OSTSTAFFINC') || !$thisstaff || !$thisstaff->isStaff()) die('Access Denied');
 $qs = array();
-$select='SELECT staff.*,CONCAT_WS(" ",firstname,lastname) as name,dept.dept_name as dept ';
+$select='SELECT staff.*,dept.dept_name as dept ';
 $from='FROM '.STAFF_TABLE.' staff '.
       'LEFT JOIN '.DEPT_TABLE.' dept ON(staff.dept_id=dept.dept_id) ';
 $where='WHERE staff.isvisible=1 ';
@@ -31,6 +31,16 @@ if($_REQUEST['did'] && is_numeric($_REQUEST['did'])) {
 $sortOptions=array('name'=>'staff.firstname,staff.lastname','email'=>'staff.email','dept'=>'dept.dept_name',
                    'phone'=>'staff.phone','mobile'=>'staff.mobile','ext'=>'phone_ext',
                    'created'=>'staff.created','login'=>'staff.lastlogin');
+
+switch ($cfg->getDefaultNameFormat()) {
+case 'last':
+case 'lastfirst':
+case 'legal':
+    $sortOptions['name'] = 'staff.lastname, staff.firstname';
+    break;
+// Otherwise leave unchanged
+}
+
 $orderWays=array('DESC'=>'DESC','ASC'=>'ASC');
 $sort=($_REQUEST['sort'] && $sortOptions[strtolower($_REQUEST['sort'])])?strtolower($_REQUEST['sort']):'name';
 //Sorting options...
@@ -111,9 +121,11 @@ else
     <?php
         if($res && db_num_rows($res)):
             $ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
-            while ($row = db_fetch_array($res)) { ?>
+            while ($row = db_fetch_array($res)) {
+            $name = new PersonsName(array('first' => $row['firstname'], 'last' => $row['lastname']));
+?>
                <tr id="<?php echo $row['staff_id']; ?>">
-                <td>&nbsp;<?php echo Format::htmlchars($row['name']); ?></td>
+                <td>&nbsp;<?php echo Format::htmlchars($name); ?></td>
                 <td>&nbsp;<?php echo Format::htmlchars($row['dept']); ?></td>
                 <td>&nbsp;<?php echo Format::htmlchars($row['email']); ?></td>
                 <td>&nbsp;<?php echo Format::phone($row['phone']); ?></td>
diff --git a/include/staff/staffmembers.inc.php b/include/staff/staffmembers.inc.php
index 34681d2eb..c5c7eaee4 100644
--- a/include/staff/staffmembers.inc.php
+++ b/include/staff/staffmembers.inc.php
@@ -1,7 +1,7 @@
 <?php
 if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin()) die('Access Denied');
 $qs = array();
-$select='SELECT staff.*,CONCAT_WS(" ",firstname,lastname) as name, grp.group_name, dept.dept_name as dept,count(m.team_id) as teams ';
+$select='SELECT staff.*, grp.group_name, dept.dept_name as dept,count(m.team_id) as teams ';
 $from='FROM '.STAFF_TABLE.' staff '.
       'LEFT JOIN '.GROUP_TABLE.' grp ON(staff.group_id=grp.group_id) '.
       'LEFT JOIN '.DEPT_TABLE.' dept ON(staff.dept_id=dept.dept_id) '.
@@ -25,6 +25,16 @@ if($_REQUEST['tid'] && is_numeric($_REQUEST['tid'])) {
 
 $sortOptions=array('name'=>'staff.firstname,staff.lastname','username'=>'staff.username','status'=>'isactive',
                    'group'=>'grp.group_name','dept'=>'dept.dept_name','created'=>'staff.created','login'=>'staff.lastlogin');
+
+switch ($cfg->getDefaultNameFormat()) {
+case 'last':
+case 'lastfirst':
+case 'legal':
+    $sortOptions['name'] = 'staff.lastname, staff.firstname';
+    break;
+// Otherwise leave unchanged
+}
+
 $orderWays=array('DESC'=>'DESC','ASC'=>'ASC');
 $sort=($_REQUEST['sort'] && $sortOptions[strtolower($_REQUEST['sort'])])?strtolower($_REQUEST['sort']):'name';
 //Sorting options...
@@ -143,11 +153,12 @@ else
                 $sel=false;
                 if($ids && in_array($row['staff_id'],$ids))
                     $sel=true;
+                $name = new PersonsName(array('first' => $row['firstname'], 'last' => $row['lastname']));
                 ?>
                <tr id="<?php echo $row['staff_id']; ?>">
                 <td width=7px>
                   <input type="checkbox" class="ckb" name="ids[]" value="<?php echo $row['staff_id']; ?>" <?php echo $sel?'checked="checked"':''; ?> >
-                <td><a href="staff.php?id=<?php echo $row['staff_id']; ?>"><?php echo Format::htmlchars($row['name']); ?></a>&nbsp;</td>
+                <td><a href="staff.php?id=<?php echo $row['staff_id']; ?>"><?php echo Format::htmlchars($name); ?></a>&nbsp;</td>
                 <td><?php echo $row['username']; ?></td>
                 <td><?php echo $row['isactive']?__('Active'):'<b>'.__('Locked').'</b>'; ?>&nbsp;<?php echo $row['onvacation']?'<small>(<i>'.__('vacation').'</i>)</small>':''; ?></td>
                 <td><a href="groups.php?id=<?php echo $row['group_id']; ?>"><?php echo Format::htmlchars($row['group_name']); ?></a></td>
-- 
GitLab