From 0f0ccf8a19eba8b177fa7534b6b73db1f06a023a Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sat, 18 Apr 2015 17:57:13 -0500
Subject: [PATCH] Implement typeahead context for canned responses

---
 include/ajax.content.php             | 19 +++++++++++++------
 include/class.staff.php              |  5 +++++
 include/staff/cannedresponse.inc.php |  6 +++---
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/include/ajax.content.php b/include/ajax.content.php
index 873747357..536b4d48f 100644
--- a/include/ajax.content.php
+++ b/include/ajax.content.php
@@ -208,12 +208,18 @@ class ContentAjaxAPI extends AjaxController {
         if (!$_GET['root'])
             Http::response(400, '`root` is required parameter');
 
-        // Get the template for this template
-        $tpl_info = EmailTemplateGroup::getTemplateDescription($_GET['root']);
-        if (!$tpl_info)
-            Http::response(422, 'No such context');
+        switch ($_GET['root']) {
+        case 'cannedresponse':
+            $roots = array('ticket');
+            break;
 
-        $global = osTicket::getVarScope();
+        default:
+            // Get the template for this template
+            $tpl_info = EmailTemplateGroup::getTemplateDescription($_GET['root']);
+            if (!$tpl_info)
+                Http::response(422, 'No such context');
+            $roots = $tpl_info['context'];
+        }
 
         $contextTypes = array(
             'assignee' => array('class' => 'Staff', 'desc' => 'Newly assigned agent'),
@@ -229,9 +235,10 @@ class ContentAjaxAPI extends AjaxController {
             'ticket' => array('class' => 'Ticket', 'desc' => 'The ticket'),
         );
         $context = array();
-        foreach ($tpl_info['context'] as $C) {
+        foreach ($roots as $C) {
             $context[$C] = $contextTypes[$C];
         }
+        $global = osTicket::getVarScope();
         $items = VariableReplacer::compileScope($context + $global);
 
         header('Content-Type: application/json');
diff --git a/include/class.staff.php b/include/class.staff.php
index c2765915d..47bda2352 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -80,10 +80,15 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
 
     static function getVarScope() {
       return array(
+        'dept' => array('class' => 'Dept', 'desc' => 'Department'),
+        'email' => 'Email address',
         'name' => array(
           'class' => 'PersonsName', 'desc' => 'Name of the agent',
         ),
+        'phone' => 'Phone number',
         'signature' => "Agent's signature",
+        'timezone' => "Agent's configured timezone",
+        'username' => 'Access username',
       );
     }
 
diff --git a/include/staff/cannedresponse.inc.php b/include/staff/cannedresponse.inc.php
index 407c5ba89..8e407d253 100644
--- a/include/staff/cannedresponse.inc.php
+++ b/include/staff/cannedresponse.inc.php
@@ -80,13 +80,13 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                     <font class="error">*&nbsp;<?php echo $errors['response']; ?></font>
                     &nbsp;&nbsp;&nbsp;(<a class="tip" href="#ticket_variables"><?php echo __('Supported Variables'); ?></a>)
                     </div>
-                <textarea name="response" class="richtext draft draft-delete" cols="21" rows="12"
-                    style="width:98%;" class="richtext draft" <?php
+                <textarea name="response" cols="21" rows="12"
+                    data-root-context="cannedresponse"
+                    style="width:98%;" class="richtext draft draft-delete" <?php
     list($draft, $attrs) = Draft::getDraftAndDataAttrs('canned',
         is_object($canned) ? $canned->getId() : false, $info['response']);
     echo $attrs; ?>><?php echo $draft ?: $info['response'];
                 ?></textarea>
-                <br><br>
                 <div><h3><?php echo __('Canned Attachments'); ?> <?php echo __('(optional)'); ?>
                 &nbsp;<i class="help-tip icon-question-sign" href="#canned_attachments"></i></h3>
                 <div class="error"><?php echo $errors['files']; ?></div>
-- 
GitLab