diff --git a/include/ajax.draft.php b/include/ajax.draft.php index 6743b328030774e5053cd137a7a91850301f91e1..7fdbf6256e511a5b1775293c34d56f0dcbea59fb 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 413f08fc9c490b34804c042a7b310c7f92d49d6e..fae8fd8d72389d6c37f2dc2892e564a99f246dec 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(); }