From 68f985f989bf1807365b2014cab20b2eafecb24b Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Fri, 15 Nov 2013 22:18:59 +0000
Subject: [PATCH] User update in real-time - no page reloading anymore.

---
 include/ajax.forms.php                     |  2 ++
 include/class.user.php                     |  9 +++++++++
 include/staff/templates/user-info.tmpl.php | 11 +++++++----
 include/staff/ticket-view.inc.php          |  5 +++--
 4 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index c196ea2b2..be185fc29 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -101,6 +101,8 @@ class DynamicFormsAjaxAPI extends AjaxController {
             }
             $cd->save();
         }
+
+        return Http::response(201, $user->to_json());
     }
 }
 
diff --git a/include/class.user.php b/include/class.user.php
index c89543f2b..269e202ca 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -119,6 +119,15 @@ class User extends UserModel {
         return new PersonsName($this->name);
     }
 
+    function to_json() {
+
+        $info = array(
+                'id'  => $this->getId(),
+                'name' => (string) $this->getName());
+
+        return JsonDataEncoder::encode($info);
+    }
+
     function asVar() {
         return (string) $this->getName();
     }
diff --git a/include/staff/templates/user-info.tmpl.php b/include/staff/templates/user-info.tmpl.php
index 867fc84f6..572f396df 100644
--- a/include/staff/templates/user-info.tmpl.php
+++ b/include/staff/templates/user-info.tmpl.php
@@ -6,13 +6,16 @@
 <form method="post" action="ajax.php/form/user-info/<?php
         echo $user->get('id'); ?>" onsubmit="javascript:
         var form = $(this);
+        var dialog = form.closest('.dialog');
         $.post(this.action, form.serialize(), function(data, status, xhr) {
-            if (!data.length) {
-                form.closest('.dialog').hide();
+            if(xhr && xhr.status == 201) {
+                var user = $.parseJSON(xhr.responseText);
+                $('#user-'+user.id+'-name').html(user.name);
+                $('div.body', dialog).empty();
+                dialog.hide();
                 $('#overlay').hide();
-                location.reload();
             } else {
-                form.closest('.dialog').empty().append(data);
+                $('div.body', dialog).html(data);
             }
         });
         return false;
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index dd113733c..d60bd2c73 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -151,8 +151,9 @@ if($ticket->isOverdue())
                             $('#user-info .body').load(this.href);
                             $('#user-info').show();
                             return false;
-                            "><i class="icon-user"></i> <?php
-                        echo Format::htmlchars($ticket->getName()); ?></a>
+                            "><i class="icon-user"></i> <span id="user-<?php echo $ticket->getOwnerId(); ?>-name"
+                            ><?php echo Format::htmlchars($ticket->getName());
+                        ?></span></a>
                         <?php
                         if(($client=$ticket->getClient())) {
                             echo sprintf('&nbsp;&nbsp;<a href="tickets.php?a=search&ownerId=%d" title="Related Tickets" data-dropdown="#action-dropdown-stats">(<b>%d</b>)</a>',
-- 
GitLab