ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjFileDefaultIconsObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\File\Icon;
22
26
31{
35 private const PATH_DEFAULT_ICON_DIR = __DIR__ . "/../../../../../public/assets/images/default_file_icons/";
36
37 public function __construct(
38 private bool $reset_default = false,
39 private bool $reset_all = false
40 ) {
41 }
42
46 public function getHash(): string
47 {
48 return hash(
49 "sha256",
50 static::class
51 );
52 }
53
57 public function getLabel(): string
58 {
59 return "Creation of the default icons for file objects.";
60 }
61
65 public function isNotable(): bool
66 {
67 return true;
68 }
69
73 public function getPreconditions(Environment $environment): array
74 {
75 return array_merge(
77 [
79 ]
80 );
81 }
82
86 public function achieve(Environment $environment): Environment
87 {
90 $environment
91 );
96
97 $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
98 $default_icon_filenames = preg_grep("/^icon_file_/", $scan_result);
99 foreach ($default_icon_filenames as $default_icon_filename) {
100 $icon_file_prefix = "icon_file_";
101 $icon_file_suffix = ".svg";
102 $suffix = str_replace($icon_file_prefix, "", $default_icon_filename);
103 $suffix = str_replace($icon_file_suffix, "", $suffix);
104
105 //check if there is an existing db entry for a default icon with this suffix
106 $query = "SELECT * FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " AS s"
107 . " INNER JOIN " . IconDatabaseRepository::ICON_TABLE_NAME . " AS i"
109 . " WHERE s." . IconDatabaseRepository::SUFFIX . " = %s AND i." . IconDatabaseRepository::IS_DEFAULT_ICON . " = %s";
110 $statement = $db->queryF(
111 $query,
112 ['text', 'integer'],
113 [$suffix, 1]
114 );
115 $num_matches = $db->numRows($statement);
116
117 //skip copying the file to the resource storage and creating the new db entries if this default icon has been added already
118 if ($num_matches > 0) {
119 continue;
120 }
121
122 $path_default_file_icon = self::PATH_DEFAULT_ICON_DIR . $default_icon_filename;
123
124 //move copy of default icon in temp dir to resource storage
125 $resource_identification = $helper->movePathToStorage(
126 $path_default_file_icon,
127 6,
128 null,
129 null,
130 true
131 );
132
133 if ($resource_identification === null) {
134 continue;
135 }
136 $rid = $resource_identification->serialize();
137 //create icon & icon suffix db entry for newly added default icon
138 $db->insert(
140 [
142 IconDatabaseRepository::SUFFIX => ['text', $suffix],
143 ]
144 );
145 $db->insert(
147 [
149 IconDatabaseRepository::ICON_ACTIVE => ['integer', true],
150 IconDatabaseRepository::IS_DEFAULT_ICON => ['integer', true]
151 ]
152 );
153 }
154
155 return $environment;
156 }
157
161 public function isApplicable(Environment $environment): bool
162 {
166 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
167
168 // this removes all icons from the database and therefore forces a re-creation of the default icons
169 if ($this->reset_all) {
171 $db->manipulate("DELETE FROM " . IconDatabaseRepository::ICON_TABLE_NAME);
172 }
174 $db->manipulate("DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME);
175 }
176 return true;
177 }
178
179 // this removes all default icons from the database and therefore forces a re-creation of the default icons but keeps custom icons
180 if ($this->reset_default && $db->tableExists(IconDatabaseRepository::ICON_TABLE_NAME)) {
181 // we can proceed if icon tables are not available
182 if (
185 ) {
186 return true;
187 }
188
189 // selects the rids of all default icons
190 $query = "SELECT rid FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
191 $statement = $db->query($query);
192 $rids = [];
193 while ($row = $db->fetchAssoc($statement)) {
195 }
196 // delete all default icons
197 $db->manipulate(
199 );
200
201 // now we deactivate all custom icons which override the default icons. therefore we must search for the suffixes related to the default icons.
202 // read all suffixes of the default icons
203 $query = "SELECT " . IconDatabaseRepository::SUFFIX . " FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
205 $rids,
206 false,
207 'text'
208 );
209 $statement = $db->query($query);
210 $suffixes = [];
211 while ($row = $db->fetchAssoc($statement)) {
212 $suffixes[] = $row[IconDatabaseRepository::SUFFIX];
213 }
214 // remove all entries of the default icons
215 $db->manipulate(
216 "DELETE FROM " . IconDatabaseRepository::SUFFIX_TABLE_NAME . " WHERE " . $db->in(
218 $rids,
219 false,
220 'text'
221 )
222 );
223
224 $rids = [];
225 if (!empty($suffixes)) {
226 // collect rids of custom icons which override the default icons
229 $suffixes,
230 false,
231 'text'
232 );
233 $statement = $db->query($query);
234
235 while ($row = $db->fetchAssoc($statement)) {
237 }
238 }
239
240 // deactivate all custom icons which override the default icons
241 if (!empty($rids)) {
242 $db->manipulate(
243 "UPDATE " . IconDatabaseRepository::ICON_TABLE_NAME . " SET " . IconDatabaseRepository::ICON_ACTIVE . " = 0 WHERE " . $db->in(
245 $rids,
246 false,
247 'text'
248 )
249 );
250 }
251
252 // clean up orphaned suffixes
253 $db->manipulate(
257 );
258
259 return true;
260 }
261
263 $scan_result = scandir(self::PATH_DEFAULT_ICON_DIR);
264 $num_default_icons_in_dir = is_countable(preg_grep("/^icon_file_/", $scan_result)) ? count(
265 preg_grep("/^icon_file_/", $scan_result)
266 ) : 0;
267
268 $query = "SELECT * FROM " . IconDatabaseRepository::ICON_TABLE_NAME . " WHERE " . IconDatabaseRepository::IS_DEFAULT_ICON . " = 1";
269 $statement = $db->query($query);
270 $num_default_icons_in_db = $db->numRows($statement);
271
272 if ($num_default_icons_in_db < $num_default_icons_in_dir) {
273 return true;
274 }
275 }
276 return false;
277 }
278}
__construct(private bool $reset_default=false, private bool $reset_all=false)
This class attempt to achieve a set of database update steps.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
achieve(Environment $environment)
Objectives can be achieved.