From f55bd41f9aeb94830a2583aa472ba35d1a085ce3 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 12 Aug 2014 15:27:40 -0500 Subject: [PATCH] Fix initial popup of [Draft Saved] Instead, only show the box if the draft has been modified since initially created / updated. --- include/ajax.draft.php | 41 ++++++++++++++++++++++++++++++++--------- include/class.draft.php | 10 ++++------ js/redactor-osticket.js | 5 +++-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/include/ajax.draft.php b/include/ajax.draft.php index ab68700d4..00ad23394 100644 --- a/include/ajax.draft.php +++ b/include/ajax.draft.php @@ -44,16 +44,32 @@ class DraftAjaxAPI extends AjaxController { } function _updateDraft($draft) { - $field_list = array('response', 'note', 'answer', 'body', - 'message', 'issue'); - foreach ($field_list as $field) { - if (isset($_POST[$field])) { - $body = urldecode($_POST[$field]); - break; + if (isset($_POST['name'])) { + $parts = array(); + if (preg_match('`(\w+)\[(\w+)\]`', $_POST['name'], $parts)) { + $body = urldecode($_POST[$parts[1]][$parts[2]]); + } + else { + $body = urldecode($_POST[$_POST['name']]); + } + } + else { + $field_list = array('response', 'note', 'answer', 'body', + 'message', 'issue'); + foreach ($field_list as $field) { + if (isset($_POST[$field])) { + $body = urldecode($_POST[$field]); + break; + } } } if (!isset($body)) - return Http::response(422, "Draft body not found in request"); + return JsonDataEncoder::encode(array( + 'error' => array( + 'message' => "Draft body not found in request", + 'code' => 422, + ) + )); if (!$draft->setBody($body)) return Http::response(500, "Unable to update draft body"); @@ -227,8 +243,15 @@ class DraftAjaxAPI extends AjaxController { 'namespace' => $namespace, ); - if (isset($_POST['name'])) - $vars['body'] = urldecode($_POST[$_POST['name']]); + if (isset($_POST['name'])) { + $parts = array(); + if (preg_match('`(\w+)\[(\w+)\]`', $_POST['name'], $parts)) { + $vars['body'] = urldecode($_POST[$parts[1]][$parts[2]]); + } + else { + $vars['body'] = urldecode($_POST[$_POST['name']]); + } + } return self::_createDraft($vars); } diff --git a/include/class.draft.php b/include/class.draft.php index d71b89e57..413f08fc9 100644 --- a/include/class.draft.php +++ b/include/class.draft.php @@ -145,8 +145,7 @@ class Draft extends VerySimpleModel { * closing a ticket, the staff_id should be left null so that all drafts * are cleaned up. */ - /* static */ - function deleteForNamespace($namespace, $staff_id=false) { + static function deleteForNamespace($namespace, $staff_id=false) { $sql = 'DELETE attach FROM '.ATTACHMENT_TABLE.' attach INNER JOIN '.DRAFT_TABLE.' draft ON (attach.object_id = draft.id AND attach.`type`=\'D\') @@ -156,11 +155,10 @@ class Draft extends VerySimpleModel { if (!db_query($sql)) return false; - $sql = 'DELETE FROM '.DRAFT_TABLE - .' WHERE `namespace` LIKE '.db_input($namespace); + $criteria = array('namespace__like'=>$namespace); if ($staff_id) - $sql .= ' AND staff_id='.db_input($staff_id); - return (!db_query($sql) || !db_affected_rows()); + $criteria['staff_id'] = $staff_id; + return static::objects()->filter($criteria)->delete(); } static function cleanup() { diff --git a/js/redactor-osticket.js b/js/redactor-osticket.js index 33b27ef38..ba85e301f 100644 --- a/js/redactor-osticket.js +++ b/js/redactor-osticket.js @@ -81,7 +81,8 @@ RedactorPlugins.draft = { } // Only show the [Draft Saved] notice if there is content in the // field that has been touched - if (!this.opts.draftOriginal || this.opts.draftOriginal == this.get()) { + if (!this.firstSave) { + this.firstSave = true; // No change yet — dont't show the button return; } @@ -120,7 +121,7 @@ RedactorPlugins.draft = { type: 'delete', async: false, success: function() { - self.draft_id = this.opts.draftId = undefined; + self.draft_id = self.opts.draftId = undefined; self.hideDraftSaved(); self.set(self.opts.draftOriginal || '', false, false); self.opts.autosave = self.opts.autoCreateUrl; -- GitLab