From 702ed4d7cdf6f31d5ec09687927866b4d8a8aff6 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 5 Jun 2014 09:51:23 -0500
Subject: [PATCH] forms: Preserve form data when changing forms

---
 include/ajax.forms.php            | 12 ++++++++++--
 include/client/open.inc.php       |  3 ++-
 include/staff/ticket-open.inc.php |  3 ++-
 open.php                          |  2 ++
 scp/tickets.php                   |  1 +
 5 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index e199c2317..9a026e83c 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -14,9 +14,17 @@ class DynamicFormsAjaxAPI extends AjaxController {
     }
 
     function getFormsForHelpTopic($topic_id, $client=false) {
-        $topic = Topic::lookup($topic_id);
+        if (!($topic = Topic::lookup($topic_id)))
+            Http::response(404, 'No such help topic');
+
+        if ($_GET || isset($_SESSION[':form-data'])) {
+            if (!is_array($_SESSION[':form-data']))
+                $_SESSION[':form-data'] = array();
+            $_SESSION[':form-data'] = array_merge($_SESSION[':form-data'], $_GET);
+        }
+
         if ($form = $topic->getForm())
-            $form->render(!$client);
+            $form->getForm($_SESSION[':form-data'])->render(!$client);
     }
 
     function getClientFormsForHelpTopic($topic_id) {
diff --git a/include/client/open.inc.php b/include/client/open.inc.php
index 415eacabc..9cb1bb657 100644
--- a/include/client/open.inc.php
+++ b/include/client/open.inc.php
@@ -33,8 +33,9 @@ if ($info['topicId'] && ($topic=Topic::lookup($info['topicId']))) {
         <td class="required">Help Topic:</td>
         <td>
             <select id="topicId" name="topicId" onchange="javascript:
+                    var data = $(':input[name]', '#dynamic-form').serialize();
                     $('#dynamic-form').load(
-                        'ajax.php/form/help-topic/' + this.value);
+                        'ajax.php/form/help-topic/' + this.value, data);
                     ">
                 <option value="" selected="selected">&mdash; Select a Help Topic &mdash;</option>
                 <?php
diff --git a/include/staff/ticket-open.inc.php b/include/staff/ticket-open.inc.php
index f9a3ad3c1..bfd0571bd 100644
--- a/include/staff/ticket-open.inc.php
+++ b/include/staff/ticket-open.inc.php
@@ -127,8 +127,9 @@ if ($info['topicId'] && ($topic=Topic::lookup($info['topicId']))) {
             </td>
             <td>
                 <select name="topicId" onchange="javascript:
+                        var data = $(':input[name]', '#dynamic-form').serialize();
                         $('#dynamic-form').load(
-                            'ajax.php/form/help-topic/' + this.value);
+                            'ajax.php/form/help-topic/' + this.value, data);
                         ">
                     <?php
                     if ($topics=Topic::getHelpTopics()) {
diff --git a/open.php b/open.php
index e0bc8fdcf..fe454521b 100644
--- a/open.php
+++ b/open.php
@@ -38,6 +38,8 @@ if ($_POST) {
     //Ticket::create...checks for errors..
     if(($ticket=Ticket::create($vars, $errors, SOURCE))){
         $msg='Support ticket request created';
+        // Drop session-backed form data
+        unset($_SESSION[':form-data']);
         //Logged in...simply view the newly created ticket.
         if($thisclient && $thisclient->isValid()) {
             session_write_close();
diff --git a/scp/tickets.php b/scp/tickets.php
index 82f0e569f..16b9d4cde 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -497,6 +497,7 @@ if($_POST && !$errors):
                         if (!$ticket->checkStaffAccess($thisstaff) || $ticket->isClosed())
                             $ticket=null;
                         Draft::deleteForNamespace('ticket.staff%', $thisstaff->getId());
+                        unset($_SESSION[':form-data']);
                     } elseif(!$errors['err']) {
                         $errors['err']='Unable to create the ticket. Correct the error(s) and try again';
                     }
-- 
GitLab