From 13d927a11f26ecb72fa0b068a417fbd134b8dbdc Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Wed, 27 May 2015 15:20:14 +0000
Subject: [PATCH] Use User/Agent specific name formatting

---
 include/ajax.users.php                   |  4 ++--
 include/class.config.php                 |  2 --
 include/class.dept.php                   |  2 +-
 include/class.nav.php                    |  3 ++-
 include/class.staff.php                  |  4 ++--
 include/class.user.php                   | 30 ++++++++++++++++++++----
 include/staff/directory.inc.php          |  4 ++--
 include/staff/staffmembers.inc.php       |  2 +-
 include/staff/tasks.inc.php              |  2 +-
 include/staff/templates/tickets.tmpl.php |  2 +-
 include/staff/templates/users.tmpl.php   |  2 +-
 include/staff/ticket-view.inc.php        |  2 +-
 include/staff/tickets.inc.php            |  4 ++--
 include/staff/users.inc.php              |  2 +-
 14 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/include/ajax.users.php b/include/ajax.users.php
index c835ba980..e00e811a9 100644
--- a/include/ajax.users.php
+++ b/include/ajax.users.php
@@ -35,7 +35,7 @@ class UsersAjaxAPI extends AjaxController {
 
         if (!$type || !strcasecmp($type, 'remote')) {
             foreach (AuthenticationBackend::searchUsers($_REQUEST['q']) as $u) {
-                $name = new PersonsName(array('first' => $u['first'], 'last' => $u['last']));
+                $name = new UsersName(array('first' => $u['first'], 'last' => $u['last']));
                 $users[] = array('email' => $u['email'], 'name'=>$name,
                     'info' => "{$u['email']} - $name (remote)",
                     'id' => "auth:".$u['id'], "/bin/true" => $_REQUEST['q']);
@@ -68,7 +68,7 @@ class UsersAjaxAPI extends AjaxController {
                             break;
                         }
                     }
-                    $name = Format::htmlchars(new PersonsName($name));
+                    $name = Format::htmlchars(new UsersName($name));
                     $users[] = array('email'=>$email, 'name'=>$name, 'info'=>"$email - $name",
                         "id" => $id, "/bin/true" => $_REQUEST['q']);
                 }
diff --git a/include/class.config.php b/include/class.config.php
index 37dc84a8d..114df78eb 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -156,7 +156,6 @@ class OsticketConfig extends Config {
         'pw_reset_window' =>    30,
         'enable_html_thread' => true,
         'allow_attachments' =>  true,
-        'name_format' =>        'full', # First Last
         'agent_name_format' =>  'full', # First Last
         'client_name_format' => 'original', # As entered
         'auto_claim_tickets'=>  true,
@@ -1072,7 +1071,6 @@ class OsticketConfig extends Config {
             'max_page_size'=>$vars['max_page_size'],
             'log_level'=>$vars['log_level'],
             'log_graceperiod'=>$vars['log_graceperiod'],
-            'name_format'=>$vars['name_format'],
             'time_format'=>$vars['time_format'],
             'date_format'=>$vars['date_format'],
             'datetime_format'=>$vars['datetime_format'],
diff --git a/include/class.dept.php b/include/class.dept.php
index 4e42d9335..0365f7e48 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -177,7 +177,7 @@ implements TemplateVariable {
                 ));
 
             $members->distinct('staff_id');
-            switch ($cfg->getDefaultNameFormat()) {
+            switch ($cfg->getAgentNameFormat()) {
             case 'last':
             case 'lastfirst':
             case 'legal':
diff --git a/include/class.nav.php b/include/class.nav.php
index e6df1e897..867f34623 100644
--- a/include/class.nav.php
+++ b/include/class.nav.php
@@ -247,7 +247,8 @@ class AdminNav extends StaffNav{
                     $subnav[]=array('desc'=>__('System'),'href'=>'settings.php?t=system','iconclass'=>'preferences');
                     $subnav[]=array('desc'=>__('Tickets'),'href'=>'settings.php?t=tickets','iconclass'=>'ticket-settings');
                     $subnav[]=array('desc'=>__('Tasks'),'href'=>'settings.php?t=tasks','iconclass'=>'lists');
-                    $subnav[]=array('desc'=>__('Access'),'href'=>'settings.php?t=access','iconclass'=>'users');
+                    $subnav[]=array('desc'=>__('Agents'),'href'=>'settings.php?t=agents','iconclass'=>'teams');
+                    $subnav[]=array('desc'=>__('Users'),'href'=>'settings.php?t=users','iconclass'=>'groups');
                     $subnav[]=array('desc'=>__('Knowledgebase'),'href'=>'settings.php?t=kb','iconclass'=>'kb-settings');
                     break;
                 case 'manage':
diff --git a/include/class.staff.php b/include/class.staff.php
index b573eaa6e..43a19c70a 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -253,7 +253,7 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
     }
 
     function getName() {
-        return new PersonsName(array('first' => $this->ht['firstname'], 'last' => $this->ht['lastname']));
+        return new AgentsName(array('first' => $this->ht['firstname'], 'last' => $this->ht['lastname']));
     }
 
     function getFirstName() {
@@ -709,7 +709,7 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
             ));
         }
 
-        switch ($cfg->getDefaultNameFormat()) {
+        switch ($cfg->getAgentNameFormat()) {
         case 'last':
         case 'lastfirst':
         case 'legal':
diff --git a/include/class.user.php b/include/class.user.php
index 9ab9009f3..751d17368 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -280,7 +280,7 @@ implements TemplateVariable {
             list($name) = explode('@', $this->getDefaultEmailAddress(), 2);
         else
             $name = $this->name;
-        return new PersonsName($name);
+        return new UsersName($name);
     }
 
     function getUpdateDate() {
@@ -742,10 +742,10 @@ implements TemplateVariable {
     function __construct($name, $format=null) {
         global $cfg;
 
-        if ($format && !isset(static::$formats[$format]))
+        if ($format && isset(static::$formats[$format]))
             $this->format = $format;
-        elseif($cfg)
-            $this->format = $cfg->getDefaultNameFormat();
+        else
+            $this->format = 'original';
 
         if (!is_array($name)) {
             $this->parts = static::splitName($name);
@@ -923,6 +923,28 @@ implements TemplateVariable {
 
 }
 
+class AgentsName extends PersonsName {
+    function __construct($name, $format=null) {
+        global $cfg;
+
+        if (!$format && $cfg)
+            $format = $cfg->getAgentNameFormat();
+
+        parent::__construct($name, $format);
+    }
+}
+
+class UsersName extends PersonsName {
+    function __construct($name, $format=null) {
+        global $cfg;
+        if (!$format && $cfg)
+            $format = $cfg->getClientNameFormat();
+
+        parent::__construct($name, $format);
+    }
+}
+
+
 class UserEmail extends UserEmailModel {
     static function ensure($address) {
         $email = static::lookup(array('address'=>$address));
diff --git a/include/staff/directory.inc.php b/include/staff/directory.inc.php
index 4e53d3597..f592cd8b5 100644
--- a/include/staff/directory.inc.php
+++ b/include/staff/directory.inc.php
@@ -37,11 +37,11 @@ $sortOptions=array('name'=>array('firstname','lastname'),'email'=>'email','dept'
                    'created'=>'created','login'=>'lastlogin');
 $orderWays=array('DESC'=>'-','ASC'=>'');
 
-switch ($cfg->getDefaultNameFormat()) {
+switch ($cfg->getAgentNameFormat()) {
 case 'last':
 case 'lastfirst':
 case 'legal':
-    $sortOptions['name'] = 'staff.lastname, staff.firstname';
+    $sortOptions['name'] = array('lastname', 'firstname');
     break;
 // Otherwise leave unchanged
 }
diff --git a/include/staff/staffmembers.inc.php b/include/staff/staffmembers.inc.php
index 6b588707f..d57258241 100644
--- a/include/staff/staffmembers.inc.php
+++ b/include/staff/staffmembers.inc.php
@@ -23,7 +23,7 @@ if ($sort && $sortOptions[$sort]) {
 
 $order_column = $order_column ? $order_column : array('firstname', 'lastname');
 
-switch ($cfg->getDefaultNameFormat()) {
+switch ($cfg->getClientNameFormat()) {
 case 'last':
 case 'lastfirst':
 case 'legal':
diff --git a/include/staff/tasks.inc.php b/include/staff/tasks.inc.php
index 4c0430b84..01c0be698 100644
--- a/include/staff/tasks.inc.php
+++ b/include/staff/tasks.inc.php
@@ -268,7 +268,7 @@ if ($thisstaff->hasPerm(Task::PERM_DELETE)) {
             $dept = Dept::getLocalById($T['dept_id'], 'name', $T['dept__name']);
             $assinee ='';
             if ($T['staff_id']) {
-                $staff =  new PersonsName($T['staff__firstname'].' '.$T['staff__lastname']);
+                $staff =  new AgentsName($T['staff__firstname'].' '.$T['staff__lastname']);
                 $assignee = sprintf('<span class="Icon staffAssigned">%s</span>',
                         Format::truncate((string) $staff, 40));
             } elseif($T['team_id']) {
diff --git a/include/staff/templates/tickets.tmpl.php b/include/staff/templates/tickets.tmpl.php
index 450b82728..57ced36e8 100644
--- a/include/staff/templates/tickets.tmpl.php
+++ b/include/staff/templates/tickets.tmpl.php
@@ -109,7 +109,7 @@ if ($results) { ?>
 
         $assigned='';
         if ($T['staff_id'])
-            $assigned = new PersonsName(array(
+            $assigned = new AgentsName(array(
                 'first' => $T['staff__firstname'],
                 'last' => $T['staff__lastname']
             ));
diff --git a/include/staff/templates/users.tmpl.php b/include/staff/templates/users.tmpl.php
index a91a586f9..a2111414b 100644
--- a/include/staff/templates/users.tmpl.php
+++ b/include/staff/templates/users.tmpl.php
@@ -92,7 +92,7 @@ if ($num) { ?>
             $ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
             while ($row = db_fetch_array($res)) {
 
-                $name = new PersonsName($row['name']);
+                $name = new UsersName($row['name']);
                 $status = 'Active';
                 $sel=false;
                 if($ids && in_array($row['id'], $ids))
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 4e44332a4..066567897 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -846,7 +846,7 @@ $tcount = $ticket->getThreadEntries($types)->count();
                                     continue;
 
                                 if (!is_object($name))
-                                    $name = new PersonsName($name);
+                                    $name = new AgentsName($name);
 
                                 $k="s$id";
                                 echo sprintf('<option value="%s" %s>%s</option>',
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index b0d5fd38e..d5844d26f 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -430,7 +430,7 @@ return false;">
                 $lc='';
                 if ($showassigned) {
                     if ($T['staff_id'])
-                        $lc = new PersonsName($T['staff__firstname'].' '.$T['staff__lastname']);
+                        $lc = new AgentsName($T['staff__firstname'].' '.$T['staff__lastname']);
                     elseif ($T['team_id'])
                         $lc = Team::getLocalById($T['team_id'], 'name', $T['team__name']);
                 }
@@ -482,7 +482,7 @@ return false;">
                             .$T['collab_count'].'"><i class="icon-group"></i></span>';
                     ?><span class="truncate" style="max-width:<?php
                         echo $T['collab_count'] ? '150px' : '170px'; ?>"><?php
-                    $un = new PersonsName($T['user__name']);
+                    $un = new UsersName($T['user__name']);
                         echo Format::htmlchars($un);
                     ?></span></div></td>
                 <?php
diff --git a/include/staff/users.inc.php b/include/staff/users.inc.php
index 5dd3f64f9..5ff926b64 100644
--- a/include/staff/users.inc.php
+++ b/include/staff/users.inc.php
@@ -163,7 +163,7 @@ else
                 if (!$U['name'])
                     list($name) = explode('@', $U['default_email__address']);
                 else
-                    $name = new PersonsName($U['name']);
+                    $name = new UsersName($U['name']);
 
                 // Account status
                 if ($U['account__id'])
-- 
GitLab