From 0df1e69af4037a105a66fca012fc909a825be010 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 12 May 2014 20:27:45 +0000
Subject: [PATCH] Make department selection on help topic optional

---
 include/class.config.php         |  7 +++++++
 include/class.dept.php           |  4 ++++
 include/class.priority.php       |  8 ++++++--
 include/class.topic.php          |  5 ++---
 include/staff/helptopics.inc.php | 15 +++++++++++++--
 5 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/include/class.config.php b/include/class.config.php
index 5cfaed44f..23772d05b 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -419,6 +419,13 @@ class OsticketConfig extends Config {
         return $this->get('default_priority_id');
     }
 
+    function getDefaultPriority() {
+        if (!isset($this->defaultPriority))
+            $this->defaultPriority = Priority::lookup($this->getDefaultPriorityId());
+
+        return $this->defaultPriority;
+    }
+
     function getDefaultTemplateId() {
         return $this->get('default_template_id');
     }
diff --git a/include/class.dept.php b/include/class.dept.php
index 04a0e1b72..1ec4116fb 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -327,6 +327,10 @@ class Dept {
         return $num;
     }
 
+    function __toString() {
+        return $this->getName();
+    }
+
     /*----Static functions-------*/
 	function getIdByName($name) {
         $id=0;
diff --git a/include/class.priority.php b/include/class.priority.php
index f4e7cf8c5..63a7434c4 100644
--- a/include/class.priority.php
+++ b/include/class.priority.php
@@ -15,12 +15,12 @@
 **********************************************************************/
 
 class Priority {
-    
+
     var $id;
     var $ht;
 
     function Priority($id){
-        
+
         $this->id =0;
         $this->load($id);
     }
@@ -65,6 +65,10 @@ class Priority {
         return ($this->ht['ispublic']);
     }
 
+    function __toString() {
+        return $this->getDesc();
+    }
+
     /* ------------- Static ---------------*/
     function lookup($id) {
         return ($id && is_numeric($id) && ($p=new Priority($id)) && $p->getId()==$id)?$p:null;
diff --git a/include/class.topic.php b/include/class.topic.php
index 8d4d460ab..ad3b21a41 100644
--- a/include/class.topic.php
+++ b/include/class.topic.php
@@ -221,7 +221,7 @@ class Topic {
         elseif(($tid=self::getIdByName($vars['topic'], $vars['pid'])) && $tid!=$id)
             $errors['topic']='Topic already exists';
 
-        if(!$vars['dept_id'])
+        if (!is_numeric($vars['dept_id']))
             $errors['dept_id']='You must select a department';
 
         if($errors) return false;
@@ -234,8 +234,7 @@ class Topic {
             .',topic='.db_input($vars['topic'])
             .',topic_pid='.db_input($vars['pid'])
             .',dept_id='.db_input($vars['dept_id'])
-            .',priority_id='.db_input(isset($vars['priority_id'])
-                ? $vars['priority_id'] : 0)
+            .',priority_id='.db_input($vars['priority_id'])
             .',sla_id='.db_input($vars['sla_id'])
             .',form_id='.db_input($vars['form_id'])
             .',page_id='.db_input($vars['page_id'])
diff --git a/include/staff/helptopics.inc.php b/include/staff/helptopics.inc.php
index 90eb45b45..8395a29e3 100644
--- a/include/staff/helptopics.inc.php
+++ b/include/staff/helptopics.inc.php
@@ -61,7 +61,7 @@ else
     <caption><?php echo $showing; ?></caption>
     <thead>
         <tr>
-            <th width="7">&nbsp;</th>        
+            <th width="7">&nbsp;</th>
             <th width="320"><a <?php echo $name_sort; ?> href="helptopics.php?<?php echo $qstr; ?>&sort=name">Help Topic</a></th>
             <th width="80"><a  <?php echo $status_sort; ?> href="helptopics.php?<?php echo $qstr; ?>&sort=status">Status</a></th>
             <th width="100"><a  <?php echo $type_sort; ?> href="helptopics.php?<?php echo $qstr; ?>&sort=type">Type</a></th>
@@ -75,14 +75,25 @@ else
         $total=0;
         $ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
         if($res && db_num_rows($res)):
+            $defaultDept = $cfg->getDefaultDept();
+            $defaultPriority = $cfg->getDefaultPriority();
             while ($row = db_fetch_array($res)) {
                 $sel=false;
                 if($ids && in_array($row['topic_id'],$ids))
                     $sel=true;
+
+                if (!$row['dept_id'] && $defaultDept) {
+                    $row['dept_id'] = $defaultDept->getId();
+                    $row['department'] = (string) $defaultDept;
+                }
+
+                if (!$row['priority'] && $defaultPriority)
+                    $row['priority'] = (string) $defaultPriority;
+
                 ?>
             <tr id="<?php echo $row['topic_id']; ?>">
                 <td width=7px>
-                  <input type="checkbox" class="ckb" name="ids[]" value="<?php echo $row['topic_id']; ?>" 
+                  <input type="checkbox" class="ckb" name="ids[]" value="<?php echo $row['topic_id']; ?>"
                             <?php echo $sel?'checked="checked"':''; ?>>
                 </td>
                 <td><a href="helptopics.php?id=<?php echo $row['topic_id']; ?>"><?php echo $row['name']; ?></a>&nbsp;</td>
-- 
GitLab