Elgg  Version master
ThreadPreloaderService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Comments;
4 
6 
13 
17  protected $children;
18 
26  public function preloadThreads(array $comments): void {
27  if (empty($comments)) {
28  return;
29  }
30 
31  $this->children = [];
32 
33  $guids = [];
34  $container_guid = 0;
35  foreach ($comments as $comment) {
36  $guids[] = $comment->guid;
37  $container_guid = $comment->container_guid;
38  }
39 
41  'type' => 'object',
42  'subtype' => 'comment',
43  'limit' => false,
44  'batch' => true,
45  'container_guid' => $container_guid,
46  'metadata_name_value_pairs' => [
47  'thread_guid' => $guids,
48  ],
49  'order_by' => new OrderByClause('e.time_created', 'ASC'),
50  'preload_owners' => true,
51  ]);
52 
53  /* @var $comment \ElggComment */
54  foreach ($batch as $comment) {
55  $parent_guid = (int) $comment->parent_guid;
56  if (!isset($this->children[$parent_guid])) {
57  $this->children[$parent_guid] = [];
58  }
59 
60  $this->children[$parent_guid][] = $comment;
61  }
62  }
63 
71  public function getChildren(int $comment_guid): array {
72  if (!isset($this->children)) {
74  if ($comment instanceof \ElggComment) {
75  $this->preloadThreads([$comment->getThreadEntity()]);
76  }
77  }
78 
79  return elgg_extract($comment_guid, $this->children, []);
80  }
81 }
if(! $entity instanceof \ElggEntity) if(! $entity->canComment()) $comment
Definition: save.php:42
$comment_guid
Definition: save.php:7
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
preloadThreads(array $comments)
Preload the comment threads for the given comments.
getChildren(int $comment_guid)
Get the children of a comment.
Extends QueryBuilder with ORDER BY clauses.
if($owner_guid) if($container_guid) if($subtypes) $comments
$container_guid
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
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
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