From cf6cd8140e062e9af887272a9c4542c1498033f0 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 25 Aug 2015 12:06:58 -0500
Subject: [PATCH] emails: Drop in-use check when deleting

---
 include/class.email.php |  7 ++++++-
 scp/emails.php          | 13 ++++---------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/class.email.php b/include/class.email.php
index 4baa862f4..92348241c 100644
--- a/include/class.email.php
+++ b/include/class.email.php
@@ -200,10 +200,15 @@ class Email extends VerySimpleModel {
         Dept::objects()
             ->filter(array('email_id' => $this->getId()))
             ->update(array(
-                'autoresp_email_id' => 0,
                 'email_id' => $cfg->getDefaultEmailId()
             ));
 
+        Dept::objects()
+            ->filter(array('autoresp_email_id' => $this->getId()))
+            ->update(array(
+                'autoresp_email_id' => 0,
+            ));
+
         return true;
     }
 
diff --git a/scp/emails.php b/scp/emails.php
index 82da3ad7c..9ff1c70a7 100644
--- a/scp/emails.php
+++ b/scp/emails.php
@@ -50,14 +50,8 @@ if($_POST){
             } else {
                 $count=count($_POST['ids']);
 
-                $sql='SELECT count(dept_id) FROM '.DEPT_TABLE.' dept '
-                    .' WHERE email_id IN ('.implode(',', db_input($_POST['ids'])).') '
-                    .' OR autoresp_email_id IN ('.implode(',', db_input($_POST['ids'])).')';
-
-                list($depts)=db_fetch_row(db_query($sql));
-                if($depts>0) {
-                    $errors['err'] = __('One or more of the selected emails is being used by a department. Remove association first!');
-                } elseif(!strcasecmp($_POST['a'], 'delete')) {
+                switch (strtolower($_POST['a'])) {
+                case 'delete':
                     $i=0;
                     foreach($_POST['ids'] as $k=>$v) {
                         if($v!=$cfg->getDefaultEmailId() && ($e=Email::lookup($v)) && $e->delete())
@@ -73,8 +67,9 @@ if($_POST){
                     elseif(!$errors['err'])
                         $errors['err'] = sprintf(__('Unable to delete %s'),
                             _N('selected email', 'selected emails', $count));
+                    break;
 
-                } else {
+                default:
                     $errors['err'] = __('Unknown action - get technical help.');
                 }
             }
-- 
GitLab