From 1d361b51dac1931ff32af342fe630c64e2c5a0bf Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Mon, 18 Sep 2017 14:37:42 -0500
Subject: [PATCH] set topics/depts to default if disabled/archived and set
 elsewhere

---
 include/class.dept.php           | 25 +++++++++++++++++++++++--
 include/class.topic.php          | 13 +++++++++++++
 include/staff/email.inc.php      |  3 +--
 include/staff/helptopics.inc.php |  7 -------
 scp/departments.php              | 22 ++++++++++++++--------
 scp/helptopics.php               | 22 ++++++++++++++--------
 6 files changed, 65 insertions(+), 27 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index e098e4661..c29632199 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -144,11 +144,32 @@ implements TemplateVariable {
       return !($this->flags & self::FLAG_ARCHIVED);
     }
 
-    function isActive()
-    {
+    function isActive() {
         return !!($this->flags & self::FLAG_ACTIVE);
     }
 
+    function clearInactiveDept($dept_id) {
+      global $cfg;
+
+      $topics = Topic::objects()->filter(array('dept_id'=>$dept_id))->values_flat('topic_id');
+      if ($topics) {
+        foreach ($topics as $topic_id) {
+          $topic = Topic::lookup($topic_id[0]);
+          $topic->dept_id = $cfg->getDefaultDeptId();
+          $topic->save();
+        }
+      }
+
+      $emails = Email::objects()->filter(array('dept_id'=>$dept_id))->values_flat('email_id');
+      if ($emails) {
+        foreach ($emails as $email_id) {
+          $email = Email::lookup($email_id[0]);
+          $email->dept_id = $cfg->getDefaultDeptId();
+          $email->save();
+        }
+      }
+    }
+
     function getEmailId() {
         return $this->email_id;
     }
diff --git a/include/class.topic.php b/include/class.topic.php
index 39a03d4df..75aa91806 100644
--- a/include/class.topic.php
+++ b/include/class.topic.php
@@ -184,6 +184,19 @@ implements TemplateVariable {
       return !!($this->flags & self::FLAG_ACTIVE);
     }
 
+    function clearInactiveTopic($topic_id) {
+      global $cfg;
+
+      $emails = Email::objects()->filter(array('topic_id'=>$topic_id))->values_flat('email_id');
+      if ($emails) {
+        foreach ($emails as $email_id) {
+          $email = Email::lookup($email_id[0]);
+          $email->topic_id = $cfg->getDefaultTopicId();
+          $email->save();
+        }
+      }
+    }
+
     function getStatus() {
       if($this->flags & self::FLAG_ACTIVE)
         return 'Active';
diff --git a/include/staff/email.inc.php b/include/staff/email.inc.php
index 86ac887a0..9633acff6 100644
--- a/include/staff/email.inc.php
+++ b/include/staff/email.inc.php
@@ -144,8 +144,7 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
 			    <?php
                     $warn = '';
                     $topics = Topic::getHelpTopics();
-                    if($info['topic_id'] && !array_key_exists($info['topic_id'], $topics))
-                    {
+                    if($info['topic_id'] && !array_key_exists($info['topic_id'], $topics)) {
                       $topics[$info['topic_id']] = $email->topic;
                       $warn = sprintf(__('%s selected must be active'), __('Help Topic'));
                     }
diff --git a/include/staff/helptopics.inc.php b/include/staff/helptopics.inc.php
index 731bee7c9..8978b97e3 100644
--- a/include/staff/helptopics.inc.php
+++ b/include/staff/helptopics.inc.php
@@ -123,13 +123,6 @@ $order_by = 'sort';
                 if ($topic->dept_id) {
                     $deptId = $topic->dept_id;
                     $dept = (string) $topic->dept;
-                    $department = Dept::lookup($deptId);
-                    if (!$department->isActive()) {
-                      $deptId = $defaultDept->getId();
-                      $dept = (string) $defaultDept;
-                      $topic->dept_id = $deptId;
-                      $topic->save();
-                    }
                 } elseif ($defaultDept) {
                     $deptId = $defaultDept->getId();
                     $dept = (string) $defaultDept;
diff --git a/scp/departments.php b/scp/departments.php
index 5bb8e0dd4..11d03812a 100644
--- a/scp/departments.php
+++ b/scp/departments.php
@@ -25,6 +25,9 @@ if($_REQUEST['id'] && !($dept=Dept::lookup($_REQUEST['id'])))
                 if(!$dept){
                     $errors['err']=sprintf(__('%s: Unknown or invalid'), __('department'));
                 }elseif($dept->update($_POST,$errors)){
+                    if ($_POST["status"] != __('Active'))
+                      Dept::clearInactiveDept($dept->getId());
+
                     $msg=sprintf(__('Successfully updated %s.'),
                         __('this department'));
                 }elseif(!$errors['err']){
@@ -96,8 +99,7 @@ if($_REQUEST['id'] && !($dept=Dept::lookup($_REQUEST['id'])))
                             ))->exclude(array(
                                 'id'=>$cfg->getDefaultDeptId()
                             ));
-                            foreach ($depts as $d)
-                            {
+                            foreach ($depts as $d) {
                               $d->setFlag(Dept::FLAG_ARCHIVED, false);
                               $d->setFlag(Dept::FLAG_ACTIVE, true);
                               if($d->save())
@@ -122,12 +124,14 @@ if($_REQUEST['id'] && !($dept=Dept::lookup($_REQUEST['id'])))
                             ))->exclude(array(
                                 'id'=>$cfg->getDefaultDeptId()
                             ));
-                            foreach ($depts as $d)
-                            {
+                            foreach ($depts as $d) {
                               $d->setFlag(Dept::FLAG_ARCHIVED, false);
                               $d->setFlag(Dept::FLAG_ACTIVE, false);
-                              if($d->save())
+                              if($d->save()) {
                                 $num++;
+                                //set dept_id to default for topics/emails using disabled dept
+                                Dept::clearInactiveDept($d->getId());
+                              }
                             }
                             if ($num > 0) {
                                 if($num==$count)
@@ -147,12 +151,14 @@ if($_REQUEST['id'] && !($dept=Dept::lookup($_REQUEST['id'])))
                             ))->exclude(array(
                                 'id'=>$cfg->getDefaultDeptId()
                             ));
-                            foreach ($depts as $d)
-                            {
+                            foreach ($depts as $d) {
                               $d->setFlag(Dept::FLAG_ARCHIVED, true);
                               $d->setFlag(Dept::FLAG_ACTIVE, false);
-                              if($d->save())
+                              if($d->save()) {
                                 $num++;
+                                //set dept_id to default for topics/emails using archived dept
+                                Dept::clearInactiveDept($d->getId());
+                              }
                             }
                             if ($num > 0) {
                                 if($num==$count)
diff --git a/scp/helptopics.php b/scp/helptopics.php
index c64a995f0..281b1c7f1 100644
--- a/scp/helptopics.php
+++ b/scp/helptopics.php
@@ -28,6 +28,9 @@ if($_POST){
             if(!$topic){
                 $errors['err']=sprintf(__('%s: Unknown or invalid'), __('help topic'));
             }elseif($topic->update($_POST,$errors)){
+              if ($_POST["status"] != __('Active'))
+                Topic::clearInactiveTopic($topic->getId());
+
                 $msg=sprintf(__('Successfully updated %s.'),
                     __('this help topic'));
             }elseif(!$errors['err']){
@@ -66,8 +69,7 @@ if($_POST){
                         $topics = Topic::objects()->filter(array(
                           'topic_id__in'=>$_POST['ids'],
                         ));
-                        foreach ($topics as $t)
-                        {
+                        foreach ($topics as $t) {
                           $t->setFlag(Topic::FLAG_ARCHIVED, false);
                           $t->setFlag(Topic::FLAG_ACTIVE, true);
                           if($t->save())
@@ -92,12 +94,14 @@ if($_POST){
                         ))->exclude(array(
                             'topic_id'=>$cfg->getDefaultTopicId()
                         ));
-                        foreach ($topics as $t)
-                        {
+                        foreach ($topics as $t) {
                           $t->setFlag(Topic::FLAG_ARCHIVED, false);
                           $t->setFlag(Topic::FLAG_ACTIVE, false);
-                          if($t->save())
+                          if($t->save()) {
                             $num++;
+                            //remove topic_id for emails using disabled topic
+                            Topic::clearInactiveTopic($t->getId());
+                          }
                         }
                         if ($num > 0) {
                             if($num==$count)
@@ -117,12 +121,14 @@ if($_POST){
                         ))->exclude(array(
                             'topic_id'=>$cfg->getDefaultTopicId()
                         ));
-                        foreach ($topics as $t)
-                        {
+                        foreach ($topics as $t) {
                           $t->setFlag(Topic::FLAG_ARCHIVED, true);
                           $t->setFlag(Topic::FLAG_ACTIVE, false);
-                          if($t->save())
+                          if($t->save()) {
                             $num++;
+                            //remove topic_id for emails using disabled topic
+                            Topic::clearInactiveTopic($t->getId());
+                          }
                         }
                         if ($num > 0) {
                             if($num==$count)
-- 
GitLab