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