aboutsummaryrefslogtreecommitdiff
path: root/mod/blog
diff options
context:
space:
mode:
Diffstat (limited to 'mod/blog')
-rw-r--r--mod/blog/actions/blog/auto_save_revision.php89
-rw-r--r--mod/blog/actions/blog/delete.php27
-rw-r--r--mod/blog/actions/blog/save.php182
-rw-r--r--mod/blog/activate.php10
-rw-r--r--mod/blog/classes/ElggBlog.php42
-rw-r--r--mod/blog/deactivate.php6
-rw-r--r--mod/blog/languages/en.php78
-rw-r--r--mod/blog/lib/blog.php478
-rw-r--r--mod/blog/manifest.xml19
-rw-r--r--mod/blog/start.php302
-rw-r--r--mod/blog/views/default/blog/css.php14
-rw-r--r--mod/blog/views/default/blog/group_module.php46
-rw-r--r--mod/blog/views/default/blog/sidebar.php30
-rw-r--r--mod/blog/views/default/blog/sidebar/archives.php37
-rw-r--r--mod/blog/views/default/blog/sidebar/revisions.php79
-rw-r--r--mod/blog/views/default/forms/blog/save.php166
-rw-r--r--mod/blog/views/default/js/blog/save_draft.php67
-rw-r--r--mod/blog/views/default/object/blog.php99
-rw-r--r--mod/blog/views/default/river/object/blog/create.php15
-rw-r--r--mod/blog/views/default/widgets/blog/content.php30
-rw-r--r--mod/blog/views/default/widgets/blog/edit.php22
21 files changed, 1838 insertions, 0 deletions
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/activate.php b/mod/blog/activate.php
new file mode 100644
index 000000000..a90525291
--- /dev/null
+++ b/mod/blog/activate.php
@@ -0,0 +1,10 @@
+<?php
+/**
+ * Register the ElggBlog class for the object/blog subtype
+ */
+
+if (get_subtype_id('object', 'blog')) {
+ update_subtype('object', 'blog', 'ElggBlog');
+} else {
+ add_subtype('object', 'blog', 'ElggBlog');
+}
diff --git a/mod/blog/classes/ElggBlog.php b/mod/blog/classes/ElggBlog.php
new file mode 100644
index 000000000..8d4401c57
--- /dev/null
+++ b/mod/blog/classes/ElggBlog.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Extended class to override the time_created
+ *
+ * @property string $status The published status of the blog post (published, draft)
+ * @property string $comments_on Whether commenting is allowed (Off, On)
+ * @property string $excerpt An excerpt of the blog post used when displaying the post
+ */
+class ElggBlog extends ElggObject {
+
+ /**
+ * Set subtype to blog.
+ */
+ protected function initializeAttributes() {
+ parent::initializeAttributes();
+
+ $this->attributes['subtype'] = "blog";
+ }
+
+ /**
+ * Can a user comment on this blog?
+ *
+ * @see ElggObject::canComment()
+ *
+ * @param int $user_guid User guid (default is logged in user)
+ * @return bool
+ * @since 1.8.0
+ */
+ public function canComment($user_guid = 0) {
+ $result = parent::canComment($user_guid);
+ if ($result == false) {
+ return $result;
+ }
+
+ if ($this->comments_on == 'Off') {
+ return false;
+ }
+
+ return true;
+ }
+
+} \ No newline at end of file
diff --git a/mod/blog/deactivate.php b/mod/blog/deactivate.php
new file mode 100644
index 000000000..4a275fa94
--- /dev/null
+++ b/mod/blog/deactivate.php
@@ -0,0 +1,6 @@
+<?php
+/**
+ * Deregister the ElggBlog class
+ */
+
+update_subtype('object', 'blog');
diff --git a/mod/blog/languages/en.php b/mod/blog/languages/en.php
new file mode 100644
index 000000000..5248a6f51
--- /dev/null
+++ b/mod/blog/languages/en.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * Blog English language file.
+ *
+ */
+
+$english = array(
+ 'blog' => 'Blogs',
+ 'blog:blogs' => 'Blogs',
+ 'blog:revisions' => 'Revisions',
+ 'blog:archives' => 'Archives',
+ 'blog:blog' => 'Blog',
+ 'item:object:blog' => 'Blogs',
+
+ 'blog:title:user_blogs' => '%s\'s blogs',
+ 'blog:title:all_blogs' => 'All site blogs',
+ 'blog:title:friends' => 'Friends\' blogs',
+
+ 'blog:group' => 'Group blog',
+ 'blog:enableblog' => 'Enable group blog',
+ 'blog:write' => 'Write a blog post',
+
+ // Editing
+ 'blog:add' => 'Add blog post',
+ 'blog:edit' => 'Edit blog post',
+ 'blog:excerpt' => 'Excerpt',
+ 'blog:body' => 'Body',
+ 'blog:save_status' => 'Last saved: ',
+ 'blog:never' => 'Never',
+
+ // Statuses
+ 'blog:status' => 'Status',
+ 'blog:status:draft' => 'Draft',
+ 'blog:status:published' => 'Published',
+ 'blog:status:unsaved_draft' => 'Unsaved Draft',
+
+ 'blog:revision' => 'Revision',
+ 'blog:auto_saved_revision' => 'Auto Saved Revision',
+
+ // messages
+ 'blog:message:saved' => 'Blog post saved.',
+ 'blog:error:cannot_save' => 'Cannot save blog post.',
+ 'blog:error:cannot_write_to_container' => 'Insufficient access to save blog to group.',
+ 'blog:messages:warning:draft' => 'There is an unsaved draft of this post!',
+ 'blog:edit_revision_notice' => '(Old version)',
+ 'blog:message:deleted_post' => 'Blog post deleted.',
+ 'blog:error:cannot_delete_post' => 'Cannot delete blog post.',
+ 'blog:none' => 'No blog posts',
+ 'blog:error:missing:title' => 'Please enter a blog title!',
+ 'blog:error:missing:description' => 'Please enter the body of your blog!',
+ 'blog:error:cannot_edit_post' => 'This post may not exist or you may not have permissions to edit it.',
+ 'blog:error:revision_not_found' => 'Cannot find this revision.',
+
+ // river
+ 'river:create:object:blog' => '%s published a blog post %s',
+ 'river:comment:object:blog' => '%s commented on the blog %s',
+
+ // notifications
+ 'blog:newpost' => 'A new blog post',
+ 'blog:notification' =>
+'
+%s made a new blog post.
+
+%s
+%s
+
+View and comment on the new blog post:
+%s
+',
+
+ // widget
+ 'blog:widget:description' => 'Display your latest blog posts',
+ 'blog:moreblogs' => 'More blog posts',
+ 'blog:numbertodisplay' => 'Number of blog posts to display',
+ 'blog:noblogs' => 'No blog posts'
+);
+
+add_translation('en', $english);
diff --git a/mod/blog/lib/blog.php b/mod/blog/lib/blog.php
new file mode 100644
index 000000000..9753f27a8
--- /dev/null
+++ b/mod/blog/lib/blog.php
@@ -0,0 +1,478 @@
+<?php
+/**
+ * Blog helper functions
+ *
+ * @package Blog
+ */
+
+
+/**
+ * Get page components to view a blog post.
+ *
+ * @param int $guid GUID of a blog entity.
+ * @return array
+ */
+function blog_get_page_content_read($guid = NULL) {
+
+ $return = array();
+
+ $blog = get_entity($guid);
+
+ // no header or tabs for viewing an individual blog
+ $return['filter'] = '';
+
+ if (!elgg_instanceof($blog, 'object', 'blog')) {
+ register_error(elgg_echo('noaccess'));
+ $_SESSION['last_forward_from'] = current_page_url();
+ forward('');
+ }
+
+ $return['title'] = $blog->title;
+
+ $container = $blog->getContainerEntity();
+ $crumbs_title = $container->name;
+ if (elgg_instanceof($container, 'group')) {
+ elgg_push_breadcrumb($crumbs_title, "blog/group/$container->guid/all");
+ } else {
+ elgg_push_breadcrumb($crumbs_title, "blog/owner/$container->username");
+ }
+
+ elgg_push_breadcrumb($blog->title);
+ $return['content'] = elgg_view_entity($blog, array('full_view' => true));
+ // check to see if we should allow comments
+ if ($blog->comments_on != 'Off' && $blog->status == 'published') {
+ $return['content'] .= elgg_view_comments($blog);
+ }
+
+ return $return;
+}
+
+/**
+ * Get page components to list a user's or all blogs.
+ *
+ * @param int $container_guid The GUID of the page owner or NULL for all blogs
+ * @return array
+ */
+function blog_get_page_content_list($container_guid = NULL) {
+
+ $return = array();
+
+ $return['filter_context'] = $container_guid ? 'mine' : 'all';
+
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => 'blog',
+ 'full_view' => false,
+ );
+
+ $current_user = elgg_get_logged_in_user_entity();
+
+ if ($container_guid) {
+ // access check for closed groups
+ group_gatekeeper();
+
+ $options['container_guid'] = $container_guid;
+ $container = get_entity($container_guid);
+ if (!$container) {
+
+ }
+ $return['title'] = elgg_echo('blog:title:user_blogs', array($container->name));
+
+ $crumbs_title = $container->name;
+ elgg_push_breadcrumb($crumbs_title);
+
+ if ($current_user && ($container_guid == $current_user->guid)) {
+ $return['filter_context'] = 'mine';
+ } else if (elgg_instanceof($container, 'group')) {
+ $return['filter'] = false;
+ } else {
+ // do not show button or select a tab when viewing someone else's posts
+ $return['filter_context'] = 'none';
+ }
+ } else {
+ $return['filter_context'] = 'all';
+ $return['title'] = elgg_echo('blog:title:all_blogs');
+ elgg_pop_breadcrumb();
+ elgg_push_breadcrumb(elgg_echo('blog:blogs'));
+ }
+
+ elgg_register_title_button();
+
+ // show all posts for admin or users looking at their own blogs
+ // show only published posts for other users.
+ $show_only_published = true;
+ if ($current_user) {
+ if (($current_user->guid == $container_guid) || $current_user->isAdmin()) {
+ $show_only_published = false;
+ }
+ }
+ if ($show_only_published) {
+ $options['metadata_name_value_pairs'] = array(
+ array('name' => 'status', 'value' => 'published'),
+ );
+ }
+
+ $list = elgg_list_entities_from_metadata($options);
+ if (!$list) {
+ $return['content'] = elgg_echo('blog:none');
+ } else {
+ $return['content'] = $list;
+ }
+
+ return $return;
+}
+
+/**
+ * Get page components to list of the user's friends' posts.
+ *
+ * @param int $user_guid
+ * @return array
+ */
+function blog_get_page_content_friends($user_guid) {
+
+ $user = get_user($user_guid);
+ if (!$user) {
+ forward('blog/all');
+ }
+
+ $return = array();
+
+ $return['filter_context'] = 'friends';
+ $return['title'] = elgg_echo('blog:title:friends');
+
+ $crumbs_title = $user->name;
+ elgg_push_breadcrumb($crumbs_title, "blog/owner/{$user->username}");
+ elgg_push_breadcrumb(elgg_echo('friends'));
+
+ elgg_register_title_button();
+
+ if (!$friends = get_user_friends($user_guid, ELGG_ENTITIES_ANY_VALUE, 0)) {
+ $return['content'] .= elgg_echo('friends:none:you');
+ return $return;
+ } else {
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => 'blog',
+ 'full_view' => FALSE,
+ );
+
+ foreach ($friends as $friend) {
+ $options['container_guids'][] = $friend->getGUID();
+ }
+
+ // admin / owners can see any posts
+ // everyone else can only see published posts
+ $show_only_published = true;
+ $current_user = elgg_get_logged_in_user_entity();
+ if ($current_user) {
+ if (($user_guid == $current_user->guid) || $current_user->isAdmin()) {
+ $show_only_published = false;
+ }
+ }
+ if ($show_only_published) {
+ $options['metadata_name_value_pairs'][] = array(
+ array('name' => 'status', 'value' => 'published')
+ );
+ }
+
+ $list = elgg_list_entities_from_metadata($options);
+ if (!$list) {
+ $return['content'] = elgg_echo('blog:none');
+ } else {
+ $return['content'] = $list;
+ }
+ }
+
+ return $return;
+}
+
+/**
+ * Get page components to show blogs with publish dates between $lower and $upper
+ *
+ * @param int $owner_guid The GUID of the owner of this page
+ * @param int $lower Unix timestamp
+ * @param int $upper Unix timestamp
+ * @return array
+ */
+function blog_get_page_content_archive($owner_guid, $lower = 0, $upper = 0) {
+
+ $now = time();
+
+ $owner = get_entity($owner_guid);
+ elgg_set_page_owner_guid($owner_guid);
+
+ $crumbs_title = $owner->name;
+ if (elgg_instanceof($owner, 'user')) {
+ $url = "blog/owner/{$owner->username}";
+ } else {
+ $url = "blog/group/$owner->guid/all";
+ }
+ elgg_push_breadcrumb($crumbs_title, $url);
+ elgg_push_breadcrumb(elgg_echo('blog:archives'));
+
+ if ($lower) {
+ $lower = (int)$lower;
+ }
+
+ if ($upper) {
+ $upper = (int)$upper;
+ }
+
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => 'blog',
+ 'full_view' => FALSE,
+ );
+
+ if ($owner_guid) {
+ $options['container_guid'] = $owner_guid;
+ }
+
+ // admin / owners can see any posts
+ // everyone else can only see published posts
+ if (!(elgg_is_admin_logged_in() || (elgg_is_logged_in() && $owner_guid == elgg_get_logged_in_user_guid()))) {
+ if ($upper > $now) {
+ $upper = $now;
+ }
+
+ $options['metadata_name_value_pairs'] = array(
+ array('name' => 'status', 'value' => 'published')
+ );
+ }
+
+ if ($lower) {
+ $options['created_time_lower'] = $lower;
+ }
+
+ if ($upper) {
+ $options['created_time_upper'] = $upper;
+ }
+
+ $list = elgg_list_entities_from_metadata($options);
+ if (!$list) {
+ $content = elgg_echo('blog:none');
+ } else {
+ $content = $list;
+ }
+
+ $title = elgg_echo('date:month:' . date('m', $lower), array(date('Y', $lower)));
+
+ return array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+}
+
+/**
+ * Get page components to edit/create a blog post.
+ *
+ * @param string $page 'edit' or 'new'
+ * @param int $guid GUID of blog post or container
+ * @param int $revision Annotation id for revision to edit (optional)
+ * @return array
+ */
+function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) {
+
+ elgg_load_js('elgg.blog');
+
+ $return = array(
+ 'filter' => '',
+ );
+
+ $vars = array();
+ $vars['id'] = 'blog-post-edit';
+ $vars['class'] = 'elgg-form-alt';
+
+ $sidebar = '';
+ if ($page == 'edit') {
+ $blog = get_entity((int)$guid);
+
+ $title = elgg_echo('blog:edit');
+
+ if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
+ $vars['entity'] = $blog;
+
+ $title .= ": \"$blog->title\"";
+
+ if ($revision) {
+ $revision = elgg_get_annotation_from_id((int)$revision);
+ $vars['revision'] = $revision;
+ $title .= ' ' . elgg_echo('blog:edit_revision_notice');
+
+ if (!$revision || !($revision->entity_guid == $guid)) {
+ $content = elgg_echo('blog:error:revision_not_found');
+ $return['content'] = $content;
+ $return['title'] = $title;
+ return $return;
+ }
+ }
+
+ $body_vars = blog_prepare_form_vars($blog, $revision);
+
+ elgg_push_breadcrumb($blog->title, $blog->getURL());
+ elgg_push_breadcrumb(elgg_echo('edit'));
+
+ elgg_load_js('elgg.blog');
+
+ $content = elgg_view_form('blog/save', $vars, $body_vars);
+ $sidebar = elgg_view('blog/sidebar/revisions', $vars);
+ } else {
+ $content = elgg_echo('blog:error:cannot_edit_post');
+ }
+ } else {
+ elgg_push_breadcrumb(elgg_echo('blog:add'));
+ $body_vars = blog_prepare_form_vars(null);
+
+ $title = elgg_echo('blog:add');
+ $content = elgg_view_form('blog/save', $vars, $body_vars);
+ }
+
+ $return['title'] = $title;
+ $return['content'] = $content;
+ $return['sidebar'] = $sidebar;
+ return $return;
+}
+
+/**
+ * Pull together blog variables for the save form
+ *
+ * @param ElggBlog $post
+ * @param ElggAnnotation $revision
+ * @return array
+ */
+function blog_prepare_form_vars($post = NULL, $revision = NULL) {
+
+ // input names => defaults
+ $values = array(
+ 'title' => NULL,
+ 'description' => NULL,
+ 'status' => 'published',
+ 'access_id' => ACCESS_DEFAULT,
+ 'comments_on' => 'On',
+ 'excerpt' => NULL,
+ 'tags' => NULL,
+ 'container_guid' => NULL,
+ 'guid' => NULL,
+ 'draft_warning' => '',
+ );
+
+ if ($post) {
+ foreach (array_keys($values) as $field) {
+ if (isset($post->$field)) {
+ $values[$field] = $post->$field;
+ }
+ }
+
+ if ($post->status == 'draft') {
+ $values['access_id'] = $post->future_access;
+ }
+ }
+
+ if (elgg_is_sticky_form('blog')) {
+ $sticky_values = elgg_get_sticky_values('blog');
+ foreach ($sticky_values as $key => $value) {
+ $values[$key] = $value;
+ }
+ }
+
+ elgg_clear_sticky_form('blog');
+
+ if (!$post) {
+ return $values;
+ }
+
+ // load the revision annotation if requested
+ if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) {
+ $values['revision'] = $revision;
+ $values['description'] = $revision->value;
+ }
+
+ // display a notice if there's an autosaved annotation
+ // and we're not editing it.
+ if ($auto_save_annotations = $post->getAnnotations('blog_auto_save', 1)) {
+ $auto_save = $auto_save_annotations[0];
+ } else {
+ $auto_save = false;
+ }
+
+ if ($auto_save && $auto_save->id != $revision->id) {
+ $values['draft_warning'] = elgg_echo('blog:messages:warning:draft');
+ }
+
+ return $values;
+}
+
+/**
+ * Forward to the new style of URLs
+ *
+ * Pre-1.7.5
+ * Group blogs page: /blog/group:<container_guid>/
+ * Group blog view: /blog/group:<container_guid>/read/<guid>/<title>
+ * 1.7.5-1.8
+ * Group blogs page: /blog/owner/group:<container_guid>/
+ * Group blog view: /blog/read/<guid>
+ *
+ *
+ * @param string $page
+ */
+function blog_url_forwarder($page) {
+
+ $viewtype = elgg_get_viewtype();
+ $qs = ($viewtype === 'default') ? "" : "?view=$viewtype";
+
+ $url = "blog/all";
+
+ // easier to work with & no notices
+ $page = array_pad($page, 4, "");
+
+ // group usernames
+ if (preg_match('~/group\:([0-9]+)/~', "/{$page[0]}/{$page[1]}/", $matches)) {
+ $guid = $matches[1];
+ $entity = get_entity($guid);
+ if (elgg_instanceof($entity, 'group')) {
+ if (!empty($page[2])) {
+ $url = "blog/view/$page[2]/";
+ } else {
+ $url = "blog/group/$guid/all";
+ }
+ register_error(elgg_echo("changebookmark"));
+ forward($url . $qs);
+ }
+ }
+
+ if (empty($page[0])) {
+ return;
+ }
+
+ // user usernames
+ $user = get_user_by_username($page[0]);
+ if (!$user) {
+ return;
+ }
+
+ if (empty($page[1])) {
+ $page[1] = 'owner';
+ }
+
+ switch ($page[1]) {
+ case "read":
+ $url = "blog/view/{$page[2]}/{$page[3]}";
+ break;
+ case "archive":
+ $url = "blog/archive/{$page[0]}/{$page[2]}/{$page[3]}";
+ break;
+ case "friends":
+ $url = "blog/friends/{$page[0]}";
+ break;
+ case "new":
+ $url = "blog/add/$user->guid";
+ break;
+ case "owner":
+ $url = "blog/owner/{$page[0]}";
+ break;
+ }
+
+ register_error(elgg_echo("changebookmark"));
+ forward($url . $qs);
+}
diff --git a/mod/blog/manifest.xml b/mod/blog/manifest.xml
new file mode 100644
index 000000000..29ee1bfc8
--- /dev/null
+++ b/mod/blog/manifest.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8">
+ <name>Blog</name>
+ <author>Core developers</author>
+ <version>1.8</version>
+ <category>bundled</category>
+ <category>content</category>
+ <category>widget</category>
+ <blurb>Blog plugin</blurb>
+ <description>Adds simple blogging capabilities to your Elgg installation.</description>
+ <website>http://elgg.org/</website>
+ <copyright>See COPYRIGHT.txt</copyright>
+ <license>GNU General Public License version 2</license>
+ <requires>
+ <type>elgg_release</type>
+ <version>1.8</version>
+ </requires>
+ <activate_on_install>true</activate_on_install>
+</plugin_manifest>
diff --git a/mod/blog/start.php b/mod/blog/start.php
new file mode 100644
index 000000000..e724b91c2
--- /dev/null
+++ b/mod/blog/start.php
@@ -0,0 +1,302 @@
+<?php
+/**
+ * Blogs
+ *
+ * @package Blog
+ *
+ * @todo
+ * - Either drop support for "publish date" or duplicate more entity getter
+ * functions to work with a non-standard time_created.
+ * - Pingbacks
+ * - Notifications
+ * - River entry for posts saved as drafts and later published
+ */
+
+elgg_register_event_handler('init', 'system', 'blog_init');
+
+/**
+ * Init blog plugin.
+ */
+function blog_init() {
+
+ elgg_register_library('elgg:blog', elgg_get_plugins_path() . 'blog/lib/blog.php');
+
+ // add a site navigation item
+ $item = new ElggMenuItem('blog', elgg_echo('blog:blogs'), 'blog/all');
+ elgg_register_menu_item('site', $item);
+
+ elgg_register_event_handler('upgrade', 'upgrade', 'blog_run_upgrades');
+
+ // add to the main css
+ elgg_extend_view('css/elgg', 'blog/css');
+
+ // register the blog's JavaScript
+ $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
+ elgg_register_simplecache_view('js/blog/save_draft');
+ elgg_register_js('elgg.blog', $blog_js);
+
+ // routing of urls
+ elgg_register_page_handler('blog', 'blog_page_handler');
+
+ // override the default url to view a blog object
+ elgg_register_entity_url_handler('object', 'blog', 'blog_url_handler');
+
+ // notifications - need to register for unique event because of draft/published status
+ elgg_register_event_handler('publish', 'object', 'object_notifications');
+ elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'blog_notify_message');
+
+ // add blog link to
+ elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'blog_owner_block_menu');
+
+ // pingbacks
+ //elgg_register_event_handler('create', 'object', 'blog_incoming_ping');
+ //elgg_register_plugin_hook_handler('pingback:object:subtypes', 'object', 'blog_pingback_subtypes');
+
+ // Register for search.
+ elgg_register_entity_type('object', 'blog');
+
+ // Add group option
+ add_group_tool_option('blog', elgg_echo('blog:enableblog'), true);
+ elgg_extend_view('groups/tool_latest', 'blog/group_module');
+
+ // add a blog widget
+ elgg_register_widget_type('blog', elgg_echo('blog'), elgg_echo('blog:widget:description'));
+
+ // register actions
+ $action_path = elgg_get_plugins_path() . 'blog/actions/blog';
+ elgg_register_action('blog/save', "$action_path/save.php");
+ elgg_register_action('blog/auto_save_revision', "$action_path/auto_save_revision.php");
+ elgg_register_action('blog/delete', "$action_path/delete.php");
+
+ // entity menu
+ elgg_register_plugin_hook_handler('register', 'menu:entity', 'blog_entity_menu_setup');
+
+ // ecml
+ elgg_register_plugin_hook_handler('get_views', 'ecml', 'blog_ecml_views_hook');
+}
+
+/**
+ * Dispatches blog pages.
+ * URLs take the form of
+ * All blogs: blog/all
+ * User's blogs: blog/owner/<username>
+ * Friends' blog: blog/friends/<username>
+ * User's archives: blog/archives/<username>/<time_start>/<time_stop>
+ * Blog post: blog/view/<guid>/<title>
+ * New post: blog/add/<guid>
+ * Edit post: blog/edit/<guid>/<revision>
+ * Preview post: blog/preview/<guid>
+ * Group blog: blog/group/<guid>/all
+ *
+ * Title is ignored
+ *
+ * @todo no archives for all blogs or friends
+ *
+ * @param array $page
+ * @return bool
+ */
+function blog_page_handler($page) {
+
+ elgg_load_library('elgg:blog');
+
+ // forward to correct URL for blog pages pre-1.8
+ blog_url_forwarder($page);
+
+ // push all blogs breadcrumb
+ elgg_push_breadcrumb(elgg_echo('blog:blogs'), "blog/all");
+
+ if (!isset($page[0])) {
+ $page[0] = 'all';
+ }
+
+ $page_type = $page[0];
+ switch ($page_type) {
+ case 'owner':
+ $user = get_user_by_username($page[1]);
+ if (!$user) {
+ forward('', '404');
+ }
+ $params = blog_get_page_content_list($user->guid);
+ break;
+ case 'friends':
+ $user = get_user_by_username($page[1]);
+ if (!$user) {
+ forward('', '404');
+ }
+ $params = blog_get_page_content_friends($user->guid);
+ break;
+ case 'archive':
+ $user = get_user_by_username($page[1]);
+ if (!$user) {
+ forward('', '404');
+ }
+ $params = blog_get_page_content_archive($user->guid, $page[2], $page[3]);
+ break;
+ case 'view':
+ $params = blog_get_page_content_read($page[1]);
+ break;
+ case 'read': // Elgg 1.7 compatibility
+ register_error(elgg_echo("changebookmark"));
+ forward("blog/view/{$page[1]}");
+ break;
+ case 'add':
+ gatekeeper();
+ $params = blog_get_page_content_edit($page_type, $page[1]);
+ break;
+ case 'edit':
+ gatekeeper();
+ $params = blog_get_page_content_edit($page_type, $page[1], $page[2]);
+ break;
+ case 'group':
+ $group = get_entity($page[1]);
+ if (!elgg_instanceof($group, 'group')) {
+ forward('', '404');
+ }
+ if (!isset($page[2]) || $page[2] == 'all') {
+ $params = blog_get_page_content_list($page[1]);
+ } else {
+ $params = blog_get_page_content_archive($page[1], $page[3], $page[4]);
+ }
+ break;
+ case 'all':
+ $params = blog_get_page_content_list();
+ break;
+ default:
+ return false;
+ }
+
+ if (isset($params['sidebar'])) {
+ $params['sidebar'] .= elgg_view('blog/sidebar', array('page' => $page_type));
+ } else {
+ $params['sidebar'] = elgg_view('blog/sidebar', array('page' => $page_type));
+ }
+
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($params['title'], $body);
+ return true;
+}
+
+/**
+ * Format and return the URL for blogs.
+ *
+ * @param ElggObject $entity Blog object
+ * @return string URL of blog.
+ */
+function blog_url_handler($entity) {
+ if (!$entity->getOwnerEntity()) {
+ // default to a standard view if no owner.
+ return FALSE;
+ }
+
+ $friendly_title = elgg_get_friendly_title($entity->title);
+
+ return "blog/view/{$entity->guid}/$friendly_title";
+}
+
+/**
+ * Add a menu item to an ownerblock
+ */
+function blog_owner_block_menu($hook, $type, $return, $params) {
+ if (elgg_instanceof($params['entity'], 'user')) {
+ $url = "blog/owner/{$params['entity']->username}";
+ $item = new ElggMenuItem('blog', elgg_echo('blog'), $url);
+ $return[] = $item;
+ } else {
+ if ($params['entity']->blog_enable != "no") {
+ $url = "blog/group/{$params['entity']->guid}/all";
+ $item = new ElggMenuItem('blog', elgg_echo('blog:group'), $url);
+ $return[] = $item;
+ }
+ }
+
+ return $return;
+}
+
+/**
+ * Add particular blog links/info to entity menu
+ */
+function blog_entity_menu_setup($hook, $type, $return, $params) {
+ if (elgg_in_context('widgets')) {
+ return $return;
+ }
+
+ $entity = $params['entity'];
+ $handler = elgg_extract('handler', $params, false);
+ if ($handler != 'blog') {
+ return $return;
+ }
+
+ if ($entity->status != 'published') {
+ // draft status replaces access
+ foreach ($return as $index => $item) {
+ if ($item->getName() == 'access') {
+ unset($return[$index]);
+ }
+ }
+
+ $status_text = elgg_echo("blog:status:{$entity->status}");
+ $options = array(
+ 'name' => 'published_status',
+ 'text' => "<span>$status_text</span>",
+ 'href' => false,
+ 'priority' => 150,
+ );
+ $return[] = ElggMenuItem::factory($options);
+ }
+
+ return $return;
+}
+
+/**
+ * Set the notification message body
+ *
+ * @param string $hook Hook name
+ * @param string $type Hook type
+ * @param string $message The current message body
+ * @param array $params Parameters about the blog posted
+ * @return string
+ */
+function blog_notify_message($hook, $type, $message, $params) {
+ $entity = $params['entity'];
+ $to_entity = $params['to_entity'];
+ $method = $params['method'];
+ if (elgg_instanceof($entity, 'object', 'blog')) {
+ $descr = $entity->excerpt;
+ $title = $entity->title;
+ $owner = $entity->getOwnerEntity();
+ return elgg_echo('blog:notification', array(
+ $owner->name,
+ $title,
+ $descr,
+ $entity->getURL()
+ ));
+ }
+ return null;
+}
+
+/**
+ * Register blogs with ECML.
+ */
+function blog_ecml_views_hook($hook, $entity_type, $return_value, $params) {
+ $return_value['object/blog'] = elgg_echo('blog:blogs');
+
+ return $return_value;
+}
+
+/**
+ * Upgrade from 1.7 to 1.8.
+ */
+function blog_run_upgrades($event, $type, $details) {
+ $blog_upgrade_version = elgg_get_plugin_setting('upgrade_version', 'blogs');
+
+ if (!$blog_upgrade_version) {
+ // When upgrading, check if the ElggBlog class has been registered as this
+ // was added in Elgg 1.8
+ if (!update_subtype('object', 'blog', 'ElggBlog')) {
+ add_subtype('object', 'blog', 'ElggBlog');
+ }
+
+ elgg_set_plugin_setting('upgrade_version', 1, 'blogs');
+ }
+}
diff --git a/mod/blog/views/default/blog/css.php b/mod/blog/views/default/blog/css.php
new file mode 100644
index 000000000..12ac4df2a
--- /dev/null
+++ b/mod/blog/views/default/blog/css.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Blog CSS
+ *
+ * @package Blog
+*/
+?>
+
+/* Blog Plugin */
+
+/* force tinymce input height for a more useful editing / blog creation area */
+form#blog-post-edit #description_parent #description_ifr {
+ height:400px !important;
+}
diff --git a/mod/blog/views/default/blog/group_module.php b/mod/blog/views/default/blog/group_module.php
new file mode 100644
index 000000000..6082cdafd
--- /dev/null
+++ b/mod/blog/views/default/blog/group_module.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Group blog module
+ */
+
+$group = elgg_get_page_owner_entity();
+
+if ($group->blog_enable == "no") {
+ return true;
+}
+
+$all_link = elgg_view('output/url', array(
+ 'href' => "blog/group/$group->guid/all",
+ 'text' => elgg_echo('link:view:all'),
+ 'is_trusted' => true,
+));
+
+elgg_push_context('widgets');
+$options = array(
+ 'type' => 'object',
+ 'subtype' => 'blog',
+ 'container_guid' => elgg_get_page_owner_guid(),
+ 'metadata_name_value_pairs' => array('name' => 'status', 'value' => 'published'),
+ 'limit' => 6,
+ 'full_view' => false,
+ 'pagination' => false,
+);
+$content = elgg_list_entities_from_metadata($options);
+elgg_pop_context();
+
+if (!$content) {
+ $content = '<p>' . elgg_echo('blog:none') . '</p>';
+}
+
+$new_link = elgg_view('output/url', array(
+ 'href' => "blog/add/$group->guid",
+ 'text' => elgg_echo('blog:write'),
+ 'is_trusted' => true,
+));
+
+echo elgg_view('groups/profile/module', array(
+ 'title' => elgg_echo('blog:group'),
+ 'content' => $content,
+ 'all_link' => $all_link,
+ 'add_link' => $new_link,
+));
diff --git a/mod/blog/views/default/blog/sidebar.php b/mod/blog/views/default/blog/sidebar.php
new file mode 100644
index 000000000..0ae2b431c
--- /dev/null
+++ b/mod/blog/views/default/blog/sidebar.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Blog sidebar
+ *
+ * @package Blog
+ */
+
+// fetch & display latest comments
+if ($vars['page'] == 'all') {
+ echo elgg_view('page/elements/comments_block', array(
+ 'subtypes' => 'blog',
+ ));
+} elseif ($vars['page'] == 'owner') {
+ echo elgg_view('page/elements/comments_block', array(
+ 'subtypes' => 'blog',
+ 'owner_guid' => elgg_get_page_owner_guid(),
+ ));
+}
+
+// only users can have archives at present
+if ($vars['page'] == 'owner' || $vars['page'] == 'group') {
+ echo elgg_view('blog/sidebar/archives', $vars);
+}
+
+if ($vars['page'] != 'friends') {
+ echo elgg_view('page/elements/tagcloud_block', array(
+ 'subtypes' => 'blog',
+ 'owner_guid' => elgg_get_page_owner_guid(),
+ ));
+}
diff --git a/mod/blog/views/default/blog/sidebar/archives.php b/mod/blog/views/default/blog/sidebar/archives.php
new file mode 100644
index 000000000..6529887d4
--- /dev/null
+++ b/mod/blog/views/default/blog/sidebar/archives.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * Blog archives
+ */
+
+$page_owner = elgg_get_page_owner_entity();
+
+if (!$page_owner) {
+ return true;
+}
+
+if (elgg_instanceof($page_owner, 'user')) {
+ $url_segment = 'blog/archive/' . $page_owner->username;
+} else {
+ $url_segment = 'blog/group/' . $page_owner->getGUID() . '/archive';
+}
+
+// This is a limitation of the URL schema.
+if ($page_owner && $vars['page'] != 'friends') {
+ $dates = array_reverse(get_entity_dates('object', 'blog', $page_owner->getGUID()));
+
+ if ($dates) {
+ $title = elgg_echo('blog:archives');
+ $content = '<ul class="blog-archives">';
+ foreach ($dates as $date) {
+ $timestamplow = mktime(0, 0, 0, substr($date,4,2) , 1, substr($date, 0, 4));
+ $timestamphigh = mktime(0, 0, 0, ((int) substr($date, 4, 2)) + 1, 1, substr($date, 0, 4));
+
+ $link = elgg_get_site_url() . $url_segment . '/' . $timestamplow . '/' . $timestamphigh;
+ $month = elgg_echo('date:month:' . substr($date, 4, 2), array(substr($date, 0, 4)));
+ $content .= "<li><a href=\"$link\" title=\"$month\">$month</a></li>";
+ }
+ $content .= '</ul>';
+
+ echo elgg_view_module('aside', $title, $content);
+ }
+}
diff --git a/mod/blog/views/default/blog/sidebar/revisions.php b/mod/blog/views/default/blog/sidebar/revisions.php
new file mode 100644
index 000000000..cd2e7f3d8
--- /dev/null
+++ b/mod/blog/views/default/blog/sidebar/revisions.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * Blog sidebar menu showing revisions
+ *
+ * @package Blog
+ */
+
+//If editing a post, show the previous revisions and drafts.
+$blog = elgg_extract('entity', $vars, FALSE);
+
+if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
+ $owner = $blog->getOwnerEntity();
+ $revisions = array();
+
+ $auto_save_annotations = $blog->getAnnotations('blog_auto_save', 1);
+ if ($auto_save_annotations) {
+ $revisions[] = $auto_save_annotations[0];
+ }
+
+ // count(FALSE) == 1! AHHH!!!
+ $saved_revisions = $blog->getAnnotations('blog_revision', 10, 0, 'time_created DESC');
+ if ($saved_revisions) {
+ $revision_count = count($saved_revisions);
+ } else {
+ $revision_count = 0;
+ }
+
+ $revisions = array_merge($revisions, $saved_revisions);
+
+ if ($revisions) {
+ $title = elgg_echo('blog:revisions');
+
+ $n = count($revisions);
+ $body = '<ul class="blog-revisions">';
+
+ $load_base_url = "blog/edit/{$blog->getGUID()}";
+
+ // show the "published revision"
+ if ($blog->status == 'published') {
+ $load = elgg_view('output/url', array(
+ 'href' => $load_base_url,
+ 'text' => elgg_echo('blog:status:published'),
+ 'is_trusted' => true,
+ ));
+
+ $time = "<span class='elgg-subtext'>"
+ . elgg_view_friendly_time($blog->time_created) . "</span>";
+
+ $body .= "<li>$load : $time</li>";
+ }
+
+ foreach ($revisions as $revision) {
+ $time = "<span class='elgg-subtext'>"
+ . elgg_view_friendly_time($revision->time_created) . "</span>";
+
+ if ($revision->name == 'blog_auto_save') {
+ $revision_lang = elgg_echo('blog:auto_saved_revision');
+ } else {
+ $revision_lang = elgg_echo('blog:revision') . " $n";
+ }
+ $load = elgg_view('output/url', array(
+ 'href' => "$load_base_url/$revision->id",
+ 'text' => $revision_lang,
+ 'is_trusted' => true,
+ ));
+
+ $text = "$load: $time";
+ $class = 'class="auto-saved"';
+
+ $n--;
+
+ $body .= "<li $class>$text</li>";
+ }
+
+ $body .= '</ul>';
+
+ echo elgg_view_module('aside', $title, $body);
+ }
+} \ No newline at end of file
diff --git a/mod/blog/views/default/forms/blog/save.php b/mod/blog/views/default/forms/blog/save.php
new file mode 100644
index 000000000..f825acca1
--- /dev/null
+++ b/mod/blog/views/default/forms/blog/save.php
@@ -0,0 +1,166 @@
+<?php
+/**
+ * Edit blog form
+ *
+ * @package Blog
+ */
+
+$blog = get_entity($vars['guid']);
+$vars['entity'] = $blog;
+
+$draft_warning = $vars['draft_warning'];
+if ($draft_warning) {
+ $draft_warning = '<span class="mbm elgg-text-help">' . $draft_warning . '</span>';
+}
+
+$action_buttons = '';
+$delete_link = '';
+$preview_button = '';
+
+if ($vars['guid']) {
+ // add a delete button if editing
+ $delete_url = "action/blog/delete?guid={$vars['guid']}";
+ $delete_link = elgg_view('output/confirmlink', array(
+ 'href' => $delete_url,
+ 'text' => elgg_echo('delete'),
+ 'class' => 'elgg-button elgg-button-delete float-alt'
+ ));
+}
+
+// published blogs do not get the preview button
+if (!$vars['guid'] || ($blog && $blog->status != 'published')) {
+ $preview_button = elgg_view('input/submit', array(
+ 'value' => elgg_echo('preview'),
+ 'name' => 'preview',
+ 'class' => 'mls',
+ ));
+}
+
+$save_button = elgg_view('input/submit', array(
+ 'value' => elgg_echo('save'),
+ 'name' => 'save',
+));
+$action_buttons = $save_button . $preview_button . $delete_link;
+
+$title_label = elgg_echo('title');
+$title_input = elgg_view('input/text', array(
+ 'name' => 'title',
+ 'id' => 'blog_title',
+ 'value' => $vars['title']
+));
+
+$excerpt_label = elgg_echo('blog:excerpt');
+$excerpt_input = elgg_view('input/text', array(
+ 'name' => 'excerpt',
+ 'id' => 'blog_excerpt',
+ 'value' => _elgg_html_decode($vars['excerpt'])
+));
+
+$body_label = elgg_echo('blog:body');
+$body_input = elgg_view('input/longtext', array(
+ 'name' => 'description',
+ 'id' => 'blog_description',
+ 'value' => $vars['description']
+));
+
+$save_status = elgg_echo('blog:save_status');
+if ($vars['guid']) {
+ $entity = get_entity($vars['guid']);
+ $saved = date('F j, Y @ H:i', $entity->time_created);
+} else {
+ $saved = elgg_echo('blog:never');
+}
+
+$status_label = elgg_echo('blog:status');
+$status_input = elgg_view('input/dropdown', array(
+ 'name' => 'status',
+ 'id' => 'blog_status',
+ 'value' => $vars['status'],
+ 'options_values' => array(
+ 'draft' => elgg_echo('blog:status:draft'),
+ 'published' => elgg_echo('blog:status:published')
+ )
+));
+
+$comments_label = elgg_echo('comments');
+$comments_input = elgg_view('input/dropdown', array(
+ 'name' => 'comments_on',
+ 'id' => 'blog_comments_on',
+ 'value' => $vars['comments_on'],
+ 'options_values' => array('On' => elgg_echo('on'), 'Off' => elgg_echo('off'))
+));
+
+$tags_label = elgg_echo('tags');
+$tags_input = elgg_view('input/tags', array(
+ 'name' => 'tags',
+ 'id' => 'blog_tags',
+ 'value' => $vars['tags']
+));
+
+$access_label = elgg_echo('access');
+$access_input = elgg_view('input/access', array(
+ 'name' => 'access_id',
+ 'id' => 'blog_access_id',
+ 'value' => $vars['access_id']
+));
+
+$categories_input = elgg_view('input/categories', $vars);
+
+// hidden inputs
+$container_guid_input = elgg_view('input/hidden', array('name' => 'container_guid', 'value' => elgg_get_page_owner_guid()));
+$guid_input = elgg_view('input/hidden', array('name' => 'guid', 'value' => $vars['guid']));
+
+
+echo <<<___HTML
+
+$draft_warning
+
+<div>
+ <label for="blog_title">$title_label</label>
+ $title_input
+</div>
+
+<div>
+ <label for="blog_excerpt">$excerpt_label</label>
+ $excerpt_input
+</div>
+
+<div>
+ <label for="blog_description">$body_label</label>
+ $body_input
+</div>
+
+<div>
+ <label for="blog_tags">$tags_label</label>
+ $tags_input
+</div>
+
+$categories_input
+
+<div>
+ <label for="blog_comments_on">$comments_label</label>
+ $comments_input
+</div>
+
+<div>
+ <label for="blog_access_id">$access_label</label>
+ $access_input
+</div>
+
+<div>
+ <label for="blog_status">$status_label</label>
+ $status_input
+</div>
+
+<div class="elgg-foot">
+ <div class="elgg-subtext mbm">
+ $save_status <span class="blog-save-status-time">$saved</span>
+ </div>
+
+ $guid_input
+ $container_guid_input
+
+ $action_buttons
+</div>
+
+___HTML;
diff --git a/mod/blog/views/default/js/blog/save_draft.php b/mod/blog/views/default/js/blog/save_draft.php
new file mode 100644
index 000000000..8cd07ff5d
--- /dev/null
+++ b/mod/blog/views/default/js/blog/save_draft.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Save draft through ajax
+ *
+ * @package Blog
+ */
+?>
+elgg.provide('elgg.blog');
+
+/*
+ * Attempt to save and update the input with the guid.
+ */
+elgg.blog.saveDraftCallback = function(data, textStatus, XHR) {
+ if (textStatus == 'success' && data.success == true) {
+ var form = $('form[id=blog-post-edit]');
+
+ // update the guid input element for new posts that now have a guid
+ form.find('input[name=guid]').val(data.guid);
+
+ oldDescription = form.find('textarea[name=description]').val();
+
+ var d = new Date();
+ var mins = d.getMinutes() + '';
+ if (mins.length == 1) {
+ mins = '0' + mins;
+ }
+ $(".blog-save-status-time").html(d.toLocaleDateString() + " @ " + d.getHours() + ":" + mins);
+ } else {
+ $(".blog-save-status-time").html(elgg.echo('error'));
+ }
+};
+
+elgg.blog.saveDraft = function() {
+ if (typeof(tinyMCE) != 'undefined') {
+ tinyMCE.triggerSave();
+ }
+
+ // only save on changed content
+ var form = $('form[id=blog-post-edit]');
+ var description = form.find('textarea[name=description]').val();
+ var title = form.find('input[name=title]').val();
+
+ if (!(description && title) || (description == oldDescription)) {
+ return false;
+ }
+
+ var draftURL = elgg.config.wwwroot + "action/blog/auto_save_revision";
+ var postData = form.serializeArray();
+
+ // force draft status
+ $(postData).each(function(i, e) {
+ if (e.name == 'status') {
+ e.value = 'draft';
+ }
+ });
+
+ $.post(draftURL, postData, elgg.blog.saveDraftCallback, 'json');
+};
+
+elgg.blog.init = function() {
+ // get a copy of the body to compare for auto save
+ oldDescription = $('form[id=blog-post-edit]').find('textarea[name=description]').val();
+
+ setInterval(elgg.blog.saveDraft, 60000);
+};
+
+elgg.register_hook_handler('init', 'system', elgg.blog.init); \ No newline at end of file
diff --git a/mod/blog/views/default/object/blog.php b/mod/blog/views/default/object/blog.php
new file mode 100644
index 000000000..4403a6006
--- /dev/null
+++ b/mod/blog/views/default/object/blog.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * View for blog objects
+ *
+ * @package Blog
+ */
+
+$full = elgg_extract('full_view', $vars, FALSE);
+$blog = elgg_extract('entity', $vars, FALSE);
+
+if (!$blog) {
+ return TRUE;
+}
+
+$owner = $blog->getOwnerEntity();
+$container = $blog->getContainerEntity();
+$categories = elgg_view('output/categories', $vars);
+$excerpt = $blog->excerpt;
+if (!$excerpt) {
+ $excerpt = elgg_get_excerpt($blog->description);
+}
+
+$owner_icon = elgg_view_entity_icon($owner, 'tiny');
+$owner_link = elgg_view('output/url', array(
+ 'href' => "blog/owner/$owner->username",
+ 'text' => $owner->name,
+ 'is_trusted' => true,
+));
+$author_text = elgg_echo('byline', array($owner_link));
+$date = elgg_view_friendly_time($blog->time_created);
+
+// The "on" status changes for comments, so best to check for !Off
+if ($blog->comments_on != 'Off') {
+ $comments_count = $blog->countComments();
+ //only display if there are commments
+ if ($comments_count != 0) {
+ $text = elgg_echo("comments") . " ($comments_count)";
+ $comments_link = elgg_view('output/url', array(
+ 'href' => $blog->getURL() . '#blog-comments',
+ 'text' => $text,
+ 'is_trusted' => true,
+ ));
+ } else {
+ $comments_link = '';
+ }
+} else {
+ $comments_link = '';
+}
+
+$metadata = elgg_view_menu('entity', array(
+ 'entity' => $vars['entity'],
+ 'handler' => 'blog',
+ 'sort_by' => 'priority',
+ 'class' => 'elgg-menu-hz',
+));
+
+$subtitle = "$author_text $date $comments_link $categories";
+
+// do not show the metadata and controls in widget view
+if (elgg_in_context('widgets')) {
+ $metadata = '';
+}
+
+if ($full) {
+
+ $body = elgg_view('output/longtext', array(
+ 'value' => $blog->description,
+ 'class' => 'blog-post',
+ ));
+
+ $params = array(
+ 'entity' => $blog,
+ 'title' => false,
+ 'metadata' => $metadata,
+ 'subtitle' => $subtitle,
+ );
+ $params = $params + $vars;
+ $summary = elgg_view('object/elements/summary', $params);
+
+ echo elgg_view('object/elements/full', array(
+ 'summary' => $summary,
+ 'icon' => $owner_icon,
+ 'body' => $body,
+ ));
+
+} else {
+ // brief view
+
+ $params = array(
+ 'entity' => $blog,
+ 'metadata' => $metadata,
+ 'subtitle' => $subtitle,
+ 'content' => $excerpt,
+ );
+ $params = $params + $vars;
+ $list_body = elgg_view('object/elements/summary', $params);
+
+ echo elgg_view_image_block($owner_icon, $list_body);
+}
diff --git a/mod/blog/views/default/river/object/blog/create.php b/mod/blog/views/default/river/object/blog/create.php
new file mode 100644
index 000000000..b808f1bdc
--- /dev/null
+++ b/mod/blog/views/default/river/object/blog/create.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Blog river view.
+ */
+
+$object = $vars['item']->getObjectEntity();
+
+$excerpt = $object->excerpt ? $object->excerpt : $object->description;
+$excerpt = strip_tags($excerpt);
+$excerpt = elgg_get_excerpt($excerpt);
+
+echo elgg_view('river/elements/layout', array(
+ 'item' => $vars['item'],
+ 'message' => $excerpt,
+));
diff --git a/mod/blog/views/default/widgets/blog/content.php b/mod/blog/views/default/widgets/blog/content.php
new file mode 100644
index 000000000..330171662
--- /dev/null
+++ b/mod/blog/views/default/widgets/blog/content.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * User blog widget display view
+ */
+
+$num = $vars['entity']->num_display;
+
+$options = array(
+ 'type' => 'object',
+ 'subtype' => 'blog',
+ 'container_guid' => $vars['entity']->owner_guid,
+ 'limit' => $num,
+ 'full_view' => FALSE,
+ 'pagination' => FALSE,
+);
+$content = elgg_list_entities($options);
+
+echo $content;
+
+if ($content) {
+ $blog_url = "blog/owner/" . elgg_get_page_owner_entity()->username;
+ $more_link = elgg_view('output/url', array(
+ 'href' => $blog_url,
+ 'text' => elgg_echo('blog:moreblogs'),
+ 'is_trusted' => true,
+ ));
+ echo "<span class=\"elgg-widget-more\">$more_link</span>";
+} else {
+ echo elgg_echo('blog:noblogs');
+}
diff --git a/mod/blog/views/default/widgets/blog/edit.php b/mod/blog/views/default/widgets/blog/edit.php
new file mode 100644
index 000000000..30c3b2e73
--- /dev/null
+++ b/mod/blog/views/default/widgets/blog/edit.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * User blog widget edit view
+ */
+
+// set default value
+if (!isset($vars['entity']->num_display)) {
+ $vars['entity']->num_display = 4;
+}
+
+$params = array(
+ 'name' => 'params[num_display]',
+ 'value' => $vars['entity']->num_display,
+ 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+);
+$dropdown = elgg_view('input/dropdown', $params);
+
+?>
+<div>
+ <?php echo elgg_echo('blog:numbertodisplay'); ?>:
+ <?php echo $dropdown; ?>
+</div>