From afc19371e7695b45c189ca84470f0e0282d0fabe Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Mon, 2 Mar 2015 20:45:13 +0000
Subject: [PATCH] forms: Add ability to delete properties form

This adds ability to delete properties on list deletion
---
 include/class.dynamic_forms.php |  2 ++
 include/class.list.php          | 17 +++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 0d089fe58..7ab537c8f 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -189,9 +189,11 @@ class DynamicForm extends VerySimpleModel {
     }
 
     function delete() {
+
         if (!$this->isDeletable())
             return false;
 
+        // Soft Delete: Mark the form as deleted.
         $this->setFlag(self::FLAG_DELETED);
         return $this->save();
     }
diff --git a/include/class.list.php b/include/class.list.php
index a182ef3ce..82142b90e 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -355,11 +355,20 @@ class DynamicList extends VerySimpleModel implements CustomList {
     function delete() {
         $fields = DynamicFormField::objects()->filter(array(
             'type'=>'list-'.$this->id))->count();
-        if ($fields == 0)
-            return parent::delete();
-        else
-            // Refuse to delete lists that are in use by fields
+
+        // Refuse to delete lists that are in use by fields
+        if ($fields != 0)
+            return false;
+
+        if (!parent::delete())
             return false;
+
+        if (($form = $this->getForm(false))) {
+            $form->delete(false);
+            $form->fields->delete();
+        }
+
+        return true;
     }
 
     private function createForm() {
-- 
GitLab