diff options
Diffstat (limited to 'mod/blog/actions')
| -rw-r--r-- | mod/blog/actions/add.php | 66 | ||||
| -rw-r--r-- | mod/blog/actions/blog/auto_save_revision.php | 89 | ||||
| -rw-r--r-- | mod/blog/actions/blog/delete.php | 27 | ||||
| -rw-r--r-- | mod/blog/actions/blog/save.php | 182 | ||||
| -rw-r--r-- | mod/blog/actions/comments/add.php | 44 | ||||
| -rw-r--r-- | mod/blog/actions/comments/delete.php | 34 | ||||
| -rw-r--r-- | mod/blog/actions/delete.php | 38 | ||||
| -rw-r--r-- | mod/blog/actions/edit.php | 70 |
8 files changed, 298 insertions, 252 deletions
diff --git a/mod/blog/actions/add.php b/mod/blog/actions/add.php deleted file mode 100644 index f51bbedac..000000000 --- a/mod/blog/actions/add.php +++ /dev/null @@ -1,66 +0,0 @@ -<?php
-
- /**
- * Elgg blog: add post action
- *
- * @package ElggBlog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Ben Werdmuller <ben@curverider.co.uk>
- * @copyright Curverider Ltd 2008
- * @link http://elgg.org/
- */
-
- // Make sure we're logged in (send us to the front page if not)
- if (!isloggedin()) forward();
-
- // Get input data
- $title = get_input('blogtitle');
- $body = get_input('blogbody');
- $tags = get_input('blogtags');
-
- // Cache to the session
- $_SESSION['blogtitle'] = $title;
- $_SESSION['blogbody'] = $body;
- $_SESSION['blogtags'] = $tags;
-
- // Convert string of tags into a preformatted array
- $tagarray = string_to_tag_array($tags);
-
- // Make sure the title / description aren't blank
- if (empty($title) || empty($body)) {
- register_error(elgg_echo("blog:blank"));
- forward("mod/blog/add.php");
-
- // Otherwise, save the blog post
- } else {
-
- // Initialise a new ElggObject
- $blog = new ElggObject();
- // Tell the system it's a blog post
- $blog->subtype = "blog";
- // Set its owner to the current user
- $blog->owner_guid = $_SESSION['user']->getGUID();
- // For now, set its access to public (we'll add an access dropdown shortly)
- $blog->access_id = 2;
- // Set its title and description appropriately
- $blog->title = $title;
- $blog->description = $body;
- // Before we can set metadata, we need to save the blog post
- if (!$blog->save()) {
- register_error(elgg_echo("blog:error"));
- forward("mod/blog/add.php");
- }
- // Now let's add tags. We can pass an array directly to the object property! Easy.
- if (is_array($tagarray)) {
- $blog->tags = $tagarray;
- }
- // Success message
- system_message(elgg_echo("blog:posted"));
- // Remove the blog post cache
- unset($_SESSION['blogtitle']); unset($_SESSION['blogbody']); unset($_SESSION['blogtags']);
- // Forward to the main blog page
- forward("mod/blog/?username=" . $_SESSION['user']->username);
-
- }
-
-?>
\ No newline at end of file diff --git a/mod/blog/actions/blog/auto_save_revision.php b/mod/blog/actions/blog/auto_save_revision.php new file mode 100644 index 000000000..e33edfaab --- /dev/null +++ b/mod/blog/actions/blog/auto_save_revision.php @@ -0,0 +1,89 @@ +<?php +/** + * Action called by AJAX periodic auto saving when editing. + * + * @package Blog + */ + +$guid = get_input('guid'); +$user = elgg_get_logged_in_user_entity(); +$title = htmlspecialchars(get_input('title', '', false), ENT_QUOTES, 'UTF-8'); +$description = get_input('description'); +$excerpt = get_input('excerpt'); + +// because get_input() doesn't use the default if the input is '' +if (empty($excerpt)) { + $excerpt = $description; +} + +// store errors to pass along +$error = FALSE; + +if ($title && $description) { + + if ($guid) { + $entity = get_entity($guid); + if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) { + $blog = $entity; + } else { + $error = elgg_echo('blog:error:post_not_found'); + } + } else { + $blog = new ElggBlog(); + $blog->subtype = 'blog'; + + // force draft and private for autosaves. + $blog->status = 'unsaved_draft'; + $blog->access_id = ACCESS_PRIVATE; + $blog->title = $title; + $blog->description = $description; + $blog->excerpt = elgg_get_excerpt($excerpt); + + // mark this as a brand new post so we can work out the + // river / revision logic in the real save action. + $blog->new_post = TRUE; + + if (!$blog->save()) { + $error = elgg_echo('blog:error:cannot_save'); + } + } + + // creat draft annotation + if (!$error) { + // annotations don't have a "time_updated" so + // we have to delete everything or the times are wrong. + + // don't save if nothing changed + if ($auto_save_annotations = $blog->getAnnotations('blog_auto_save', 1)) { + $auto_save = $auto_save_annotations[0]; + } else { + $auto_save == FALSE; + } + + if (!$auto_save) { + $annotation_id = $blog->annotate('blog_auto_save', $description); + } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value != $description) { + $blog->deleteAnnotations('blog_auto_save'); + $annotation_id = $blog->annotate('blog_auto_save', $description); + } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value == $description) { + // this isn't an error because we have an up to date annotation. + $annotation_id = $auto_save->id; + } + + if (!$annotation_id) { + $error = elgg_echo('blog:error:cannot_auto_save'); + } + } +} else { + $error = elgg_echo('blog:error:missing:description'); +} + +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); +} +exit; diff --git a/mod/blog/actions/blog/delete.php b/mod/blog/actions/blog/delete.php new file mode 100644 index 000000000..ca4eb8a7f --- /dev/null +++ b/mod/blog/actions/blog/delete.php @@ -0,0 +1,27 @@ +<?php +/** + * Delete blog entity + * + * @package Blog + */ + +$blog_guid = get_input('guid'); +$blog = get_entity($blog_guid); + +if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) { + $container = get_entity($blog->container_guid); + if ($blog->delete()) { + system_message(elgg_echo('blog:message:deleted_post')); + if (elgg_instanceof($container, 'group')) { + forward("blog/group/$container->guid/all"); + } else { + forward("blog/owner/$container->username"); + } + } else { + register_error(elgg_echo('blog:error:cannot_delete_post')); + } +} else { + register_error(elgg_echo('blog:error:post_not_found')); +} + +forward(REFERER);
\ No newline at end of file diff --git a/mod/blog/actions/blog/save.php b/mod/blog/actions/blog/save.php new file mode 100644 index 000000000..82a9e6c51 --- /dev/null +++ b/mod/blog/actions/blog/save.php @@ -0,0 +1,182 @@ +<?php +/** + * Save blog entity + * + * Can be called by clicking save button or preview button. If preview button, + * we automatically save as draft. The preview button is only available for + * non-published drafts. + * + * Drafts are saved with the access set to private. + * + * @package Blog + */ + +// start a new sticky form session in case of failure +elgg_make_sticky_form('blog'); + +// save or preview +$save = (bool)get_input('save'); + +// store errors to pass along +$error = FALSE; +$error_forward_url = REFERER; +$user = elgg_get_logged_in_user_entity(); + +// edit or create a new entity +$guid = get_input('guid'); + +if ($guid) { + $entity = get_entity($guid); + if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) { + $blog = $entity; + } else { + register_error(elgg_echo('blog:error:post_not_found')); + forward(get_input('forward', REFERER)); + } + + // save some data for revisions once we save the new edit + $revision_text = $blog->description; + $new_post = $blog->new_post; +} else { + $blog = new ElggBlog(); + $blog->subtype = 'blog'; + $new_post = TRUE; +} + +// set the previous status for the hooks to update the time_created and river entries +$old_status = $blog->status; + +// set defaults and required values. +$values = array( + 'title' => '', + 'description' => '', + 'status' => 'draft', + 'access_id' => ACCESS_DEFAULT, + 'comments_on' => 'On', + 'excerpt' => '', + 'tags' => '', + 'container_guid' => (int)get_input('container_guid'), +); + +// fail if a required entity isn't set +$required = array('title', 'description'); + +// load from POST and do sanity and access checking +foreach ($values as $name => $default) { + if ($name === 'title') { + $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8'); + } else { + $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': + $values[$name] = string_to_tag_array($value); + break; + + case 'excerpt': + if ($value) { + $values[$name] = elgg_get_excerpt($value); + } + break; + + case 'container_guid': + // this can't be empty or saving the base entity fails + 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; + + default: + $values[$name] = $value; + break; + } +} + +// if preview, force status to be draft +if ($save == false) { + $values['status'] = 'draft'; +} + +// if draft, set access to private and cache the future access +if ($values['status'] == 'draft') { + $values['future_access'] = $values['access_id']; + $values['access_id'] = ACCESS_PRIVATE; +} + +// assign values to the entity, stopping on error. +if (!$error) { + foreach ($values as $name => $value) { + $blog->$name = $value; + } +} + +// only try to save base entity if no errors +if (!$error) { + if ($blog->save()) { + // remove sticky form entries + elgg_clear_sticky_form('blog'); + + // remove autosave draft if exists + $blog->deleteAnnotations('blog_auto_save'); + + // no longer a brand new post. + $blog->deleteMetadata('new_post'); + + // if this was an edit, create a revision annotation + if (!$new_post && $revision_text) { + $blog->annotate('blog_revision', $revision_text); + } + + system_message(elgg_echo('blog:message:saved')); + + $status = $blog->status; + + // add to river if changing status or published, regardless of new post + // because we remove it for drafts. + if (($new_post || $old_status == 'draft') && $status == 'published') { + add_to_river('river/object/blog/create', 'create', $blog->owner_guid, $blog->getGUID()); + + // we only want notifications sent when post published + register_notification_object('object', 'blog', elgg_echo('blog:newpost')); + elgg_trigger_event('publish', 'object', $blog); + + // reset the creation time for posts that move from draft to published + if ($guid) { + $blog->time_created = time(); + $blog->save(); + } + } elseif ($old_status == 'published' && $status == 'draft') { + elgg_delete_river(array( + 'object_guid' => $blog->guid, + 'action_type' => 'create', + )); + } + + if ($blog->status == 'published' || $save == false) { + forward($blog->getURL()); + } else { + forward("blog/edit/$blog->guid"); + } + } else { + register_error(elgg_echo('blog:error:cannot_save')); + forward($error_forward_url); + } +} else { + register_error($error); + forward($error_forward_url); +} diff --git a/mod/blog/actions/comments/add.php b/mod/blog/actions/comments/add.php deleted file mode 100644 index 8a54cde13..000000000 --- a/mod/blog/actions/comments/add.php +++ /dev/null @@ -1,44 +0,0 @@ -<?php
-
- /**
- * Elgg blog: add comment action
- *
- * @package ElggBlog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Ben Werdmuller <ben@curverider.co.uk>
- * @copyright Curverider Ltd 2008
- * @link http://elgg.org/
- */
-
- // Make sure we're logged in; forward to the front page if not
- if (!isloggedin()) forward();
-
- // Get input
- $blogpost_guid = (int) get_input('blogpost_guid');
- $comment = get_input('comment');
-
- // Let's see if we can get an entity with the specified GUID, and that it's a blog post
- if ($blogpost = get_entity($blogpost_guid)) {
- if ($blogpost->getSubtype() == "blog") {
-
- // If posting the comment was successful, say so
- if ($blogpost->annotate('comment',$comment,$blogpost->access_id, $_SESSION['guid'])) {
-
- system_message(elgg_echo("comment:success"));
-
- } else {
- system_message(elgg_echo("comment:failure"));
- }
-
- }
-
- } else {
-
- system_message(elgg_echo("blog:notfound"));
-
- }
-
- // Forward to the
- forward("mod/blog/read.php?blogpost=" . $blogpost_guid);
-
-?>
\ No newline at end of file diff --git a/mod/blog/actions/comments/delete.php b/mod/blog/actions/comments/delete.php deleted file mode 100644 index 8eee2c961..000000000 --- a/mod/blog/actions/comments/delete.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php
-
- /**
- * Elgg blog: delete comment action
- *
- * @package ElggBlog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Ben Werdmuller <ben@curverider.co.uk>
- * @copyright Curverider Ltd 2008
- * @link http://elgg.org/
- */
-
- // Ensure we're logged in
- if (!isloggedin()) forward();
-
- // Make sure we can get the comment in question
- $comment_id = (int) get_input('comment_id');
- if ($comment = get_annotation($comment_id)) {
-
- $url = "mod/blog/read.php?blogpost=" . $comment->entity_guid;
- if ($comment->canEdit()) {
- $comment->delete();
- system_message(elgg_echo("comment:deleted"));
- forward($url);
- }
-
- } else {
- $url = "";
- }
-
- system_message(elgg_echo("comment:notdeleted"));
- forward($url);
-
-?>
\ No newline at end of file diff --git a/mod/blog/actions/delete.php b/mod/blog/actions/delete.php deleted file mode 100644 index cb494e59f..000000000 --- a/mod/blog/actions/delete.php +++ /dev/null @@ -1,38 +0,0 @@ -<?php
-
- /**
- * Elgg blog: delete post action
- *
- * @package ElggBlog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Ben Werdmuller <ben@curverider.co.uk>
- * @copyright Curverider Ltd 2008
- * @link http://elgg.org/
- */
-
- // Make sure we're logged in (send us to the front page if not)
- if (!isloggedin()) forward();
-
- // Get input data
- $guid = (int) get_input('blogpost');
-
- // Make sure we actually have permission to edit
- $blog = get_entity($guid);
- if ($blog->getSubtype() == "blog" && $blog->canEdit()) {
-
- // Get owning user
- $owner = get_entity($blog->getOwner());
- // Delete it!
- $rowsaffected = $blog->delete();
- if ($rowsaffected > 0) {
- // Success message
- system_message(elgg_echo("blog:deleted"));
- } else {
- system_message(elgg_echo("blog:notdeleted"));
- }
- // Forward to the main blog page
- forward("mod/blog/?username=" . $owner->username);
-
- }
-
-?>
\ No newline at end of file diff --git a/mod/blog/actions/edit.php b/mod/blog/actions/edit.php deleted file mode 100644 index fba4763a6..000000000 --- a/mod/blog/actions/edit.php +++ /dev/null @@ -1,70 +0,0 @@ -<?php
-
- /**
- * Elgg blog: edit post action
- *
- * @package ElggBlog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Ben Werdmuller <ben@curverider.co.uk>
- * @copyright Curverider Ltd 2008
- * @link http://elgg.org/
- */
-
- // Make sure we're logged in (send us to the front page if not)
- if (!isloggedin()) forward();
-
- // Get input data
- $guid = (int) get_input('blogpost');
- $title = get_input('blogtitle');
- $body = get_input('blogbody');
- $tags = get_input('blogtags');
-
- // Make sure we actually have permission to edit
- $blog = get_entity($guid);
- if ($blog->getSubtype() == "blog" && $blog->canEdit()) {
-
- // Cache to the session
- $_SESSION['blogtitle'] = $title;
- $_SESSION['blogbody'] = $body;
- $_SESSION['blogtags'] = $tags;
-
- // Convert string of tags into a preformatted array
- $tagarray = string_to_tag_array($tags);
-
- // Make sure the title / description aren't blank
- if (empty($title) || empty($body)) {
- register_error(elgg_echo("blog:blank"));
- forward("mod/blog/add.php");
-
- // Otherwise, save the blog post
- } else {
-
- // Get owning user
- $owner = get_entity($blog->getOwner());
- // For now, set its access to public (we'll add an access dropdown shortly)
- $blog->access_id = 2;
- // Set its title and description appropriately
- $blog->title = $title;
- $blog->description = $body;
- // Before we can set metadata, we need to save the blog post
- if (!$blog->save()) {
- register_error(elgg_echo("blog:error"));
- forward("mod/blog/add.php");
- }
- // Now let's add tags. We can pass an array directly to the object property! Easy.
- $blog->clearMetadata('tags');
- if (is_array($tagarray)) {
- $blog->tags = $tagarray;
- }
- // Success message
- system_message(elgg_echo("blog:posted"));
- // Remove the blog post cache
- unset($_SESSION['blogtitle']); unset($_SESSION['blogbody']); unset($_SESSION['blogtags']);
- // Forward to the main blog page
- forward("mod/blog/?username=" . $owner->username);
-
- }
-
- }
-
-?>
\ No newline at end of file |
