From 42b669932223165e3cf77026cd137ce8f377bd4c Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 27 Feb 2015 10:13:23 -0600
Subject: [PATCH] topic: forms: Hide forms with no visible fields

---
 include/ajax.forms.php            |  2 ++
 include/class.dynamic_forms.php   | 17 +++++++++++++++++
 include/client/open.inc.php       |  9 +--------
 include/staff/ticket-open.inc.php |  9 +--------
 4 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index 169290c94..fcdc4fbc5 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -25,6 +25,8 @@ class DynamicFormsAjaxAPI extends AjaxController {
         }
 
         foreach ($topic->getForms() as $form) {
+            if (!$form->hasAnyVisibleFields())
+                continue;
             ob_start();
             $form->getForm($_SESSION[':form-data'])->render(!$client);
             $html .= ob_get_clean();
diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 11bb3894c..015c2a42e 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -125,6 +125,23 @@ class DynamicForm extends VerySimpleModel {
         }
     }
 
+    function hasAnyVisibleFields($user=false) {
+        global $thisstaff, $thisclient;
+        $user = $user ?: $thisstaff ?: $thisclient;
+        $visible = 0;
+        $isstaff = $user instanceof Staff;
+        foreach ($this->getFields() as $F) {
+            if ($isstaff) {
+                if ($F->isVisibleToStaff())
+                    $visible++;
+            }
+            elseif ($F->isVisibleToUsers()) {
+                $visible++;
+            }
+        }
+        return $visible > 0;
+    }
+
     function instanciate($sort=1) {
         return DynamicFormEntry::create(array(
             'form_id'=>$this->get('id'), 'sort'=>$sort));
diff --git a/include/client/open.inc.php b/include/client/open.inc.php
index e29f2b651..e04c9f6ee 100644
--- a/include/client/open.inc.php
+++ b/include/client/open.inc.php
@@ -82,14 +82,7 @@ if ($info['topicId'] && ($topic=Topic::lookup($info['topicId']))) {
     </tbody>
     <tbody id="dynamic-form">
         <?php foreach ($forms as $form) {
-            $hasFields = false;
-            foreach ($form->getFields() as $f) {
-                if ($f->isVisibleToUsers()) {
-                    $hasFields = true;
-                    break;
-                }
-            }
-            if (!$hasFields)
+            if (!$form->hasAnyVisibleFields())
                 continue;
             include(CLIENTINC_DIR . 'templates/dynamic-form.tmpl.php');
         } ?>
diff --git a/include/staff/ticket-open.inc.php b/include/staff/ticket-open.inc.php
index e9edb4643..9591f1794 100644
--- a/include/staff/ticket-open.inc.php
+++ b/include/staff/ticket-open.inc.php
@@ -263,14 +263,7 @@ if ($_POST)
         <tbody id="dynamic-form">
         <?php
             foreach ($forms as $form) {
-                $hasFields = false;
-                foreach ($form->getFields() as $f) {
-                    if ($f->isVisibleToStaff()) {
-                        $hasFields = true;
-                        break;
-                    }
-                }
-                if (!$hasFields)
+                if (!$form->hasAnyVisibleFields())
                     continue;
                 print $form->getForm()->getMedia();
                 include(STAFFINC_DIR .  'templates/dynamic-form.tmpl.php');
-- 
GitLab