Elgg  Version master
Icons.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Entity;
4 
7 
13 trait Icons {
14 
24  public function saveIconFromUploadedFile(string $input_name, string $type = 'icon', array $coords = []): bool {
25  return _elgg_services()->iconService->saveIconFromUploadedFile($this, $input_name, $type, $coords);
26  }
27 
37  public function saveIconFromLocalFile(string $filename, string $type = 'icon', array $coords = []): bool {
38  return _elgg_services()->iconService->saveIconFromLocalFile($this, $filename, $type, $coords);
39  }
40 
50  public function saveIconFromElggFile(\ElggFile $file, string $type = 'icon', array $coords = []): bool {
51  return _elgg_services()->iconService->saveIconFromElggFile($this, $file, $type, $coords);
52  }
53 
63  public function getIcon(string $size, string $type = 'icon'): \ElggIcon {
64  return _elgg_services()->iconService->getIcon($this, $size, $type);
65  }
66 
74  public function deleteIcon(string $type = 'icon'): bool {
75  return _elgg_services()->iconService->deleteIcon($this, $type);
76  }
77 
86  public function getIconLastChange(string $size, string $type = 'icon'): ?int {
87  return _elgg_services()->iconService->getIconLastChange($this, $size, $type);
88  }
89 
98  public function hasIcon(string $size, string $type = 'icon'): bool {
99  return _elgg_services()->iconService->hasIcon($this, $size, $type);
100  }
101 
114  public function getIconURL(string|array $params = []): string {
115  return _elgg_services()->iconService->getIconURL($this, $params);
116  }
117 
127  public function saveIconCoordinates(array $coords, string $type = 'icon'): void {
128  // remove noise from the coords array
129  $allowed_keys = ['x1', 'x2', 'y1', 'y2'];
130  $coords = array_filter($coords, function($value, $key) use ($allowed_keys) {
131  return in_array($key, $allowed_keys) && is_int($value);
132  }, ARRAY_FILTER_USE_BOTH);
133 
134  if (!isset($coords['x1']) || !isset($coords['x2']) || !isset($coords['y1']) || !isset($coords['y2'])) {
135  throw new InvalidArgumentException('Please provide correct coordinates [x1, x2, y1, y2]');
136  }
137 
138  if ($coords['x1'] < 0 || $coords['x2'] < 0 || $coords['y1'] < 0 || $coords['y2'] < 0) {
139  throw new RangeException("Coordinates can't have negative numbers");
140  }
141 
142  $this->{"{$type}_coords"} = serialize($coords);
143  }
144 
153  public function getIconCoordinates(string $type = 'icon'): array {
154  if (!isset($this->{"{$type}_coords"})) {
155  return [];
156  }
157 
158  $coords = unserialize($this->{"{$type}_coords"}) ?: [];
159 
160  // cast to integers
161  array_walk($coords, function(&$value) {
162  $value = (int) $value;
163  });
164 
165  // remove invalid values
166  return array_filter($coords, function($value) {
167  return $value >= 0;
168  });
169  }
170 
179  public function removeIconCoordinates(string $type = 'icon'): void {
180  unset($this->{"{$type}_coords"});
181  }
182 
192  public function lockIconThumbnailGeneration(string $type = 'icon'): void {
193  $this->{"{$type}_thumbnail_locked"} = time();
194  }
195 
206  public function isIconThumbnailGenerationLocked(string $type = 'icon', int $ttl = 30): bool {
207  if (!isset($this->{"{$type}_thumbnail_locked"})) {
208  return false;
209  }
210 
211  $locked = (int) $this->{"{$type}_thumbnail_locked"};
212  return $locked > (time() - $ttl);
213  }
214 
224  public function unlockIconThumbnailGeneration(string $type = 'icon'): void {
225  unset($this->{"{$type}_thumbnail_locked"});
226  }
227 }
$input_name
Definition: crop.php:24
$params
Saves global plugin settings.
Definition: save.php:13
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
$type
Definition: delete.php:21
$value
Definition: generic.php:51
$size
Definition: thumb.php:23
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
Entity icon class.
Definition: ElggIcon.php:8
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351