Elgg  Version master
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 
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_redirect_response(string $forward_url=REFERRER, int $status_code=ELGG_HTTP_FOUND, bool $secure_forward_url=true)
Prepare a silent redirect response to be returned by a page or an action handler. ...
$request
Definition: livesearch.php:12
if(empty($count)) $offset
Definition: pagination.php:26
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
const ELGG_VALUE_GUID
Definition: constants.php:113
Database abstraction query builder.
elgg_comments_per_page(\ElggEntity $container=null)
How many comments appear per page.
__invoke(Request $request)
Redirect to the comment in context of the containing page.
$comment_guid
Definition: save.php:7
getParam($key, $default=null, $filter=true)
Get an element of the params array.
Definition: Request.php:67
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
Redirect a comment url to the full view of the entity being commented on with the correct offset to s...
$limit
Definition: pagination.php:28
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
elgg_http_build_url(array $parts, bool $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:131
$container
Definition: delete.php:23
$count
Definition: ban.php:24
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:181
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
elgg_comments_are_latest_first(\ElggEntity $container=null)
Are comments displayed with latest first?
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
getContainerEntity()
Get the container entity for this object.
$qb
Definition: queue.php:12