ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilContainerReferenceGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 {
28  public const MAX_SELECTION_ENTRIES = 50;
29  public const MODE_CREATE = 1;
30  public const MODE_EDIT = 2;
31 
32  protected ilTabsGUI $tabs;
34  protected array $existing_objects = [];
35 
36  protected string $target_type;
37  protected string $reference_type;
40 
41  public function __construct($a_data, int $a_id, bool $a_call_by_reference = true, bool $a_prepare_output = true)
42  {
44  global $DIC;
45 
46  $this->lng = $DIC->language();
47  $this->ctrl = $DIC->ctrl();
48  $this->tabs = $DIC->tabs();
49  $this->locator = $DIC["ilLocator"];
50  $this->user = $DIC->user();
51  $this->access = $DIC->access();
52  $this->error = $DIC["ilErr"];
53  $this->settings = $DIC->settings();
54  $lng = $DIC->language();
55  parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
56 
57  $lng->loadLanguageModule('objref');
58  $this->cont_request = $DIC
59  ->containerReference()
60  ->internal()
61  ->gui()
62  ->standardRequest();
63  }
64 
65  public function executeCommand(): void
66  {
67  $ilCtrl = $this->ctrl;
68  $ilTabs = $this->tabs;
69 
70  if ($this->cont_request->getCreationMode() === self::MODE_CREATE) {
71  $this->setCreationMode(true);
72  }
73 
74  $next_class = $ilCtrl->getNextClass($this);
75  $cmd = $ilCtrl->getCmd();
76 
77  $this->prepareOutput();
78 
79  switch ($next_class) {
80  case "ilpropertyformgui":
81  $form = $this->initForm($this->creation_mode ? self::MODE_CREATE : self::MODE_EDIT);
82  $this->ctrl->forwardCommand($form);
83  break;
84 
85  case 'ilpermissiongui':
86  $ilTabs->setTabActive('perm_settings');
87  $ilCtrl->forwardCommand(new ilPermissionGUI($this));
88  break;
89 
90  default:
91  if ($cmd === null || $cmd === '' || $cmd === 'view') {
92  $cmd = "edit";
93  }
94  $cmd .= "Object";
95  $this->$cmd();
96  break;
97  }
98  }
99 
100  protected function addLocatorItems(): void
101  {
102  $ilLocator = $this->locator;
103 
104  if ($this->object instanceof ilObject) {
105  $ilLocator->addItem($this->object->getPresentationTitle(), $this->ctrl->getLinkTarget($this));
106  }
107  }
108 
109  public function redirectObject(): void
110  {
111  $ilCtrl = $this->ctrl;
112 
113  $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", $this->object->getTargetRefId());
114  $ilCtrl->redirectByClass("ilrepositorygui", "");
115  }
116 
117  public function createObject(): void
118  {
119  $ilAccess = $this->access;
121 
122  $new_type = $this->cont_request->getNewType();
123  if (!$ilAccess->checkAccess(
124  "create_" . $this->getReferenceType(),
125  '',
126  $this->cont_request->getRefId(),
127  $new_type
128  )) {
129  $ilErr->raiseError($this->lng->txt("permission_denied"), $ilErr->MESSAGE);
130  }
131  $this->ctrl->saveParameter($this, "crtptrefid");
132  $this->ctrl->saveParameter($this, "crtcb");
133  $form = $this->initForm(self::MODE_CREATE);
134  $this->tpl->setContent($form->getHTML());
135  }
136 
137  public function saveObject(): void
138  {
139  $ilAccess = $this->access;
140 
141  if ($this->cont_request->getTargetId() === 0) {
142  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
143  $this->createObject();
144  return;
145  }
146  if (!$ilAccess->checkAccess(
147  'visible',
148  '',
149  $this->cont_request->getTargetId()
150  )) {
151  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'));
152  $this->createObject();
153  return;
154  }
155 
156  if (!$this->checkPermissionBool("create", "", $this->requested_new_type)) {
157  $this->error->raiseError($this->lng->txt("no_create_permission"), $this->error->MESSAGE);
158  }
159 
160  $this->lng->loadLanguageModule($this->requested_new_type);
161  $this->ctrl->setParameter($this, "new_type", $this->requested_new_type);
162 
163  $form = $this->initCreateForm($this->requested_new_type);
164  if ($form->checkInput()) {
165  $this->ctrl->setParameter($this, "new_type", "");
166 
167  $class_name = "ilObj" . $this->obj_definition->getClassName($this->requested_new_type);
168  $newObj = new $class_name();
169  $newObj->setType($this->requested_new_type);
170  $newObj->setTitle($form->getInput("title"));
171  $newObj->setDescription($form->getInput("desc"));
172  $newObj->processAutoRating();
173  $newObj->create();
174 
175  $this->putObjectInTree($newObj);
176 
177  if ($this->form->getInput('didactic_template')) {
178  $dtpl = $this->getDidacticTemplateVar("dtpl");
179  // Object calls initCreateForm again in getDidacticTemplateVar
180  $this->form->checkInput();
181  $newObj->applyDidacticTemplate($dtpl);
182  }
183 
184  $this->afterSave($newObj);
185  }
186 
187  $form->setValuesByPost();
188  $this->tpl->setContent($form->getHTML());
189  }
190 
191  protected function initCreateForm(string $new_type): ilPropertyFormGUI
192  {
193  return $this->initForm(self::MODE_CREATE);
194  }
195 
196  protected function afterSave(ilObject $new_object): void
197  {
198  $target_obj_id = ilObject::_lookupObjId((int) $this->form->getInput('target_id'));
199  $new_object->setTargetId($target_obj_id);
200  $new_object->setTitleType((int) $this->form->getInput('title_type'));
201 
202  if ((int) $this->form->getInput('title_type') === ilContainerReference::TITLE_TYPE_CUSTOM) {
203  $new_object->setTitle($this->form->getInput('title'));
204  } elseif ((int) $this->form->getInput('title_type') === ilContainerReference::TITLE_TYPE_REUSE) {
205  $new_object->setTitle(ilObject::_lookupTitle($new_object->getTargetId()));
206  }
207 
208  $new_object->update();
209 
210  $this->tpl->setOnScreenMessage('success', $this->lng->txt("object_added"), true);
211  $this->ctrl->setParameter($this, 'ref_id', $new_object->getRefId());
212  $this->ctrl->setParameter($this, 'creation_mode', 0);
213  $this->ctrl->redirect($this, 'firstEdit');
214  }
215 
216  protected function firstEditObject(): void
217  {
218  $this->editObject();
219  }
220 
221  public function editReferenceObject(): void
222  {
223  $this->editObject();
224  }
225 
226  public function editObject(?ilPropertyFormGUI $form = null): void
227  {
228  global $DIC;
229 
230  $main_tpl = $DIC->ui()->mainTemplate();
231 
232  $ilTabs = $this->tabs;
233 
234  $ilTabs->setTabActive('settings');
235 
236  if (!$form instanceof ilPropertyFormGUI) {
237  $form = $this->initForm();
238  }
239  $main_tpl->setContent($form->getHTML());
240  }
241 
242  protected function initForm(int $a_mode = self::MODE_EDIT): ilPropertyFormGUI
243  {
244  $form = new ilPropertyFormGUI();
245 
246  if ($a_mode === self::MODE_CREATE) {
247  $form->setTitle($this->lng->txt($this->getReferenceType() . '_new'));
248 
249  $this->ctrl->setParameter($this, 'creation_mode', $a_mode);
250  $this->ctrl->setParameter(
251  $this,
252  'new_type',
253  $this->cont_request->getNewType()
254  );
255  } else {
256  $form->setTitle($this->lng->txt($this->reference_type . '_settings'));
257  }
258 
259  $form->setFormAction($this->ctrl->getFormAction($this));
260  if ($a_mode === self::MODE_CREATE) {
261  $lv = $this->getTargetType() . "r_add"; // see also https://mantis.ilias.de/view.php?id=31863
262  $form->addCommandButton('save', $this->lng->txt($lv));
263  $form->addCommandButton('cancel', $this->lng->txt('cancel'));
264  } else {
265  $form->addCommandButton('update', $this->lng->txt('save'));
266  }
267 
268  // title type
269  $ttype = new ilRadioGroupInputGUI($this->lng->txt('title'), 'title_type');
270  if ($a_mode === self::MODE_EDIT) {
271  $ttype->setValue((string) $this->object->getTitleType());
272  } else {
273  $ttype->setValue((string) ilContainerReference::TITLE_TYPE_REUSE);
274  }
275 
276  $reuse = new ilRadioOption($this->lng->txt('objref_reuse_title'));
278  $ttype->addOption($reuse);
279 
280  $custom = new ilRadioOption($this->lng->txt('objref_custom_title'));
282 
283  // title
284  $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
285  $title->setSize(min(40, ilObject::TITLE_LENGTH));
286  $title->setMaxLength(ilObject::TITLE_LENGTH);
287  $title->setRequired(true);
288 
289  if ($a_mode === self::MODE_EDIT) {
290  $title->setValue($this->object->getTitle());
291  }
292 
293  $custom->addSubItem($title);
294  $ttype->addOption($custom);
295  $form->addItem($ttype);
296 
297  $repo = new ilRepositorySelector2InputGUI($this->lng->txt("objref_edit_ref"), "target_id");
298  $repo->setRequired(true);
299  $repo->getExplorerGUI()->setSelectableTypes([$this->getTargetType()]);
300  $repo->getExplorerGUI()->setTypeWhiteList(
301  array_merge(
302  [$this->getTargetType()],
303  ["root", "cat", "grp", "fold", "crs"]
304  )
305  );
306  $repo->getExplorerGUI()->setClickablePermission('visible');
307  $repo->setInfo($this->lng->txt($this->getReferenceType() . '_edit_info'));
308 
309  if ($a_mode === self::MODE_EDIT) {
310  $repo->getExplorerGUI()->setPathOpen($this->object->getTargetRefId());
311  $repo->setValue($this->object->getTargetRefId());
312  }
313 
314  $form->addItem($repo);
315  $this->form = $form;
316  return $form;
317  }
318 
319  protected function loadPropertiesFromSettingsForm(ilPropertyFormGUI $form): bool
320  {
321  global $DIC;
322 
323  $ok = true;
324  $access = $DIC->access();
325 
326  // check access
327  if (
328  !$access->checkAccess('visible', '', (int) $form->getInput('target_id'))
329  ) {
330  $ok = false;
331  $form->getItemByPostVar('target_id')->setAlert($this->lng->txt('permission_denied'));
332  }
333  // check target type
334  if (ilObject::_lookupType((int) $form->getInput('target_id'), true) !== $this->target_type) {
335  $ok = false;
336  $form->getItemByPostVar('target_id')->setAlert(
337  $this->lng->txt('objref_failure_target_type') .
338  ': ' .
339  $this->lng->txt('obj_' . $this->target_type)
340  );
341  }
342 
343  $this->object->setTargetId(
344  ilObject::_lookupObjId((int) $form->getInput('target_id'))
345  );
346 
347  // set title after target id, so that the title can be reused immediately
348  $this->object->setTitleType((int) $form->getInput('title_type'));
349  if ((int) $form->getInput('title_type') === ilContainerReference::TITLE_TYPE_CUSTOM) {
350  $this->object->setTitle($form->getInput('title'));
351  } elseif ((int) $form->getInput('title_type') === ilContainerReference::TITLE_TYPE_REUSE) {
352  $this->object->setTitle(ilObject::_lookupTitle($this->object->getTargetId()));
353  }
354 
355  return $ok;
356  }
357 
358  public function updateObject(): void
359  {
360  $this->checkPermission('write');
361 
362  $form = $this->initForm();
363  if (
364  $form->checkInput() &&
365  $this->loadPropertiesFromSettingsForm($form)
366  ) {
367  $this->object->update();
368  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
369  $this->ctrl->redirect($this, 'edit');
370  }
371  $form->setValuesByPost();
372  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
373  $this->editObject($form);
374  }
375 
376  public function getTargetType(): string
377  {
378  return $this->target_type;
379  }
380 
381  public function getReferenceType(): string
382  {
383  return $this->reference_type;
384  }
385 
386  protected function getTabs(): void
387  {
388  global $DIC;
389 
390  $ilHelp = $DIC['ilHelp'];
391  $ilHelp->setScreenIdComponent($this->getReferenceType());
392 
393  if ($this->access->checkAccess('write', '', $this->object->getRefId())) {
394  $this->tabs_gui->addTarget(
395  "settings",
396  $this->ctrl->getLinkTarget($this, "edit"),
397  [],
398  ""
399  );
400  }
401  if ($this->access->checkAccess('edit_permission', '', $this->object->getRefId())) {
402  $this->tabs_gui->addTarget(
403  "perm_settings",
404  $this->ctrl->getLinkTargetByClass([get_class($this), 'ilpermissiongui'], "perm"),
405  ["perm", "info", "owner"],
406  'ilpermissiongui'
407  );
408  }
409  }
410 
411  public function getId(): int
412  {
413  return $this->obj_id;
414  }
415 }
This class represents an option in a radio group.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
loadPropertiesFromSettingsForm(ilPropertyFormGUI $form)
getItemByPostVar(string $a_post_var)
const TITLE_LENGTH
prepareOutput(bool $show_sub_objects=true)
checkAccess(string $a_permission, string $a_cmd, int $a_ref_id, string $a_type="", ?int $a_obj_id=null, ?int $a_tree_id=null)
check access for an object (provide $a_type and $a_obj_id if available for better performance) ...
initForm(int $a_mode=self::MODE_EDIT)
getDidacticTemplateVar(string $type)
Get didactic template setting from creation screen.
setParameterByClass(string $a_class, string $a_parameter, $a_value)
loadLanguageModule(string $a_module)
Load language module.
setTitle(string $title)
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
editObject(?ilPropertyFormGUI $form=null)
$ilErr
Definition: raiseError.php:33
setTabActive(string $a_id)
static _lookupObjId(int $ref_id)
setCreationMode(bool $mode=true)
If true, a creation screen is displayed the current [ref_id] does belong to the parent class The mode...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilLanguage $lng
This class represents a property in a property form.
static _lookupTitle(int $obj_id)
setFormAction(string $a_formaction)
Class ilObjectGUI Basic methods of all Output classes.
setValue(string $a_value)
global $DIC
Definition: shib_login.php:26
putObjectInTree(ilObject $obj, ?int $parent_node_id=null)
Add object to tree at given position.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
checkPermissionBool(string $perm, string $cmd="", string $type="", ?int $ref_id=null)
form( $class_path, string $cmd, string $submit_caption="")
__construct(Container $dic, ilPlugin $plugin)
ilAccessHandler $access
static _lookupType(int $id, bool $reference=false)
ilLocatorGUI $locator
checkPermission(string $perm, string $cmd="", string $type="", ?int $ref_id=null)