Elgg  Version 1.12
comments.php
Go to the documentation of this file.
1 <?php
16 function _elgg_comments_init() {
17  elgg_register_entity_type('object', 'comment');
18  elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_comment_setup_entity_menu', 900);
19  elgg_register_plugin_hook_handler('entity:url', 'object', '_elgg_comment_url_handler');
20  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_comments_container_permissions_override');
21  elgg_register_plugin_hook_handler('permissions_check', 'object', '_elgg_comments_permissions_override');
22  elgg_register_plugin_hook_handler('email', 'system', '_elgg_comments_notification_email_subject');
23 
24  elgg_register_event_handler('update:after', 'all', '_elgg_comments_access_sync');
25 
26  elgg_register_page_handler('comment', '_elgg_comments_page_handler');
27 
28  elgg_register_ajax_view('core/ajax/edit_comment');
29 }
30 
38 function _elgg_comments_page_handler($page) {
39 
40  switch ($page[0]) {
41 
42  case 'edit':
44 
45  if (empty($page[1])) {
46  register_error(elgg_echo('generic_comment:notfound'));
48  }
49  $comment = get_entity($page[1]);
50  if (!($comment instanceof \ElggComment) || !$comment->canEdit()) {
51  register_error(elgg_echo('generic_comment:notfound'));
53  }
54 
55  $target = $comment->getContainerEntity();
56  if (!($target instanceof \ElggEntity)) {
57  register_error(elgg_echo('generic_comment:notfound'));
59  }
60 
61  $title = elgg_echo('generic_comments:edit');
62  elgg_push_breadcrumb($target->getDisplayName(), $target->getURL());
64 
65  $params = array(
66  'entity' => $target,
67  'comment' => $comment,
68  'is_edit_page' => true,
69  );
70  $content = elgg_view_form('comment/save', null, $params);
71 
72  $params = array(
73  'content' => $content,
74  'title' => $title,
75  'filter' => '',
76  );
77  $body = elgg_view_layout('content', $params);
79 
80  return true;
81  break;
82 
83  case 'view':
84  _elgg_comment_redirect(elgg_extract(1, $page), elgg_extract(2, $page));
85  break;
86 
87  default:
88  return false;
89  break;
90  }
91 }
92 
102 function _elgg_comment_redirect($comment_guid, $fallback_guid) {
103  $fail = function () {
104  register_error(elgg_echo('generic_comment:notfound'));
105  forward(REFERER);
106  };
107 
109  if (!$comment) {
110  // try fallback if given
111  $fallback = get_entity($fallback_guid);
112  if (!$fallback) {
113  $fail();
114  }
115 
116  register_error(elgg_echo('generic_comment:notfound_fallback'));
117  forward($fallback->getURL());
118  }
119 
120  if (!elgg_instanceof($comment, 'object', 'comment')) {
121  $fail();
122  }
123 
124  $container = $comment->getContainerEntity();
125  if (!$container) {
126  $fail();
127  }
128 
129  // this won't work with threaded comments, but core doesn't support that yet
131  'type' => 'object',
132  'subtype' => 'comment',
133  'container_guid' => $container->guid,
134  'count' => true,
135  'wheres' => ["e.guid < " . (int)$comment->guid],
136  ]);
137  $limit = (int)get_input('limit');
138  if (!$limit) {
139  $limit = elgg_trigger_plugin_hook('config', 'comments_per_page', [], 25);
140  }
141  $offset = floor($count / $limit) * $limit;
142  if (!$offset) {
143  $offset = null;
144  }
145 
147  'offset' => $offset,
148  ]) . "#elgg-object-{$comment->guid}";
149 
150  forward($url);
151 }
152 
165  if (elgg_in_context('widgets')) {
166  return $return;
167  }
168 
169  $entity = $params['entity'];
170  if (!elgg_instanceof($entity, 'object', 'comment')) {
171  return $return;
172  }
173 
174  // Remove edit link and access level from the menu
175  foreach ($return as $key => $item) {
176  if ($item->getName() === 'access') {
177  unset($return[$key]);
178  }
179  }
180 
181  return $return;
182 }
183 
198  $entity = $params['entity'];
199  /* @var \ElggObject $entity */
200 
201  if (!elgg_instanceof($entity, 'object', 'comment') || !$entity->getOwnerEntity()) {
202  // not a comment or has no owner
203 
204  // @todo handle anonymous comments
205  return $return;
206  }
207 
208  $container = $entity->getContainerEntity();
209  if (!$container) {
210  return $return;
211  }
212 
213  return "comment/view/{$entity->guid}/{$container->guid}";
214 }
215 
232 
233  // is someone trying to comment, if so override permissions check
234  if ($params['subtype'] === 'comment') {
235  return true;
236  }
237 
238  return $return;
239 }
240 
252  $entity = $params['entity'];
253  $user = $params['user'];
254 
255  if (elgg_instanceof($entity, 'object', 'comment') && $user) {
256  return $entity->getOwnerGUID() == $user->getGUID();
257  }
258 
259  return $return;
260 }
261 
277 function _elgg_comments_notification_email_subject($hook, $type, $returnvalue, $params) {
278  if (!is_array($returnvalue)) {
279  // another hook handler returned a non-array, let's not override it
280  return;
281  }
282 
283  if (empty($returnvalue['params']['notification'])) {
284  return;
285  }
286 
288  $notification = $returnvalue['params']['notification'];
289 
290  if ($notification instanceof Elgg\Notifications\Notification) {
291  $object = elgg_extract('object', $notification->params);
292 
293  if ($object instanceof ElggComment) {
294  $container = $object->getContainerEntity();
295 
296  $returnvalue['subject'] = 'Re: ' . $container->getDisplayName();
297  }
298  }
299 
300  return $returnvalue;
301 }
302 
314  if (!($entity instanceof \ElggEntity)) {
315  return true;
316  }
317 
318  // need to override access in case comments ended up with ACCESS_PRIVATE
319  // and to ensure write permissions
320  $ia = elgg_set_ignore_access(true);
321  $options = array(
322  'type' => 'object',
323  'subtype' => 'comment',
324  'container_guid' => $entity->getGUID(),
325  'wheres' => array(
326  "e.access_id != {$entity->access_id}"
327  ),
328  'limit' => 0,
329  );
330 
331  $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false);
332  foreach ($batch as $comment) {
333  // Update comment access_id
334  $comment->access_id = $entity->access_id;
335  $comment->save();
336  }
337 
339 
340  return true;
341 }
342 
355  global $CONFIG;
356  $value[] = "{$CONFIG->path}engine/tests/ElggCommentTest.php";
357  return $value;
358 }
359 
360 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
361  $events->registerHandler('init', 'system', '_elgg_comments_init');
362  $hooks->registerHandler('unit_test', 'system', '_elgg_comments_test');
363 };
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1132
elgg_push_breadcrumb($title, $link=null)
Adds a breadcrumb to the breadcrumbs stack.
Definition: navigation.php:248
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
_elgg_comments_container_permissions_override($hook, $type, $return, $params)
Allow users to comment on entities not owned by them.
Definition: comments.php:231
_elgg_comment_url_handler($hook, $type, $return, $params)
Format and return the URL for a comment.
Definition: comments.php:197
$object
Definition: upgrade.php:12
$content
Definition: comments.php:31
$value
Definition: longtext.php:26
$ia
Definition: upgrade.php:26
$return
Definition: opendd.php:15
if(!$count) $offset
Definition: pagination.php:26
_elgg_comments_page_handler($page)
Page handler for generic comments manipulation.
Definition: comments.php:38
_elgg_comments_permissions_override($hook, $type, $return, $params)
By default, only authors can edit their comments.
Definition: comments.php:251
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:408
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:717
$comment
Definition: delete.php:10
$url
Definition: exceptions.php:24
$title
Definition: save.php:22
_elgg_comments_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: comments.php:354
$params
Definition: login.php:72
$target
Definition: create.php:11
$limit
Definition: comments.php:17
_elgg_comments_access_sync($event, $type, $entity)
Update comment access to match that of the container.
Definition: comments.php:313
elgg_gatekeeper()
Used at the top of a page to mark it as logged in users only.
Definition: pagehandler.php:58
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:926
Save menu items.
if($entity) $container
Definition: access.php:62
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$key
Definition: summary.php:34
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
const REFERER
Definition: elgglib.php:2034
$item
Definition: item.php:12
global $CONFIG
$user
Definition: ban.php:13
_elgg_comments_init()
Comments initialization function.
Definition: comments.php:16
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:244
elgg echo
Translates a string.
Definition: languages.js:48
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:494
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:790
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
$options
RSS comments view.
Definition: comments.php:8
_elgg_comment_setup_entity_menu($hook, $type, $return, $params)
Setup the menu shown with a comment.
Definition: comments.php:164
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
_elgg_comment_redirect($comment_guid, $fallback_guid)
Redirect to the comment in context of the containing page.
Definition: comments.php:102
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1271
$comment_guid
Definition: delete.php:9
elgg_view_layout($layout_name, $vars=array())
Displays a layout with optional parameters.
Definition: views.php:608
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:533
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:382
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:208
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
elgg_view_page($title, $body, $page_shell= 'default', $vars=array())
Assembles and outputs a full page.
Definition: views.php:423
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:715
$entity
Definition: delete.php:10
elgg_view_form($action, $form_vars=array(), $body_vars=array())
Definition: views.php:1298
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382