Elgg  Version master
comments.php
Go to the documentation of this file.
1 <?php
15 
16 $entity = elgg_extract('entity', $vars);
17 if (!$entity instanceof \ElggEntity) {
18  return;
19 }
20 
21 $show_add_form = elgg_extract('show_add_form', $vars, true);
22 
24 
25 $limit = elgg_extract('limit', $vars, get_input('limit'));
26 if (!isset($limit)) {
28 }
29 
31  'id' => elgg_extract('id', $vars, 'comments'),
32  'class' => elgg_extract_class($vars, 'elgg-comments'),
33 ];
34 
36  'type' => 'object',
37  'subtype' => 'comment',
38  'container_guid' => $entity->guid,
39  'full_view' => true,
40  'limit' => $limit,
41  'offset' => (int) get_input('offset'),
42  'distinct' => false,
43  'url_fragment' => $module_vars['id'],
44  'order_by' => [new OrderByClause('e.guid', $latest_first ? 'DESC' : 'ASC')],
45  'list_class' => 'comments-list',
46  'pagination' => true,
47  'preload_owners' => true,
48 ];
49 
51 
52 if (!$entity instanceof \ElggComment) {
53  $module_title = elgg_echo('comments');
54 
55  $options['metadata_name_value_pairs'] = ['level' => 1];
56 
57  $show_guid = (int) elgg_extract('show_guid', $vars);
58  if ($show_guid && $limit) {
59  // show the offset that includes the comment
61  'type' => 'object',
62  'subtype' => 'comment',
63  'container_guid' => $entity->guid,
64  'metadata_name_value_pairs' => ['level' => 1],
65  'wheres' => [
66  function(QueryBuilder $qb, $main_alias) use ($show_guid, $latest_first) {
67  $operator = $latest_first ? '>' : '<';
68 
69  return $qb->compare("{$main_alias}.guid", $operator, $show_guid, ELGG_VALUE_INTEGER);
70  },
71  ],
72  ]);
73  $options['offset'] = (int) floor($count / $limit) * $limit;
74  }
75 
77 
78  $count_options = $options;
79  unset($count_options['offset']);
80  $options['count'] = elgg_count_entities($count_options);
81 
82  // preload comment threads
83  elgg()->thread_preloader->preloadThreads($comments);
84 } else {
85  $comments = elgg()->thread_preloader->getChildren($entity->guid);
86 
87  // load children of thread
88  $options['limit'] = false;
89  $options['offset'] = 0;
90  $options['pagination'] = false;
91  $options['count'] = count($comments);
92 
93  $module_vars['header'] = false;
94 }
95 
97 
99 $form = '';
100 
102 $show_login_form = elgg_extract('show_login_form', $vars, $show_login_form);
103 
104 if ($show_add_form && $entity->canComment()) {
105  if ($entity instanceof \ElggComment) {
106  $form = elgg_format_element('div', [
107  'id' => "elgg-form-comment-save-{$entity->guid}",
108  'data-comments-placeholder' => $entity->guid,
109  'class' => 'hidden',
110  ]);
111  } else {
112  $form_vars = [
113  'id' => "elgg-form-comment-save-{$entity->guid}",
114  'prevent_double_submit' => false,
115  ];
116 
117  if (!$entity instanceof \ElggComment && $latest_first && $comments_list && elgg_get_config('comment_box_collapses')) {
118  $form_vars['class'] = 'hidden';
119 
120  $module_vars['menu'] = elgg_view_menu('comments', [
121  'items' => [
122  [
123  'name' => 'add',
124  'text' => elgg_echo('generic_comments:add'),
125  'href' => '#' . $form_vars['id'],
126  'icon' => 'plus',
127  'class' => ['elgg-button', 'elgg-button-action', 'elgg-toggle'],
128  ],
129  ],
130  ]);
131  }
132 
133  $form = elgg_view_form('comment/save', $form_vars, $vars);
134  }
136  $login_form_contents = elgg_view_form('login', [], ['returntoreferer' => true]);
137 
138  $login_form = elgg_view('output/longtext', [
139  'value' => elgg_echo('generic_comment:login_required'),
140  ]);
141 
142  $login_form .= elgg_view_module('dropdown', '', $login_form_contents, ['id' => 'comments-login']);
143 
144  $menu = elgg_view('output/url', [
145  'href' => elgg_get_login_url([], '#comments-login'),
146  'text' => elgg_echo('login'),
147  'class' => ['elgg-button', 'elgg-button-action', 'elgg-popup'],
148  'data-position' => json_encode([
149  'my' => 'right top',
150  'at' => 'right bottom',
151  ]),
152  ]);
153 
154  $form = elgg_view_message('notice', $login_form, [
155  'menu' => $menu,
156  'class' => 'mtl',
157  ]);
158 }
159 
160 if ($latest_first || $entity instanceof \ElggComment) {
161  $content = $form . $content;
162 } else {
163  $content .= $form;
164 }
165 
166 if (empty($content)) {
167  return;
168 }
169 
elgg
Definition: install.js:27
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
elgg_view_module(string $type, string $title, string $body, array $vars=[])
Wrapper function for the module display pattern.
Definition: views.php:919
$show_login_form
Definition: comments.php:101
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
elgg_view_form(string $action, array $form_vars=[], array $body_vars=[])
Definition: views.php:1033
if(!isset($limit)) $options
Definition: comments.php:35
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
elgg_get_login_url(array $query=[], string $fragment= '')
Returns site&#39;s login URL Triggers a &#39;login_url&#39;, &#39;site&#39; event that can be used by plugins to alter th...
Definition: users.php:228
if(!isset($limit)) $module_title
Definition: comments.php:50
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
Database abstraction query builder.
elgg_comments_per_page(\ElggEntity $container=null)
How many comments appear per page.
$form
Definition: comments.php:99
if(!isset($limit)) $module_vars
Definition: comments.php:30
$latest_first
Definition: comments.php:23
$entity
Definition: comments.php:16
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$content
Definition: comments.php:98
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
elgg_view_menu($menu, array $vars=[])
Render a menu.
Definition: views.php:441
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
$comments_list
Definition: comments.php:96
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
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
$count
Definition: ban.php:24
$menu
Form body for setting up site menu.
Definition: save.php:7
Extends QueryBuilder with ORDER BY clauses.
elgg_view_entity_list(array $entities, array $vars=[])
Returns a rendered list of entities with pagination.
Definition: views.php:636
elgg_comments_are_latest_first(\ElggEntity $container=null)
Are comments displayed with latest first?
$vars
Definition: theme.php:5
elgg_view_message(string $type, string $body, array $vars=[])
Wrapper function for the message display pattern.
Definition: views.php:940
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:277
if($owner_guid) if($container_guid) if($subtypes) $comments
if(!$entity instanceof\ElggEntity) $show_add_form
Definition: comments.php:21
$limit
Definition: comments.php:25
$qb
Definition: queue.php:12