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

draft: Fixup lookup of draft objects via AJAX

parent 392d6429
Branches
Tags
No related merge requests found
...@@ -21,15 +21,15 @@ class DraftAjaxAPI extends AjaxController { ...@@ -21,15 +21,15 @@ class DraftAjaxAPI extends AjaxController {
)); ));
} }
function _getDraft($id) { function _getDraft($draft) {
if (!($draft = Draft::lookup($id))) if (!$draft || !$draft instanceof Draft)
Http::response(205, "Draft not found. Create one first"); Http::response(205, "Draft not found. Create one first");
$body = Format::viewableImages($draft->getBody()); $body = Format::viewableImages($draft->getBody());
echo JsonDataEncoder::encode(array( echo JsonDataEncoder::encode(array(
'body' => $body, 'body' => $body,
'draft_id' => (int)$id, 'draft_id' => $draft->getId(),
)); ));
} }
...@@ -134,18 +134,25 @@ class DraftAjaxAPI extends AjaxController { ...@@ -134,18 +134,25 @@ class DraftAjaxAPI extends AjaxController {
global $thisclient; global $thisclient;
if ($thisclient) { if ($thisclient) {
if (!($id = Draft::findByNamespaceAndStaff($namespace, try {
$thisclient->getId()))) $draft = Draft::lookupByNamespaceAndStaff($namespace,
$thisclient->getId());
}
catch (DoesNotExist $e) {
Http::response(205, "Draft not found. Create one first"); Http::response(205, "Draft not found. Create one first");
}
} }
else { else {
if (substr($namespace, -12) != substr(session_id(), -12)) if (substr($namespace, -12) != substr(session_id(), -12))
Http::response(404, "Draft not found"); 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"); Http::response(205, "Draft not found. Create one first");
}
} }
return self::_getDraft($draft);
return self::_getDraft($id);
} }
function updateDraftClient($id) { function updateDraftClient($id) {
...@@ -221,11 +228,15 @@ class DraftAjaxAPI extends AjaxController { ...@@ -221,11 +228,15 @@ class DraftAjaxAPI extends AjaxController {
if (!$thisstaff) if (!$thisstaff)
Http::response(403, "Login required for draft creation"); Http::response(403, "Login required for draft creation");
elseif (!($id = Draft::findByNamespaceAndStaff($namespace, try {
$thisstaff->getId()))) $draft = Draft::lookupByNamespaceAndStaff($namespace,
$thisstaff->getId());
}
catch (DoesNotExist $e) {
Http::response(205, "Draft not found. Create one first"); Http::response(205, "Draft not found. Create one first");
}
return self::_getDraft($id); return self::_getDraft($draft);
} }
function updateDraft($id) { function updateDraft($id) {
......
...@@ -95,7 +95,7 @@ class Draft extends VerySimpleModel { ...@@ -95,7 +95,7 @@ class Draft extends VerySimpleModel {
// Change image.php urls back to content-id's // Change image.php urls back to content-id's
$body = Format::sanitize($body, false); $body = Format::sanitize($body, false);
$this->body = $body; $this->body = $body ?: ' ';
$this->updated = SqlFunction::NOW(); $this->updated = SqlFunction::NOW();
return $this->save(); return $this->save();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment