ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclDetailedViewGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  protected \ILIAS\UI\Factory $ui_factory;
27  protected \ILIAS\UI\Renderer $renderer;
29  protected ilDclTable $table;
30  protected int $tableview_id;
32  protected int $next_record_id = 0;
33  protected int $prev_record_id = 0;
34  protected int $current_record_position = 0;
35  protected array $record_ids = [];
36  protected bool $is_enabled_paging = true;
37  protected ilLanguage $lng;
40 
43  protected ?int $record_id;
46 
47  public function __construct(ilObjDataCollectionGUI $a_dcl_object, int $tableview_id)
48  {
49  global $DIC;
50 
51  $tpl = $DIC->ui()->mainTemplate();
52  $this->ctrl = $DIC->ctrl();
53  $this->dcl_gui_object = $a_dcl_object;
54  $this->lng = $DIC->language();
55  $this->http = $DIC->http();
56  $this->refinery = $DIC->refinery();
57  $this->main_tpl = $DIC->ui()->mainTemplate();
58  $this->ui_factory = $DIC->ui()->factory();
59  $this->renderer = $DIC->ui()->renderer();
60 
61  $this->record_id = null;
62  if ($this->http->wrapper()->query()->has('record_id')) {
63  $this->record_id = $this->http->wrapper()->query()->retrieve(
64  'record_id',
65  $this->refinery->kindlyTo()->int()
66  );
67  }
68  if ($this->http->wrapper()->post()->has('record_id')) {
69  $this->record_id = $this->http->wrapper()->post()->retrieve(
70  'record_id',
71  $this->refinery->kindlyTo()->int()
72  );
73  }
74  $ref_id = $this->dcl_gui_object->getRefId();
75 
76  if ($this->record_id) {
77  $this->record_obj = ilDclCache::getRecordCache($this->record_id);
78  $this->table = $this->record_obj->getTable();
79  if (!$this->record_obj->hasPermissionToView($ref_id)) {
80  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_msg_no_perm_view'), true);
81  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, "show");
82  }
83  }
84 
85  // content style (using system defaults)
86  $tpl->setCurrentBlock("SyntaxStyle");
87  $tpl->setVariable("LOCATION_SYNTAX_STYLESHEET", ilObjStyleSheet::getSyntaxStylePath());
88  $tpl->parseCurrentBlock();
89 
90  $tpl->setCurrentBlock("ContentStyle");
91  $tpl->setVariable("LOCATION_CONTENT_STYLESHEET", ilObjStyleSheet::getContentStylePath(0));
92  $tpl->parseCurrentBlock();
93 
94  // Comments
95  $repId = $this->dcl_gui_object->getDataCollectionObject()->getId();
97  $this->commentGUI = new ilCommentGUI($repId, $objId);
98  $this->commentGUI->enablePublicNotes();
99  $this->commentGUI->enablePublicNotesDeletion();
100  $this->ctrl->setParameterByClass(ilCommentGUI::class, "record_id", $this->record_id);
101  $this->ctrl->setParameterByClass(ilCommentGUI::class, "rep_id", $repId);
102 
103  $this->tableview_id = $tableview_id;
104 
105  if ($this->http->wrapper()->query()->has('disable_paging')
106  && $this->http->wrapper()->query()->retrieve('disable_paging', $this->refinery->kindlyTo()->bool())) {
107  $this->is_enabled_paging = false;
108  }
109  // Find current, prev and next records for navigation
110  if ($this->is_enabled_paging) {
111  $this->determineNextPrevRecords();
112  }
113  }
114 
115  public function executeCommand(): void
116  {
117  $this->ctrl->setParameter($this, 'tableview_id', $this->tableview_id);
118 
119  if (!$this->checkAccess()) {
120  if ($this->table->getVisibleTableViews(0, true)) {
121  $this->offerAlternativeViews();
122  } else {
123  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
124  }
125 
126  return;
127  }
128 
129  $cmd = $this->ctrl->getCmd();
130  $cmdClass = $this->ctrl->getCmdClass();
131  switch (strtolower($cmdClass)) {
132  case 'ilcommentgui':
133  $this->commentGUI->executeCommand();
134  break;
135  default:
136  $this->$cmd();
137  break;
138  }
139  }
140 
141  protected function offerAlternativeViews(): void
142  {
143  global $DIC;
144  $tpl = $DIC['tpl'];
145  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('dcl_msg_info_alternatives'));
146  $table_gui = new ilDclTableViewTableGUI($this, 'renderRecord', $this->table, $this->dcl_gui_object->getRefId());
147  $tpl->setContent($table_gui->getHTML());
148  }
149 
150  public function renderRecord(bool $editComments = false): void
151  {
152  global $DIC;
153  $x = $DIC->help();
154  $DIC->help()->setScreenId('dcl_record');
155  $ilTabs = $DIC->tabs();
156  $tpl = $DIC->ui()->mainTemplate();
157  $ilCtrl = $DIC->ctrl();
158 
159  $rctpl = new ilDataCollectionGlobalTemplate("tpl.record_view.html", false, true, "Modules/DataCollection");
160 
161  $ilTabs->setTabActive("id_content");
162 
163  if (!$this->tableview_id) {
164  $ilCtrl->redirectByClass(ilDclRecordListGUI::class, "listRecords");
165  }
166 
167  // see ilObjDataCollectionGUI->executeCommand about instantiation
168  $pageObj = new ilDclDetailedViewDefinitionGUI($this->tableview_id);
169  $pageObj->setOutputMode($pageObj::PRESENTATION);
170 
171  $html = $pageObj->showPage();
172  $rctpl->addCss("./Services/COPage/css/content.css");
173  $rctpl->fillCssFiles();
174  $table = ilDclCache::getTableCache($this->record_obj->getTableId());
175  foreach ($table->getRecordFields() as $field) {
176  //ILIAS_Ref_Links
177  $pattern = '/\[dcliln field="' . preg_quote($field->getTitle(), "/") . '"\](.*?)\[\/dcliln\]/';
178  if (preg_match($pattern, $html)) {
179  $html = preg_replace(
180  $pattern,
181  $this->record_obj->getRecordFieldSingleHTML($field->getId(), $this->setOptions("$1")),
182  $html
183  );
184  }
185 
186  //DataCollection Ref Links
187  $pattern = '/\[dclrefln field="' . preg_quote($field->getTitle(), "/") . '"\](.*?)\[\/dclrefln\]/';
188  if (preg_match($pattern, $html)) {
189  $this->currentField = $field;
190  $html = preg_replace_callback($pattern, [$this, "doReplace"], $html);
191  }
192 
193  $pattern = '/\[ext tableOf="' . preg_quote($field->getTitle(), "/") . '" field="(.*?)"\]/';
194  if (preg_match($pattern, $html)) {
195  $this->currentField = $field;
196  $html = preg_replace_callback($pattern, [$this, "doExtReplace"], $html);
197  }
198 
199  $html = str_ireplace(
200  "[" . $field->getTitle() . "]",
201  $this->record_obj->getRecordFieldSingleHTML($field->getId(), ['tableview_id' => $this->tableview_id]),
202  $html
203  );
204  }
205  foreach ($table->getStandardFields() as $field) {
206  $html = str_ireplace(
207  "[" . $field->getId() . "]",
208  $this->record_obj->getRecordFieldSingleHTML($field->getId(), ['tableview_id' => $this->tableview_id]),
209  $html
210  );
211  }
212  $rctpl->setVariable("CONTENT", $html);
213 
214  //Permanent Link
215  $tpl->setPermanentLink(
216  'dcl',
217  filter_input(INPUT_GET, 'ref_id', FILTER_VALIDATE_INT),
218  $this->table->getId() . '_' . $this->tableview_id . '_' . $this->record_obj->getId()
219  );
220 
221  // Buttons for previous/next records
222 
223  if ($this->is_enabled_paging) {
224  $prevNextLinks = $this->renderPrevNextLinks();
225  $rctpl->setVariable('PREV_NEXT_RECORD_LINKS', $prevNextLinks);
226  $ilCtrl->clearParameters($this); // #14083
227  $rctpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this));
228  $rctpl->setVariable('RECORD', $this->lng->txt('dcl_record'));
229  $rctpl->setVariable(
230  'RECORD_FROM_TOTAL',
231  sprintf(
232  $this->lng->txt('dcl_record_from_total'),
234  count($this->record_ids)
235  )
236  );
237  $rctpl->setVariable('TABLEVIEW_ID', $this->tableview_id);
238  $rctpl->setVariable('SELECT_OPTIONS', $this->renderSelectOptions());
239  }
240 
241  // Edit Button
242  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
243 
244  if ($this->record_obj->hasPermissionToEdit($ref_id)) {
245  $ilCtrl->setParameterByClass(ilDclRecordEditGUI::class, 'table_id', $this->table->getId());
246  $ilCtrl->setParameterByClass(ilDclRecordEditGUI::class, 'tableview_id', $this->tableview_id);
247  $ilCtrl->setParameterByClass(ilDclRecordEditGUI::class, 'redirect', ilDclRecordEditGUI::REDIRECT_DETAIL);
248  $ilCtrl->saveParameterByClass(ilDclRecordEditGUI::class, 'record_id');
249  $edit_button = $this->ui_factory->button()->standard(
250  $this->lng->txt('dcl_edit_record'),
251  $ilCtrl->getLinkTargetByClass(ilDclRecordEditGUI::class, 'edit')
252  );
253  $rctpl->setVariable('EDIT_RECORD_BUTTON', $this->renderer->render($edit_button));
254  }
255 
256  // Comments
257  if ($this->table->getPublicCommentsEnabled()) {
258  $rctpl->setVariable('COMMENTS', $this->renderComments($editComments));
259  }
260 
261  $tpl->setContent($rctpl->get());
262  }
263 
264  public function doReplace(array $found): string
265  {
266  return $this->record_obj->getRecordFieldSingleHTML($this->currentField->getId(), $this->setOptions($found[1]));
267  }
268 
269  public function doExtReplace(array $found): ?string
270  {
271  $ref_rec_ids = $this->record_obj->getRecordFieldValue($this->currentField->getId());
272  if (!is_array($ref_rec_ids)) {
273  $ref_rec_ids = [$ref_rec_ids];
274  }
275  if (!count($ref_rec_ids) || !$ref_rec_ids) {
276  return null;
277  }
278  $ref_recs = [];
279  foreach ($ref_rec_ids as $ref_rec_id) {
280  $ref_recs[] = ilDclCache::getRecordCache($ref_rec_id);
281  }
282  $field = $ref_recs[0]->getTable()->getFieldByTitle($found[1]);
283 
284  $tpl = new ilTemplate("tpl.reference_list.html", true, true, "Modules/DataCollection");
285  $tpl->setCurrentBlock("reference_list");
286 
287  if (!$field) {
288  if (ilObjDataCollectionAccess::hasWriteAccess($this->dcl_gui_object->getRefId())) {
289  $this->main_tpl->setOnScreenMessage(
290  'info',
291  "Bad Viewdefinition at [ext tableOf=\"" . $found[1] . "\" ...]",
292  true
293  );
294  }
295 
296  return null;
297  }
298 
299  foreach ($ref_recs as $ref_record) {
300  $tpl->setCurrentBlock("reference");
301  $tpl->setVariable("CONTENT", $ref_record->getRecordFieldHTML($field->getId()));
302  $tpl->parseCurrentBlock();
303  }
304  return $tpl->get();
305  }
306 
307  protected function renderComments(bool $edit = false): string
308  {
309  if (!$edit) {
310  return $this->commentGUI->getListHTML();
311  } else {
312  return $this->commentGUI->addNoteForm();
313  }
314  }
315 
319  protected function determineNextPrevRecords(): void
320  {
321  if (!ilSession::has("dcl_record_ids") || ilSession::get('dcl_record_ids') != $this->table->getId()) {
322  $this->loadSession();
323  }
324 
325  if (ilSession::has("dcl_record_ids") && count(ilSession::get("dcl_record_ids"))) {
326  $this->record_ids = ilSession::get("dcl_record_ids");
327  foreach ($this->record_ids as $k => $recId) {
328  if ($recId == $this->record_id) {
329  if ($k != 0) {
330  $this->prev_record_id = $this->record_ids[$k - 1];
331  }
332  if (($k + 1) < count($this->record_ids)) {
333  $this->next_record_id = $this->record_ids[$k + 1];
334  }
335  $this->current_record_position = $k + 1;
336  break;
337  }
338  }
339  }
340  }
341 
345  protected function renderPrevNextLinks(): string
346  {
347  global $DIC;
348  $ilCtrl = $DIC['ilCtrl'];
349  $ilCtrl->setParameter($this, 'tableview_id', $this->tableview_id);
350  $prevStr = $this->lng->txt('dcl_prev_record');
351  $nextStr = $this->lng->txt('dcl_next_record');
352  $ilCtrl->setParameter($this, 'record_id', $this->prev_record_id);
353  $url = $ilCtrl->getLinkTarget($this, 'renderRecord');
354  $out = ($this->prev_record_id) ? "<a href='$url'>$prevStr</a>" : "<span class='light'>$prevStr</span>";
355  $out .= " | ";
356  $ilCtrl->setParameter($this, 'record_id', $this->next_record_id);
357  $url = $ilCtrl->getLinkTarget($this, 'renderRecord');
358  $out .= ($this->next_record_id) ? "<a href='$url'>$nextStr</a>" : "<span class='light'>$nextStr</span>";
359 
360  return $out;
361  }
362 
366  protected function renderSelectOptions(): string
367  {
368  $out = '';
369  foreach ($this->record_ids as $k => $recId) {
370  $selected = ($recId == $this->record_id) ? " selected" : "";
371  $out .= "<option value='$recId'$selected>" . ($k + 1) . "</option>";
372  }
373 
374  return $out;
375  }
376 
381  private function setOptions(string $link_name): array
382  {
383  $options = [];
384  $options['link']['display'] = true;
385  $options['link']['name'] = $link_name;
386 
387  return $options;
388  }
389 
393  private function loadSession(): void
394  {
395  $list = new ilDclRecordListTableGUI(
396  new ilDclRecordListGUI($this->dcl_gui_object, $this->table->getId(), $this->tableview_id),
397  "listRecords",
398  $this->table,
399  $this->tableview_id
400  );
401  $list->initFilter();
402  $list->determineOffsetAndOrder();
403  $this->table->getPartialRecords(
404  (string) $this->table->getId(),
405  $list->getOrderField(),
406  $list->getOrderDirection(),
407  $list->getLimit(),
408  $list->getOffset(),
409  $list->getFilter()
410  );
411  }
412 
416  protected function checkAccess(): bool
417  {
418  $ref_id = $this->dcl_gui_object->getRefId();
419  $has_accass = ilObjDataCollectionAccess::hasAccessTo($ref_id, $this->table->getId(), $this->tableview_id);
420  $is_active = ilDclDetailedViewDefinition::isActive($this->tableview_id);
421  return $has_accass && $is_active;
422  }
423 }
ILIAS Refinery Factory $refinery
static get(string $a_var)
loadSession()
If we come from a goto Link we need to build up the session data.
setOptions(string $link_name)
setOptions string $link_name
renderSelectOptions()
Render select options.
ilGlobalTemplateInterface $main_tpl
ilDclDetailedViewDefinitionGUI: ilPageEditorGUI, ilEditClipboardGUI, ilMediaPoolTargetSelector ilDcl...
$objId
Definition: xapitoken.php:57
ilDclBaseFieldModel $currentField
ilDclBaseRecordModel $record_obj
global $DIC
Definition: feed.php:28
ilObjDataCollectionGUI: ilInfoScreenGUI, ilNoteGUI, ilCommonActionDispatcherGUI ilObjDataCollectionG...
$ref_id
Definition: ltiauth.php:67
static http()
Fetches the global http state from ILIAS.
renderPrevNextLinks()
Determine and return the markup for the previous/next records.
static getTableCache(int $table_id=null)
ilObjDataCollectionGUI $dcl_gui_object
$out
Definition: buildRTE.php:24
getRecordFields()
Returns all fields of this table which are NOT standard fields.
$url
Definition: ltiregstart.php:35
static has($a_var)
renderRecord(bool $editComments=false)
static hasWriteAccess(int $ref, ?int $user_id=0)
static getContentStylePath(int $a_style_id, bool $add_random=true, bool $add_token=true)
get content style path static (to avoid full reading)
static getRecordCache(?int $record_id)
Comment GUI.
determineNextPrevRecords()
Find the previous/next record from the current position.
__construct(ilObjDataCollectionGUI $a_dcl_object, int $tableview_id)
ilDclDetailedViewGUI: ilDclDetailedViewDefinitionGUI, ilEditClipboardGUI, ilCommentGUI ...