add.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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');
  9. $comment_guid = (int) get_input('comment_guid');
  10. $comment_text = get_input('generic_comment');
  11. if (empty($comment_text)) {
  12. register_error(elgg_echo("generic_comment:blank"));
  13. forward(REFERER);
  14. }
  15. if ($comment_guid) {
  16. // Edit an existing comment
  17. $comment = get_entity($comment_guid);
  18. if (!elgg_instanceof($comment, 'object', 'comment')) {
  19. register_error(elgg_echo("generic_comment:notfound"));
  20. forward(REFERER);
  21. }
  22. if (!$comment->canEdit()) {
  23. register_error(elgg_echo("actionunauthorized"));
  24. forward(REFERER);
  25. }
  26. $comment->description = $comment_text;
  27. if ($comment->save()) {
  28. system_message(elgg_echo('generic_comment:updated'));
  29. } else {
  30. register_error(elgg_echo('generic_comment:failure'));
  31. }
  32. } else {
  33. // Create a new comment on the target entity
  34. $entity = get_entity($entity_guid);
  35. if (!$entity) {
  36. register_error(elgg_echo("generic_comment:notfound"));
  37. forward(REFERER);
  38. }
  39. $user = elgg_get_logged_in_user_entity();
  40. $comment = new ElggComment();
  41. $comment->description = $comment_text;
  42. $comment->owner_guid = $user->getGUID();
  43. $comment->container_guid = $entity->getGUID();
  44. $comment->access_id = $entity->access_id;
  45. $guid = $comment->save();
  46. if (!$guid) {
  47. register_error(elgg_echo("generic_comment:failure"));
  48. forward(REFERER);
  49. }
  50. // Notify if poster wasn't owner
  51. if ($entity->owner_guid != $user->guid) {
  52. notify_user($entity->owner_guid,
  53. $user->guid,
  54. elgg_echo('generic_comment:email:subject'),
  55. elgg_echo('generic_comment:email:body', array(
  56. $entity->title,
  57. $user->name,
  58. $comment_text,
  59. $entity->getURL(),
  60. $user->name,
  61. $user->getURL()
  62. )),
  63. array(
  64. 'object' => $comment,
  65. 'action' => 'create',
  66. )
  67. );
  68. }
  69. // Add to river
  70. elgg_create_river_item(array(
  71. 'view' => 'river/object/comment/create',
  72. 'action_type' => 'comment',
  73. 'subject_guid' => $user->guid,
  74. 'object_guid' => $guid,
  75. 'target_guid' => $entity_guid,
  76. ));
  77. $comment_guid = $comment->getGUID();
  78. system_message(elgg_echo('generic_comment:posted'));
  79. }
  80. if($comment_guid){
  81. $comment = get_entity($comment_guid);
  82. echo elgg_view_entity($comment, array('full_view' => true));
  83. }
  84. // Forward back to the page where the action occurred
  85. forward(REFERER);