auto_save_revision.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $container_guid = (int)get_input('container_guid');
  39. if ($container_guid) {
  40. $blog->container_guid = $container_guid;
  41. }
  42. if (!$blog->save()) {
  43. $error = elgg_echo('blog:error:cannot_save');
  44. }
  45. }
  46. // creat draft annotation
  47. if (!$error) {
  48. // annotations don't have a "time_updated" so
  49. // we have to delete everything or the times are wrong.
  50. // don't save if nothing changed
  51. $auto_save_annotations = $blog->getAnnotations(array(
  52. 'annotation_name' => 'blog_auto_save',
  53. 'limit' => 1,
  54. ));
  55. if ($auto_save_annotations) {
  56. $auto_save = $auto_save_annotations[0];
  57. } else {
  58. $auto_save = FALSE;
  59. }
  60. if (!$auto_save) {
  61. $annotation_id = $blog->annotate('blog_auto_save', $description);
  62. } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value != $description) {
  63. $blog->deleteAnnotations('blog_auto_save');
  64. $annotation_id = $blog->annotate('blog_auto_save', $description);
  65. } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value == $description) {
  66. // this isn't an error because we have an up to date annotation.
  67. $annotation_id = $auto_save->id;
  68. }
  69. if (!$annotation_id) {
  70. $error = elgg_echo('blog:error:cannot_auto_save');
  71. }
  72. }
  73. } else {
  74. $error = elgg_echo('blog:error:missing:description');
  75. }
  76. if ($error) {
  77. $json = array('success' => FALSE, 'message' => $error);
  78. echo json_encode($json);
  79. } else {
  80. $msg = elgg_echo('blog:message:saved');
  81. $json = array('success' => TRUE, 'message' => $msg, 'guid' => $blog->getGUID());
  82. echo json_encode($json);
  83. }
  84. exit;