Elgg  Version 6.1
Preloader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Comments;
4 
11 class Preloader {
12 
18  public function __construct(protected DataService $data) {
19  }
20 
28  public function preloadForList(array $items): void {
29  $guids = $this->getGuidsToPreload($items);
30 
32  }
33 
41  protected function preloadCountsFromQuery(array $guids): void {
42  if (empty($guids)) {
43  return;
44  }
45 
46  $count_rows = elgg_get_entities([
47  'type' => 'object',
48  'subtype' => 'comment',
49  'container_guids' => $guids,
50  'selects' => ['e.container_guid', 'COUNT(*) AS cnt'],
51  'group_by' => 'e.container_guid',
52  'limit' => false,
53  'callback' => false,
54  ]);
55 
56  foreach ($guids as $guid) {
57  $this->data->setCommentsCount($guid, 0);
58  }
59 
60  foreach ($count_rows as $row) {
61  $this->data->setCommentsCount($row->container_guid, $row->cnt);
62  }
63  }
64 
72  protected function getGuidsToPreload(array $items): array {
73  $guids = [];
74 
75  foreach ($items as $item) {
76  if ($item instanceof \ElggEntity) {
77  if ($item->hasCapability('commentable')) {
78  $guids[$item->guid] = true;
79  }
80  } elseif ($item instanceof \ElggRiverItem) {
81  $guids[$item->object_guid] = true;
82  }
83  }
84 
85  return $this->data->filterGuids(array_keys($guids));
86  }
87 
95  public static function preload(\Elgg\Event $event): void {
96  $vars = $event->getValue();
97 
98  $items = (array) elgg_extract('items', $vars, []);
99  if (!elgg_is_logged_in() || count($items) < 3) {
100  return;
101  }
102 
103  $preload = elgg_extract('preload_comments_count', $vars);
104  if (!isset($preload)) {
105  $list_class = elgg_extract('list_class', $vars);
106  $preload = !elgg_in_context('widgets') && in_array($list_class, ['elgg-list-river', 'elgg-list-entity', 'comments-list']);
107  }
108 
109  if (!$preload) {
110  return;
111  }
112 
113  $preloader = new self(\Elgg\Comments\DataService::instance());
114  $preloader->preloadForList($items);
115  }
116 }
if(!$items) $item
Definition: delete.php:13
Comments dataservice.
Definition: DataService.php:11
getGuidsToPreload(array $items)
Convert entities to guids.
Definition: Preloader.php:72
Comments preloader.
Definition: Preloader.php:11
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:64
static instance()
Get a DataService instance.
Definition: DataService.php:75
elgg_in_context(string $context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
static preload(\Elgg\Event $event)
Event handler for listings to determine if preloading is needed.
Definition: Preloader.php:95
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$items
Definition: delete.php:8
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:256
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
preloadCountsFromQuery(array $guids)
Preload comments count based on guids.
Definition: Preloader.php:41
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
preloadForList(array $items)
Preload comments count for a set of items.
Definition: Preloader.php:28
$vars
Definition: theme.php:5
__construct(protected DataService $data)
Create a preloader.
Definition: Preloader.php:18
Models an event passed to event handlers.
Definition: Event.php:11
$guid
Reset an ElggUpgrade.
Definition: reset.php:6