ILIAS  release_8 Revision v8.24
class.ilContainerReferenceGUI.php
Go to the documentation of this file.
1<?php
2
19declare(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 include_once("Services/AccessControl/classes/class.ilPermissionGUI.php");
88 $ilCtrl->forwardCommand(new ilPermissionGUI($this));
89 break;
90
91 default:
92 if ($cmd === null || $cmd === '' || $cmd === 'view') {
93 $cmd = "edit";
94 }
95 $cmd .= "Object";
96 $this->$cmd();
97 break;
98 }
99 }
100
101 protected function addLocatorItems(): void
102 {
103 $ilLocator = $this->locator;
104
105 if ($this->object instanceof ilObject) {
106 $ilLocator->addItem($this->object->getPresentationTitle(), $this->ctrl->getLinkTarget($this));
107 }
108 }
109
110 public function redirectObject(): void
111 {
112 $ilCtrl = $this->ctrl;
113
114 $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", $this->object->getTargetRefId());
115 $ilCtrl->redirectByClass("ilrepositorygui", "");
116 }
117
118 public function createObject(): void
119 {
120 $ilAccess = $this->access;
122
123 $new_type = $this->cont_request->getNewType();
124 if (!$ilAccess->checkAccess(
125 "create_" . $this->getReferenceType(),
126 '',
127 $this->cont_request->getRefId(),
128 $new_type
129 )) {
130 $ilErr->raiseError($this->lng->txt("permission_denied"), $ilErr->MESSAGE);
131 }
132 $this->ctrl->saveParameter($this, "crtptrefid");
133 $this->ctrl->saveParameter($this, "crtcb");
134 $form = $this->initForm(self::MODE_CREATE);
135 $this->tpl->setContent($form->getHTML());
136 }
137
138 public function saveObject(): void
139 {
140 $ilAccess = $this->access;
141
142 if ($this->cont_request->getTargetId() === 0) {
143 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
144 $this->createObject();
145 return;
146 }
147 if (!$ilAccess->checkAccess(
148 'visible',
149 '',
150 $this->cont_request->getTargetId()
151 )) {
152 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'));
153 $this->createObject();
154 return;
155 }
156
157 parent::saveObject();
158 }
159
160 protected function initCreateForm(string $new_type): ilPropertyFormGUI
161 {
162 return $this->initForm(self::MODE_CREATE);
163 }
164
165 protected function afterSave(ilObject $new_object): void
166 {
167 $target_obj_id = ilObject::_lookupObjId((int) $this->form->getInput('target_id'));
168 $new_object->setTargetId($target_obj_id);
169 $new_object->setTitleType((int) $this->form->getInput('title_type'));
170
171 if ((int) $this->form->getInput('title_type') === ilContainerReference::TITLE_TYPE_CUSTOM) {
172 $new_object->setTitle($this->form->getInput('title'));
173 } elseif ((int) $this->form->getInput('title_type') === ilContainerReference::TITLE_TYPE_REUSE) {
174 $new_object->setTitle(ilObject::_lookupTitle($new_object->getTargetId()));
175 }
176
177 $new_object->update();
178
179 $this->tpl->setOnScreenMessage('success', $this->lng->txt("object_added"), true);
180 $this->ctrl->setParameter($this, 'ref_id', $new_object->getRefId());
181 $this->ctrl->setParameter($this, 'creation_mode', 0);
182 $this->ctrl->redirect($this, 'firstEdit');
183 }
184
185 protected function firstEditObject(): void
186 {
187 $this->editObject();
188 }
189
190 public function editReferenceObject(): void
191 {
192 $this->editObject();
193 }
194
195 public function editObject(ilPropertyFormGUI $form = null): void
196 {
197 global $DIC;
198
199 $main_tpl = $DIC->ui()->mainTemplate();
200
201 $ilTabs = $this->tabs;
202
203 $ilTabs->setTabActive('settings');
204
205 if (!$form instanceof ilPropertyFormGUI) {
206 $form = $this->initForm();
207 }
208 $main_tpl->setContent($form->getHTML());
209 }
210
211 protected function initForm(int $a_mode = self::MODE_EDIT): ilPropertyFormGUI
212 {
213 $form = new ilPropertyFormGUI();
214
215 if ($a_mode === self::MODE_CREATE) {
216 $form->setTitle($this->lng->txt($this->getReferenceType() . '_new'));
217
218 $this->ctrl->setParameter($this, 'creation_mode', $a_mode);
219 $this->ctrl->setParameter(
220 $this,
221 'new_type',
222 $this->cont_request->getNewType()
223 );
224 } else {
225 $form->setTitle($this->lng->txt($this->reference_type . '_settings'));
226 }
227
228 $form->setFormAction($this->ctrl->getFormAction($this));
229 if ($a_mode === self::MODE_CREATE) {
230 $lv = $this->getTargetType() . "r_add"; // see also https://mantis.ilias.de/view.php?id=31863
231 $form->addCommandButton('save', $this->lng->txt($lv));
232 $form->addCommandButton('cancel', $this->lng->txt('cancel'));
233 } else {
234 $form->addCommandButton('update', $this->lng->txt('save'));
235 }
236
237 // title type
238 $ttype = new ilRadioGroupInputGUI($this->lng->txt('title'), 'title_type');
239 if ($a_mode === self::MODE_EDIT) {
240 $ttype->setValue((string) $this->object->getTitleType());
241 } else {
242 $ttype->setValue((string) ilContainerReference::TITLE_TYPE_REUSE);
243 }
244
245 $reuse = new ilRadioOption($this->lng->txt('objref_reuse_title'));
246 $reuse->setValue((string) ilContainerReference::TITLE_TYPE_REUSE);
247 $ttype->addOption($reuse);
248
249 $custom = new ilRadioOption($this->lng->txt('objref_custom_title'));
250 $custom->setValue((string) ilContainerReference::TITLE_TYPE_CUSTOM);
251
252 // title
253 $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
254 $title->setSize(min(40, ilObject::TITLE_LENGTH));
255 $title->setMaxLength(ilObject::TITLE_LENGTH);
256 $title->setRequired(true);
257
258 if ($a_mode === self::MODE_EDIT) {
259 $title->setValue($this->object->getTitle());
260 }
261
262 $custom->addSubItem($title);
263 $ttype->addOption($custom);
264 $form->addItem($ttype);
265
266 $repo = new ilRepositorySelector2InputGUI($this->lng->txt("objref_edit_ref"), "target_id");
267 $repo->setRequired(true);
268 $repo->getExplorerGUI()->setSelectableTypes([$this->getTargetType()]);
269 $repo->getExplorerGUI()->setTypeWhiteList(
270 array_merge(
271 [$this->getTargetType()],
272 ["root", "cat", "grp", "fold", "crs"]
273 )
274 );
275 $repo->getExplorerGUI()->setClickablePermission('visible');
276 $repo->setInfo($this->lng->txt($this->getReferenceType() . '_edit_info'));
277
278 if ($a_mode === self::MODE_EDIT) {
279 $repo->getExplorerGUI()->setPathOpen($this->object->getTargetRefId());
280 $repo->setValue($this->object->getTargetRefId());
281 }
282
283 $form->addItem($repo);
284 $this->form = $form;
285 return $form;
286 }
287
289 {
290 global $DIC;
291
292 $ok = true;
293 $access = $DIC->access();
294
295 // check access
296 if (
297 !$access->checkAccess('visible', '', (int) $form->getInput('target_id'))
298 ) {
299 $ok = false;
300 $form->getItemByPostVar('target_id')->setAlert($this->lng->txt('permission_denied'));
301 }
302 // check target type
303 if (ilObject::_lookupType((int) $form->getInput('target_id'), true) !== $this->target_type) {
304 $ok = false;
305 $form->getItemByPostVar('target_id')->setAlert(
306 $this->lng->txt('objref_failure_target_type') .
307 ': ' .
308 $this->lng->txt('obj_' . $this->target_type)
309 );
310 }
311
312 $this->object->setTargetId(
313 ilObject::_lookupObjId((int) $form->getInput('target_id'))
314 );
315
316 // set title after target id, so that the title can be reused immediately
317 $this->object->setTitleType((int) $form->getInput('title_type'));
318 if ((int) $form->getInput('title_type') === ilContainerReference::TITLE_TYPE_CUSTOM) {
319 $this->object->setTitle($form->getInput('title'));
320 } elseif ((int) $form->getInput('title_type') === ilContainerReference::TITLE_TYPE_REUSE) {
321 $this->object->setTitle(ilObject::_lookupTitle($this->object->getTargetId()));
322 }
323
324 return $ok;
325 }
326
327 public function updateObject(): void
328 {
329 $this->checkPermission('write');
330
331 $form = $this->initForm();
332 if (
333 $form->checkInput() &&
334 $this->loadPropertiesFromSettingsForm($form)
335 ) {
336 $this->object->update();
337 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
338 $this->ctrl->redirect($this, 'edit');
339 }
341 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
342 $this->editObject($form);
343 }
344
345 public function getTargetType(): string
346 {
347 return $this->target_type;
348 }
349
350 public function getReferenceType(): string
351 {
353 }
354
355 protected function getTabs(): void
356 {
357 global $DIC;
358
359 $ilHelp = $DIC['ilHelp'];
360 $ilHelp->setScreenIdComponent($this->getReferenceType());
361
362 if ($this->access->checkAccess('write', '', $this->object->getRefId())) {
363 $this->tabs_gui->addTarget(
364 "settings",
365 $this->ctrl->getLinkTarget($this, "edit"),
366 [],
367 ""
368 );
369 }
370 if ($this->access->checkAccess('edit_permission', '', $this->object->getRefId())) {
371 $this->tabs_gui->addTarget(
372 "perm_settings",
373 $this->ctrl->getLinkTargetByClass([get_class($this), 'ilpermissiongui'], "perm"),
374 ["perm", "info", "owner"],
375 'ilpermissiongui'
376 );
377 }
378 }
379
380 public function getId(): int
381 {
382 return $this->obj_id;
383 }
384}
error(string $a_errmsg)
addLocatorItems()
should be overwritten to add object specific items (repository items are preloaded)
updateObject()
updates object entry in object_data
editObject(ilPropertyFormGUI $form=null)
initForm(int $a_mode=self::MODE_EDIT)
loadPropertiesFromSettingsForm(ilPropertyFormGUI $form)
afterSave(ilObject $new_object)
Post (successful) object creation hook.
getTabs()
@abstract overwrite in derived GUI class of your object type
setParameterByClass(string $a_class, string $a_parameter, $a_value)
@inheritDoc
Error Handling & global info handling uses PEAR error class.
setFormAction(string $a_formaction)
loadLanguageModule(string $a_module)
Load language module.
addItem(string $a_title, string $a_link, string $a_frame="", int $a_ref_id=0, ?string $type=null)
Class ilObjectGUI Basic methods of all Output classes.
ilAccessHandler $access
__construct($data, int $id=0, bool $call_by_reference=true, bool $prepare_output=true)
checkPermission(string $perm, string $cmd="", string $type="", ?int $ref_id=null)
setCreationMode(bool $mode=true)
if true, a creation screen is displayed the current [ref_id] don't belong to the current class!...
ilLocatorGUI $locator
prepareOutput(bool $show_sub_objects=true)
ilLanguage $lng
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
const TITLE_LENGTH
setTitle(string $title)
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
New PermissionGUI (extends from old ilPermission2GUI) RBAC related output.
This class represents a property form user interface.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
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-...
getItemByPostVar(string $a_post_var)
This class represents a property in a property form.
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTabActive(string $a_id)
This class represents a text property in a property form.
global $DIC
Definition: feed.php:28
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)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
form( $class_path, string $cmd)
$ilErr
Definition: raiseError.php:17