save.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Save blog entity
  4. *
  5. * Can be called by clicking save button or preview button. If preview button,
  6. * we automatically save as draft. The preview button is only available for
  7. * non-published drafts.
  8. *
  9. * Drafts are saved with the access set to private.
  10. *
  11. * @package Blog
  12. */
  13. // start a new sticky form session in case of failure
  14. elgg_make_sticky_form('blog');
  15. // save or preview
  16. $save = (bool)get_input('save');
  17. // store errors to pass along
  18. $error = FALSE;
  19. $error_forward_url = REFERER;
  20. $user = elgg_get_logged_in_user_entity();
  21. // edit or create a new entity
  22. $guid = get_input('guid');
  23. if ($guid) {
  24. $entity = get_entity($guid);
  25. if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) {
  26. $blog = $entity;
  27. } else {
  28. register_error(elgg_echo('blog:error:post_not_found'));
  29. forward(get_input('forward', REFERER));
  30. }
  31. // save some data for revisions once we save the new edit
  32. $revision_text = $blog->description;
  33. $new_post = $blog->new_post;
  34. } else {
  35. $blog = new ElggBlog();
  36. $blog->subtype = 'blog';
  37. $new_post = TRUE;
  38. }
  39. // set the previous status for the hooks to update the time_created and river entries
  40. $old_status = $blog->status;
  41. // set defaults and required values.
  42. $values = array(
  43. 'title' => '',
  44. 'description' => '',
  45. 'status' => 'draft',
  46. 'access_id' => ACCESS_DEFAULT,
  47. 'comments_on' => 'On',
  48. 'excerpt' => '',
  49. 'tags' => '',
  50. 'container_guid' => (int)get_input('container_guid'),
  51. );
  52. // fail if a required entity isn't set
  53. $required = array('title', 'description');
  54. // load from POST and do sanity and access checking
  55. foreach ($values as $name => $default) {
  56. if ($name === 'title') {
  57. $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8');
  58. } else {
  59. $value = get_input($name, $default);
  60. }
  61. if (in_array($name, $required) && empty($value)) {
  62. $error = elgg_echo("blog:error:missing:$name");
  63. }
  64. if ($error) {
  65. break;
  66. }
  67. switch ($name) {
  68. case 'tags':
  69. $values[$name] = string_to_tag_array($value);
  70. break;
  71. case 'excerpt':
  72. if ($value) {
  73. $values[$name] = elgg_get_excerpt($value);
  74. }
  75. break;
  76. case 'container_guid':
  77. // this can't be empty or saving the base entity fails
  78. if (!empty($value)) {
  79. if (can_write_to_container($user->getGUID(), $value)) {
  80. $values[$name] = $value;
  81. } else {
  82. $error = elgg_echo("blog:error:cannot_write_to_container");
  83. }
  84. } else {
  85. unset($values[$name]);
  86. }
  87. break;
  88. default:
  89. $values[$name] = $value;
  90. break;
  91. }
  92. }
  93. // if preview, force status to be draft
  94. if ($save == false) {
  95. $values['status'] = 'draft';
  96. }
  97. // if draft, set access to private and cache the future access
  98. if ($values['status'] == 'draft') {
  99. $values['future_access'] = $values['access_id'];
  100. $values['access_id'] = ACCESS_PRIVATE;
  101. }
  102. // assign values to the entity, stopping on error.
  103. if (!$error) {
  104. foreach ($values as $name => $value) {
  105. $blog->$name = $value;
  106. }
  107. }
  108. // only try to save base entity if no errors
  109. if (!$error) {
  110. if ($blog->save()) {
  111. // remove sticky form entries
  112. elgg_clear_sticky_form('blog');
  113. // remove autosave draft if exists
  114. $blog->deleteAnnotations('blog_auto_save');
  115. // no longer a brand new post.
  116. $blog->deleteMetadata('new_post');
  117. // if this was an edit, create a revision annotation
  118. if (!$new_post && $revision_text) {
  119. $blog->annotate('blog_revision', $revision_text);
  120. }
  121. system_message(elgg_echo('blog:message:saved'));
  122. $status = $blog->status;
  123. // add to river if changing status or published, regardless of new post
  124. // because we remove it for drafts.
  125. if (($new_post || $old_status == 'draft') && $status == 'published') {
  126. elgg_create_river_item(array(
  127. 'view' => 'river/object/blog/create',
  128. 'action_type' => 'create',
  129. 'subject_guid' => $blog->owner_guid,
  130. 'object_guid' => $blog->getGUID(),
  131. ));
  132. elgg_trigger_event('publish', 'object', $blog);
  133. // reset the creation time for posts that move from draft to published
  134. if ($guid) {
  135. $blog->time_created = time();
  136. $blog->save();
  137. }
  138. } elseif ($old_status == 'published' && $status == 'draft') {
  139. elgg_delete_river(array(
  140. 'object_guid' => $blog->guid,
  141. 'action_type' => 'create',
  142. ));
  143. }
  144. if ($blog->status == 'published' || $save == false) {
  145. forward($blog->getURL());
  146. } else {
  147. forward("blog/edit/$blog->guid");
  148. }
  149. } else {
  150. register_error(elgg_echo('blog:error:cannot_save'));
  151. forward($error_forward_url);
  152. }
  153. } else {
  154. register_error($error);
  155. forward($error_forward_url);
  156. }