diff --git a/ajax.php b/ajax.php index 8ea5226439f971b9cd917a13f53c099579394ed1..80e3a7838693ca355859285019aa861043b0b057 100644 --- a/ajax.php +++ b/ajax.php @@ -33,6 +33,7 @@ $dispatcher = patterns('', url_post('^(?P<id>\d+)$', 'updateDraftClient'), url_delete('^(?P<id>\d+)$', 'deleteDraftClient'), url_post('^(?P<id>\d+)/attach$', 'uploadInlineImageClient'), + url_post('^(?P<namespace>[\w.]+)/attach$', 'uploadInlineImageEarlyClient'), url_get('^(?P<namespace>[\w.]+)$', 'getDraftClient'), url_post('^(?P<namespace>[\w.]+)$', 'createDraftClient') )), diff --git a/include/ajax.draft.php b/include/ajax.draft.php index 7fdbf6256e511a5b1775293c34d56f0dcbea59fb..5fc24e77a9c5e08d3e8607a6613c4e1a7fdaec6b 100644 --- a/include/ajax.draft.php +++ b/include/ajax.draft.php @@ -111,6 +111,8 @@ class DraftAjaxAPI extends AjaxController { echo JsonDataEncoder::encode(array( 'content_id' => 'cid:'.$f->getKey(), + // Return draft_id to connect the auto draft creation + 'draft_id' => $draft->getId(), 'filelink' => sprintf('image.php?h=%s', $f->getDownloadHash()) )); } @@ -123,7 +125,7 @@ class DraftAjaxAPI extends AjaxController { Http::response(403, "Valid session required"); $vars = array( - 'staff_id' => ($thisclient) ? $thisclient->getId() : 0, + 'staff_id' => ($thisclient) ? $thisclient->getId() : 1<<31, 'namespace' => $namespace, ); @@ -208,6 +210,22 @@ class DraftAjaxAPI extends AjaxController { return self::_uploadInlineImage($draft); } + function uploadInlineImageEarlyClient($namespace) { + global $thisclient; + + if (!$thisclient && substr($namespace, -12) != substr(session_id(), -12)) + Http::response(403, "Valid session required"); + + $draft = Draft::create(array( + 'staff_id' => ($thisclient) ? $thisclient->getId() : 1<<31, + 'namespace' => $namespace, + )); + if (!$draft->save()) + Http::response(500, 'Unable to create draft'); + + return $this->uploadInlineImageClient($draft->getId()); + } + // Staff interface for drafts ======================================== function createDraft($namespace) { global $thisstaff; @@ -265,6 +283,22 @@ class DraftAjaxAPI extends AjaxController { return self::_uploadInlineImage($draft); } + function uploadInlineImageEarly($namespace) { + global $thisstaff; + + if (!$thisstaff) + Http::response(403, "Login required for image upload"); + + $draft = Draft::create(array( + 'staff_id' => $thisstaff->getId(), + 'namespace' => $namepace + )); + if (!$draft->save()) + Http::response(500, 'Unable to create draft'); + + return $this->uploadInlineImage($draft->getId()); + } + function deleteDraft($id) { global $thisstaff; diff --git a/js/redactor-osticket.js b/js/redactor-osticket.js index 07f9eaaafb71d6459f4c9b50c26809f37b96b56f..6e50a61e399c697ab7fe16e7dabf1dddf7ab4ff5 100644 --- a/js/redactor-osticket.js +++ b/js/redactor-osticket.js @@ -22,7 +22,7 @@ RedactorPlugins.draft = { if (this.opts.draftObjectId) autosave_url += '.' + this.opts.draftObjectId; this.opts.autosave = this.opts.autoCreateUrl = autosave_url; - this.opts.autosaveInterval = 10; + this.opts.autosaveInterval = 30; this.opts.autosaveCallback = this.afterUpdateDraft; this.opts.autosaveErrorCallback = this.autosaveFailed; this.opts.imageUploadErrorCallback = this.displayError; @@ -33,9 +33,11 @@ RedactorPlugins.draft = { 'ajax.php/draft/'+this.opts.draftId+'/attach'; } else { - // Autosave quickly to and setup image upload in the - // afterUpdateDraft callback - this.opts.autosaveInterval = 1; + // Just upload the file. A draft will be created automatically + // and will be configured locally in the afterUpateDraft() + this.opts.clipboardUploadUrl = + this.opts.imageUpload = this.opts.autoCreateUrl + '/attach'; + this.opts.imageUploadCallback = this.afterUpdateDraft; } this.$draft_saved = $('<span>') @@ -67,17 +69,14 @@ RedactorPlugins.draft = { // If the draft was created, a draft_id will be sent back — update // the URL to send updates in the future - if (data.draft_id) { + if (!this.opts.draftId && data.draft_id) { this.opts.draftId = data.draft_id; this.opts.autosave = 'ajax.php/draft/' + data.draft_id; this.opts.clipboardUploadUrl = this.opts.imageUpload = 'ajax.php/draft/'+this.opts.draftId+'/attach'; - if (this.opts.autosaveInterval < 10) { - clearInterval(this.autosaveInterval); - this.opts.autosaveInterval = 10; - this.autosave(); - } + if (!this.get()) + this.set(' ', false); } // Only show the [Draft Saved] notice if there is content in the // field that has been touched diff --git a/scp/ajax.php b/scp/ajax.php index 1168513601eb45882783c339934006856b36c550..09dce15dd85103fadcc55bb0b2b25c0a7cc08aac 100644 --- a/scp/ajax.php +++ b/scp/ajax.php @@ -155,6 +155,7 @@ $dispatcher = patterns('', url_post('^(?P<id>\d+)$', 'updateDraft'), url_delete('^(?P<id>\d+)$', 'deleteDraft'), url_post('^(?P<id>\d+)/attach$', 'uploadInlineImage'), + url_post('^(?P<namespace>[\w.]+)/attach$', 'uploadInlineImageEarly'), url_get('^(?P<namespace>[\w.]+)$', 'getDraft'), url_post('^(?P<namespace>[\w.]+)$', 'createDraft'), url_get('^images/browse$', 'getFileList')