From 948ce7ffbf4d7dcd7ba318bf635668e53bf323f4 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 27 Sep 2013 15:54:32 +0000
Subject: [PATCH] Fix changing and saving of client's name

---
 include/ajax.forms.php                     |  9 ++++---
 include/class.user.php                     | 28 ++++++++++++++++------
 include/staff/templates/user-info.tmpl.php |  1 +
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index ee95f722a..27fac6672 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -39,10 +39,10 @@ class DynamicFormsAjaxAPI extends AjaxController {
     function _getUserForms() {
         $static = new Form(array(
             'name' => new TextboxField(array(
-                'label'=>'Full Name', 'configuration'=>array('size'=>40))
+                'label'=>'Full Name', 'required'=>true, 'configuration'=>array('size'=>40))
             ),
             'email' => new TextboxField(array(
-                'label'=>'Default Email', 'configuration'=>array(
+                'label'=>'Default Email', 'required'=>true, 'configuration'=>array(
                     'validator'=>'email', 'size'=>40))
             ),
         ));
@@ -86,11 +86,10 @@ class DynamicFormsAjaxAPI extends AjaxController {
         }
 
         $data = $static->getClean();
-        $user->first = $data['first'];
-        $user->last = $data['last'];
+        $user->name = $data['name'];
         $user->default_email->address = $data['email'];
-        $user->save();
         $user->default_email->save();
+        $user->save();
 
         // Save custom data
         foreach ($custom_data as $cd)
diff --git a/include/class.user.php b/include/class.user.php
index b6bc367f3..8a01c159c 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -112,6 +112,23 @@ class User extends UserModel {
         }
         return $data;
     }
+
+    function save($refetch=false) {
+        // Drop commas and reorganize the name without them
+        $parts = array_map('trim', explode(',', $this->name));
+        switch (count($parts)) {
+            case 2:
+                // Assume last, first --or-- last suff., first
+                $this->name = $parts[1].' '.$parts[0];
+                // XXX: Consider last, first suff.
+                break;
+            case 3:
+                // Assume last, first, suffix, write 'first last suffix'
+                $this->name = $parts[1].' '.$parts[0].' '.$parts[2];
+                break;
+        }
+        return parent::save($refetch);
+    }
 }
 User::_inspect();
 
@@ -146,7 +163,10 @@ class PersonsName {
     }
 
     function getLastFirst() {
-        return $this->parts['last'].', '.$this->parts['first'];
+        $name = $this->parts['last'].', '.$this->parts['first'];
+        if ($this->parts['suffix'])
+            $name .= ', '.$this->parts['suffix'];
+        return $name;
     }
 
     function __toString() {
@@ -159,12 +179,6 @@ class PersonsName {
     static function splitName($name) {
         $results = array();
 
-        // If there is a comma in the name, reverse the name
-        if (mb_strpos($name, ',' !== false)) {
-            list($last, $first) = explode(',', $name);
-            $name = $first.' '.$last;
-        }
-
         $r = explode(' ', $name);
         $size = count($r);
 
diff --git a/include/staff/templates/user-info.tmpl.php b/include/staff/templates/user-info.tmpl.php
index b07e19561..3cfb9d467 100644
--- a/include/staff/templates/user-info.tmpl.php
+++ b/include/staff/templates/user-info.tmpl.php
@@ -8,6 +8,7 @@
             if (!data.length) {
                 form.closest('.dialog').hide();
                 $('#overlay').hide();
+                location.reload();
             } else {
                 form.closest('.dialog').empty().append(data);
             }
-- 
GitLab