ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjStudyProgrammeAutoCategoriesGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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 
57  public $prg_ref_id;
58 
62  public $object;
63 
68 
72  protected $button_factory;
73 
77  public $ui_factory;
78 
82  public $ui_renderer;
83 
87  protected $request;
88 
89 
90  public function __construct(
92  ilCtrl $ilCtrl,
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:
128  case self::CMD_DELETE_CONFIRMATION:
129  case self::CMD_GET_ASYNC_MODAL:
130  $this->$cmd();
131  break;
132  case self::CMD_SAVE:
133  $this->$cmd();
134  $this->ctrl->redirect($this, 'view');
135  break;
136  case self::CMD_PROFILE_NOT_PUBLIC:
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  list($title, $link) = $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($link),
179  $this->ui_renderer->render($usr),
180  $this->ui_renderer->render($actions),
181  $title
182  ];
183  }
184  usort($data, function ($a, $b) {
185  return strnatcmp($a[4], $b[4]);
186  });
187 
188  $table = new ilStudyProgrammeAutoCategoriesTableGUI($this, "view", "");
189  $table->setData($data);
190 
191  $this->tpl->setContent(
192  $this->ui_renderer->render($collected_modals)
193  . $table->getHTML()
194  );
195  }
196 
200  protected function save()
201  {
202  $form = $this->getForm();
203  $form->setValuesByPost();
204  $form->checkInput();
205 
206  $cat_ref_id = $form->getInput(self::F_CATEGORY_REF);
207  $current_ref_id = $form->getInput(self::F_CATEGORY_ORIGINAL_REF);
208 
209  if (ilObject::_lookupType((int) $cat_ref_id, true) !== 'cat') {
210  \ilUtil::sendFailure(sprintf($this->lng->txt('not_a_valid_cat_id'), $cat_ref_id), true);
211  return;
212  }
213 
214  if (
215  !is_null($current_ref_id) &&
216  $current_ref_id !== $cat_ref_id
217  ) {
218  $ids = [(int) $current_ref_id];
219  $this->getObject()->deleteAutomaticContentCategories($ids);
220  }
221 
222 
223  $this->getObject()->storeAutomaticContentCategory(
224  (int) $cat_ref_id
225  );
226  }
227 
228  protected function deleteConfirmation()
229  {
230  $get = $this->request->getQueryParams();
231  $post = $this->request->getParsedBody();
232  $field = self::CHECKBOX_CATEGORY_REF_IDS;
233 
234  $field_ids_in_get = array_key_exists($field, $get);
235  $field_ids_in_post = array_key_exists($field, $post);
236 
237  if ($field_ids_in_get) {
238  $cat_ids = $get[$field];
239  $msg = $this->lng->txt('prg_delete_single_confirmation');
240  } elseif ($field_ids_in_post) {
241  $cat_ids = implode(' ', $post[$field]);
242  $msg = $this->lng->txt('prg_delete_confirmation');
243  } else {
244  ilUtil::sendInfo($this->lng->txt('prg_delete_nothing_selected'), true);
245  $this->ctrl->redirect($this, self::CMD_VIEW);
246  }
247 
248  $cat_ids = base64_encode($cat_ids);
249 
250  $this->ctrl->setParameterByClass(self::class, $field, $cat_ids);
251  $delete = $this->ctrl->getFormActionByClass(self::class, self::CMD_DELETE);
252  $cancel = $this->ctrl->getFormActionByClass(self::class, self::CMD_VIEW);
253  $this->ctrl->clearParameterByClass(self::class, $field);
254 
255  $buttons = [
256  $this->button_factory->standard($this->lng->txt('prg_confirm_delete'), $delete),
257  $this->button_factory->standard($this->lng->txt('prg_cancel'), $cancel)
258  ];
259 
260  $message_box = $this->message_box_factory->confirmation($msg)->withButtons($buttons);
261 
262  $this->tpl->setContent($this->ui_renderer->render($message_box));
263  }
264 
265  protected function delete()
266  {
267  $field = self::CHECKBOX_CATEGORY_REF_IDS;
268  $get = $this->request->getQueryParams();
269 
270  if (!array_key_exists($field, $get)) {
271  ilUtil::sendFailure($this->lng->txt('prg_delete_failure'), true);
272  $this->ctrl->redirect($this, self::CMD_VIEW);
273  }
274 
275  $cat_ids = base64_decode($get[$field]);
276  $cat_ids = explode(' ', trim($cat_ids));
277  $cat_ids = array_map('intval', $cat_ids);
278 
279  $this->getObject()->deleteAutomaticContentCategories($cat_ids);
280 
281  $msg = $this->lng->txt('prg_delete_single_success');
282  if (count($cat_ids) > 1) {
283  $msg = $this->lng->txt('prg_delete_success');
284  }
285 
286  ilUtil::sendSuccess($msg, true);
287  $this->ctrl->redirect($this, self::CMD_VIEW);
288  }
289 
294  public function setRefId(int $prg_ref_id)
295  {
296  $this->prg_ref_id = $prg_ref_id;
297  }
298 
303  protected function getObject() : ilObjStudyProgramme
304  {
305  if ($this->object === null ||
306  (int) $this->object->getRefId() !== $this->prg_ref_id
307  ) {
308  $this->object = ilObjStudyProgramme::getInstanceByRefId($this->prg_ref_id);
309  }
310  return $this->object;
311  }
312 
313  protected function getModal($current_ref_id = null)
314  {
315  if (!is_null($current_ref_id)) {
316  $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, $current_ref_id);
317  }
318  $link = $this->ctrl->getLinkTarget($this, "getAsyncModalOutput", "", true);
319  $this->ctrl->setParameter($this, self::CHECKBOX_CATEGORY_REF_IDS, null);
320  $modal = $this->ui_factory->modal()->roundtrip(
321  '',
322  []
323  )->withAsyncRenderUrl(
324  $link
325  );
326 
327  return $modal;
328  }
329 
330  protected function getAsyncModalOutput()
331  {
332  $current_ref_id = null;
333  if (array_key_exists(self::CHECKBOX_CATEGORY_REF_IDS, $_GET)) {
334  $current_ref_id = $_GET[self::CHECKBOX_CATEGORY_REF_IDS];
335  }
336  $form = $this->getForm($current_ref_id);
337  $form_id = "form_" . $form->getId();
338  $submit = $this->ui_factory->button()->primary($this->lng->txt('add'), "#")->withOnLoadCode(
339  function ($id) use ($form_id) {
340  return "$('#{$id}').click(function() { $('#{$form_id}').submit(); return false; });";
341  }
342  );
343  $modal = $this->ui_factory->modal()->roundtrip(
344  $this->lng->txt('modal_categories_title'),
345  $this->ui_factory->legacy($form->getHtml())
346  )->withActionButtons([$submit]);
347 
348  echo $this->ui_renderer->renderAsync($modal);
349  exit;
350  }
351 
352  protected function getForm($current_ref_id = null)
353  {
354  $form = new ilPropertyFormGUI();
355 
356  if (is_null($current_ref_id)) {
357  $current_ref_id = "";
358  }
359  $form->setId(uniqid((string) $current_ref_id));
360 
361  $form->setFormAction($this->ctrl->getFormAction($this, "save"));
363  $this->lng->txt("category"),
364  self::F_CATEGORY_REF,
365  false
366  );
367  $cat->getExplorerGUI()->setSelectableTypes(["cat"]);
368  $cat->getExplorerGUI()->setTypeWhiteList(["root", "cat"]);
369  if ($current_ref_id != "") {
370  $cat->getExplorerGUI()->setPathOpen($current_ref_id);
371  $cat->setValue($current_ref_id);
372  }
373  $cat->getExplorerGUI()->setRootId(ROOT_FOLDER_ID);
374  $cat->getExplorerGUI()->setAjax(false);
375  $form->addItem($cat);
376 
377  $hi = new ilHiddenInputGUI(self::F_CATEGORY_ORIGINAL_REF);
378  $hi->setValue($current_ref_id);
379  $form->addItem($hi);
380 
381  return $form;
382  }
383 
387  protected function getToolbar(Signal $add_cat_signal)
388  {
389  $btn = $this->ui_factory->button()->primary($this->lng->txt('add_category'), '')
390  ->withOnClick($add_cat_signal);
391  $this->toolbar->addComponent($btn);
392  }
393 
394  protected function getItemAction(
395  int $cat_ref_id,
396  Signal $signal
397  ) : Standard {
398  $items = [];
399  $items[] = $this->ui_factory
400  ->button()
401  ->shy($this->lng->txt('edit'), '')
402  ->withOnClick($signal)
403  ;
404 
405  $this->ctrl->setParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS, $cat_ref_id);
406  $link = $this->ctrl->getLinkTarget($this, self::CMD_DELETE_CONFIRMATION);
407  $this->ctrl->clearParameterByClass(self::class, self::CHECKBOX_CATEGORY_REF_IDS);
408 
409  $items[] = $this->ui_factory
410  ->button()
411  ->shy($this->lng->txt('delete'), $link)
412  ;
413 
414  $dd = $this->ui_factory->dropdown()->standard($items);
415 
416  return $dd;
417  }
418 
419  protected function getUserRepresentation(int $usr_id) : Shy
420  {
421  $username = ilObjUser::_lookupName($usr_id);
422  $editor = implode(' ', [
423  $username['firstname'],
424  $username['lastname'],
425  '(' . $username['login'] . ')'
426  ]);
427  $usr = ilObjectFactory::getInstanceByObjId($usr_id);
428  $url = ilLink::_getStaticLink($usr_id, 'usr');
429  if (!$usr->hasPublicProfile()) {
430  $url = $this->ctrl->getLinkTarget($this, self::CMD_PROFILE_NOT_PUBLIC);
431  }
432  return $this->ui_factory->button()->shy($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  function ($c) {
441  return ilObject::_lookupTitle($c["obj_id"]);
442  },
443  $this->tree->getPathFull($cat_ref_id)
444  );
445  $path = implode(' > ', $hops);
446  $title = array_pop($hops);
447  return [$title, $this->ui_factory->button()->shy($path, $url)];
448  }
449 }
static _lookupName($a_user_id)
lookup user name
An entity that renders components to a string output.
Definition: Renderer.php:14
setRefId(int $prg_ref_id)
Set ref-id of StudyProgramme before using this GUI.
This class provides processing control methods.
exit
Definition: login.php:29
$data
Definition: storeScorm.php:23
$c
Definition: cli.php:37
This class represents a property form user interface.
const ROOT_FOLDER_ID
Definition: constants.php:30
$_GET["client_id"]
static _lookupTitle($a_id)
lookup object title
__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)
Builds a Color from either hex- or rgb values.
Definition: Factory.php:13
static getInstanceByRefId($a_ref_id)
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
This class represents a hidden form property in a property form.
This describes a standard button.
Definition: Standard.php:12
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType($a_id, $a_reference=false)
lookup object type
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$url