Elgg  Version 5.1
SyncContainerAccessHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Comments;
4 
11 
19  public function __invoke(\Elgg\Event $event) {
20  $entity = $event->getObject();
21  if (!$entity instanceof \ElggEntity) {
22  return;
23  }
24 
25  // only need update query if access_id of entity has been changed
26  if (!array_key_exists('access_id', $entity->getOriginalAttributes())) {
27  return;
28  }
29 
30  // need to override access in case comments ended up with ACCESS_PRIVATE
31  // and to ensure write permissions
32  elgg_call(ELGG_IGNORE_ACCESS, function() use ($entity) {
34  'type' => 'object',
35  'subtype' => 'comment',
36  'container_guid' => $entity->guid,
37  'wheres' => [
38  function(\Elgg\Database\QueryBuilder $qb, $main_alias) use ($entity) {
39  return $qb->compare("{$main_alias}.access_id", '!=', $entity->access_id, ELGG_VALUE_INTEGER);
40  },
41  ],
42  'limit' => false,
43  'batch' => true,
44  'batch_inc_offset' => false,
45  ]);
46 
47  foreach ($comments as $comment) {
48  // Update comment access_id
49  $comment->access_id = $entity->access_id;
50  $comment->save();
51  }
52  });
53  }
54 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:299
$comments
Keeps comments access in sync with container access.
The Elgg database.
Definition: Database.php:25
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
__invoke(\Elgg\Event $event)
Update comment access to match that of the container.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
$qb
Definition: queue.php:11
Models an event passed to event handlers.
Definition: Event.php:11