Elgg  Version 7.1
save.php
Go to the documentation of this file.
1 <?php
6 use Elgg\Values;
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 
12 if (empty($comment_text)) {
13  return elgg_error_response(elgg_echo('generic_comment:blank'));
14 }
15 
16 if ($comment_guid) {
17  // Edit an existing comment
19  if (!$comment instanceof \ElggComment) {
20  return elgg_error_response(elgg_echo('generic_comment:notfound'));
21  }
22 
23  if (!$comment->canEdit()) {
24  return elgg_error_response(elgg_echo('actionunauthorized'));
25  }
26 
27  $comment->description = $comment_text;
28  if (!$comment->save()) {
29  return elgg_error_response(elgg_echo('generic_comment:failure'));
30  }
31 
33  $success_message = elgg_echo('generic_comment:updated');
34 } else {
35  // Create a new comment on the target entity
37  if (!$entity instanceof \ElggEntity) {
38  return elgg_error_response(elgg_echo('generic_comment:notfound'));
39  }
40 
41  if (!$entity->canComment()) {
42  return elgg_error_response(elgg_echo('actionunauthorized'));
43  }
44 
45  $comment = new \ElggComment();
46  $comment->description = $comment_text;
47 
48  if ($entity instanceof \ElggComment) {
49  $comment->level = $entity->getLevel() + 1;
50  $comment->parent_guid = $entity->guid;
51  $comment->thread_guid = $entity->getThreadGUID();
52 
53  // make sure comment is contained in the content
54  $entity = $entity->getContainerEntity();
55  }
56 
57  $comment->container_guid = $entity->guid;
58  $comment->access_id = $entity->access_id;
59 
60  if (!$comment->save()) {
61  return elgg_error_response(elgg_echo('generic_comment:failure'));
62  }
63 
64  // only river for top level comments
65  if ($comment->getLevel() === 1) {
66  // Add to river
68  'view' => 'river/object/comment/create',
69  'action_type' => 'comment',
70  'object_guid' => $comment->guid,
71  'target_guid' => $entity->guid,
72  ]);
73  }
74 
75  $success_message = elgg_echo('generic_comment:posted');
76 }
77 
78 $comment_count = $entity->countComments();
79 
81  'guid' => $comment->guid,
82  'output' => elgg_view_entity($comment),
83  'container_guid' => $entity->guid,
84  'container_comment_count' => $comment_count,
85  'container_comment_badge_text' => $comment_count ? Values::shortFormatOutput($comment_count, 1) : null,
86 ];
87 
$result
Definition: save.php:80
$comment_text
Definition: save.php:10
if(! $entity instanceof \ElggEntity) if(! $entity->canComment()) $comment
Definition: save.php:45
if(! $comment->save()) if($comment->getLevel()===1) $success_message
Definition: save.php:75
$comment_guid
Definition: save.php:9
$entity_guid
Definition: save.php:8
$comment_count
Definition: save.php:78
canEdit(int $user_guid=0)
Can a user edit this entity?
Definition: ElggEntity.php:397
getContainerEntity()
Get the container entity for this object.
Definition: ElggEntity.php:552
save()
Save this data to the appropriate database table.bool
Definition: ElggEntity.php:588
getURL()
Gets the URL for this entity.
Definition: ElggEntity.php:573
Functions for use as event handlers or other situations where you need a globally accessible callable...
Definition: Values.php:12
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
elgg_view_entity(\ElggEntity $entity, array $vars=[])
Returns a string of a rendered entity.
Definition: views.php:508
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_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.
elgg_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
$entity
Definition: save.php:17