ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilObjStudyProgrammeAutoCategoriesGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5use GuzzleHttp\Psr7\ServerRequest;
13
22{
23 const F_CATEGORY_REF = 'f_cr';
24 const F_CATEGORY_ORIGINAL_REF = 'f_cr_org';
25 const CHECKBOX_CATEGORY_REF_IDS = 'c_catids';
26
27 const CMD_VIEW = 'view';
28 const CMD_SAVE = 'save';
29 const CMD_GET_ASYNC_MODAL = 'getAsyncModalOutput';
30 const CMD_DELETE = 'delete';
31 const CMD_DELETE_CONFIRMATION = 'deleteConfirmation';
32 const CMD_PROFILE_NOT_PUBLIC = 'profile_not_public';
33
37 public $tpl;
38
42 public $ctrl;
43
47 public $toolbar;
48
52 public $lng;
53
58
62 public $object;
63
68
72 protected $button_factory;
73
78
83
87 protected $request;
88
89
90 public function __construct(
93 ilToolbarGUI $ilToolbar,
99 ServerRequest $request,
100 ilTree $tree
101 ) {
102 $this->tpl = $tpl;
103 $this->ctrl = $ilCtrl;
104 $this->toolbar = $ilToolbar;
105 $this->lng = $lng;
106 $this->ui_factory = $ui_factory;
107 $this->message_box_factory = $message_box_factory;
108 $this->button_factory = $button_factory;
109 $this->ui_renderer = $ui_renderer;
110 $this->request = $request;
111 $this->tree = $tree;
112 }
113
114 public function executeCommand()
115 {
116 $cmd = $this->ctrl->getCmd();
117 $next_class = $this->ctrl->getNextClass($this);
118
119 switch ($next_class) {
120 case "ilpropertyformgui":
121 $form = $this->getForm($this->creation_mode ? self::MODE_CREATE : self::MODE_EDIT);
122 $this->ctrl->forwardCommand($form);
123 break;
124 default:
125 switch ($cmd) {
126 case self::CMD_VIEW:
127 case self::CMD_DELETE:
130 $this->$cmd();
131 break;
132 case self::CMD_SAVE:
133 $this->$cmd();
134 $this->ctrl->redirect($this, 'view');
135 break;
137 $this->view(true);
138 break;
139 default:
140 throw new ilException("ilObjStudyProgrammeAutoCategoriesGUI: " .
141 "Command not supported: $cmd");
142 }
143 }
144 }
145
149 protected function view(bool $profile_not_public = false)
150 {
151 if ($profile_not_public) {
152 ilUtil::sendInfo($this->lng->txt('prg_profile_not_public'));
153 }
154
155 $collected_modals = [];
156
157 $modal = $this->getModal();
158 $this->getToolbar($modal->getShowSignal());
159 $collected_modals[] = $modal;
160
161 $data = [];
162 foreach ($this->getObject()->getAutomaticContentCategories() as $ac) {
163 $ref_id = $ac->getCategoryRefId();
164 if (ilObject::_lookupType($ref_id, true) !== 'cat' || $this->tree->isDeleted($ref_id)) {
165 continue;
166 }
167 $title = $this->getItemPath($ref_id);
168 $usr = $this->getUserRepresentation($ac->getLastEditorId());
169 $modal = $this->getModal($ref_id);
170 $collected_modals[] = $modal;
171 $actions = $this->getItemAction(
172 $ac->getCategoryRefId(),
173 $modal->getShowSignal()
174 );
175
176 $data[] = [
177 $ac,
178 $this->ui_renderer->render($title),
179 $this->ui_renderer->render($usr),
180 $this->ui_renderer->render($actions)
181 ];
182 }
183 $table = new ilStudyProgrammeAutoCategoriesTableGUI($this, "view", "");
184 $table->setData($data);
185
186 $this->tpl->setContent(
187 $this->ui_renderer->render($collected_modals)
188 . $table->getHTML()
189 );
190 }
191
195 protected function save()
196 {
197 $form = $this->getForm();
198 $form->setValuesByPost();
199 $form->checkInput();
200
201 $cat_ref_id = $form->getInput(self::F_CATEGORY_REF);
202 $current_ref_id = $form->getInput(self::F_CATEGORY_ORIGINAL_REF);
203
204 if (ilObject::_lookupType((int) $cat_ref_id, true) !== 'cat') {
205 \ilUtil::sendFailure(sprintf($this->lng->txt('not_a_valid_cat_id'), $cat_ref_id), true);
206 return;
207 }
208
209 if (
210 !is_null($current_ref_id) &&
211 $current_ref_id !== $cat_ref_id
212 ) {
213 $ids = [(int) $current_ref_id];
214 $this->getObject()->deleteAutomaticContentCategories($ids);
215 }
216
217
218 $this->getObject()->storeAutomaticContentCategory(
219 (int) $cat_ref_id
220 );
221 }
222
223 protected function deleteConfirmation()
224 {
225 $get = $this->request->getQueryParams();
226 $post = $this->request->getParsedBody();
228
229 $field_ids_in_get = array_key_exists($field, $get);
230 $field_ids_in_post = array_key_exists($field, $post);
231
232 if ($field_ids_in_get) {
233 $cat_ids = $get[$field];
234 $msg = $this->lng->txt('prg_delete_single_confirmation');
235 } elseif ($field_ids_in_post) {
236 $cat_ids = implode(' ', $post[$field]);
237 $msg = $this->lng->txt('prg_delete_confirmation');
238 } else {
239 ilUtil::sendInfo($this->lng->txt('prg_delete_nothing_selected'), true);
240 $this->ctrl->redirect($this, self::CMD_VIEW);
241 }
242
243 $cat_ids = base64_encode($cat_ids);
244
245 $this->ctrl->setParameterByClass(self::class, $field, $cat_ids);
246 $delete = $this->ctrl->getFormActionByClass(self::class, self::CMD_DELETE);
247 $cancel = $this->ctrl->getFormActionByClass(self::class, self::CMD_VIEW);
248 $this->ctrl->clearParameterByClass(self::class, $field);
249
250 $buttons = [
251 $this->button_factory->standard($this->lng->txt('prg_confirm_delete'), $delete),
252 $this->button_factory->standard($this->lng->txt('prg_cancel'), $cancel)
253 ];
254
255 $message_box = $this->message_box_factory->confirmation($msg)->withButtons($buttons);
256
257 $this->tpl->setContent($this->ui_renderer->render($message_box));
258 }
259
260 protected function delete()
261 {
263 $get = $this->request->getQueryParams();
264
265 if (!array_key_exists($field, $get)) {
266 ilUtil::sendFailure($this->lng->txt('prg_delete_failure'), true);
267 $this->ctrl->redirect($this, self::CMD_VIEW);
268 }
269
270 $cat_ids = base64_decode($get[$field]);
271 $cat_ids = explode(' ', trim($cat_ids));
272 $cat_ids = array_map('intval', $cat_ids);
273
274 $this->getObject()->deleteAutomaticContentCategories($cat_ids);
275
276 $msg = $this->lng->txt('prg_delete_single_success');
277 if (count($cat_ids) > 1) {
278 $msg = $this->lng->txt('prg_delete_success');
279 }
280
281 ilUtil::sendSuccess($msg, true);
282 $this->ctrl->redirect($this, self::CMD_VIEW);
283 }
284
289 public function setRefId(int $prg_ref_id)
290 {
291 $this->prg_ref_id = $prg_ref_id;
292 }
293
298 protected function getObject() : ilObjStudyProgramme
299 {
300 if ($this->object === null ||
301 (int) $this->object->getRefId() !== $this->prg_ref_id
302 ) {
303 $this->object = ilObjStudyProgramme::getInstanceByRefId($this->prg_ref_id);
304 }
305 return $this->object;
306 }
307
308 protected function getModal($current_ref_id = null)
309 {
310 if (!is_null($current_ref_id)) {
311 $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, $current_ref_id);
312 }
313 $link = $this->ctrl->getLinkTarget($this, "getAsyncModalOutput", "", true);
314 $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, null);
315 $modal = $this->ui_factory->modal()->roundtrip(
316 '',
317 []
318 )->withAsyncRenderUrl(
319 $link
320 );
321
322 return $modal;
323 }
324
325 protected function getAsyncModalOutput()
326 {
327 $current_ref_id = null;
328 if (array_key_exists(self::CHECKBOX_CATEGORY_REF_IDS, $_GET)) {
329 $current_ref_id = $_GET[self::CHECKBOX_CATEGORY_REF_IDS];
330 }
331 $form = $this->getForm($current_ref_id);
332 $form_id = "form_" . $form->getId();
333 $submit = $this->ui_factory->button()->primary($this->lng->txt('search'), "#")->withOnLoadCode(
334 function ($id) use ($form_id) {
335 return "$('#{$id}').click(function() { $('#{$form_id}').submit(); return false; });";
336 }
337 );
338 $modal = $this->ui_factory->modal()->roundtrip(
339 $this->lng->txt('modal_categories_title'),
340 $this->ui_factory->legacy($form->getHtml())
341 )->withActionButtons([$submit]);
342
343 echo $this->ui_renderer->renderAsync($modal);
344 exit;
345 }
346
347 protected function getForm($current_ref_id = null)
348 {
349 $form = new ilPropertyFormGUI();
350
351 if (is_null($current_ref_id)) {
352 $current_ref_id = "";
353 }
354 $form->setId(uniqid((string) $current_ref_id));
355
356 $form->setFormAction($this->ctrl->getFormAction($this, "save"));
358 $this->lng->txt("category"),
359 self::F_CATEGORY_REF,
360 false
361 );
362 $cat->getExplorerGUI()->setSelectableTypes(["cat"]);
363 $cat->getExplorerGUI()->setTypeWhiteList(["root", "cat"]);
364 if ($current_ref_id != "") {
365 $cat->getExplorerGUI()->setPathOpen($current_ref_id);
366 $cat->setValue($current_ref_id);
367 }
368 $cat->getExplorerGUI()->setRootId(ROOT_FOLDER_ID);
369 $cat->getExplorerGUI()->setAjax(false);
370 $form->addItem($cat);
371
372 $hi = new ilHiddenInputGUI(self::F_CATEGORY_ORIGINAL_REF);
373 $hi->setValue($current_ref_id);
374 $form->addItem($hi);
375
376 return $form;
377 }
378
382 protected function getToolbar(Signal $add_cat_signal)
383 {
384 $btn = $this->ui_factory->button()->primary($this->lng->txt('add_category'), '')
385 ->withOnClick($add_cat_signal);
386 $this->toolbar->addComponent($btn);
387 }
388
389 protected function getItemAction(
390 int $cat_ref_id,
391 Signal $signal
392 ) : Standard {
393 $items = [];
394 $items[] = $this->ui_factory
395 ->button()
396 ->shy($this->lng->txt('edit'), '')
397 ->withOnClick($signal)
398 ;
399
400 $this->ctrl->setParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS, $cat_ref_id);
401 $link = $this->ctrl->getLinkTarget($this, self::CMD_DELETE_CONFIRMATION);
402 $this->ctrl->clearParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS);
403
404 $items[] = $this->ui_factory
405 ->button()
406 ->shy($this->lng->txt('delete'), $link)
407 ;
408
409 $dd = $this->ui_factory->dropdown()->standard($items);
410
411 return $dd;
412 }
413
414 protected function getUserRepresentation(int $usr_id) : Shy
415 {
416 $username = ilObjUser::_lookupName($usr_id);
417 $editor = implode(' ', [
418 $username['firstname'],
419 $username['lastname'],
420 '(' . $username['login'] . ')'
421 ]);
423 $url = ilLink::_getStaticLink($usr_id, 'usr');
424 if (!$usr->hasPublicProfile()) {
425 $url = $this->ctrl->getLinkTarget($this, self::CMD_PROFILE_NOT_PUBLIC);
426 }
427 return $this->ui_factory->button()->shy($editor, $url);
428 }
429
430 protected function getItemPath(int $cat_ref_id) : Shy
431 {
432 $url = ilLink::_getStaticLink($cat_ref_id, 'cat');
433
434 $hops = array_map(
435 function ($c) {
436 return ilObject::_lookupTitle($c["obj_id"]);
437 },
438 $this->tree->getPathFull($cat_ref_id)
439 );
440 $path = implode(' > ', $hops);
441
442 return $this->ui_factory->button()->shy($path, $url);
443 }
444}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
This class 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, ServerRequest $request, ilTree $tree)
setRefId(int $prg_ref_id)
Set ref-id of StudyProgramme before using this GUI.
static getInstanceByRefId($a_ref_id)
static _lookupName($a_user_id)
lookup user name
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupTitle($a_id)
lookup object title
static _lookupType($a_id, $a_reference=false)
lookup object type
This class represents a property form user interface.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
global $ilCtrl
Definition: ilias.php:18
withOnClick(Signal $signal)
@inheritdocs
This is how a factory for buttons looks like.
Definition: Factory.php:13
This describes a standard button.
Definition: Standard.php:13
This describes a Standard Dropdown.
Definition: Standard.php:11
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
exit
Definition: login.php:29
$url
$data
Definition: storeScorm.php:23