From 36abf69637671a1cf65101212596118d155ef54a Mon Sep 17 00:00:00 2001 From: brettp Date: Fri, 12 Mar 2010 02:12:47 +0000 Subject: Rough first version of a new blog plugin. git-svn-id: http://code.elgg.org/elgg/trunk@5370 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/blog/actions/blog/save_draft.php | 146 +++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 mod/blog/actions/blog/save_draft.php (limited to 'mod/blog/actions/blog/save_draft.php') diff --git a/mod/blog/actions/blog/save_draft.php b/mod/blog/actions/blog/save_draft.php new file mode 100644 index 000000000..64a79c667 --- /dev/null +++ b/mod/blog/actions/blog/save_draft.php @@ -0,0 +1,146 @@ +canEdit()) { + $blog = $entity; + } else { + register_error(elgg_echo('blog:error:post_not_found')); + forward(get_input('forward', $_SERVER['HTTP_REFERER'])); + } + $success_forward_url = get_input('forward', $blog->getURL()); +} else { + $blog = new ElggObject(); + $blog->subtype = 'blog'; + $success_forward_url = get_input('forward'); +} + +// set defaults and required values. +$values = array( + 'title' => '', + 'description' => '', + 'status' => 'draft', + //'publish_date' => '', + 'access_id' => ACCESS_DEFAULT, + 'comments_on' => 'On', + 'excerpt' => '', + 'tags' => '', + 'container_guid' => '' +); + +$required = array('title', 'description'); + +foreach ($values as $name => $default) { + $values[$name] = get_input($name, $default); +} + + +// load from POST and do sanity and access checking +foreach ($values as $name => $default) { + $value = get_input($name, $default); + + if (in_array($name, $required) && empty($value)) { + $error = elgg_echo("blog:error:missing:$name"); + } + + if ($error) { + break; + } + + switch ($name) { + case 'tags': + if ($value) { + $values[$name] = string_to_tag_array($value); + } else { + unset ($values[$name]); + } + break; + + case 'excerpt': + // restrict to 300 chars + if ($value) { + $value = substr(strip_tags($value), 0, 300); + } else { + $value = substr(strip_tags($values['description']), 0, 300); + } + $values[$name] = $value; + break; + + case 'container_guid': + // this can't be empty. + if (!empty($value)) { + if (can_write_to_container($user->getGUID(), $value)) { + $values[$name] = $value; + } else { + $error = elgg_echo("blog:error:cannot_write_to_container"); + } + } else { + unset($values[$name]); + } + break; + + // don't try to set the guid + case 'guid': + unset($values['guid']); + break; + + default: + $values[$name] = $value; + break; + } +} + +// assign values to the entity, stopping on error. +if (!$error) { + foreach ($values as $name => $value) { + if (!$blog->$name = $value) { + $error = elgg_echo('blog:error:cannot_save'); + break; + } + } +} + +// only try to save base entity if no errors +if (!$error && !$blog->save()) { + $error = elgg_echo('blog:error:cannot_save'); +} + +// forward with success or failure +if ($ajax) { + if ($error) { + $json = array('success' => FALSE, 'message' => $error); + echo json_encode($json); + } else { + $msg = elgg_echo('blog:message:saved'); + $json = array('success' => TRUE, 'message' => $msg, 'guid' => $blog->getGUID()); + echo json_encode($json); + } +} else { + if ($error) { + register_error($error); + forward($error_forward_url); + } else { + system_message(elgg_echo('blog:message:saved')); + forward($success_forward_url); + } +} \ No newline at end of file -- cgit v1.2.3