ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSystemStyleIconFolder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
30  protected array $icons = [];
31 
35  protected string $path = '';
36 
41 
45  public function __construct(string $path)
46  {
47  $this->setPath($path);
48  $this->read();
49  }
50 
55  public function read(): void
56  {
57  $this->readIconsFromFolder($this->getPath());
58  $this->sortIcons();
59  }
60 
64  protected function sortIcons(): void
65  {
66  usort($this->icons, [$this, 'compareIconsByName']);
67  }
68 
70  {
71  if ($a->getType() == $b->getType()) {
72  return strcmp($a->getName(), $b->getName());
73  } elseif ($a->getType() == 'svg') {
74  return -1;
75  } elseif ($b->getType() == 'svg') {
76  return 1;
77  } else {
78  return strcmp($a->getType(), $b->getType());
79  }
80  }
81 
82  public function sortIconsByPath(): void
83  {
84  usort($this->icons, static function (ilSystemStyleIcon $a, ilSystemStyleIcon $b): int {
85  return strcmp($a->getPath(), $b->getPath());
86  });
87  }
88 
93  protected function readIconsFromFolder(string $src = ''): void
94  {
95  try {
96  $dir_iterator = new RecursiveDirectoryIterator($src);
97  } catch (UnexpectedValueException $e) {
99  }
100 
101  $rec_it = new RecursiveIteratorIterator($dir_iterator);
102 
103  foreach ($rec_it as $file) {
104  if (!$file->isReadable()) {
106  }
107  if ($file->isFile()) {
108  $extension = $file->getExtension();
109  if ($extension == 'gif' || $extension == 'svg' || $extension == 'png') {
110  $this->addIcon(new ilSystemStyleIcon($file->getFilename(), $file->getPathname(), $extension));
111  }
112  }
113  }
114  }
115 
119  public function changeIconColors(array $color_changes): void
120  {
121  foreach ($this->getIcons() as $icon) {
122  $icon->changeColors($color_changes);
123  }
124  }
125 
129  public function addIcon(ilSystemStyleIcon $icon): void
130  {
131  $this->icons[] = $icon;
132  }
133 
138  public function getIcons(): array
139  {
140  return $this->icons;
141  }
142 
146  public function getIconByName(string $name): ilSystemStyleIcon
147  {
148  foreach ($this->icons as $icon) {
149  if ($icon->getName() == $name) {
150  return $icon;
151  }
152  }
154  }
155 
159  public function getIconByPath(string $path): ilSystemStyleIcon
160  {
161  foreach ($this->icons as $icon) {
162  if ($icon->getPath() == $path) {
163  return $icon;
164  }
165  }
167  }
168 
173  public function getIconsSortedByFolder(): array
174  {
175  $folders = [];
176 
177  foreach ($this->getIcons() as $icon) {
178  $folders[dirname($icon->getPath())][] = $icon;
179  }
180 
181  ksort($folders);
182 
183  foreach ($folders as $id => $folder) {
184  ksort($folders[$id]);
185  }
186 
187  return $folders;
188  }
189 
193  public function setIcons(array $icons): void
194  {
195  $this->icons = $icons;
196  }
197 
198  public function getPath(): string
199  {
200  return $this->path;
201  }
202 
203  public function setPath(string $path): void
204  {
205  $this->path = $path;
206  }
207 
209  {
210  if (!isset($this->color_set)) {
211  $this->extractColorSet();
212  }
213  return $this->color_set;
214  }
215 
219  protected function extractColorSet(): void
220  {
221  $this->color_set = new ilSystemStyleIconColorSet();
222  foreach ($this->getIcons() as $icon) {
223  $this->color_set->mergeColorSet($icon->getColorSet());
224  }
225  }
226 
231  public function getUsagesOfColor(string $color_id): array
232  {
233  $icons = [];
234  foreach ($this->getIcons() as $icon) {
235  if ($icon->usesColor($color_id)) {
236  $icons[] = $icon;
237  }
238  }
239  return $icons;
240  }
241 
245  public function getUsagesOfColorAsString(string $color_id): string
246  {
247  $usage_string = '';
248  foreach ($this->getUsagesOfColor($color_id) as $icon) {
249  $usage_string .= rtrim($icon->getName(), '.svg') . '; ';
250  }
251  return $usage_string;
252  }
253 
254  public function setColorSet(ilSystemStyleIconColorSet $color_set): void
255  {
256  $this->color_set = $color_set;
257  }
258 }
Class for advanced editing exception handling in ILIAS.
sortIcons()
Sorts the Icons by name and type.
setColorSet(ilSystemStyleIconColorSet $color_set)
getUsagesOfColor(string $color_id)
Gets the usages of a certain color.
addIcon(ilSystemStyleIcon $icon)
Adds an icon to the folders abstraction.
extractColorSet()
Gets the color sets of all icons an merges them into one.
getIconsSortedByFolder()
Sorts all icons by their occurrence in folders.
getIcons()
Gets an Icon from the folders abstraction.
Abstracts an Icon and the necessary actions to get all colors out of an svg Icon. ...
if($format !==null) $name
Definition: metadata.php:247
string $path
Path to the root of the folder.
changeIconColors(array $color_changes)
Changes a set of colors in all icons contained in the folder.
__construct(string $path)
ilSystemStyleIconFolder constructor.
compareIconsByName(ilSystemStyleIcon $a, ilSystemStyleIcon $b)
ilSystemStyleIconColorSet $color_set
Complete color set of all icons contained in this folder.
Abstracts a folder containing a set of icons.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
read()
Reads the folder recursively and sorts the icons by name and type.
getUsagesOfColorAsString(string $color_id)
Gets the usages of a color as string.