Skip to content
Snippets Groups Projects
Commit f55bd41f authored by Jared Hancock's avatar Jared Hancock
Browse files

Fix initial popup of [Draft Saved]

Instead, only show the box if the draft has been modified since initially
created / updated.
parent eb3b9ba7
Branches
Tags
No related merge requests found
...@@ -44,16 +44,32 @@ class DraftAjaxAPI extends AjaxController { ...@@ -44,16 +44,32 @@ class DraftAjaxAPI extends AjaxController {
} }
function _updateDraft($draft) { function _updateDraft($draft) {
$field_list = array('response', 'note', 'answer', 'body', if (isset($_POST['name'])) {
'message', 'issue'); $parts = array();
foreach ($field_list as $field) { if (preg_match('`(\w+)\[(\w+)\]`', $_POST['name'], $parts)) {
if (isset($_POST[$field])) { $body = urldecode($_POST[$parts[1]][$parts[2]]);
$body = urldecode($_POST[$field]); }
break; 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)) 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)) if (!$draft->setBody($body))
return Http::response(500, "Unable to update draft body"); return Http::response(500, "Unable to update draft body");
...@@ -227,8 +243,15 @@ class DraftAjaxAPI extends AjaxController { ...@@ -227,8 +243,15 @@ class DraftAjaxAPI extends AjaxController {
'namespace' => $namespace, 'namespace' => $namespace,
); );
if (isset($_POST['name'])) if (isset($_POST['name'])) {
$vars['body'] = urldecode($_POST[$_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); return self::_createDraft($vars);
} }
......
...@@ -145,8 +145,7 @@ class Draft extends VerySimpleModel { ...@@ -145,8 +145,7 @@ class Draft extends VerySimpleModel {
* closing a ticket, the staff_id should be left null so that all drafts * closing a ticket, the staff_id should be left null so that all drafts
* are cleaned up. * are cleaned up.
*/ */
/* static */ static function deleteForNamespace($namespace, $staff_id=false) {
function deleteForNamespace($namespace, $staff_id=false) {
$sql = 'DELETE attach FROM '.ATTACHMENT_TABLE.' attach $sql = 'DELETE attach FROM '.ATTACHMENT_TABLE.' attach
INNER JOIN '.DRAFT_TABLE.' draft INNER JOIN '.DRAFT_TABLE.' draft
ON (attach.object_id = draft.id AND attach.`type`=\'D\') ON (attach.object_id = draft.id AND attach.`type`=\'D\')
...@@ -156,11 +155,10 @@ class Draft extends VerySimpleModel { ...@@ -156,11 +155,10 @@ class Draft extends VerySimpleModel {
if (!db_query($sql)) if (!db_query($sql))
return false; return false;
$sql = 'DELETE FROM '.DRAFT_TABLE $criteria = array('namespace__like'=>$namespace);
.' WHERE `namespace` LIKE '.db_input($namespace);
if ($staff_id) if ($staff_id)
$sql .= ' AND staff_id='.db_input($staff_id); $criteria['staff_id'] = $staff_id;
return (!db_query($sql) || !db_affected_rows()); return static::objects()->filter($criteria)->delete();
} }
static function cleanup() { static function cleanup() {
......
...@@ -81,7 +81,8 @@ RedactorPlugins.draft = { ...@@ -81,7 +81,8 @@ RedactorPlugins.draft = {
} }
// Only show the [Draft Saved] notice if there is content in the // Only show the [Draft Saved] notice if there is content in the
// field that has been touched // 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 // No change yet — dont't show the button
return; return;
} }
...@@ -120,7 +121,7 @@ RedactorPlugins.draft = { ...@@ -120,7 +121,7 @@ RedactorPlugins.draft = {
type: 'delete', type: 'delete',
async: false, async: false,
success: function() { success: function() {
self.draft_id = this.opts.draftId = undefined; self.draft_id = self.opts.draftId = undefined;
self.hideDraftSaved(); self.hideDraftSaved();
self.set(self.opts.draftOriginal || '', false, false); self.set(self.opts.draftOriginal || '', false, false);
self.opts.autosave = self.opts.autoCreateUrl; self.opts.autosave = self.opts.autoCreateUrl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment