From ea9f0c482ca062e5ff4bda0a4a32b5f89d1360e4 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 11 Jul 2014 15:05:48 -0500
Subject: [PATCH] forms: Preserve data for help topic fields

This patch fixes a glitch in the dynamic forms processing system preventing
the validation and capture of data for help topic custom fields. Now the
`$vars` managed inside of `Ticket::create` is connected directly to the
`DynamicFormEntry` created for the data rather than using the magical
connection to `$_POST`.
---
 include/class.dynamic_forms.php | 18 ++++++++++++++----
 include/class.ticket.php        |  9 +++++++--
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index c0a203646..050c8f330 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -519,6 +519,7 @@ class DynamicFormEntry extends VerySimpleModel {
     var $_form;
     var $_errors = false;
     var $_clean = false;
+    var $_source = null;
 
     function getId() {
         return $this->get('id');
@@ -572,12 +573,21 @@ class DynamicFormEntry extends VerySimpleModel {
     function getFields() {
         if (!isset($this->_fields)) {
             $this->_fields = array();
-            foreach ($this->getAnswers() as $a)
-                $this->_fields[] = $a->getField();
+            foreach ($this->getAnswers() as $a) {
+                $T = $this->_fields[] = $a->getField();
+                $T->setForm($this);
+            }
         }
         return $this->_fields;
     }
 
+    function getSource() {
+        return $this->_source ?: (isset($this->id) ? false : $_POST);
+    }
+    function setSource($source) {
+        $this->_source = $source;
+    }
+
     function getField($name) {
 
         foreach ($this->getFields() as $field)
@@ -768,8 +778,8 @@ class DynamicFormEntry extends VerySimpleModel {
         if (count($this->dirty))
             $this->set('updated', new SqlFunction('NOW'));
         parent::save();
-        foreach ($this->getAnswers() as $a) {
-            $field = $a->getField();
+        foreach ($this->getFields() as $field) {
+            $a = $field->getAnswer();
             if ($this->object_type == 'U'
                     && in_array($field->get('name'), array('name','email')))
                 continue;
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 6518d12ea..b2967f288 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2307,9 +2307,11 @@ class Ticket {
 
         if ($vars['topicId'] && ($topic=Topic::lookup($vars['topicId']))) {
             if ($topic_form = $topic->getForm()) {
+                $TF = $topic_form->getForm($vars);
                 $topic_form = $topic_form->instanciate();
-                if (!$topic_form->getForm()->isValid($field_filter('topic')))
-                    $errors = array_merge($errors, $topic_form->getForm()->errors());
+                $topic_form->setSource($vars);
+                if (!$TF->isValid($field_filter('topic')))
+                    $errors = array_merge($errors, $TF->errors());
             }
         }
 
@@ -2338,6 +2340,9 @@ class Ticket {
         if(!Validator::process($fields, $vars, $errors) && !$errors['err'])
             $errors['err'] ='Missing or invalid data - check the errors and try again';
 
+        if ($vars['topicId'] && !$topic)
+            $errors['topicId'] = 'Invalid help topic selected';
+
         //Make sure the due date is valid
         if($vars['duedate']) {
             if(!$vars['time'] || strpos($vars['time'],':')===false)
-- 
GitLab