Elgg  Version 4.3
MoveIconsOnOwnerChangeHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Icons;
4 
11 
26  public function __invoke(\Elgg\Event $event) {
27  $entity = $event->getObject();
28  if (!$entity instanceof \ElggEntity) {
29  return;
30  }
31 
32  $original_attributes = $entity->getOriginalAttributes();
33  if (empty($original_attributes['owner_guid'])) {
34  return;
35  }
36 
37  $previous_owner_guid = $original_attributes['owner_guid'];
38  $new_owner_guid = $entity->owner_guid;
39 
40  $sizes = array_keys(elgg_get_icon_sizes($entity->getType(), $entity->getSubtype()));
41  foreach ($sizes as $size) {
42  // using the Icon Service because we don't want to auto generate the 'new' icon
43  $new_icon = _elgg_services()->iconService->getIcon($entity, $size, 'icon', false);
44  if ($new_icon->owner_guid == $entity->guid) {
45  // we do not need to update icons that are owned by the entity itself
46  continue;
47  }
48 
49  if ($new_icon->owner_guid != $new_owner_guid) {
50  // a plugin implements some custom logic
51  continue;
52  }
53 
54  $old_icon = new \ElggIcon();
55  $old_icon->owner_guid = $previous_owner_guid;
56  $old_icon->setFilename($new_icon->getFilename());
57  if (!$old_icon->exists()) {
58  // there is no icon to move
59  continue;
60  }
61 
62  if ($new_icon->exists()) {
63  // there is already a new icon
64  // just removing the old one
65  $old_icon->delete();
66  elgg_log("Entity $entity->guid has been transferred to a new owner but an icon was "
67  . "left behind under {$old_icon->getFilenameOnFilestore()}. "
68  . "Old icon has been deleted", 'NOTICE');
69  continue;
70  }
71 
72  $old_icon->transfer($new_icon->owner_guid, $new_icon->getFilename());
73  elgg_log("Entity $entity->guid has been transferred to a new owner. "
74  . "Icon was moved from {$old_icon->getFilenameOnFilestore()} to {$new_icon->getFilenameOnFilestore()}.", 'NOTICE');
75  }
76  }
77 }
elgg_get_icon_sizes($entity_type=null, $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
__invoke(\Elgg\Event $event)
Listen to entity ownership changes and update icon ownership by moving icons to their new owner&#39;s dir...
$entity
Definition: reset.php:8
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
Models an event passed to event handlers.
Definition: Event.php:11
$size
Definition: thumb.php:23
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638