From a0d4cc69e5f831bb73bb829284476be84619d6e8 Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Tue, 26 Sep 2017 16:48:17 -0500
Subject: [PATCH] feature: Create Task Ticket Thread Action
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This adds a Ticket Thread Action for creating a Task. This action will
popup the New Task modal and will pre-fill the body of the New Task with
the body of the Thread Entry from which the action was made from.

Example:
Thread Entry - “I will need to check on the status of your account.”
Click Create Task from that Thread’s Actions
System will show New Task popup with the body as “I will need to check on
the status of your account.”
---
 include/ajax.tickets.php              |  8 +++++-
 include/class.thread_actions.php      | 39 +++++++++++++++++++++++++++
 include/staff/templates/task.tmpl.php |  2 +-
 scp/ajax.php                          |  1 +
 4 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index aa98426b0..68c47315c 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -1304,7 +1304,7 @@ function refer($tid, $target=null) {
          include STAFFINC_DIR . 'ticket-tasks.inc.php';
     }
 
-    function addTask($tid) {
+    function addTask($tid, $thread_id=NULL) {
         global $thisstaff;
 
         if (!($ticket=Ticket::lookup($tid)))
@@ -1313,6 +1313,9 @@ function refer($tid, $target=null) {
         if (!$ticket->checkStaffPerm($thisstaff, Task::PERM_CREATE))
             Http::response(403, 'Permission denied');
 
+        if ($thread_id && !($thread_entry=ThreadEntry::lookup($thread_id)))
+            Http::response(404, 'Unknown ticket thread');
+
         $info=$errors=array();
 
         if ($_POST) {
@@ -1358,6 +1361,9 @@ function refer($tid, $target=null) {
                 __('Add New Task')
                 );
 
+        if ($thread_entry)
+            $vars['description'] = Format::htmlchars($thread_entry->getBody());
+
          include STAFFINC_DIR . 'templates/task.tmpl.php';
     }
 
diff --git a/include/class.thread_actions.php b/include/class.thread_actions.php
index dffc93631..ed0ae5764 100644
--- a/include/class.thread_actions.php
+++ b/include/class.thread_actions.php
@@ -429,3 +429,42 @@ class TEA_ResendThreadEntry extends TEA_EditAndResendThreadEntry {
     }
 }
 ThreadEntry::registerAction(/* trans */ 'Manage', 'TEA_ResendThreadEntry');
+
+class TEA_CreateTask extends ThreadEntryAction {
+    static $id = 'create_task';
+    static $name = /* trans */ 'Create Task';
+    static $icon = 'plus';
+
+    function isVisible() {
+        global $thisstaff;
+
+        return $thisstaff && $thisstaff->hasPerm(Task::PERM_CREATE, false);
+    }
+
+    function getJsStub() {
+        return sprintf(<<<JS
+var url = '%s';
+var redirect = $(this).data('redirect');
+$.dialog(url, [201], function(xhr, resp) {
+    if (!!redirect)
+        $.pjax({url: redirect, container:'#pjax-container'});
+    else
+        $.pjax({url: 'ajax.php/tickets/378/tasks', container: '#tasks_content', push: false});
+});
+JS
+        , $this->getAjaxUrl()
+        );
+    }
+
+    function trigger() {
+        switch ($_SERVER['REQUEST_METHOD']) {
+        case 'GET':
+            return $this->trigger__get();
+        }
+    }
+
+    private function trigger__get() {
+        include STAFFINC_DIR . 'templates/task.tmpl.php';
+    }
+}
+ThreadEntry::registerAction(/* trans */ 'Manage', 'TEA_CreateTask');
diff --git a/include/staff/templates/task.tmpl.php b/include/staff/templates/task.tmpl.php
index 7bcafc77d..cbf6e0a33 100644
--- a/include/staff/templates/task.tmpl.php
+++ b/include/staff/templates/task.tmpl.php
@@ -25,7 +25,7 @@ if ($info['error']) {
 <form method="post" class="org" action="<?php echo $info['action'] ?: '#tasks/add'; ?>">
     <?php
         $form = $form ?: TaskForm::getInstance();
-        echo $form->getForm()->asTable(' ',
+        echo $form->getForm($vars)->asTable(' ',
                 array('draft-namespace' => $namespace)
                 );
 
diff --git a/scp/ajax.php b/scp/ajax.php
index 039af6b3e..b08f54767 100644
--- a/scp/ajax.php
+++ b/scp/ajax.php
@@ -154,6 +154,7 @@ $dispatcher = patterns('',
         url_get('^(?P<tid>\d+)/canned-resp/(?P<cid>\w+).(?P<format>json|txt)', 'cannedResponse'),
         url_get('^(?P<tid>\d+)/status/(?P<status>\w+)(?:/(?P<sid>\d+))?$', 'changeTicketStatus'),
         url_post('^(?P<tid>\d+)/status$', 'setTicketStatus'),
+        url('^(?P<tid>\d+)/thread/(?P<thread_id>\d+)/create_task$', 'addTask'),
         url('^(?P<tid>\d+)/thread/(?P<thread_id>\d+)/(?P<action>\w+)$', 'triggerThreadAction'),
         url_get('^status/(?P<status>\w+)(?:/(?P<sid>\d+))?$', 'changeSelectedTicketsStatus'),
         url_post('^status/(?P<state>\w+)$', 'setSelectedTicketsStatus'),
-- 
GitLab