save.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Action for adding and editing comments
  4. *
  5. * @package Elgg.Core
  6. * @subpackage Comments
  7. */
  8. $entity_guid = (int) get_input('entity_guid', 0, false);
  9. $comment_guid = (int) get_input('comment_guid', 0, false);
  10. $comment_text = get_input('generic_comment');
  11. $is_edit_page = (bool) get_input('is_edit_page', false, false);
  12. if (empty($comment_text)) {
  13. register_error(elgg_echo("generic_comment:blank"));
  14. forward(REFERER);
  15. }
  16. if ($comment_guid) {
  17. // Edit an existing comment
  18. $comment = get_entity($comment_guid);
  19. if (!elgg_instanceof($comment, 'object', 'comment')) {
  20. register_error(elgg_echo("generic_comment:notfound"));
  21. forward(REFERER);
  22. }
  23. if (!$comment->canEdit()) {
  24. register_error(elgg_echo("actionunauthorized"));
  25. forward(REFERER);
  26. }
  27. $comment->description = $comment_text;
  28. if ($comment->save()) {
  29. system_message(elgg_echo('generic_comment:updated'));
  30. if (elgg_is_xhr()) {
  31. // @todo move to its own view object/comment/content in 1.x
  32. echo elgg_view('output/longtext', array(
  33. 'value' => $comment->description,
  34. 'class' => 'elgg-inner',
  35. 'data-role' => 'comment-text',
  36. ));
  37. }
  38. } else {
  39. register_error(elgg_echo('generic_comment:failure'));
  40. }
  41. } else {
  42. // Create a new comment on the target entity
  43. $entity = get_entity($entity_guid);
  44. if (!$entity) {
  45. register_error(elgg_echo("generic_comment:notfound"));
  46. forward(REFERER);
  47. }
  48. $user = elgg_get_logged_in_user_entity();
  49. $comment = new ElggComment();
  50. $comment->description = $comment_text;
  51. $comment->owner_guid = $user->getGUID();
  52. $comment->container_guid = $entity->getGUID();
  53. $comment->access_id = $entity->access_id;
  54. $guid = $comment->save();
  55. if (!$guid) {
  56. register_error(elgg_echo("generic_comment:failure"));
  57. forward(REFERER);
  58. }
  59. // Notify if poster wasn't owner
  60. if ($entity->owner_guid != $user->guid) {
  61. $owner = $entity->getOwnerEntity();
  62. notify_user($owner->guid,
  63. $user->guid,
  64. elgg_echo('generic_comment:email:subject', array(), $owner->language),
  65. elgg_echo('generic_comment:email:body', array(
  66. $entity->title,
  67. $user->name,
  68. $comment_text,
  69. $comment->getURL(),
  70. $user->name,
  71. $user->getURL()
  72. ), $owner->language),
  73. array(
  74. 'object' => $comment,
  75. 'action' => 'create',
  76. )
  77. );
  78. }
  79. // Add to river
  80. elgg_create_river_item(array(
  81. 'view' => 'river/object/comment/create',
  82. 'action_type' => 'comment',
  83. 'subject_guid' => $user->guid,
  84. 'object_guid' => $guid,
  85. 'target_guid' => $entity_guid,
  86. ));
  87. system_message(elgg_echo('generic_comment:posted'));
  88. }
  89. if ($is_edit_page) {
  90. forward($comment->getURL());
  91. }
  92. forward(REFERER);