auto_save_revision.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Action called by AJAX periodic auto saving when editing.
  4. *
  5. * @package Blog
  6. */
  7. $guid = get_input('guid');
  8. $user = elgg_get_logged_in_user_entity();
  9. $title = htmlspecialchars(get_input('title', '', false), ENT_QUOTES, 'UTF-8');
  10. $description = get_input('description');
  11. $excerpt = get_input('excerpt');
  12. // because get_input() doesn't use the default if the input is ''
  13. if (empty($excerpt)) {
  14. $excerpt = $description;
  15. }
  16. // store errors to pass along
  17. $error = FALSE;
  18. if ($title && $description) {
  19. if ($guid) {
  20. $entity = get_entity($guid);
  21. if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) {
  22. $blog = $entity;
  23. } else {
  24. $error = elgg_echo('blog:error:post_not_found');
  25. }
  26. } else {
  27. $blog = new ElggBlog();
  28. $blog->subtype = 'blog';
  29. // force draft and private for autosaves.
  30. $blog->status = 'unsaved_draft';
  31. $blog->access_id = ACCESS_PRIVATE;
  32. $blog->title = $title;
  33. $blog->description = $description;
  34. $blog->excerpt = elgg_get_excerpt($excerpt);
  35. // mark this as a brand new post so we can work out the
  36. // river / revision logic in the real save action.
  37. $blog->new_post = TRUE;
  38. if (!$blog->save()) {
  39. $error = elgg_echo('blog:error:cannot_save');
  40. }
  41. }
  42. // creat draft annotation
  43. if (!$error) {
  44. // annotations don't have a "time_updated" so
  45. // we have to delete everything or the times are wrong.
  46. // don't save if nothing changed
  47. $auto_save_annotations = $blog->getAnnotations(array(
  48. 'annotation_name' => 'blog_auto_save',
  49. 'limit' => 1,
  50. ));
  51. if ($auto_save_annotations) {
  52. $auto_save = $auto_save_annotations[0];
  53. } else {
  54. $auto_save = FALSE;
  55. }
  56. if (!$auto_save) {
  57. $annotation_id = $blog->annotate('blog_auto_save', $description);
  58. } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value != $description) {
  59. $blog->deleteAnnotations('blog_auto_save');
  60. $annotation_id = $blog->annotate('blog_auto_save', $description);
  61. } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value == $description) {
  62. // this isn't an error because we have an up to date annotation.
  63. $annotation_id = $auto_save->id;
  64. }
  65. if (!$annotation_id) {
  66. $error = elgg_echo('blog:error:cannot_auto_save');
  67. }
  68. }
  69. } else {
  70. $error = elgg_echo('blog:error:missing:description');
  71. }
  72. if ($error) {
  73. $json = array('success' => FALSE, 'message' => $error);
  74. echo json_encode($json);
  75. } else {
  76. $msg = elgg_echo('blog:message:saved');
  77. $json = array('success' => TRUE, 'message' => $msg, 'guid' => $blog->getGUID());
  78. echo json_encode($json);
  79. }
  80. exit;