]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Updated blog to support multiple revisions and automatic saving of drafts.
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 18 Mar 2010 21:45:48 +0000 (21:45 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 18 Mar 2010 21:45:48 +0000 (21:45 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@5437 36083f99-b078-4883-b0ff-0f9b5a30f544

mod/blog/actions/blog/auto_save_revision.php [new file with mode: 0644]
mod/blog/actions/blog/save.php
mod/blog/actions/blog/save_draft.php [deleted file]
mod/blog/blog_lib.php
mod/blog/languages/en.php
mod/blog/start.php
mod/blog/views/default/blog/forms/edit.php
mod/blog/views/default/blog/sidebar_edit.php [new file with mode: 0644]
mod/blog/views/default/blog/sidebar_menu.php
mod/blog/views/default/blog/sidebar_revisions.php [new file with mode: 0644]
mod/blog/views/default/object/blog.php

diff --git a/mod/blog/actions/blog/auto_save_revision.php b/mod/blog/actions/blog/auto_save_revision.php
new file mode 100644 (file)
index 0000000..fa9010e
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+/**
+ * Action called by AJAX periodic auto saving when editing.
+ *
+ * @package Blog
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008-2010
+ * @link http://elgg.org/
+ */
+
+$guid = get_input('guid');
+$user = get_loggedin_user();
+$title = get_input('title');
+$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 ElggObject();
+               $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 = blog_make_excerpt($excerpt);
+               // must be present or doesn't show up when metadata sorting.
+               $blog->publish_date = time();
+               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->clearAnnotations('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);
+}
\ No newline at end of file
index eade7cad89e5f9969a14b80b54e895b7ab77f96a..0ca063496610aa38fdd8b8e705ba4ce32042fafb 100644 (file)
@@ -9,16 +9,16 @@
  * @link http://elgg.org/
  */
 
-elgg_make_sticky_form();
-
-// edit or create a new entity
-$guid = get_input('guid');
-$user = get_loggedin_user();
-$ajax = get_input('ajax');
+// start a new sticky form session in case of failure
+//elgg_make_sticky_form();
 
 // store errors to pass along
 $error = FALSE;
 $error_forward_url = $_SERVER['HTTP_REFERER'];
+$user = get_loggedin_user();
+
+// edit or create a new entity
+$guid = get_input('guid');
 
 if ($guid) {
        $entity = get_entity($guid);
@@ -29,10 +29,15 @@ if ($guid) {
                forward(get_input('forward', $_SERVER['HTTP_REFERER']));
        }
        $success_forward_url = get_input('forward', $blog->getURL());
+
+       // save some data for revisions once we save the new edit
+       $revision_value = $blog->description;
+       $new_post = FALSE;
 } else {
        $blog = new ElggObject();
        $blog->subtype = 'blog';
        $success_forward_url = get_input('forward');
+       $new_post = TRUE;
 }
 
 // set defaults and required values.
@@ -40,7 +45,7 @@ $values = array(
        'title' => '',
        'description' => '',
        'status' => 'draft',
-       //'publish_date' => time(),
+       'publish_date' => time(),
        'access_id' => ACCESS_DEFAULT,
        'comments_on' => 'On',
        'excerpt' => '',
@@ -48,13 +53,9 @@ $values = array(
        'container_guid' => ''
 );
 
+// fail if a required entity isn't set
 $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);
@@ -77,17 +78,16 @@ foreach ($values as $name => $default) {
                        break;
 
                case 'excerpt':
-                       // restrict to 300 chars
                        if ($value) {
-                               $value = substr(strip_tags($value), 0, 300);
+                               $value = blog_make_excerpt($value);
                        } else {
-                               $value = substr(strip_tags($values['description']), 0, 300);
+                               $value = blog_make_excerpt($values['description']);
                        }
                        $values[$name] = $value;
                        break;
 
                case 'container_guid':
-                       // this can't be empty.
+                       // 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;
@@ -99,6 +99,13 @@ foreach ($values as $name => $default) {
                        }
                        break;
 
+               case 'publish_date':
+                       if (empty($value)) {
+                               $value = time();
+                       }
+                       $values[$name] = $value;
+                       break;
+
                // don't try to set the guid
                case 'guid':
                        unset($values['guid']);
@@ -113,170 +120,44 @@ foreach ($values as $name => $default) {
 // 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_save1' . $name);
+               if (FALSE === ($blog->$name = $value)) {
+                       $error = elgg_echo('blog:error:cannot_save' . "$name=$value");
                        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);
-       }
-}
-
-
-
-/*
- * This might have been a good idea.
- * It's not.
+if (!$error) {
+       if ($blog->save()) {
+               // remove sticky form entries
+               elgg_clear_sticky_form();
 
-// edit or create a new entity
-$guid = get_input('guid');
-$user = get_loggedin_user();
-$ajax = get_input('ajax', FALSE);
+               // remove autosave draft if exists
+               $blog->clearAnnotations('blog_auto_save');
 
-// store errors to pass along
-$error = FALSE;
-$error_forward_url = $_SERVER['HTTP_REFERER'];
+               // if this was an edit, create a revision
+               if (!$new_post && $revision_value) {
+                       // create a revision annotation
+                       $blog->annotate('blog_revision', $revision_value);
+               }
 
-if ($guid) {
-       $entity = get_entity($guid);
-       if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) {
-               $blog = $entity;
+               system_message(elgg_echo('blog:message:saved'));
+               forward($success_forward_url);
        } 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' => '',
-       '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) {
-
-       if ($error) {
-               break;
-       }
-
-       $value = get_input($name, $default);
-
-       if (in_array($name, $required) && empty($value)) {
-               register_error(elgg_echo("blog:error:missing:$name"));
+               register_error(elgg_echo('blog:error:cannot_save'));
                forward($error_forward_url);
        }
-
-       switch ($name) {
-               case 'tags':
-                       $values[$name] = string_to_tag_array($value);
-                       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.
-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 or return ajax data.
-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);
-               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);
-       }
+       register_error($error);
+       forward($error_forward_url);
 }
 
-*/
\ No newline at end of file
+// forward with success or failure
+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
diff --git a/mod/blog/actions/blog/save_draft.php b/mod/blog/actions/blog/save_draft.php
deleted file mode 100644 (file)
index 64a79c6..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-<?php
-/**
- * Save blog entity
- *
- * @package Blog
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Curverider Ltd
- * @copyright Curverider Ltd 2008-2010
- * @link http://elgg.org/
- */
-
-elgg_make_sticky_form();
-
-// edit or create a new entity
-$guid = get_input('guid');
-$user = get_loggedin_user();
-$ajax = get_input('ajax');
-
-// store errors to pass along
-$error = FALSE;
-$error_forward_url = $_SERVER['HTTP_REFERER'];
-
-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', $_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
index 0fe1b80d4a684fc85a90c3336e2be29643b72792..993728dcc7f2d3d4a263c0e587facd13ac72a0e1 100644 (file)
  * @param int $guid of a blog entity.
  * @return string html
  */
-function blog_get_page_content_read($owner_guid, $guid) {
+function blog_get_page_content_read($owner_guid = NULL, $guid = NULL) {
+       $content = elgg_view('page_elements/content_header', array('context' => $context, 'type' => 'blog'));
+
        if ($guid) {
                $blog = get_entity($guid);
 
                if (!elgg_instanceof($blog, 'object', 'blog') && $blog->status == 'final') {
-                       $content = elgg_echo('blog:error:post_not_found');
+                       $content .= elgg_echo('blog:error:post_not_found');
                } else {
                        elgg_push_breadcrumb($blog->title, $blog->getURL());
-                       $content = elgg_view_entity($blog, TRUE);
+                       $content .= elgg_view_entity($blog, TRUE);
                }
        } else {
-               $content = elgg_list_entities_from_metadata(array(
+               $options = array(
                        'type' => 'object',
                        'subtype' => 'blog',
-                       'owner_guid' => $owner_guid,
                        'full_view' => FALSE,
-                       'metadata_name_value_pair' => array('name' => 'status', 'value' => 'final')
-               ));
+                       'order_by_metadata' => array('name'=>'publish_date', 'direction'=>'DESC', 'as'=>'int')
+               );
+
+               if ($owner_guid) {
+                       $options['owner_guid'] = $owner_guid;
+               }
+
+               // show all posts for admin or users looking at their own blogs
+               // show only published posts for other users.
+               if (!(isadminloggedin() || (isloggedin() && $owner_guid == get_loggedin_userid()))) {
+                       $options['metadata_name_value_pairs'] = array(
+                               array('name' => 'status', 'value' => 'published'),
+                               array('name' => 'publish_date', 'operand' => '<', 'value' => time())
+                       );
+               }
+
+               $content .= elgg_list_entities_from_metadata($options);
        }
 
-       return $content;
+       return array('content' => $content);
 }
 
 /**
  * Returns HTML to edit a blog post.
  *
  * @param int $guid
+ * @param int annotation id optional revision to edit
  * @return string html
  */
-function blog_get_page_content_edit($guid) {
+function blog_get_page_content_edit($guid, $revision = NULL) {
        $vars = array();
        if ($guid) {
-               $blog = get_entity($guid);
-               $vars['entity'] = $blog;
+               $blog = get_entity((int)$guid);
+
+               if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
+                       $vars['entity'] = $blog;
+
+                       if ($revision) {
+                               $revision = get_annotation((int)$revision);
+                               $vars['revision'] = $revision;
+
+                               if (!$revision || !($revision->entity_guid == $guid)) {
+                                       $content = elgg_echo('blog:error:revision_not_found');
+                               }
+                       }
+
+                       elgg_push_breadcrumb($blog->title, $blog->getURL());
+                       elgg_push_breadcrumb(elgg_echo('edit'));
 
-               if (!elgg_instanceof($blog, 'object', 'blog') || !$blog->canEdit()) {
+                       $content = elgg_view('blog/forms/edit', $vars);
+                       $sidebar = elgg_view('blog/sidebar_revisions', array('entity' => $blog));
+                       //$sidebar .= elgg_view('blog/sidebar_related');
+               } else {
                        $content = elgg_echo('blog:error:post_not_found');
                }
-
-               elgg_push_breadcrumb($blog->title, $blog->getURL());
-               elgg_push_breadcrumb(elgg_echo('edit'));
        } else {
                elgg_push_breadcrumb(elgg_echo('blog:new'));
+               $content = elgg_view('blog/forms/edit', $vars);
+               //$sidebar = elgg_view('blog/sidebar_related');
        }
 
-       $content = elgg_view('blog/forms/edit', $vars);
-
-       return $content;
+       return array('content' => $content, 'sidebar' => $sidebar);
 }
 
 /**
@@ -175,7 +206,12 @@ function blog_save_blog($info) {
        return $return;
 }
 
-
-class ElggBlog extends ElggObject {
-
+/**
+ * Returns an appropriate excerpt for a blog.
+ *
+ * @param string $text
+ * @return string
+ */
+function blog_make_excerpt($text) {
+       return substr(strip_tags($text), 0, 300);
 }
\ No newline at end of file
index e8db1716ee1027d82e17709297154ef964f4ef0a..4d448856e414918838042fc47f9cecfff3f7f4f1 100644 (file)
@@ -6,6 +6,9 @@
 
 $english = array(
        'item:object:blog' => 'Blog',
+       'blog:blogs' => 'Blogs',
+       'blog:owned_blogs' => '%s blogs',
+       'blog:revisions' => 'Revisions',
 
        'blog:blog' => 'Blog',
        'blog:yours' => 'Your blog',
@@ -27,11 +30,15 @@ $english = array(
        'blog:status:published' => 'Published',
 
        '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)',
+
 );
 
 add_translation('en', $english);
\ No newline at end of file
index b3a8e32eecbc1ddbfefa2fed3f6c0dae06e0b3ac..bf0d3fb56bb51571da3057873ea106eef021f4ce 100644 (file)
@@ -39,7 +39,6 @@ function blog_init() {
        elgg_extend_view('css', 'blog/css');
 
        register_page_handler('blog', 'blog_page_handler');
-       register_page_handler('blog_ajax', 'blog_ajax_page_handler');
 
        register_entity_url_handler('blog_url_handler', 'object', 'blog');
 
@@ -60,6 +59,7 @@ function blog_init() {
        $action_path = dirname(__FILE__) . '/actions/blog';
 
        register_action('blog/save', FALSE, "$action_path/save.php");
+       register_action('blog/auto_save_revision', FALSE, "$action_path/auto_save_revision.php");
        register_action('blog/delete', FALSE, "$action_path/delete.php");
 }
 
@@ -111,13 +111,13 @@ function blog_page_handler($page) {
                switch ($action) {
                        case 'read':
                                $title = sprintf(elgg_echo('blog:title:user_blogs'), $user->name);
-                               $content = elgg_view('page_elements/content_header', array('context' => $content, 'type' => 'blog'));
-                               $content .= blog_get_page_content_read($user->getGUID(), $page2);
+                               $content_info = blog_get_page_content_read($user->getGUID(), $page2);
                                break;
 
                        case 'new':
                        case 'edit':
-                               $content = blog_get_page_content_edit($page2);
+                               //$sidebar = elgg_view('blog/sidebar_edit', array('blog_guid' => $page2));
+                               $content_info = blog_get_page_content_edit($page2, $page3);
                                break;
 
                        case 'archives':
@@ -136,69 +136,20 @@ function blog_page_handler($page) {
                }
        } else {
                $title = elgg_echo('blog:title:all_blogs');
-               $content = elgg_view('page_elements/content_header', array('context' => $content, 'type' => 'blog'));
-               $content .= elgg_list_entities_from_metadata(array(
-                       'type' => 'object',
-                       'subtype' => 'blog',
-                       'full_view' => FALSE,
-                       'metadata_name_value_pair' => array('name' => 'status', 'value' => 'final')
-               ));
+               $content_info = blog_get_page_content_read();
        }
 
-       $sidebar = elgg_view('blog/sidebar_menu');
-       $content = elgg_view('navigation/breadcrumbs') . $content;
+       $sidebar .= elgg_view('blog/sidebar_menu');
+       if (isset($content_info['sidebar'])) {
+               $sidebar .= $content_info['sidebar'];
+       }
+       $content = elgg_view('navigation/breadcrumbs') . $content_info['content'];
 
        $body = elgg_view_layout('one_column_with_sidebar', $content, $sidebar);
 
        page_draw($title, $body);
 }
 
-/**
- * Handles ajax calls for blog.
- *
- * @param array $page
- */
-function blog_ajax_page_handler($page) {
-       $action = isset($page[0]) ? $page[0] : FALSE;
-
-       var_dump($page);
-
-       switch ($action) {
-               case 'save_draft':
-                       // @todo recycle the save action
-                       $values = array(
-                               'title' => '',
-                               'description' => '',
-                               'status' => 'draft',
-                               'access_id' => ACCESS_DEFAULT,
-                               'comments_on' => 'On',
-                               'excerpt' => '',
-                               'tags' => '',
-                               'container_guid' => '',
-                               'guid' => ''
-                       );
-
-                       foreach ($values as $name => $default) {
-                               $values[$name] = get_input($name, $default);
-                       }
-
-
-
-                       /*
-                       If a title and body, create a blog post marked as a draft and update the
-                       GUID
-                       */
-                       break;
-
-               default:
-                       $content = 0;
-                       break;
-       }
-
-       exit;
-}
-
-
 /**
  * Format and return the correct URL for blogs.
  *
@@ -220,4 +171,4 @@ function blog_url_handler($entity) {
 }
 
 
-register_elgg_event_handler('init', 'system', 'blog_init');
\ No newline at end of file
+register_elgg_event_handler('init', 'system', 'blog_init');
index b1989857303c1c627a8890e0b50571758eb44818..83ee8208e80e9f4baa560665db0e54c8b8b7114b 100644 (file)
@@ -9,11 +9,12 @@
  * @link http://elgg.org/
  */
 
+// input names => defaults
 $values = array(
        'title' => NULL,
        'description' => NULL,
-       'status' => 'final',
-       //'publish_date' => NULL,
+       'status' => 'published',
+       'publish_date' => NULL,
        'access_id' => ACCESS_DEFAULT,
        'comments_on' => 'On',
        'excerpt' => NULL,
@@ -25,17 +26,42 @@ $values = array(
 $forward = $_SERVER['HTTP_REFERER'];
 
 $action_buttons = '';
-$guid_input = '';
 $delete_link = '';
+$draft_warning = '';
 
 // if entity is set, we're editing.
 if (isset ($vars['entity'])) {
        $blog = $vars['entity'];
 
        if ($blog && ($blog instanceof ElggObject) && ($blog->getSubtype() == 'blog')) {
-               foreach (array_keys($values) as $field) {
-                       $values[$field] = $blog->$field;
-               }
+               // passed in values override sticky values in input views
+               // if in a sticky form, don't send the overrides and let the view figure it out.
+               //if (!elgg_is_sticky_form()) {
+                       foreach (array_keys($values) as $field) {
+                               $values[$field] = $blog->$field;
+                       }
+
+                       // load the revision annotation if requested
+                       if (isset($vars['revision']) && $vars['revision'] instanceof ElggAnnotation && $vars['revision']->entity_guid == $blog->getGUID()) {
+                               $revision = $vars['revision'];
+                               $values['description'] = $vars['revision']->value;
+                       }
+
+                       // display a notice if there's an autosaved annotation
+                       // and we're not editing it.
+                       if ($auto_save_annotations = $blog->getAnnotations('blog_auto_save', 1)) {
+                               $auto_save = $auto_save_annotations[0];
+                       } else {
+                               $auto_save == FALSE;
+                       }
+
+                       if ($auto_save && $auto_save->id != $revision->id) {
+                               $draft_warning = '<span class="message warning">'
+                                       . elgg_echo('blog:messages:warning:draft')
+                                       . '</span>';
+                       }
+
+               //}
        } else {
                echo elgg_echo('blog:error:post_not_found');
                return FALSE;
@@ -48,8 +74,6 @@ if (isset ($vars['entity'])) {
                'text' => elgg_echo('delete'),
                'class' => 'action_button disabled'
        ));
-
-       $guid_input = elgg_view('input/hidden', array('internalname' => 'guid', 'value' => $values['guid']));
 }
 
 $save_button = elgg_view('input/submit', array('value' => elgg_echo('save'), 'class' => 'submit_button'));
@@ -77,7 +101,7 @@ $body_input = elgg_view('input/longtext', array(
 ));
 
 $save_status = elgg_echo('blog:save_status');
-$never = elgg_echo('never');
+$never = elgg_echo('blog:never');
 
 $status_label = elgg_echo('blog:status');
 $status_input = elgg_view('input/pulldown', array(
@@ -85,8 +109,8 @@ $status_input = elgg_view('input/pulldown', array(
        'internalid' => 'blog_status',
        'value' => $values['status'],
        'options_values' => array(
-               'draft' => elgg_echo('blog:draft'),
-               'final' => elgg_echo('blog:final')
+               'draft' => elgg_echo('blog:status:draft'),
+               'published' => elgg_echo('blog:status:published')
        )
 ));
 
@@ -121,12 +145,20 @@ $publish_date_input = elgg_view('input/datepicker', array(
 
 // hidden inputs
 //$container_guid_input = elgg_view('input/hidden', array('internalname' => 'container_guid', 'value' => $values['container_guid']));
+$guid_input = elgg_view('input/hidden', array('internalname' => 'guid', 'value' => $values['guid']));
 $forward_input = elgg_view('input/hidden', array('internalname' => 'forward', 'value' => $forward));
-$page_title = elgg_echo('blog:edit')." ".$values['title'];
+$page_title = elgg_echo('blog:edit') . " " . $values['title'];
+
+// display notice if editing an old revision
+if (isset($vars['revision']) && $vars['revision'] instanceof ElggAnnotation) {
+       $page_title .= ' ' . elgg_echo('blog:edit_revision_notice');
+}
 
 $form_body = <<<___END
 <h2>$page_title</h2>
 
+$draft_warning
+
 <p class="margin_top">
        <label for="blog_title">$title_label</label>
        $title_input
@@ -137,13 +169,12 @@ $form_body = <<<___END
 $excerpt_input
 </p>
 
-<p>
-       <label for="blog_description">$body_label</label>
-       $body_input
-</p>
+<label for="blog_description">$body_label</label>
+$body_input
+<br />
 
 <p id="blog_save_status">
-       $save_status:<span id="blog_save_status_time">$never</span>
+       $save_status <span id="blog_save_status_time">$never</span>
 </p>
 
 <p>
@@ -190,6 +221,8 @@ echo elgg_view('input/form', array(
        'body' => $form_body
 ));
 
+elgg_clear_sticky_form('blog');
+
 ?>
 
 <script type="text/javascript">
@@ -201,13 +234,16 @@ echo elgg_view('input/form', array(
        function blogSaveDraftCallback(data, textStatus, XHR) {
                if (textStatus == 'success' && data.success == true) {
                        var form = $('form[name=blog_post]');
-                       form.append('<input type="hidden" name="guid" value="' + data.guid + '" />');
+
+                       // 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.getHours() + ":" + mins);
-
                } else {
                        $("#blog_save_status_time").html("<?php echo elgg_echo('error'); ?>");
                }
@@ -218,16 +254,16 @@ echo elgg_view('input/form', array(
                        tinyMCE.triggerSave();
                }
 
-               // only save when content exists
+               // only save on changed content
                var form = $('form[name=blog_post]');
-               var title = form.children('input[name=title]').val();
-               var description = form.children('textarea[name=description]').val();
+               var description = form.find('textarea[name=description]').val();
+               var title = form.find('input[name=title]').val();
 
-               if (!(title && description)) {
+               if (!(description && title) || (description == oldDescription)) {
                        return false;
                }
 
-               var draftURL = "<?php echo $vars['url']; ?>action/blog/save?ajax=1";
+               var draftURL = "<?php echo $vars['url']; ?>action/blog/auto_save_revision";
                var postData = form.serializeArray();
 
                // force draft status
@@ -238,10 +274,12 @@ echo elgg_view('input/form', array(
                });
 
                $.post(draftURL, postData, blogSaveDraftCallback, 'json');
-
        }
 
-       $(document).ready(function(){
+       $(document).ready(function() {
+               // get a copy of the body to compare for auto save
+               oldDescription = $('form[name=blog_post]').find('textarea[name=description]').val();
+
                $('#excerpt.excerpt').each(function(){
                        var allowed = 200;
 
diff --git a/mod/blog/views/default/blog/sidebar_edit.php b/mod/blog/views/default/blog/sidebar_edit.php
new file mode 100644 (file)
index 0000000..d75c28a
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Blog sidebar menu for editing / creating a blog post.
+ *
+ * @package Blog
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008-2010
+ * @link http://elgg.org/
+ */
+
+//If editing a post, show the previous revisions and drafts.
+$blog_guid = isset($vars['blog_guid']) ? $vars['blog_guid'] : FALSE;
+$blog = get_entity($blog_guid);
+
+if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
+       $revisions = array();
+       if ($auto_save_annotations = $blog->getAnnotations('blog_auto_save', 1)) {
+               $revisions[] = $auto_save_annotations[0];
+       }
+
+       // count(FALSE) == 1!  AHHH!!!
+       if ($saved_revisions = $blog->getAnnotations('blog_revision', 10, 0, 'time_created DESC')) {
+               $revision_count = count($saved_revisions);
+       } else {
+               $revision_count = 0;
+       }
+
+       $revisions = array_merge($revisions, $saved_revisions);
+}
+
+if ($revisions) {
+       echo '<ul class="blog_revisions">';
+       $load_base_url = "{$vars['url']}pg/blog/{$owner->username}/edit/{$blog->getGUID()}/";
+
+       foreach ($revisions as $revision) {
+               $time = friendly_time($revision->time_created);
+               $load = elgg_view('output/url', array(
+                       'href' => $load_base_url . $revision->id,
+                       'text' => elgg_echo('load')
+               ));
+
+
+               if ($revision->name == 'blog_auto_save') {
+                       $name = elgg_echo('blog:auto_saved_revision');
+                       $text = "$name: $time $load";
+                       $class = 'class="auto_saved"';
+               } else {
+                       $name = elgg_echo('blog:revision');
+                       $text = "$name: $time $load";
+                       $class = 'class="auto_saved"';
+
+                       $revision_count--;
+               }
+
+               echo <<<___END
+<li $class>
+$text
+</li>
+
+___END;
+       }
+
+       echo '</ul>';
+}
\ No newline at end of file
index 71bb1ec3996625cb89355507fb73a74808478a3e..8ecec9a536b27198c41e80ea7e30087f6df5395e 100644 (file)
@@ -31,14 +31,8 @@ Owned blogs;
 $loggedin_user = get_loggedin_user();
 $page_owner = page_owner_entity();
 
-if ($loggedin_user) {
-
-}
 ?>
 
-<ul class="submenu">
-       <li><a href="">Drafts</a></li>
-</ul>
 <?php
        // include a view for plugins to extend
        echo elgg_view("blogs/sidebar", array("object_type" => 'blog'));
diff --git a/mod/blog/views/default/blog/sidebar_revisions.php b/mod/blog/views/default/blog/sidebar_revisions.php
new file mode 100644 (file)
index 0000000..f777316
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Blog sidebar menu showing revisions
+ *
+ * @package Blog
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008-2010
+ * @link http://elgg.org/
+ */
+
+//If editing a post, show the previous revisions and drafts.
+$blog = isset($vars['entity']) ? $vars['entity'] : FALSE;
+
+
+if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
+       $owner = $blog->getOwnerEntity();
+       $revisions = array();
+
+       if ($auto_save_annotations = $blog->getAnnotations('blog_auto_save', 1)) {
+               $revisions[] = $auto_save_annotations[0];
+       }
+
+       // count(FALSE) == 1!  AHHH!!!
+       if ($saved_revisions = $blog->getAnnotations('blog_revision', 10, 0, 'time_created DESC')) {
+               $revision_count = count($saved_revisions);
+       } else {
+               $revision_count = 0;
+       }
+
+       $revisions = array_merge($revisions, $saved_revisions);
+
+       if ($revisions) {
+               echo '<h3>' . elgg_echo('blog:revisions') . '</h3>';
+
+               $n = count($revisions);
+
+               echo '<ul class="blog_revisions">';
+               $load_base_url = "{$vars['url']}pg/blog/{$owner->username}/edit/{$blog->getGUID()}/";
+
+               foreach ($revisions as $revision) {
+                       $time = friendly_time($revision->time_created);
+                       $load = elgg_view('output/url', array(
+                               'href' => $load_base_url . $revision->id,
+                               'text' => elgg_echo('load')
+                       ));
+
+                       if ($revision->name == 'blog_auto_save') {
+                               $name = elgg_echo('blog:auto_saved_revision');
+                               $text = "$name: $time $load";
+                               $class = 'class="auto_saved"';
+                       } else {
+                               $name = elgg_echo('blog:revision') . " $n";
+                               $text = "$name: $time $load";
+                               $class = 'class="auto_saved"';
+                       }
+
+                       $n--;
+
+                       echo <<<___END
+<li $class>
+$text
+</li>
+
+___END;
+               }
+
+               echo '</ul>';
+       }
+}
\ No newline at end of file
index f1240256a3880d02872b207c67d061b6b152b578..4a854126165b905b979fb631f67d15c53a29f698 100644 (file)
@@ -24,9 +24,10 @@ $excerpt = $blog->excerpt;
 $body = $blog->description;
 $owner_icon = elgg_view("profile/icon",array('entity' => $owner, 'size' => 'tiny'));
 $tags = elgg_view('output/tags', array('tags' => $blog->tags));
-$date = friendly_time($blog->time_created);
+$date = friendly_time($blog->publish_date);
 
-if ($blog->comments_on == 'Yes') {
+// The "on" status changes for comments, so best to check for !Off
+if ($blog->comments_on != 'Off') {
        $comments_count = elgg_count_comments($blog);
        $comments_link = "<a href=\"{$blog->getURL()}#annotations\">" . sprintf(elgg_echo("comments"), $comments_count) . '</a>';
 } else {
@@ -34,27 +35,31 @@ if ($blog->comments_on == 'Yes') {
 }
 
 // links to delete or edit.
+$edit = '';
 if ($blog->canEdit()) {
        $edit_url = "{$vars['url']}pg/blog/{$owner->username}/edit/{$blog->getGUID()}/";
        $edit_link = "<a href=\"$edit_url\">" . elgg_echo('edit') . '</a>';
 
        $delete_url = "{$vars['url']}action/blog/delete?guid={$blog->getGUID()}";
-       $delete_link = "<span class='delete_button'>".elgg_view('output/confirmlink', array(
+       $delete_link = "<span class='delete_button'>" . elgg_view('output/confirmlink', array(
                'href' => $delete_url,
                'text' => elgg_echo('delete'),
                'confirm' => elgg_echo('deleteconfirm')
-       ))."</span>";
+       )) . "</span>";
 
-       $edit = "$edit_link $delete_link";
-} else {
-       $edit = '';
+       $status = '';
+       if ($blog->status != 'published') {
+               $status_text = elgg_echo("blog:status:{$blog->status}");
+               $status = "<span class='blog_status'>$status_text</a>";
+       }
+
+       $edit = "$status $edit_link $delete_link";
 }
 
        // include a view for plugins to extend
        $edit = elgg_view("blogs/options", array("object_type" => 'blog')) .$edit;
 
 if ($full) {
-       // The "on" status changes for comments, so best to check for !Off
        if ($blog->comments_on != 'Off') {
                $comments = elgg_view_comments($blog);
        } else {
@@ -67,7 +72,7 @@ echo <<<___END
 <div class="blogpost clearfloat">
        <div id="content_header" class="clearfloat">
                <div class="content_header_title"><h2>$owner_title</h2></div>
-       </div>  
+       </div>
        <div class="entity_listing_icon">
                $owner_icon
        </div>
@@ -90,7 +95,7 @@ ___END;
 
 } else {
        echo <<<___END
-<div class="blog entity_listing clearfloat">
+<div class="blog $status_class entity_listing clearfloat">
        <div class="entity_listing_icon">
                $owner_icon
        </div>