diff --git a/include/ajax.draft.php b/include/ajax.draft.php index ab68700d4c4be6d8f481c444f3cadc795103023e..00ad2339466eef8ebc876e3f555ce782933f6ca5 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 d71b89e57a22d5c1695adc7346788797e7e67056..413f08fc9c490b34804c042a7b310c7f92d49d6e 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 33b27ef38e9896d5e5aa8606b58e4e343e6348fc..ba85e301f8482f12ca7abe3add593857b1d27e4d 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;