From ce3d69ae1034cfdf7da44cbb0285d06859826f45 Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Mon, 11 Feb 2019 14:45:06 -0600
Subject: [PATCH] FAQ Issues

This commit fixes several issues with how we manage FAQs and related objects.

1. When trying to add a Help Topic to an FAQ, we should add the record to the faq_topic table after saving the faq so that we can accurately retrieve the faq_id

2. When deleting a Help Topic, we need to make sure we're using the topic->delete function rather than deleting based on a QuerySet so that the related FAQ Topics will also be deleted.

3. When deleting a FAQ Category, we need to ensure that we delete all related FAQs and FAQ Topics. To do this, we should use the delete function from the FAQ class first to delete all related FAQs and FAQ Topics and then we should use the Category delete function to delete the remaining Category (remove faqs->expunge from the category->delete function since it we now pass through faq->delete as well)
---
 include/class.category.php |  1 -
 include/class.faq.php      |  6 +++---
 scp/categories.php         | 18 +++++++++++++-----
 scp/helptopics.php         | 13 ++++++++-----
 4 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/include/class.category.php b/include/class.category.php
index d8cbb72f2..1c2991bd1 100644
--- a/include/class.category.php
+++ b/include/class.category.php
@@ -160,7 +160,6 @@ class Category extends VerySimpleModel {
     function delete() {
         try {
             parent::delete();
-            $this->faqs->expunge();
         }
         catch (OrmException $e) {
             return false;
diff --git a/include/class.faq.php b/include/class.faq.php
index 4eb1c2d83..9525ae4a5 100644
--- a/include/class.faq.php
+++ b/include/class.faq.php
@@ -301,7 +301,7 @@ class FAQ extends VerySimpleModel {
         try {
             parent::delete();
             // Cleanup help topics.
-            $this->topics->delete();
+            $this->topics->expunge();
             // Cleanup attachments.
             $this->attachments->deleteAll();
         }
@@ -391,11 +391,11 @@ class FAQ extends VerySimpleModel {
         $this->notes = Format::sanitize($vars['notes']);
         $this->keywords = ' ';
 
-        $this->updateTopics($vars['topics']);
-
         if (!$this->save())
             return false;
 
+        $this->updateTopics($vars['topics']);
+
         // General attachments (for all languages)
         // ---------------------
         // Delete removed attachments.
diff --git a/scp/categories.php b/scp/categories.php
index bea8e9a24..b408d6955 100644
--- a/scp/categories.php
+++ b/scp/categories.php
@@ -96,15 +96,23 @@ if($_POST){
                         }
                         break;
                     case 'delete':
-                        $i = Category::objects()->filter(array(
+                        $categories = Category::objects()->filter(array(
                             'category_id__in'=>$_POST['ids']
-                        ))->delete();
+                        ));
+                        foreach ($categories as $c) {
+                            if ($faqs = FAQ::objects()
+                                  ->filter(array('category_id'=>$c->getId()))) {
+                                      foreach ($faqs as $key => $faq)
+                                          $faq->delete();
+                                  }
+                             $c->delete();
+                        }
 
-                        if ($i==$count)
+                        if (count($categories)==$count)
                             $msg = sprintf(__('Successfully deleted %s.'),
                                 _N('selected category', 'selected categories', $count));
-                        elseif ($i > 0)
-                            $warn = sprintf(__('%1$d of %2$d %3$s deleted'), $i, $count,
+                        elseif ($categories > 0)
+                            $warn = sprintf(__('%1$d of %2$d %3$s deleted'), $categories, $count,
                                 _N('selected category', 'selected categories', $count));
                         elseif (!$errors['err'])
                             $errors['err'] = sprintf(__('Unable to delete %s.'),
diff --git a/scp/helptopics.php b/scp/helptopics.php
index b00c231d5..68db1e4d7 100644
--- a/scp/helptopics.php
+++ b/scp/helptopics.php
@@ -102,15 +102,18 @@ if($_POST){
                         }
                         break;
                     case 'delete':
-                        $i = Topic::objects()->filter(array(
+                        $topics = Topic::objects()->filter(array(
                             'topic_id__in'=>$_POST['ids']
-                        ))->delete();
+                        ));
+
+                        foreach ($topics as $t)
+                            $t->delete();
 
-                        if($i && $i==$count)
+                        if($topics && $topics==$count)
                             $msg = sprintf(__('Successfully deleted %s.'),
                                 _N('selected help topic', 'selected help topics', $count));
-                        elseif($i>0)
-                            $warn = sprintf(__('%1$d of %2$d %3$s deleted'), $i, $count,
+                        elseif($topics>0)
+                            $warn = sprintf(__('%1$d of %2$d %3$s deleted'), $topics, $count,
                                 _N('selected help topic', 'selected help topics', $count));
                         elseif(!$errors['err'])
                             $errors['err']  = sprintf(__('Unable to delete %s.'),
-- 
GitLab