ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjStudyProgrammeAutoCategoriesGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31
40{
41 private const F_CATEGORY_REF = 'f_cr';
42 private const F_CATEGORY_ORIGINAL_REF = 'f_cr_org';
43 public const CHECKBOX_CATEGORY_REF_IDS = 'c_catids';
44
45 private const CMD_VIEW = 'view';
46 private const CMD_SAVE = 'save';
47 public const CMD_GET_ASYNC_MODAL = 'getAsyncModalOutput';
48 private const CMD_DELETE = 'delete';
49 private const CMD_DELETE_CONFIRMATION = 'deleteConfirmation';
50 private const CMD_PROFILE_NOT_PUBLIC = 'profile_not_public';
51
53 public ilCtrl $ctrl;
56 public ?int $prg_ref_id;
62 protected Psr\Http\Message\ServerRequestInterface $request;
63 protected ilTree $tree;
66
67
68 public function __construct(
70 ilCtrl $ilCtrl,
71 ilToolbarGUI $ilToolbar,
77 Psr\Http\Message\ServerRequestInterface $request,
79 ILIAS\HTTP\Wrapper\RequestWrapper $request_wrapper,
81 ) {
82 $this->tpl = $tpl;
83 $this->ctrl = $ilCtrl;
84 $this->toolbar = $ilToolbar;
85 $this->lng = $lng;
86 $this->ui_factory = $ui_factory;
87 $this->message_box_factory = $message_box_factory;
88 $this->button_factory = $button_factory;
89 $this->ui_renderer = $ui_renderer;
90 $this->request = $request;
91 $this->tree = $tree;
92 $this->request_wrapper = $request_wrapper;
93 $this->refinery = $refinery;
94 }
95
96 public function executeCommand(): void
97 {
98 $cmd = $this->ctrl->getCmd();
99 $next_class = $this->ctrl->getNextClass($this);
100
101 switch ($next_class) {
102 case "ilpropertyformgui":
103 $form = $this->getForm();
104 $this->ctrl->forwardCommand($form);
105 break;
106 default:
107 switch ($cmd) {
108 case self::CMD_VIEW:
109 case self::CMD_DELETE:
112 $this->$cmd();
113 break;
114 case self::CMD_SAVE:
115 $this->$cmd();
116 $this->ctrl->redirect($this, 'view');
117 break;
119 $this->view(true);
120 break;
121 default:
122 throw new ilException("ilObjStudyProgrammeAutoCategoriesGUI: Command not supported: $cmd");
123 }
124 }
125 }
126
130 protected function view(bool $profile_not_public = false): void
131 {
132 if ($profile_not_public) {
133 $this->tpl->setOnScreenMessage("info", $this->lng->txt('prg_profile_not_public'));
134 }
135
136 $collected_modals = [];
137
138 $modal = $this->getModal();
139 $this->getToolbar($modal->getShowSignal());
140 $collected_modals[] = $modal;
141
142 $data = [];
143 foreach ($this->getObject()->getAutomaticContentCategories() as $ac) {
144 $ref_id = $ac->getCategoryRefId();
145 if (ilObject::_lookupType($ref_id, true) !== 'cat' || $this->tree->isDeleted($ref_id)) {
146 continue;
147 }
148 [$title, $link] = $this->getItemPath($ref_id);
149 $usr = $this->getUserRepresentation($ac->getLastEditorId()) ?? $this->ui_factory->legacy()->content('-');
150 $modal = $this->getModal($ref_id);
151 $collected_modals[] = $modal;
152 $actions = $this->getItemAction(
153 $ac->getCategoryRefId(),
154 $modal->getShowSignal()
155 );
156
157 $data[] = [
158 $ac,
159 $this->ui_renderer->render($link),
160 $this->ui_renderer->render($usr),
161 $this->ui_renderer->render($actions),
162 $title
163 ];
164 }
165 usort($data, static function (array $a, array $b): int {
166 return strnatcasecmp($a[4], $b[4]);
167 });
168
169 $table = new ilStudyProgrammeAutoCategoriesTableGUI($this, "view", "");
170 $table->setData($data);
171
172 $this->tpl->setContent(
173 $this->ui_renderer->render($collected_modals)
174 . $table->getHTML()
175 );
176 }
177
181 protected function save(): void
182 {
183 $form = $this->getForm();
184 $form->setValuesByPost();
185 $form->checkInput();
186
187 $cat_ref_id = $form->getInput(self::F_CATEGORY_REF);
188 $current_ref_id = $form->getInput(self::F_CATEGORY_ORIGINAL_REF);
189
190 if (ilObject::_lookupType((int) $cat_ref_id, true) !== 'cat') {
191 $this->tpl->setOnScreenMessage(
192 "failure",
193 sprintf($this->lng->txt('not_a_valid_cat_id'), $cat_ref_id),
194 true
195 );
196 return;
197 }
198
199 if (!is_null($current_ref_id) && $current_ref_id !== $cat_ref_id) {
200 $ids = [(int) $current_ref_id];
201 $this->getObject()->deleteAutomaticContentCategories($ids);
202 }
203
204 $this->getObject()->storeAutomaticContentCategory((int) $cat_ref_id);
205 }
206
207 protected function deleteConfirmation(): void
208 {
209 $get = $this->request->getQueryParams();
210 $post = $this->request->getParsedBody();
212
213 $field_ids_in_get = array_key_exists($field, $get);
214 $field_ids_in_post = array_key_exists($field, $post);
215
216 $msg = '';
217 $cat_ids = '';
218 if ($field_ids_in_get) {
219 $cat_ids = $get[$field];
220 $msg = $this->lng->txt('prg_delete_single_confirmation');
221 } elseif ($field_ids_in_post) {
222 $cat_ids = implode(' ', $post[$field]);
223 $msg = $this->lng->txt('prg_delete_confirmation');
224 } else {
225 $this->tpl->setOnScreenMessage("info", $this->lng->txt('prg_delete_nothing_selected'), true);
226 $this->ctrl->redirect($this, self::CMD_VIEW);
227 }
228
229 $cat_ids = base64_encode($cat_ids);
230
231 $this->ctrl->setParameterByClass(self::class, $field, $cat_ids);
232 $delete = $this->ctrl->getFormActionByClass(self::class, self::CMD_DELETE);
233 $cancel = $this->ctrl->getFormActionByClass(self::class, self::CMD_VIEW);
234 $this->ctrl->clearParameterByClass(self::class, $field);
235
236 $buttons = [
237 $this->button_factory->standard($this->lng->txt('prg_confirm_delete'), $delete),
238 $this->button_factory->standard($this->lng->txt('prg_cancel'), $cancel)
239 ];
240
241 $message_box = $this->message_box_factory->confirmation($msg)->withButtons($buttons);
242
243 $this->tpl->setContent($this->ui_renderer->render($message_box));
244 }
245
246 protected function delete(): void
247 {
249 $get = $this->request->getQueryParams();
250
251 if (!array_key_exists($field, $get)) {
252 $this->tpl->setOnScreenMessage("failure", $this->lng->txt('prg_delete_failure'), true);
253 $this->ctrl->redirect($this, self::CMD_VIEW);
254 }
255
256 $cat_ids = base64_decode($get[$field]);
257 $cat_ids = explode(' ', trim($cat_ids));
258 $cat_ids = array_map('intval', $cat_ids);
259
260 $this->getObject()->deleteAutomaticContentCategories($cat_ids);
261
262 $msg = $this->lng->txt('prg_delete_single_success');
263 if (count($cat_ids) > 1) {
264 $msg = $this->lng->txt('prg_delete_success');
265 }
266
267 $this->tpl->setOnScreenMessage("success", $msg, true);
268 $this->ctrl->redirect($this, self::CMD_VIEW);
269 }
270
274 public function setRefId(int $prg_ref_id): void
275 {
276 $this->prg_ref_id = $prg_ref_id;
277 }
278
282 protected function getObject(): ilObjStudyProgramme
283 {
284 if ($this->object === null ||
285 $this->object->getRefId() !== $this->prg_ref_id
286 ) {
287 $this->object = ilObjStudyProgramme::getInstanceByRefId($this->prg_ref_id);
288 }
289 return $this->object;
290 }
291
292 protected function getModal(?int $current_ref_id = null): RoundTrip
293 {
294 if (!is_null($current_ref_id)) {
295 $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, (string) $current_ref_id);
296 }
297 $link = $this->ctrl->getLinkTarget($this, self::CMD_GET_ASYNC_MODAL, "", true);
298 $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, null);
299 return $this->ui_factory->modal()->roundtrip(
300 '',
301 []
302 )->withAsyncRenderUrl(
303 $link
304 );
305 }
306
307 protected function getAsyncModalOutput(): void
308 {
309 $current_ref_id = null;
310 if ($this->request_wrapper->has(self::CHECKBOX_CATEGORY_REF_IDS)) {
311 $current_ref_id = $this->request_wrapper->retrieve(
312 self::CHECKBOX_CATEGORY_REF_IDS,
313 $this->refinery->kindlyTo()->int()
314 );
315 }
316 $form = $this->getForm($current_ref_id);
317 $form_id = "form_" . $form->getId();
318 $submit = $this->ui_factory->button()->primary($this->lng->txt('add'), "#")->withOnLoadCode(
319 function ($id) use ($form_id) {
320 return "$('#$id').click(function() { document.getElementById('$form_id').submit(); return false; });";
321 }
322 );
323 $modal = $this->ui_factory->modal()
324 ->roundtrip(
325 $this->lng->txt('modal_categories_title'),
326 $this->ui_factory->legacy()->content($form->getHtml())
327 )
328 ->withActionButtons([$submit])
329 ->withAdditionalOnLoadCode(
330 function ($id) use ($form) {
331 $selector_post_var = self::F_CATEGORY_REF;
332 $js = $form->getItemByPostVar($selector_post_var)->getOnloadCode();
333 $js[] = "
334 document.getElementById('$id').onclose = ()=> {
335 let smodal = document.querySelectorAll('body dialog')
336 .item(document.querySelectorAll('body dialog').length-1);
337 smodal?.remove();
338 }
339 ";
340 return implode(';', $js);
341 }
342 );
343
344 echo $this->ui_renderer->renderAsync($modal);
345 exit;
346 }
347
348 protected function getForm(?int $current_ref_id = null): ilPropertyFormGUI
349 {
350 $form = new ilPropertyFormGUI();
351
352 $form->setId(uniqid((string) $current_ref_id, true));
353
354 $form->setFormAction($this->ctrl->getFormAction($this, "save"));
356 $this->lng->txt("category"),
357 self::F_CATEGORY_REF,
358 false
359 );
360 $cat->getExplorerGUI()->setSelectableTypes(["cat"]);
361 $cat->getExplorerGUI()->setTypeWhiteList(["root", "cat"]);
362 if ($current_ref_id !== null) {
363 $cat->getExplorerGUI()->setPathOpen($current_ref_id);
364 $cat->setValue($current_ref_id);
365 }
366 $cat->getExplorerGUI()->setRootId(ROOT_FOLDER_ID);
367 $cat->getExplorerGUI()->setAjax(true);
368 $form->addItem($cat);
369
370 $hi = new ilHiddenInputGUI(self::F_CATEGORY_ORIGINAL_REF);
371 $hi->setValue((string) $current_ref_id ?? "");
372 $form->addItem($hi);
373
374 return $form;
375 }
376
380 protected function getToolbar(Signal $add_cat_signal): void
381 {
382 $btn = $this->ui_factory->button()->primary($this->lng->txt('add_category'), '')
383 ->withOnClick($add_cat_signal);
384 $this->toolbar->addComponent($btn);
385 }
386
387 protected function getItemAction(
388 int $cat_ref_id,
389 Signal $signal
390 ): Standard {
391 $items = [];
392 $items[] = $this->ui_factory
393 ->button()
394 ->shy($this->lng->txt('edit'), '')
395 ->withOnClick($signal)
396 ;
397
398 $this->ctrl->setParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS, $cat_ref_id);
399 $link = $this->ctrl->getLinkTarget($this, self::CMD_DELETE_CONFIRMATION);
400 $this->ctrl->clearParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS);
401
402 $items[] = $this->ui_factory
403 ->button()
404 ->shy($this->lng->txt('delete'), $link)
405 ;
406
407 return $this->ui_factory->dropdown()->standard($items);
408 }
409
410 protected function getUserRepresentation(int $usr_id): ?Link
411 {
412 $username = ilObjUser::_lookupName($usr_id);
413 if (array_filter($username) === []) {
414 return null;
415 }
416
417 $editor = implode(' ', [
418 $username['firstname'],
419 $username['lastname'],
420 '(' . $username['login'] . ')'
421 ]);
422
423 $back_url = $this->ctrl->getLinkTarget($this, self::CMD_VIEW);
424 $this->ctrl->setParameterByClass(PublicProfileGUI::class, 'back_url', urlencode($back_url));
425 $this->ctrl->setParameterByClass(PublicProfileGUI::class, 'user_id', $usr_id);
426 $url = $this->ctrl->getLinkTargetByClass(PublicProfileGUI::class, 'view');
427
429 if (!$usr->hasPublicProfile()) {
430 $url = $this->ctrl->getLinkTarget($this, self::CMD_PROFILE_NOT_PUBLIC);
431 }
432 return $this->ui_factory->link()->standard($editor, $url);
433 }
434
435 protected function getItemPath(int $cat_ref_id): array
436 {
437 $url = ilLink::_getStaticLink($cat_ref_id, 'cat');
438
439 $hops = array_map(
440 static function (array $c): string {
441 return ilObject::_lookupTitle((int) $c["obj_id"]);
442 },
443 $this->tree->getPathFull($cat_ref_id)
444 );
445 $path = implode(' > ', $hops);
446 return [$path, $this->ui_factory->link()->standard($path, $url)];
447 }
448}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
GUI class for public user profile presentation.
Class ilCtrl provides processing control methods.
Base class for ILIAS Exception handling.
This class represents a hidden form property in a property form.
language handling
__construct(ilGlobalTemplateInterface $tpl, ilCtrl $ilCtrl, ilToolbarGUI $ilToolbar, ilLanguage $lng, Factory $ui_factory, MessageBox\Factory $message_box_factory, Button\Factory $button_factory, Renderer $ui_renderer, Psr\Http\Message\ServerRequestInterface $request, ilTree $tree, ILIAS\HTTP\Wrapper\RequestWrapper $request_wrapper, ILIAS\Refinery\Factory $refinery)
setRefId(int $prg_ref_id)
Set ref-id of StudyProgramme before using this GUI.
static _lookupName(int $a_user_id)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const ROOT_FOLDER_ID
Definition: constants.php:32
$c
Definition: deliver.php:25
exit
Interface RequestWrapper.
This describes a standard button.
Definition: Standard.php:27
withOnClick(Signal $signal)
Get a component like this, triggering a signal of another component on click.
This describes a Standard Dropdown.
Definition: Standard.php:27
This is how a factory for Message Boxes looks like.
Definition: Factory.php:27
An entity that renders components to a string output.
Definition: Renderer.php:31
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
$post
Definition: ltitoken.php:46
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$url
Definition: shib_logout.php:68