From 4137e304ef68098d7c4f9370cdd5860db900a989 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Thu, 25 Sep 2014 11:16:31 -0500 Subject: [PATCH] draft: Fixup lookup of draft objects via AJAX --- include/ajax.draft.php | 33 ++++++++++++++++++++++----------- include/class.draft.php | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/include/ajax.draft.php b/include/ajax.draft.php index 6743b3280..7fdbf6256 100644 --- a/include/ajax.draft.php +++ b/include/ajax.draft.php @@ -21,15 +21,15 @@ class DraftAjaxAPI extends AjaxController { )); } - function _getDraft($id) { - if (!($draft = Draft::lookup($id))) + function _getDraft($draft) { + if (!$draft || !$draft instanceof Draft) Http::response(205, "Draft not found. Create one first"); $body = Format::viewableImages($draft->getBody()); echo JsonDataEncoder::encode(array( 'body' => $body, - 'draft_id' => (int)$id, + 'draft_id' => $draft->getId(), )); } @@ -134,18 +134,25 @@ class DraftAjaxAPI extends AjaxController { global $thisclient; if ($thisclient) { - if (!($id = Draft::findByNamespaceAndStaff($namespace, - $thisclient->getId()))) + try { + $draft = Draft::lookupByNamespaceAndStaff($namespace, + $thisclient->getId()); + } + catch (DoesNotExist $e) { Http::response(205, "Draft not found. Create one first"); + } } else { if (substr($namespace, -12) != substr(session_id(), -12)) Http::response(404, "Draft not found"); - elseif (!($id = Draft::findByNamespaceAndStaff($namespace, 0))) + try { + $draft = Draft::lookupByNamespaceAndStaff($namespace, 0); + } + catch (DoesNotExist $e) { Http::response(205, "Draft not found. Create one first"); + } } - - return self::_getDraft($id); + return self::_getDraft($draft); } function updateDraftClient($id) { @@ -221,11 +228,15 @@ class DraftAjaxAPI extends AjaxController { if (!$thisstaff) Http::response(403, "Login required for draft creation"); - elseif (!($id = Draft::findByNamespaceAndStaff($namespace, - $thisstaff->getId()))) + try { + $draft = Draft::lookupByNamespaceAndStaff($namespace, + $thisstaff->getId()); + } + catch (DoesNotExist $e) { Http::response(205, "Draft not found. Create one first"); + } - return self::_getDraft($id); + return self::_getDraft($draft); } function updateDraft($id) { diff --git a/include/class.draft.php b/include/class.draft.php index 413f08fc9..fae8fd8d7 100644 --- a/include/class.draft.php +++ b/include/class.draft.php @@ -95,7 +95,7 @@ class Draft extends VerySimpleModel { // Change image.php urls back to content-id's $body = Format::sanitize($body, false); - $this->body = $body; + $this->body = $body ?: ' '; $this->updated = SqlFunction::NOW(); return $this->save(); } -- GitLab