ILIAS  release_8 Revision v8.24
class.ilSystemStyleIcon.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
29 protected string $path = '';
30
34 protected string $name = '';
35
39 protected string $type = '';
40
45
46 public function __construct(string $name, string $path, string $type)
47 {
48 $this->setName($name);
49 $this->setType($type);
50 $this->setPath($path);
51 }
52
56 public function changeColors(array $color_changes): void
57 {
58 if ($this->getType() == 'svg') {
59 $icon = file_get_contents($this->getPath());
60 foreach ($color_changes as $old_color => $new_color) {
61 $icon = preg_replace('/#' . $old_color . '/i', '#' . $new_color, $icon, -1);
62 }
63 file_put_contents($this->getPath(), $icon);
64 }
65 $this->extractColorSet();
66 }
67
68 public function getType(): string
69 {
70 return $this->type;
71 }
72
73 public function setType(string $type): void
74 {
75 $this->type = $type;
76 }
77
78 public function getName(): string
79 {
80 return $this->name;
81 }
82
83 public function setName(string $name): void
84 {
85 $this->name = $name;
86 }
87
88 public function __toString(): string
89 {
90 return $this->getName();
91 }
92
93 public function getPath(): string
94 {
95 return $this->path;
96 }
97
98 public function setPath(string $path): void
99 {
100 $this->path = $path;
101 }
102
107 public function getDirRelToCustomizing(): string
108 {
109 $path = strstr($this->getPath(), 'global/skin');
110 if (!$path) {
111 return '';
112 }
113 return dirname($path);
114 }
115
117 {
118 if (!isset($this->color_set)) {
119 $this->extractColorSet();
120 }
121 return $this->color_set;
122 }
123
127 protected function extractColorSet(): void
128 {
129 $regex_for_extracting_color = '/((?<=#)[\dabcdef]{6})|((?<=#)[\dabcdef]{3})/i';
130
131 $this->color_set = new ilSystemStyleIconColorSet();
132 if ($this->getType() == 'svg') {
133 $icon_content = file_get_contents($this->getPath());
134 $color_matches = [];
135 preg_match_all($regex_for_extracting_color, $icon_content, $color_matches);
136 if (is_array($color_matches) && is_array($color_matches[0])) {
137 foreach ($color_matches[0] as $color_value) {
138 $numeric = strtoupper(str_replace('#', '', $color_value));
139 $color = new ilSystemStyleIconColor('id_' . $numeric, $color_value, $numeric, $color_value);
140 $this->getColorSet()->addColor($color);
141 }
142 }
143 }
144 }
145
147 {
148 $this->color_set = $color_set;
149 }
150
151 public function usesColor(string $color_id): bool
152 {
153 return $this->getColorSet()->doesColorExist($color_id);
154 }
155}
Abstracts an Icon and the necessary actions to get all colors out of an svg Icon.
__construct(string $name, string $path, string $type)
string $type
Extension of the icon.
getDirRelToCustomizing()
Only get dir rel to the Customizing dir without name and extension from.
string $name
Name of the Icon.
usesColor(string $color_id)
setColorSet(ilSystemStyleIconColorSet $color_set)
ilSystemStyleIconColorSet $color_set
Color set extracted from the icon.
extractColorSet()
Extracts all colors from the icon by parsing the svg file for a regular expresion.
changeColors(array $color_changes)
Changes colors in the svg file of the icon and updates the icon abstraction by extracting the colors ...
string $path
Path to the icon including name and extension.