From 3770653fa0a7d7c06ee8668c974835325b3f62a9 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 1 Sep 2014 22:17:28 -0500
Subject: [PATCH] forms: Remove (some of the) magic from field config

---
 include/class.dynamic_forms.php                |  2 +-
 include/class.forms.php                        | 18 +++++++++++++++---
 .../templates/dynamic-field-config.tmpl.php    | 10 ++--------
 3 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 5a2ecec0b..4fd745e8c 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -448,7 +448,7 @@ class DynamicFormField extends VerySimpleModel {
      */
     function setConfiguration(&$errors=array()) {
         $config = array();
-        foreach ($this->getConfigurationForm() as $name=>$field) {
+        foreach ($this->getConfigurationForm($_POST)->getFields() as $name=>$field) {
             $config[$name] = $field->to_php($field->getClean());
             $errors = array_merge($errors, $field->errors());
         }
diff --git a/include/class.forms.php b/include/class.forms.php
index aa9405d80..d8c93f2c7 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -28,8 +28,11 @@ class Form {
 
     function __construct($fields=array(), $source=null, $options=array()) {
         $this->fields = $fields;
-        foreach ($fields as $f)
+        foreach ($fields as $k=>$f) {
             $f->setForm($this);
+            if (!$f->get('name') && $k)
+                $f->set('name', $k);
+        }
         if (isset($options['title']))
             $this->title = $options['title'];
         if (isset($options['instructions']))
@@ -535,12 +538,21 @@ class FormField {
         return false;
     }
 
-    function getConfigurationForm() {
+    function getConfigurationForm($source=null) {
         if (!$this->_cform) {
             $type = static::getFieldType($this->get('type'));
             $clazz = $type[1];
             $T = new $clazz();
-            $this->_cform = $T->getConfigurationOptions();
+            $config = $this->getConfiguration();
+            $this->_cform = new Form($T->getConfigurationOptions(), $source);
+            if (!$source && $config) {
+                foreach ($this->_cform->getFields() as $name=>$f) {
+                    if (isset($config[$name]))
+                        $f->value = $config[$name];
+                    elseif ($f->get('default'))
+                        $f->value = $f->get('default');
+                }
+            }
         }
         return $this->_cform;
     }
diff --git a/include/staff/templates/dynamic-field-config.tmpl.php b/include/staff/templates/dynamic-field-config.tmpl.php
index da2621b3f..af2574652 100644
--- a/include/staff/templates/dynamic-field-config.tmpl.php
+++ b/include/staff/templates/dynamic-field-config.tmpl.php
@@ -5,15 +5,9 @@
             echo $field->get('id'); ?>">
         <?php
         echo csrf_token();
-        $config = $field->getConfiguration();
-        $form = new Form($field->getConfigurationForm());
+        $form = $field->getConfigurationForm();
         echo $form->getMedia();
-        foreach ($form->getFields() as $name=>$f) {
-            if (isset($config[$name]))
-                $f->value = $config[$name];
-            else if ($f->get('default'))
-                $f->value = $f->get('default');
-            ?>
+        foreach ($form->getFields() as $name=>$f) { ?>
             <div class="flush-left custom-field">
             <div class="field-label">
             <label for="<?php echo $f->getWidget()->name; ?>">
-- 
GitLab