Elgg  Version 6.1
save.php
Go to the documentation of this file.
1 <?php
6 $entity_guid = (int) get_input('entity_guid', 0, false);
7 $comment_guid = (int) get_input('comment_guid', 0, false);
8 $comment_text = get_input('generic_comment');
9 
10 if (empty($comment_text)) {
11  return elgg_error_response(elgg_echo('generic_comment:blank'));
12 }
13 
14 if ($comment_guid) {
15  // Edit an existing comment
17  if (!$comment instanceof \ElggComment) {
18  return elgg_error_response(elgg_echo('generic_comment:notfound'));
19  }
20 
21  if (!$comment->canEdit()) {
22  return elgg_error_response(elgg_echo('actionunauthorized'));
23  }
24 
25  $comment->description = $comment_text;
26  if (!$comment->save()) {
27  return elgg_error_response(elgg_echo('generic_comment:failure'));
28  }
29 
30  $success_message = elgg_echo('generic_comment:updated');
31 } else {
32  // Create a new comment on the target entity
34  if (!$entity instanceof \ElggEntity) {
35  return elgg_error_response(elgg_echo('generic_comment:notfound'));
36  }
37 
38  if (!$entity->canComment()) {
39  return elgg_error_response(elgg_echo('actionunauthorized'));
40  }
41 
42  $comment = new \ElggComment();
43  $comment->description = $comment_text;
44 
45  if ($entity instanceof \ElggComment) {
46  $comment->level = $entity->getLevel() + 1;
47  $comment->parent_guid = $entity->guid;
48  $comment->thread_guid = $entity->getThreadGUID();
49 
50  // make sure comment is contained in the content
51  $entity = $entity->getContainerEntity();
52  }
53 
54  $comment->container_guid = $entity->guid;
55  $comment->access_id = $entity->access_id;
56 
57  if (!$comment->save()) {
58  return elgg_error_response(elgg_echo('generic_comment:failure'));
59  }
60 
61  // only river for top level comments
62  if ($comment->getLevel() === 1) {
63  // Add to river
65  'view' => 'river/object/comment/create',
66  'action_type' => 'comment',
67  'object_guid' => $comment->guid,
68  'target_guid' => $entity->guid,
69  ]);
70  }
71 
72  $success_message = elgg_echo('generic_comment:posted');
73 }
74 
76 
77 // return to activity page if posted from there
78 // this can be removed once saving new comments is ajaxed
79 if (!empty($_SERVER['HTTP_REFERER'])) {
80  // don't redirect to URLs from client without verifying within site
81  $site_url = preg_quote(elgg_get_site_url(), '~');
82  if (preg_match("~^{$site_url}activity(/|\\z)~", $_SERVER['HTTP_REFERER'], $m)) {
83  $forward = "{$m[0]}#elgg-object-{$comment->guid}";
84  }
85 }
86 
88  'guid' => $comment->guid,
89  'output' => elgg_view_entity($comment),
90 ];
91 
$comment_text
Definition: save.php:8
elgg_ok_response($content= '', string|array $message= '', string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!empty($_SERVER['HTTP_REFERER'])) $result
Definition: save.php:87
elgg_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
$comment_guid
Definition: save.php:7
canEdit(int $user_guid=0)
Can a user edit this entity?
Definition: ElggEntity.php:397
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
elgg_error_response(string|array $message= '', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
if(!$comment->save()) if($comment->getLevel()===1) $success_message
Definition: save.php:72
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_view_entity(\ElggEntity $entity, array $vars=[])
Returns a string of a rendered entity.
Definition: views.php:493
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
$entity
Definition: save.php:17
$site_url
Definition: upgrade.php:3
$forward
Definition: save.php:75
getURL()
Gets the URL for this entity.
Definition: ElggEntity.php:573