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