From 1da5e2ebef28a4d97d634e800fcb901e5e8b3989 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 28 Nov 2014 20:35:13 +0000
Subject: [PATCH] forms: Add flags field to form field.

This is necessary so we can consolidate various settings and permissions
into one bit mask field.
---
 include/class.dynamic_forms.php          | 29 +++++++-----------------
 include/class.forms.php                  | 12 ++++++++++
 include/i18n/en_US/form.yaml             | 16 +++++++++++++
 setup/inc/streams/core/install-mysql.sql |  1 +
 4 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 1ff552bd0..4bed6b6fd 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -689,10 +689,12 @@ class DynamicFormEntry extends VerySimpleModel {
                 return $ans;
         return null;
     }
+
     function setAnswer($name, $value, $id=false) {
         foreach ($this->getAnswers() as $ans) {
-            if ($ans->getField()->get('name') == $name) {
-                $ans->getField()->reset();
+            $f = $ans->getField();
+            if ($f->isStorable() && $f->get('name') == $name) {
+                $f->reset();
                 $ans->set('value', $value);
                 if ($id !== false)
                     $ans->set('value_id', $id);
@@ -907,18 +909,8 @@ class DynamicFormEntry extends VerySimpleModel {
                 $this->_fields[] = $fImpl;
                 $this->_form = null;
 
-                // Omit fields without data
-                // For user entries, the name and email fields should not be
-                // saved with the rest of the data
-                if ($this->object_type == 'U'
-                        && in_array($field->get('name'), array('name','email')))
-                    continue;
-
-                if ($this->object_type == 'O'
-                        && in_array($field->get('name'), array('name')))
-                    continue;
-
-                if (!$field->hasData())
+                // Omit fields without data and non-storable fields.
+                if (!$field->hasData() || !$field->isStorable())
                     continue;
 
                 $a->save();
@@ -937,15 +929,10 @@ class DynamicFormEntry extends VerySimpleModel {
             $this->set('updated', new SqlFunction('NOW'));
         parent::save();
         foreach ($this->getFields() as $field) {
-            $a = $field->getAnswer();
-            if ($this->object_type == 'U'
-                    && in_array($field->get('name'), array('name','email')))
-                continue;
-
-            if ($this->object_type == 'O'
-                    && in_array($field->get('name'), array('name')))
+            if (!$field->isStorable())
                 continue;
 
+            $a = $field->getAnswer();
             // Set the entry ID here so that $field->getClean() can use the
             // entry-id if necessary
             $a->set('entry_id', $this->get('id'));
diff --git a/include/class.forms.php b/include/class.forms.php
index e744f172c..7cebfb42f 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -397,6 +397,18 @@ class FormField {
         return (($this->get('edit_mask') & 32) == 0);
     }
 
+    /**
+     * isStorable
+     *
+     * Indicate if this field data is storable locally (default).Some field's data
+     * might beed to be stored elsewhere for optimization reasons at the
+     * application level.
+     *
+     */
+
+    function isStorable() {
+        return (($this->get('flags') & 2) == 0);
+    }
 
     /**
      * parse
diff --git a/include/i18n/en_US/form.yaml b/include/i18n/en_US/form.yaml
index b5bde9db9..d8759c8fc 100644
--- a/include/i18n/en_US/form.yaml
+++ b/include/i18n/en_US/form.yaml
@@ -15,6 +15,7 @@
 #               useful for page and email templates, where %{ ticket.<name> }
 #               will be used to retrieve the data from the field.
 #   hint:       Help text shown with the field
+#   flags:      Bit mask for settings & options
 #   edit_mask:  Mask out edits to the field (1=>delete, 2=>change name,
 #                   4=>privacy setting, 8=>requirement setting)
 #   private:    True if the field should be hidden from the client
@@ -36,6 +37,7 @@
       label: Email Address
       required: true
       sort: 1
+      flags: 3
       edit_mask: 15
       configuration:
         size: 40
@@ -46,6 +48,7 @@
       label: Full Name
       required: true
       sort: 2
+      flags: 3
       edit_mask: 15
       configuration:
         size: 40
@@ -55,6 +58,7 @@
       label: Phone Number
       required: false
       sort: 3
+      flags: 1
     - type: memo # notrans
       name: notes
       label: Internal Notes
@@ -81,6 +85,7 @@
       required: true
       edit_mask: 15
       sort: 1
+      flags: 1
       configuration:
         size: 40
         length: 50
@@ -92,6 +97,7 @@
       required: true
       edit_mask: 15
       sort: 2
+      flags: 3
     - id: 22
       type: priority # notrans
       name: priority # notrans
@@ -100,6 +106,7 @@
       private: true
       edit_mask: 3
       sort: 3
+      flags: 1
 - type: C # notrans
   title: Company Information
   instructions: Details available in email templates
@@ -110,6 +117,7 @@
       label: Company Name
       required: true
       sort: 1
+      flags: 1
       edit_mask: 3
       configuration:
         size: 40
@@ -118,6 +126,7 @@
       name: website # notrans
       label: Website
       sort: 2
+      flags: 1
       configuration:
         size: 40
         length: 64
@@ -126,6 +135,7 @@
       label: Phone Number
       required: false
       sort: 3
+      flags: 1
       configuration:
         ext: false
     - type: memo # notrans
@@ -133,6 +143,7 @@
       label: Address
       required: false
       sort: 4
+      flags: 1
       configuration:
         rows: 2
         cols: 40
@@ -148,6 +159,7 @@
       label: Name
       required: true
       sort: 1
+      flags: 3
       edit_mask: 15
       configuration:
         size: 40
@@ -157,6 +169,7 @@
       label: Address
       required: false
       sort: 2
+      flags: 1
       configuration:
         rows: 2
         cols: 40
@@ -167,11 +180,13 @@
       label: Phone
       required: false
       sort: 3
+      flags: 1
     - type: text
       name: website
       label: Website
       required: false
       sort: 4
+      flags: 1
       configuration:
         size: 40
         length: 0
@@ -180,6 +195,7 @@
       label: Internal Notes
       required: false
       sort: 5
+      flags: 1
       configuration:
         rows: 4
         cols: 40
diff --git a/setup/inc/streams/core/install-mysql.sql b/setup/inc/streams/core/install-mysql.sql
index b10118f2f..f32ff6d74 100644
--- a/setup/inc/streams/core/install-mysql.sql
+++ b/setup/inc/streams/core/install-mysql.sql
@@ -127,6 +127,7 @@ DROP TABLE IF EXISTS `%TABLE_PREFIX%form_field`;
 CREATE TABLE `%TABLE_PREFIX%form_field` (
     `id` int(11) unsigned NOT NULL auto_increment,
     `form_id` int(11) unsigned NOT NULL,
+    `flags` int(10) unsigned DEFAULT 1,
     `type` varchar(255) NOT NULL DEFAULT 'text',
     `label` varchar(255) NOT NULL,
     `required` tinyint(1) NOT NULL DEFAULT 0,
-- 
GitLab