Elgg  Version 4.3
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) use ($show_guid, $latest_first) {
67  $operator = $latest_first ? '>' : '<';
68 
69  return $qb->compare('e.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  $form_vars = [
106  'id' => "elgg-form-comment-save-{$entity->guid}",
107  'prevent_double_submit' => false,
108  ];
109  if ($entity instanceof \ElggComment) {
110  $form_vars['class'] = 'hidden';
111  }
112 
113  if (!$entity instanceof \ElggComment && $latest_first && $comments_list && elgg_get_config('comment_box_collapses')) {
114  $form_vars['class'] = 'hidden';
115 
116  $module_vars['menu'] = elgg_view_menu('comments', [
117  'items' => [
118  [
119  'name' => 'add',
120  'text' => elgg_echo('generic_comments:add'),
121  'href' => '#' . $form_vars['id'],
122  'icon' => 'plus',
123  'class' => ['elgg-button', 'elgg-button-action', 'elgg-toggle'],
124  ],
125  ],
126  ]);
127  }
128 
129  $form = elgg_view_form('comment/save', $form_vars, $vars);
131  $login_form_contents = elgg_view_form('login', [], ['returntoreferer' => true]);
132 
133  $login_form = elgg_view('output/longtext', [
134  'value' => elgg_echo('generic_comment:login_required'),
135  ]);
136 
137  $login_form .= elgg_view_module('dropdown', '', $login_form_contents, ['id' => 'comments-login']);
138 
139  $menu = elgg_view('output/url', [
140  'href' => elgg_get_login_url([], '#comments-login'),
141  'text' => elgg_echo('login'),
142  'class' => ['elgg-button', 'elgg-button-action', 'elgg-popup'],
143  'data-position' => json_encode([
144  'my' => 'right top',
145  'at' => 'right bottom',
146  ]),
147  ]);
148 
149  $form = elgg_view_message('notice', $login_form, [
150  'menu' => $menu,
151  'class' => 'mtl',
152  ]);
153 }
154 
155 if ($latest_first || $entity instanceof \ElggComment) {
156  $content = $form . $content;
157 } else {
158  $content .= $form;
159 }
160 
161 if (empty($content)) {
162  return;
163 }
164 
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
$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
if(!isset($limit)) $options
Definition: comments.php:35
elgg_view_entity_list($entities, array $vars=[])
Returns a rendered list of entities with pagination.
Definition: views.php:661
$comments
if(!isset($limit)) $module_title
Definition: comments.php:50
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
Database abstraction query builder.
$menu
Admin sidebar – just outputs the page menus.
Definition: sidebar.php:6
$form
Definition: comments.php:99
if(!isset($limit)) $module_vars
Definition: comments.php:30
elgg_comments_per_page(ElggEntity $container=null)
How many comments appear per page.
$latest_first
Definition: comments.php:23
$entity
Definition: comments.php:16
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:569
$content
Definition: comments.php:98
elgg_view_menu($menu, array $vars=[])
Render a menu.
Definition: views.php:468
$comments_list
Definition: comments.php:96
elgg_view_message($type, $body, array $vars=[])
Wrapper function for the message display pattern.
Definition: views.php:965
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
elgg_view_form($action, $form_vars=[], $body_vars=[])
Definition: views.php:1064
get_input($variable, $default=null, $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_view_module($type, $title, $body, array $vars=[])
Wrapper function for the module display pattern.
Definition: views.php:944
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.
$count
Definition: ban.php:24
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:547
Extends QueryBuilder with ORDER BY clauses.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
elgg echo
Translates a string.
Definition: deprecated.js:530
$vars['head']
Definition: html.php:24
if(!$entity instanceof\ElggEntity) $show_add_form
Definition: comments.php:21
$limit
Definition: comments.php:25
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:179
$qb
Definition: queue.php:11
elgg_comments_are_latest_first(ElggEntity $container=null)
Are comments displayed with latest first?
var elgg
Definition: elgglib.js:4
elgg_get_login_url(array $query=[], $fragment= '')
Returns site&#39;s login URL Triggers a &#39;login_url&#39;, &#39;site&#39; plugin hook that can be used by plugins to al...
Definition: users.php:185
elgg_get_config($name, $default=null)
Get an Elgg configuration value.