Elgg  Version 4.3
CommentEntityRedirector.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
8 use Elgg\Request;
9 
17 
26  public function __invoke(Request $request) {
27 
28  $comment_guid = (int) $request->getParam('guid');
29  $fallback_guid = (int) $request->getParam('container_guid');
30 
32  if (!$comment instanceof \ElggComment) {
33  // try fallback if given
34  $fallback = get_entity($fallback_guid);
35  if (!$fallback instanceof \ElggEntity) {
36  throw new EntityNotFoundException(elgg_echo('generic_comment:notfound'));
37  }
38 
39  return elgg_redirect_response($fallback->getURL());
40  }
41 
42  $top_comment = $comment->getThreadEntity();
43  if (!$top_comment instanceof \ElggComment) {
44  throw new EntityNotFoundException(elgg_echo('generic_comment:notfound'));
45  }
46 
47  $container = $comment->getContainerEntity();
48  if (!$container instanceof \ElggEntity) {
49  throw new EntityNotFoundException(elgg_echo('generic_comment:notfound'));
50  }
51 
52  $operator = elgg_comments_are_latest_first($container) ? '>' : '<';
53 
55  'type' => 'object',
56  'subtype' => 'comment',
57  'container_guid' => $container->guid,
58  'metadata_name_value_pairs' => ['level' => 1],
59  'wheres' => [
60  function(QueryBuilder $qb, $main_alias) use ($top_comment, $operator) {
61  return $qb->compare("{$main_alias}.guid", $operator, $top_comment->guid, ELGG_VALUE_GUID);
62  },
63  ],
64  ]);
65 
66  $limit = (int) get_input('limit');
67  if ($limit < 1) {
69  }
70 
71  $offset = floor($count / $limit) * $limit;
72  if ($offset < 1) {
73  $offset = null;
74  }
75 
77  'offset' => $offset,
78  ]);
79 
80  // make sure there's only one fragment (#)
81  $parts = parse_url($url);
82  $parts['fragment'] = "elgg-object-{$comment->guid}";
83  $url = elgg_http_build_url($parts, false);
84 
86  }
87 }
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:481
if(!$entity) if(!$entity->canComment()) $comment
Definition: save.php:42
$request
Definition: livesearch.php:11
const ELGG_VALUE_GUID
Definition: constants.php:128
if(!$count) $offset
Definition: pagination.php:26
Database abstraction query builder.
__invoke(Request $request)
Redirect to the comment in context of the containing page.
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:135
elgg_comments_per_page(ElggEntity $container=null)
How many comments appear per page.
if($pagination &&($position== 'after'||$position== 'both')) $limit
Definition: list.php:108
$comment_guid
Definition: save.php:7
getParam($key, $default=null, $filter=true)
Get an element of the params array.
Definition: Request.php:79
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
Redirect a comment url to the full view of the entity being commented on with the correct offset to s...
elgg_redirect_response($forward_url=REFERRER, int $status_code=ELGG_HTTP_FOUND)
Prepare a silent redirect response to be returned by a page or an action handler. ...
get_input($variable, $default=null, $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:556
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
$container
Definition: delete.php:23
$count
Definition: ban.php:24
elgg_http_build_url(array $parts, $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:431
Request container.
Definition: Request.php:12
Aggregate action for saving settings.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
$qb
Definition: queue.php:11
elgg_comments_are_latest_first(ElggEntity $container=null)
Are comments displayed with latest first?
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:69