Elgg  Version master
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 {
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 }
$vars
Definition: theme.php:5
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
if(! $items) $item
Definition: delete.php:13
$items
Definition: delete.php:8
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
if(! $entity instanceof \ElggUser) $data
Definition: attributes.php:13
if(!empty($options) &&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:64
Comments dataservice.
Definition: DataService.php:11
static instance()
Get a DataService instance.
Definition: DataService.php:75
Comments preloader.
Definition: Preloader.php:11
getGuidsToPreload(array $items)
Convert entities to guids.
Definition: Preloader.php:72
preloadForList(array $items)
Preload comments count for a set of items.
Definition: Preloader.php:28
__construct(protected DataService $data)
Create a preloader.
Definition: Preloader.php:18
static preload(\Elgg\Event $event)
Event handler for listings to determine if preloading is needed.
Definition: Preloader.php:95
preloadCountsFromQuery(array $guids)
Preload comments count based on guids.
Definition: Preloader.php:41
Models an event passed to event handlers.
Definition: Event.php:11
elgg_in_context(string $context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
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
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43