From 38910e539249fa411329df17b15ab9ea76b6a08e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 25 Jun 2012 22:45:10 -0500
Subject: [PATCH] Support canned responses in email filters

---
 include/class.filter.php                  |  7 +++++++
 include/class.ticket.php                  | 18 ++++++++++++++++++
 include/staff/filter.inc.php              | 21 +++++++++++++++++++++
 main.inc.php                              |  2 +-
 scp/filters.php                           |  1 +
 setup/inc/sql/b19dc97d-71e05961.patch.sql | 13 +++++++++++++
 setup/inc/sql/osticket-v1.7-mysql.sql     |  1 +
 setup/inc/sql/osticket-v1.7-mysql.sql.md5 |  2 +-
 8 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 setup/inc/sql/b19dc97d-71e05961.patch.sql

diff --git a/include/class.filter.php b/include/class.filter.php
index 9c3065c10..396d45050 100644
--- a/include/class.filter.php
+++ b/include/class.filter.php
@@ -99,6 +99,10 @@ class Filter {
         return $this->ht['team_id'];
     }
 
+    function getCannedResponse() {
+        return $this->ht['canned_response'];
+    }
+
     function stopOnMatch(){
         return ($this->ht['stop_on_match']);
     }
@@ -274,6 +278,8 @@ class Filter {
             if ($email['reply-to-name']) 
                 $ticket['name'] = $email['reply-to-name'];
         }
+        if ($this->getCannedResponse())
+            $ticket['canned_response'] = $this->getCannedResponse();
     }
 
     function update($vars,&$errors){
@@ -395,6 +401,7 @@ class Filter {
              ',reject_email='.db_input(isset($vars['reject_email'])?1:0).
              ',use_replyto_email='.db_input(isset($vars['use_replyto_email'])?1:0).
              ',disable_autoresponder='.db_input(isset($vars['disable_autoresponder'])?1:0).
+             ',canned_response='.db_input($vars['canned_response']).
              ',notes='.db_input($vars['notes']);
        
 
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 6b933239d..37b39bfda 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2004,10 +2004,28 @@ class Ticket{
             $autorespond=false;
         }
 
+        // If a canned-response is immediately queued for this ticket,
+        // disable the autoresponse
+        if ($vars['canned_response'])
+            $autorespond=false;
+
         /***** See if we need to send some alerts ****/
 
         $ticket->onNewTicket($vars['message'], $autorespond, $alertstaff);
 
+        if ($vars['canned_response']
+                && ($canned = Canned::lookup($vars['canned_response']))) {
+            $files = array();
+            foreach ($canned->getAttachments() as $file)
+                $files[] = $file['id'];
+            $ticket->postReply(array(
+                    'msgId'     => $msgid,
+                    'response'  =>
+                        $ticket->replaceTemplateVars($canned->getResponse()),
+                    'cannedattachments' => $files
+                ), null, $errors, true);
+        }
+
         /************ check if the user JUST reached the max. open tickets limit **********/
         if($cfg->getMaxOpenTickets()>0
                     && ($client=$ticket->getClient())
diff --git a/include/staff/filter.inc.php b/include/staff/filter.inc.php
index 6f8cce2c4..c84e539b6 100644
--- a/include/staff/filter.inc.php
+++ b/include/staff/filter.inc.php
@@ -181,6 +181,27 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                     <strong>Disable</strong> auto-response. <em>(Overwrites Dept. settings)</em>
             </td>
         </tr>
+        <tr>
+            <td width="180">
+                Canned Response:
+            </td>
+                <td>
+                <select name="canned_response">
+                    <option value="">&mdash; None &mdash;</option>
+                    <?php
+                    if($canneds=Canned::responsesByDeptId($thisstaff->getDeptId())) {
+                        foreach ($canneds as $id => $title) {
+                            $selected=($info['canned_response'] &&
+                                    $id==$info['canned_response'])
+                                ? 'selected="selected"' : '';
+                            echo sprintf('<option value="%d" %s>%s</option>',
+                                $id, $selected, $title);
+                        }
+                    }
+                    ?>
+                </select>
+            </td>
+        </tr>
         <tr>
             <td width="180">
                 Department:
diff --git a/main.inc.php b/main.inc.php
index 5184b250e..36f7bf07f 100644
--- a/main.inc.php
+++ b/main.inc.php
@@ -60,7 +60,7 @@
 
     #Current version && schema signature (Changes from version to version)
     define('THIS_VERSION','1.7-DPR4'); //Shown on admin panel
-    define('SCHEMA_SIGNATURE','b19dc97d19f7a30f59663c812d1f3ddc'); //MD5 signature of the db schema. (used to trigger upgrades)
+    define('SCHEMA_SIGNATURE','71e05961fdb7a993a21704ae513512bc'); //MD5 signature of the db schema. (used to trigger upgrades)
 
     #load config info
     $configfile='';
diff --git a/scp/filters.php b/scp/filters.php
index f39b794d0..fbbf923a9 100644
--- a/scp/filters.php
+++ b/scp/filters.php
@@ -15,6 +15,7 @@
 **********************************************************************/
 require('admin.inc.php');
 include_once(INCLUDE_DIR.'class.filter.php');
+require_once(INCLUDE_DIR.'class.canned.php');
 $filter=null;
 if($_REQUEST['id'] && !($filter=Filter::lookup($_REQUEST['id'])))
     $errors['err']='Unknown or invalid filter.';
diff --git a/setup/inc/sql/b19dc97d-71e05961.patch.sql b/setup/inc/sql/b19dc97d-71e05961.patch.sql
new file mode 100644
index 000000000..2dde76625
--- /dev/null
+++ b/setup/inc/sql/b19dc97d-71e05961.patch.sql
@@ -0,0 +1,13 @@
+/**
+ * Support canned response definition for email filters
+ *
+ * @version 1.7-rc1 canned-response-in-filter
+ */
+
+ALTER TABLE `%TABLE_PREFIX%email_filter`
+    ADD `canned_response` int(11) unsigned NOT NULL default '0'
+        AFTER `disable_autoresponder`;
+
+-- Finished with patch
+UPDATE `%TABLE_PREFIX%config`
+    SET `schema_signature`='71e05961fdb7a993a21704ae513512bc';
diff --git a/setup/inc/sql/osticket-v1.7-mysql.sql b/setup/inc/sql/osticket-v1.7-mysql.sql
index 7f29d024f..54b4b5442 100644
--- a/setup/inc/sql/osticket-v1.7-mysql.sql
+++ b/setup/inc/sql/osticket-v1.7-mysql.sql
@@ -237,6 +237,7 @@ CREATE TABLE `%TABLE_PREFIX%email_filter` (
   `reject_email` tinyint(1) unsigned NOT NULL default '0',
   `use_replyto_email` tinyint(1) unsigned NOT NULL default '0',
   `disable_autoresponder` tinyint(1) unsigned NOT NULL default '0',
+  `canned_response` int(11) unsigned NOT NULL default '0',
   `email_id` int(10) unsigned NOT NULL default '0',
   `priority_id` int(10) unsigned NOT NULL default '0',
   `dept_id` int(10) unsigned NOT NULL default '0',
diff --git a/setup/inc/sql/osticket-v1.7-mysql.sql.md5 b/setup/inc/sql/osticket-v1.7-mysql.sql.md5
index a1d99aa52..b9831f788 100644
--- a/setup/inc/sql/osticket-v1.7-mysql.sql.md5
+++ b/setup/inc/sql/osticket-v1.7-mysql.sql.md5
@@ -1 +1 @@
-b19dc97d19f7a30f59663c812d1f3ddc
+71e05961fdb7a993a21704ae513512bc
-- 
GitLab