Elgg  Version 3.0
save.php
Go to the documentation of this file.
1 <?php
9 $entity_guid = (int) get_input('entity_guid', 0, false);
10 $comment_guid = (int) get_input('comment_guid', 0, false);
11 $comment_text = get_input('generic_comment');
12 
13 if (empty($comment_text)) {
14  return elgg_error_response(elgg_echo('generic_comment:blank'));
15 }
16 
17 if ($comment_guid) {
18  // Edit an existing comment
20 
21  if (!$comment instanceof ElggComment) {
22  return elgg_error_response(elgg_echo('generic_comment:notfound'));
23  }
24  if (!$comment->canEdit()) {
25  return elgg_error_response(elgg_echo('actionunauthorized'));
26  }
27 
28  $comment->description = $comment_text;
29  if (!$comment->save()) {
30  return elgg_error_response(elgg_echo('generic_comment:failure'));
31  }
32 
33  $success_message = elgg_echo('generic_comment:updated');
34 } else {
35  // Create a new comment on the target entity
37  if (!$entity) {
38  return elgg_error_response(elgg_echo('generic_comment:notfound'));
39  }
40 
42 
43  $comment = new ElggComment();
44  $comment->description = $comment_text;
45  $comment->owner_guid = $user->getGUID();
46  $comment->container_guid = $entity->getGUID();
47  $comment->access_id = $entity->access_id;
48  $guid = $comment->save();
49 
50  if (!$guid) {
51  return elgg_error_response(elgg_echo('generic_comment:failure'));
52  }
53 
54  // Add to river
56  'view' => 'river/object/comment/create',
57  'action_type' => 'comment',
58  'object_guid' => $guid,
59  'target_guid' => $entity_guid,
60  ]);
61 
62  $success_message = elgg_echo('generic_comment:posted');
63 }
64 
65 $forward = $comment->getURL();
66 
67 // return to activity page if posted from there
68 // this can be removed once saving new comments is ajaxed
69 if (!empty($_SERVER['HTTP_REFERER'])) {
70  // don't redirect to URLs from client without verifying within site
71  $site_url = preg_quote(elgg_get_site_url(), '~');
72  if (preg_match("~^{$site_url}activity(/|\\z)~", $_SERVER['HTTP_REFERER'], $m)) {
73  $forward = "{$m[0]}#elgg-object-{$comment->guid}";
74  }
75 }
76 
78  'guid' => $comment->guid,
79  'output' => elgg_view_entity($comment),
80 ];
81 
$comment_text
Definition: save.php:11
$guid
Definition: save.php:48
if(!empty($_SERVER['HTTP_REFERER'])) $result
Definition: save.php:77
elgg_create_river_item(array $options=[])
Adds an item to the river.
Definition: river.php:33
$comment
Definition: save.php:43
$comment_guid
Definition: save.php:10
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$entity_guid
Definition: save.php:9
if(!$entity) $user
Definition: save.php:41
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_view_entity(\ElggEntity $entity, array $vars=[])
Returns a string of a rendered entity.
Definition: views.php:842
elgg_error_response($error= '', $forward_url=REFERRER, $status_code=ELGG_HTTP_OK)
Prepare an error response to be returned by a page or an action handler.
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
$success_message
Definition: save.php:62
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
$entity
Definition: save.php:18
$site_url
Definition: upgrade.php:3
$forward
Definition: save.php:65
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:29
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87