ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjFileIconsOverviewGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\File\Icon;
22 
28 {
29  public const CMD_INDEX = 'index';
30  public const CMD_OPEN_CREATION_FORM = 'openCreationForm';
31  public const CMD_OPEN_UPDATING_FORM = 'openUpdatingForm';
32  public const CMD_CHANGE_ACTIVATION = 'changeActivation';
33  public const CMD_CREATE = 'create';
34  public const CMD_UPDATE = 'update';
35  public const CMD_DELETE = 'delete';
36  public const P_RID = 'rid';
37 
38  private \ilCtrl $ctrl;
39  private \ilLanguage $lng;
40  private \ilToolbarGUI $toolbar;
41  private \ILIAS\UI\Factory $ui_factory;
42  private \ILIAS\UI\Renderer $ui_renderer;
43  private \ilGlobalTemplateInterface $main_tpl;
44  private \ILIAS\HTTP\Wrapper\WrapperFactory $wrapper;
45  private \Psr\Http\Message\RequestInterface $http_request;
46  private \ILIAS\Refinery\Factory $refinery;
47  private \ILIAS\ResourceStorage\Services $storage;
49  private \ilFileServicesSettings $file_service_settings;
50 
51  final public function __construct()
52  {
53  global $DIC;
54  $this->ctrl = $DIC->ctrl();
55  $this->lng = $DIC->language();
56  $this->lng->loadLanguageModule('file');
57  $this->toolbar = $DIC->toolbar();
58  $this->main_tpl = $DIC->ui()->mainTemplate();
59  $this->ui_factory = $DIC->ui()->factory();
60  $this->ui_renderer = $DIC->ui()->renderer();
61  $this->http_request = $DIC->http()->request();
62  $this->wrapper = $DIC->http()->wrapper();
63  $this->refinery = $DIC->refinery();
64  $this->storage = $DIC->resourceStorage();
65  $this->icon_repo = new IconDatabaseRepository();
66  $this->file_service_settings = $DIC->fileServiceSettings();
67  }
68 
69  final public function executeCommand(): void
70  {
71  $next_class = $this->ctrl->getNextClass(self::class);
72  if ($next_class === strtolower(ilIconUploadHandlerGUI::class)) {
73  $upload_handler = new ilIconUploadHandlerGUI();
74  $this->ctrl->forwardCommand($upload_handler);
75  }
76 
77  match ($cmd = $this->ctrl->getCmd(self::CMD_INDEX)) {
78  self::CMD_OPEN_CREATION_FORM => $this->openCreationForm(),
79  self::CMD_OPEN_UPDATING_FORM => $this->openUpdatingForm(),
80  self::CMD_CHANGE_ACTIVATION => $this->changeActivation(),
81  self::CMD_CREATE => $this->create(),
82  self::CMD_UPDATE => $this->update(),
83  self::CMD_DELETE => $this->delete(),
84  default => $this->index(),
85  };
86  }
87 
88  private function index(): void
89  {
90  // toolbar: add new icon button
91  $btn_new_icon = $this->ui_factory->button()->standard(
92  $this->lng->txt('add_icon'),
93  $this->ctrl->getLinkTargetByClass(self::class, self::CMD_OPEN_CREATION_FORM)
94  );
95  $this->toolbar->addComponent($btn_new_icon);
96 
97  // Listing of icons
98  $listing = new IconListingUI(
99  $this->icon_repo,
100  $this
101  );
102 
103  $content = [];
104  $content[] = $listing->getFilter();
105  $content[] = $listing->getIconList();
106  $content[] = $listing->getDeletionModals();
107 
108  $this->main_tpl->setContent(
109  $this->ui_renderer->render($content)
110  );
111  }
112 
113  private function openCreationForm(): void
114  {
115  $icon_form_ui = new IconFormUI(new NullIcon('', true), IconFormUI::MODE_CREATE, $this->icon_repo);
116  $icon_form = $icon_form_ui->getIconForm();
117  $this->main_tpl->setContent(
118  $this->ui_renderer->render($icon_form)
119  );
120  }
121 
122  private function openUpdatingForm(): void
123  {
124  $to_str = $this->refinery->to()->string();
125  $rid = $this->wrapper->query()->has(self::P_RID) ? $rid = $this->wrapper->query()->retrieve(
126  self::P_RID,
127  $to_str
128  ) : "";
129  $this->ctrl->setParameter($this, self::P_RID, $rid); //store rid for giving icon to form in update function
130  $icon = $this->icon_repo->getIconByRid($rid);
131  $icon_form_ui = new IconFormUI($icon, IconFormUI::MODE_EDIT, $this->icon_repo);
132  $icon_form = $icon_form_ui->getIconForm();
133  $this->main_tpl->setContent(
134  $this->ui_renderer->render($icon_form)
135  );
136  }
137 
138  public function changeActivation(): void
139  {
140  $to_str = $this->refinery->to()->string();
141  $rid = $this->wrapper->query()->has(self::P_RID) ? $rid = $this->wrapper->query()->retrieve(
142  self::P_RID,
143  $to_str
144  ) : "";
145  $icon = $this->icon_repo->getIconByRid($rid);
146  $suffixes = $icon->getSuffixes();
147  $icon->isActive();
148  $is_default_icon = $icon->isDefaultIcon();
149 
150  if (!$icon->isActive()) {
151  // in case of a change from deactivated to activated no two icons with overlapping suffixes must be active at the same time
152  if ($this->icon_repo->causesNoActiveSuffixesConflict($suffixes, true, $icon)) {
153  $this->icon_repo->updateIcon($rid, true, $is_default_icon, $suffixes);
154 
155  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_icon_activated'), true);
156  } else {
157  $this->main_tpl->setOnScreenMessage(
158  'failure',
159  $this->lng->txt('msg_error_active_suffixes_conflict'),
160  true
161  );
162  }
163  } else {
164  $this->icon_repo->updateIcon($rid, false, $is_default_icon, $suffixes);
165  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_icon_deactivated'), true);
166  }
167 
168  $this->ctrl->redirect($this, self::CMD_INDEX);
169  }
170 
171  public function create(): void
172  {
173  $ui = new IconFormUI(new NullIcon(), IconFormUI::MODE_CREATE, $this->icon_repo);
174  $form = $ui->getIconForm();
175 
176  if ($this->http_request->getMethod() === "POST") {
177  $form = $form->withRequest($this->http_request);
178  $result = $form->getData();
179 
180  if ($result !== null) {
181  $rid = $result[0][IconFormUI::INPUT_ICON][0];
182  $active = $result[0][IconFormUI::INPUT_ACTIVE];
183  $suffixes = $result[0][IconFormUI::INPUT_SUFFIXES];
184 
185  $this->icon_repo->createIcon($rid, $active, false, $suffixes);
186 
187  // check if one of the suffixes is not whitelisted
188  if (array_diff($suffixes, $this->file_service_settings->getWhiteListedSuffixes()) !== []) {
189  $this->main_tpl->setOnScreenMessage(
190  'info',
191  $this->lng->txt('msg_error_active_suffixes_not_whitelisted'),
192  true
193  );
194  }
195 
196  // check if one of the suffixes is blacklisted
197  if (array_intersect($suffixes, $this->file_service_settings->getBlackListedSuffixes()) !== []) {
198  $this->main_tpl->setOnScreenMessage(
199  'info',
200  $this->lng->txt('msg_error_active_suffixes_blacklisted'),
201  true
202  );
203  }
204 
205  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_icon_created'), true);
206  $this->ctrl->redirect($this, self::CMD_INDEX);
207  } else {
208  $this->main_tpl->setContent(
209  $this->ui_renderer->render([$form])
210  );
211  }
212  }
213  }
214 
215  public function update(): void
216  {
217  $to_str = $this->refinery->to()->string();
218  $rid = $this->wrapper->query()->has(self::P_RID) ? $rid = $this->wrapper->query()->retrieve(
219  self::P_RID,
220  $to_str
221  ) : "";
222  $this->ctrl->saveParameter(
223  $this,
224  self::P_RID
225  ); //save rid to still have it when re-submitting a previously wrongly filled out form
226  $icon = $this->icon_repo->getIconByRid($rid);
227  $ui = new IconFormUI($icon, IconFormUI::MODE_EDIT, $this->icon_repo);
228  $form = $ui->getIconForm();
229 
230  if ($this->http_request->getMethod() === "POST") {
231  $form = $form->withRequest($this->http_request);
232  $result = $form->getData();
233 
234  if ($result !== null) {
235  $rid = $result[0][IconFormUI::INPUT_ICON][0];
236  $active = $result[0][IconFormUI::INPUT_ACTIVE];
237  $suffixes = $result[0][IconFormUI::INPUT_SUFFIXES];
238 
239  $this->icon_repo->updateIcon($rid, $active, false, $suffixes);
240  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_icon_updated'), true);
241  $this->ctrl->redirect($this, self::CMD_INDEX);
242  } else {
243  $this->main_tpl->setContent(
244  $this->ui_renderer->render([$form])
245  );
246  }
247  }
248  }
249 
250  public function delete(): void
251  {
252  $to_str = $this->refinery->to()->string();
253  $rid = $this->wrapper->query()->has(self::P_RID) ? $rid = $this->wrapper->query()->retrieve(
254  self::P_RID,
255  $to_str
256  ) : "";
257 
258  // delete icon from irss
259  $is_deleted_from_irss = false;
260  $id = $this->storage->manage()->find($rid);
261  if ($id instanceof \ILIAS\ResourceStorage\Identification\ResourceIdentification) {
262  $this->storage->manage()->remove($id, new ilObjFileIconStakeholder());
263  $is_deleted_from_irss = true;
264  }
265  // delete icon from db
266  $is_deleted_from_db = $this->icon_repo->deleteIconByRid($rid);
267 
268  if ($is_deleted_from_irss && $is_deleted_from_db) {
269  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_icon_deletion'), true);
270  } elseif ($is_deleted_from_irss) {
271  $this->main_tpl->setOnScreenMessage(
272  'failure',
273  $this->lng->txt('msg_error_icon_deletion') . " " . $this->lng->txt('msg_icon_missing_from_db'),
274  true
275  );
276  } elseif ($is_deleted_from_db) {
277  $this->main_tpl->setOnScreenMessage(
278  'failure',
279  $this->lng->txt('msg_error_icon_deletion') . " " . $this->lng->txt('msg_icon_missing_from_irss'),
280  true
281  );
282  } else {
283  $this->main_tpl->setOnScreenMessage(
284  'failure',
285  $this->lng->txt('msg_error_icon_deletion') . " " . $this->lng->txt(
286  'msg_icon_missing_from_db'
287  ) . " " . $this->lng->txt('msg_icon_missing_from_irss'),
288  true
289  );
290  }
291  $this->ctrl->redirect($this, self::CMD_INDEX);
292  }
293 }
Class ChatMainBarProvider .
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23