From f83834dc5802725fda3ba4d7be7954a25a8c214c Mon Sep 17 00:00:00 2001
From: Jared Hancock <gravydish@gmail.com>
Date: Fri, 20 Jul 2012 15:51:41 -0500
Subject: [PATCH] Allow disabling canned responses that are used by email
 filters

But don't send out canned responses for new tickets that matched a filter
with an attached canned response if that response is not enabled. Also,
explicitly block deletes to filters that are in use.
---
 include/class.canned.php            | 8 +++++++-
 include/class.ticket.php            | 3 ++-
 include/staff/cannedreplies.inc.php | 8 ++------
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/include/class.canned.php b/include/class.canned.php
index 09a3877ad..e479c1873 100644
--- a/include/class.canned.php
+++ b/include/class.canned.php
@@ -31,9 +31,11 @@ class Canned {
         if(!$id && !($id=$this->getId()))
             return false;
 
-        $sql='SELECT canned.*, count(attach.file_id) as attachments '
+        $sql='SELECT canned.*, count(attach.file_id) as attachments, '
+            .' (count(filter.id) > 0) as in_use '
             .' FROM '.CANNED_TABLE.' canned '
             .' LEFT JOIN '.CANNED_ATTACHMENT_TABLE.' attach ON (attach.canned_id=canned.canned_id) ' 
+            .' LEFT JOIN '.EMAIL_FILTER_TABLE.' filter ON (canned.canned_id = filter.canned_response_id) '
             .' WHERE canned.canned_id='.db_input($id);
         if(!($res=db_query($sql)) ||  !db_num_rows($res))
             return false;
@@ -62,6 +64,9 @@ class Canned {
         return $this->isEnabled();
     }
 
+    function isInUse() {
+        return $this->ht['in_use'];
+    }
     
     function getTitle() {
         return $this->ht['title'];
@@ -168,6 +173,7 @@ class Canned {
     }
 
     function delete(){
+        if ($this->isInUse()) return false;
 
         $sql='DELETE FROM '.CANNED_TABLE.' WHERE canned_id='.db_input($this->getId()).' LIMIT 1';
         if(db_query($sql) && ($num=db_affected_rows())) {
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 0d9a8ced0..8f51bd592 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2013,7 +2013,8 @@ class Ticket{
         $ticket->onNewTicket($vars['message'], $autorespond, $alertstaff);
 
         if ($vars['cannedResponseId']
-                && ($canned = Canned::lookup($vars['cannedResponseId']))) {
+                && ($canned = Canned::lookup($vars['cannedResponseId']))
+                && $canned->isEnabled()) {
             $files = array();
             foreach ($canned->getAttachments() as $file)
                 $files[] = $file['id'];
diff --git a/include/staff/cannedreplies.inc.php b/include/staff/cannedreplies.inc.php
index e9ad6401f..b9e508630 100644
--- a/include/staff/cannedreplies.inc.php
+++ b/include/staff/cannedreplies.inc.php
@@ -2,12 +2,10 @@
 if(!defined('OSTSCPINC') || !$thisstaff) die('Access Denied');
 
 $qstr='';
-$sql='SELECT canned.*, count(attach.file_id) as files, dept.dept_name as department, '.
-     ' (filter.id is not null) as in_use '.
+$sql='SELECT canned.*, count(attach.file_id) as files, dept.dept_name as department '.
      ' FROM '.CANNED_TABLE.' canned '.
      ' LEFT JOIN '.DEPT_TABLE.' dept ON (dept.dept_id=canned.dept_id) '.
-     ' LEFT JOIN '.CANNED_ATTACHMENT_TABLE.' attach ON (attach.canned_id=canned.canned_id) '.
-     ' LEFT JOIN '.EMAIL_FILTER_TABLE.' filter ON (canned.canned_id = filter.canned_response_id) ';
+     ' LEFT JOIN '.CANNED_ATTACHMENT_TABLE.' attach ON (attach.canned_id=canned.canned_id) ';
 $sql.=' WHERE 1';
 
 $sortOptions=array('title'=>'canned.title','status'=>'canned.isenabled','dept'=>'department','updated'=>'canned.updated');
@@ -83,11 +81,9 @@ else
                 ?>
             <tr id="<?php echo $row['canned_id']; ?>">
                 <td width=7px>
-                <?php if (!$row['in_use']) { ?>
                   <input type="checkbox" name="ids[]" value="<?php echo $row['canned_id']; ?>"
                             <?php echo $sel?'checked="checked"':''; ?>  <?php echo $default?'disabled="disabled"':''; ?>
                                 onClick="highLight(this.value,this.checked);"/>
-                <?php } ?>
                 </td>
                 <td>
                     <a href="canned.php?id=<?php echo $row['canned_id']; ?>"><?php echo Format::truncate($row['title'],200); echo "&nbsp;$files"; ?></a>&nbsp;
-- 
GitLab