Elgg  Version 4.3
Preloader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Comments;
4 
11 class Preloader {
12 
16  protected $data;
17 
23  public function __construct(DataService $data) {
24  $this->data = $data;
25  }
26 
34  public function preloadForList(array $items): void {
35  $guids = $this->getGuidsToPreload($items);
36 
38  }
39 
47  protected function preloadCountsFromQuery(array $guids): void {
48  if (empty($guids)) {
49  return;
50  }
51 
52  $count_rows = elgg_get_entities([
53  'type' => 'object',
54  'subtype' => 'comment',
55  'container_guids' => $guids,
56  'selects' => ['e.container_guid', 'COUNT(*) AS cnt'],
57  'group_by' => 'e.container_guid',
58  'limit' => false,
59  'callback' => false,
60  ]);
61 
62  foreach ($guids as $guid) {
63  $this->data->setCommentsCount($guid, 0);
64  }
65  foreach ($count_rows as $row) {
66  $this->data->setCommentsCount($row->container_guid, $row->cnt);
67  }
68  }
69 
77  protected function getGuidsToPreload(array $items): array {
78  $guids = [];
79 
80  foreach ($items as $item) {
81  if ($item instanceof \ElggEntity) {
82  if ($item->hasCapability('commentable')) {
83  $guids[$item->guid] = true;
84  }
85  } elseif ($item instanceof \ElggRiverItem) {
86  $guids[$item->object_guid] = true;
87  }
88  }
89 
90  return $this->data->filterGuids(array_keys($guids));
91  }
92 
100  public static function preload(\Elgg\Hook $hook): void {
101  $vars = $hook->getValue();
102 
103  $items = (array) elgg_extract('items', $vars, []);
104  if (!elgg_is_logged_in() || count($items) < 3) {
105  return;
106  }
107 
108  $preload = elgg_extract('preload_comments_count', $vars);
109  if (!isset($preload)) {
110  $list_class = elgg_extract('list_class', $vars);
111  $preload = !elgg_in_context('widgets') && in_array($list_class, ['elgg-list-river', 'elgg-list-entity', 'comments-list']);
112  }
113 
114  if (!$preload) {
115  return;
116  }
117 
118  $preloader = new self(\Elgg\Comments\DataService::instance());
119  $preloader->preloadForList($items);
120  }
121 }
if(!$items) $item
Definition: delete.php:13
Comments dataservice.
Definition: DataService.php:11
getGuidsToPreload(array $items)
Convert entities to guids.
Definition: Preloader.php:77
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
static preload(\Elgg\Hook $hook)
Hook handler for listings to determine if preloading is needed.
Definition: Preloader.php:100
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
__construct(DataService $data)
Create a preloader.
Definition: Preloader.php:23
$items
Definition: delete.php:8
Models an event passed to hook handlers.
Definition: Hook.php:11
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
preloadCountsFromQuery(array $guids)
Preload comments count based on guids.
Definition: Preloader.php:47
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
$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:34
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$vars['head']
Definition: html.php:24
$guid
Reset an ElggUpgrade.
Definition: reset.php:6