From 160f32efc347a81b9f6cbc1271980dcfe8f8cea2 Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Wed, 20 Sep 2017 16:37:43 -0500
Subject: [PATCH] only show active topics/depts in filter actions. Error for
 saving blank topic/dept

---
 include/class.filter.php        | 36 +++++++++++++++++++++++++++++++++
 include/class.filter_action.php | 18 +++++++++++------
 include/class.forms.php         | 15 +++++---------
 3 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/include/class.filter.php b/include/class.filter.php
index 60431d55c..e53edf9b2 100644
--- a/include/class.filter.php
+++ b/include/class.filter.php
@@ -566,11 +566,47 @@ class Filter {
                     'sort' => (int) $sort,
                 ));
                 $I->setConfiguration($errors, $vars);
+                $config = json_decode($I->ht['configuration'], true);
+
+                if ($I->ht['type'] == 'dept') {
+                  $dept = Dept::lookup($config['dept_id']);
+                  if (!$dept || !$dept->isActive()) {
+                    $errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Department');
+                    continue;
+                  }
+                }
+
+                if ($I->ht['type'] == 'topic') {
+                  $topic = Topic::lookup($config['topic_id']);
+                  if (!$topic || !$topic->isActive()) {
+                    $errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Help Topic');
+                    continue;
+                  }
+                }
+
                 $I->save();
                 break;
             case 'I': # existing filter action
                 if ($I = FilterAction::lookup($info)) {
                     $I->setConfiguration($errors, $vars);
+
+                    $config = json_decode($I->ht['configuration'], true);
+
+                    if ($I->ht['type'] == 'dept') {
+                      $dept = Dept::lookup($config['dept_id']);
+                      if (!$dept || !$dept->isActive()) {
+                        $errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Department');
+                        continue;
+                      }
+                    }
+                    if ($I->ht['type'] == 'topic') {
+                      $topic = Topic::lookup($config['topic_id']);
+                      if (!$topic || !$topic->isActive()) {
+                        $errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Help Topic');
+                        continue;
+                      }
+                    }
+
                     $I->sort = (int) $sort;
                     $I->save();
                 }
diff --git a/include/class.filter_action.php b/include/class.filter_action.php
index 22a8a14e7..00be2a860 100644
--- a/include/class.filter_action.php
+++ b/include/class.filter_action.php
@@ -295,12 +295,17 @@ class FA_RouteDepartment extends TriggerAction {
     }
 
     function getConfigurationOptions() {
+      $depts = Dept::getDepartments(null, true, false);
+
         return array(
-                'dept_id' => new DepartmentField(array(
-                    'configuration' => array(
-                        'prompt' => __('Unchanged'),
-                        'data' => array('quick-add' => 'department'),
-                    ),
+                'dept_id' => new ChoiceField(array(
+                'configuration' => array(
+                    'prompt' => __('Unchanged'),
+                    'data' => array('quick-add' => 'department'),
+                ),
+                'choices' =>
+                    $depts +
+                    array(':new:' => '— '.__('Add New').' —'),
                 'validators' => function($self, $clean) {
                     if ($clean === ':new:')
                         $self->addError(__('Select a department'));
@@ -429,7 +434,8 @@ class FA_AssignTopic extends TriggerAction {
     }
 
     function getConfigurationOptions() {
-        $choices = Topic::getHelpTopics(false, Topic::DISPLAY_DISABLED);
+        $choices = Topic::getHelpTopics(false, false);
+
         return array(
             'topic_id' => new ChoiceField(array(
                 'configuration' => array('prompt' => __('Unchanged')),
diff --git a/include/class.forms.php b/include/class.forms.php
index 3278a128a..d5ba6a0b0 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2356,12 +2356,9 @@ class DepartmentField extends ChoiceField {
         global $cfg;
 
         $selected = self::getWidget();
-        if($selected && $selected->value)
-        {
-          if(is_array($selected->value))
-          {
-            foreach ($selected->value as $k => $v)
-            {
+        if($selected && $selected->value) {
+          if(is_array($selected->value)) {
+            foreach ($selected->value as $k => $v) {
               $current_id = $k;
               $current_name = $v;
             }
@@ -2380,8 +2377,7 @@ class DepartmentField extends ChoiceField {
             ->values('id', 'name');
 
         $choices = array();
-        if ($depts = Dept::getDepartments(null, true, Dept::DISPLAY_DISABLED))
-        {
+        if ($depts = Dept::getDepartments(null, true, Dept::DISPLAY_DISABLED)) {
           //create array w/queryset
           $active = array();
           foreach ($active_depts as $dept)
@@ -2391,8 +2387,7 @@ class DepartmentField extends ChoiceField {
           $active[$current_id] = $current_name;
 
 
-          foreach ($depts as $id => $name)
-          {
+          foreach ($depts as $id => $name) {
             $choices[$id] = $name;
             if(!array_key_exists($id, $active) && $current_id)
               unset($choices[$id]);
-- 
GitLab