save.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $blog->container_guid = (int) get_input('container_guid');
  38. $new_post = TRUE;
  39. }
  40. // set the previous status for the hooks to update the time_created and river entries
  41. $old_status = $blog->status;
  42. // set defaults and required values.
  43. $values = array(
  44. 'title' => '',
  45. 'description' => '',
  46. 'status' => 'draft',
  47. 'access_id' => ACCESS_DEFAULT,
  48. 'comments_on' => 'On',
  49. 'excerpt' => '',
  50. 'tags' => '',
  51. 'publication_date' => '',
  52. 'expiration_date' => '',
  53. 'show_owner' => 'no'
  54. );
  55. // fail if a required entity isn't set
  56. $required = array('title', 'description');
  57. // load from POST and do sanity and access checking
  58. foreach ($values as $name => $default) {
  59. if ($name === 'title') {
  60. $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8');
  61. } else {
  62. $value = get_input($name, $default);
  63. }
  64. if (in_array($name, $required) && empty($value)) {
  65. $error = elgg_echo("blog:error:missing:$name");
  66. break;
  67. }
  68. switch ($name) {
  69. case 'tags':
  70. $values[$name] = string_to_tag_array($value);
  71. break;
  72. case 'excerpt':
  73. if ($value) {
  74. $values[$name] = elgg_get_excerpt($value);
  75. }
  76. break;
  77. case 'publication_date':
  78. if (!empty($value)) {
  79. $values[$name] = $value;
  80. // publication date has not yet passed, set as draft
  81. if (strtotime($value) > time()) {
  82. $save = false;
  83. }
  84. }
  85. break;
  86. case 'expiration_date':
  87. if (!empty($value)) {
  88. $values[$name] = $value;
  89. if ($new_post) {
  90. // new blogs can't expire directly
  91. if (strtotime($value) < time()) {
  92. $error = elgg_echo("blog_tools:action:save:error:expiration_date");
  93. }
  94. } else {
  95. // if expiration is passed, set as draft
  96. if (strtotime($value) < time()) {
  97. $save = false;
  98. }
  99. }
  100. }
  101. break;
  102. default:
  103. $values[$name] = $value;
  104. break;
  105. }
  106. }
  107. // if preview, force status to be draft
  108. if ($save == false) {
  109. $values['status'] = 'draft';
  110. }
  111. // if draft, set access to private and cache the future access
  112. if ($values['status'] == 'draft') {
  113. $values['future_access'] = $values['access_id'];
  114. $values['access_id'] = ACCESS_PRIVATE;
  115. }
  116. // assign values to the entity, stopping on error.
  117. if (!$error) {
  118. foreach ($values as $name => $value) {
  119. $blog->$name = $value;
  120. }
  121. }
  122. // only try to save base entity if no errors
  123. if (!$error) {
  124. if ($blog->save()) {
  125. // handle icon upload
  126. if (get_input("remove_icon") == "yes") {
  127. // remove existing icons
  128. blog_tools_remove_blog_icon($blog);
  129. } else {
  130. $icon_file = get_resized_image_from_uploaded_file("icon", 100, 100);
  131. $icon_sizes = elgg_get_config("icon_sizes");
  132. if (!empty($icon_file) && !empty($icon_sizes)) {
  133. // create icon
  134. $prefix = "blogs/" . $blog->getGUID();
  135. $fh = new ElggFile();
  136. $fh->owner_guid = $blog->getOwnerGUID();
  137. foreach ($icon_sizes as $icon_name => $icon_info) {
  138. $icon_file = get_resized_image_from_uploaded_file("icon", $icon_info["w"], $icon_info["h"], $icon_info["square"], $icon_info["upscale"]);
  139. if (!empty($icon_file)) {
  140. $fh->setFilename($prefix . $icon_name . ".jpg");
  141. if ($fh->open("write")) {
  142. $fh->write($icon_file);
  143. $fh->close();
  144. }
  145. }
  146. }
  147. $blog->icontime = time();
  148. }
  149. }
  150. // remove sticky form entries
  151. elgg_clear_sticky_form('blog');
  152. // remove autosave draft if exists
  153. $blog->deleteAnnotations('blog_auto_save');
  154. // no longer a brand new post.
  155. $blog->deleteMetadata('new_post');
  156. // if this was an edit, create a revision annotation
  157. if (!$new_post && $revision_text) {
  158. $blog->annotate('blog_revision', $revision_text);
  159. }
  160. system_message(elgg_echo('blog:message:saved'));
  161. $status = $blog->status;
  162. // add to river if changing status or published, regardless of new post
  163. // because we remove it for drafts.
  164. if (($new_post || $old_status == 'draft') && $status == 'published') {
  165. elgg_create_river_item(array(
  166. 'view' => 'river/object/blog/create',
  167. 'action_type' => 'create',
  168. 'subject_guid' => $blog->owner_guid,
  169. 'object_guid' => $blog->getGUID(),
  170. ));
  171. // we only want notifications sent when post published
  172. elgg_trigger_event('publish', 'object', $blog);
  173. // reset the creation time for posts that move from draft to published
  174. if ($guid) {
  175. $blog->time_created = time();
  176. $blog->save();
  177. }
  178. } elseif ($old_status == 'published' && $status == 'draft') {
  179. elgg_delete_river(array(
  180. 'object_guid' => $blog->guid,
  181. 'action_type' => 'create',
  182. ));
  183. }
  184. if ($blog->status == 'published' || $save == false) {
  185. forward($blog->getURL());
  186. } else {
  187. forward("blog/edit/$blog->guid");
  188. }
  189. } else {
  190. register_error(elgg_echo('blog:error:cannot_save'));
  191. forward($error_forward_url);
  192. }
  193. } else {
  194. register_error($error);
  195. forward($error_forward_url);
  196. }