diff --git a/include/ajax.forms.php b/include/ajax.forms.php index 169290c945bac96986ef2ef222710691256851b5..fcdc4fbc57b734cb89c955df36cb41dd238fe156 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 11bb3894c4b5d816c4587f57afaf7d0b38b2b56b..015c2a42ed461518d9760c876c005ef4190ae469 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 e29f2b651be929de68e8d50f759e7bef41c28c33..e04c9f6eefcb4e165aa9918e602503281f5c3fcd 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/helptopic.inc.php b/include/staff/helptopic.inc.php index cf43338fa5abf33fc15477418cc5d485d6a48653..7df72585c1efdc152630af9438bf53a09419106d 100644 --- a/include/staff/helptopic.inc.php +++ b/include/staff/helptopic.inc.php @@ -1,6 +1,6 @@ <?php if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin()) die('Access Denied'); -$info = $qs = array(); +$info = $qs = $forms = array(); if($topic && $_REQUEST['a']!='add') { $title=__('Update Help Topic'); $action='update'; @@ -17,8 +17,8 @@ if($topic && $_REQUEST['a']!='add') { $submit_text=__('Add Topic'); $info['isactive']=isset($info['isactive'])?$info['isactive']:1; $info['ispublic']=isset($info['ispublic'])?$info['ispublic']:1; - $info['form_id'] = Topic::FORM_USE_PARENT; $qs += array('a' => $_REQUEST['a']); + $forms = TicketForm::objects(); } $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); ?> @@ -387,21 +387,7 @@ foreach ($forms as $F) { <br/> <strong><?php echo __('Add Custom Form'); ?></strong>: - <select name="form_id" onchange="javascript: - event.preventDefault(); - var $this = $(this), - val = $this.val(); - if (!val) return; - $.ajax({ - url: 'ajax.php/form/' + val + '/fields/view', - dataType: 'json', - success: function(json) { - if (json.success) { - $(json.html).appendTo('#topic-forms').effect('highlight'); - $this.find(':selected').prop('disabled', true); - } - } - });"> + <select name="form_id" id="newform"> <option value=""><?php echo '— '.__('Add a custom form') . ' —'; ?></option> <?php foreach (DynamicForm::objects()->filter(array('type'=>'G')) as $F) { ?> <option value="<?php echo $F->get('id'); ?>" @@ -438,6 +424,23 @@ $(function() { }; $('[name=sequence_id]').on('change', update_example); $('[name=number_format]').on('keyup', update_example); + + $('form select#newform').change(function() { + var $this = $(this), + val = $this.val(); + if (!val) return; + $.ajax({ + url: 'ajax.php/form/' + val + '/fields/view', + dataType: 'json', + success: function(json) { + if (json.success) { + $(json.html).appendTo('#topic-forms').effect('highlight'); + $this.find(':selected').prop('disabled', true); + } + } + }); + }); + }); $('table#topic-forms').sortable({ items: 'tbody', diff --git a/include/staff/ticket-open.inc.php b/include/staff/ticket-open.inc.php index e9edb4643ade0c9aa8dbf48f3b3c498c641a4ddc..9591f1794afe9ba08f62db40f9fafb073fb88954 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'); diff --git a/scp/helptopics.php b/scp/helptopics.php index 01c01337b5f82a083dfd69089c01fc6d5d94fcae..adb1eb7c4fcc8c2830fd2b2e3e72b734a0a25d2b 100644 --- a/scp/helptopics.php +++ b/scp/helptopics.php @@ -36,8 +36,9 @@ if($_POST){ } break; case 'create': - $topic = Topic::create(); - if ($topic->update($_POST, $errors)) { + $_topic = Topic::create(); + if ($_topic->update($_POST, $errors)) { + $topic = $_topic; $msg=sprintf(__('Successfully added %s'), Format::htmlchars($_POST['topic'])); $_REQUEST['a']=null; }elseif(!$errors['err']){