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