ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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 bool $is_enabled_paging = true;
33  protected ilLanguage $lng;
36 
39  protected ?int $record_id;
42 
43  public function __construct(ilObjDataCollectionGUI $a_dcl_object, int $tableview_id)
44  {
45  global $DIC;
46 
47  $tpl = $DIC->ui()->mainTemplate();
48  $this->ctrl = $DIC->ctrl();
49  $this->dcl_gui_object = $a_dcl_object;
50  $this->lng = $DIC->language();
51  $this->http = $DIC->http();
52  $this->refinery = $DIC->refinery();
53  $this->main_tpl = $DIC->ui()->mainTemplate();
54  $this->ui_factory = $DIC->ui()->factory();
55  $this->renderer = $DIC->ui()->renderer();
56 
57  if (
58  !$this->http->wrapper()->query()->has('table_id') ||
59  !$this->http->wrapper()->query()->has('tableview_id')
60  ) {
61  $this->main_tpl->setOnScreenMessage($this->main_tpl::MESSAGE_TYPE_FAILURE, 'Table not found', true);
62  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, "show");
63  }
64 
65  $this->tableview_id = $this->http->wrapper()->query()->retrieve('tableview_id', $this->refinery->kindlyTo()->int());
66  $table_id = $this->http->wrapper()->query()->retrieve('table_id', $this->refinery->kindlyTo()->int());
67  $this->table = ilDclCache::getTableCache($table_id);
68  $this->loadSession();
69 
70  $this->record_id = null;
71  if ($this->http->wrapper()->query()->has('record_id')) {
72  $this->record_id = $this->http->wrapper()->query()->retrieve(
73  'record_id',
74  $this->refinery->kindlyTo()->int()
75  );
76  } elseif ($this->http->wrapper()->query()->has('record_pos')) {
77  $record_ids = ilSession::get('dcl_record_ids');
78  $pos = $this->http->wrapper()->query()->retrieve('record_pos', $this->refinery->kindlyTo()->int());
79  if ($record_ids === [] || !array_key_exists($pos, $record_ids)) {
80  $this->ctrl->redirectToURL($this->http->request()->getServerParams()['HTTP_REFERER']);
81  }
82  $this->record_id = $record_ids[$pos];
83  }
84  if ($this->http->wrapper()->post()->has('record_id')) {
85  $this->record_id = $this->http->wrapper()->post()->retrieve(
86  'record_id',
87  $this->refinery->kindlyTo()->int()
88  );
89  }
90 
91  if ($this->record_id) {
92  $this->record_obj = ilDclCache::getRecordCache($this->record_id);
93  if (!$this->record_obj->hasPermissionToView($this->dcl_gui_object->getRefId())) {
94  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_msg_no_perm_view'), true);
95  $this->ctrl->redirectByClass(ilDclRecordListGUI::class, "show");
96  }
97  }
98 
99  // content style (using system defaults)
100  $tpl->setCurrentBlock("SyntaxStyle");
101  $tpl->setVariable("LOCATION_SYNTAX_STYLESHEET", ilObjStyleSheet::getSyntaxStylePath());
102  $tpl->parseCurrentBlock();
103 
104  $tpl->setCurrentBlock("ContentStyle");
105  $tpl->setVariable("LOCATION_CONTENT_STYLESHEET", ilObjStyleSheet::getContentStylePath(0));
106  $tpl->parseCurrentBlock();
107 
108  // Comments
109  $repId = $this->dcl_gui_object->getDataCollectionObject()->getId();
111  $this->commentGUI = new ilCommentGUI($repId, $objId);
112  $this->commentGUI->enablePublicNotes();
113  $this->commentGUI->enablePublicNotesDeletion();
114  $this->ctrl->setParameterByClass(ilCommentGUI::class, "record_id", $this->record_id);
115  $this->ctrl->setParameterByClass(ilCommentGUI::class, "rep_id", $repId);
116 
117  $this->tableview_id = $tableview_id;
118 
119  if ($this->http->wrapper()->query()->has('disable_paging')
120  && $this->http->wrapper()->query()->retrieve('disable_paging', $this->refinery->kindlyTo()->bool())) {
121  $this->is_enabled_paging = false;
122  }
123  }
124 
125  public function executeCommand(): void
126  {
127  $this->ctrl->setParameter($this, 'tableview_id', $this->tableview_id);
128 
129  if (!$this->checkAccess()) {
130  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
131  return;
132  }
133 
134  $cmd = $this->ctrl->getCmd();
135  $cmdClass = $this->ctrl->getCmdClass();
136  switch (strtolower($cmdClass)) {
137  case 'ilcommentgui':
138  $this->commentGUI->executeCommand();
139  break;
140  default:
141  $this->$cmd();
142  break;
143  }
144  }
145 
146  public function jumpToRecord(): void
147  {
148  $this->ctrl->setParameterByClass(self::class, 'record_id', $this->record_id);
149  $this->ctrl->redirectByClass(self::class, 'renderRecord');
150  }
151 
152  public function renderRecord(bool $editComments = false): void
153  {
154  global $DIC;
155  $x = $DIC->help();
156  $DIC->help()->setScreenId('dcl_record');
157  $ilTabs = $DIC->tabs();
158  $tpl = $DIC->ui()->mainTemplate();
159  $ilCtrl = $DIC->ctrl();
160 
161  $rctpl = new ilDataCollectionGlobalTemplate("tpl.record_view.html", false, true, "components/ILIAS/DataCollection");
162 
163  $ilTabs->setTabActive("id_content");
164 
165  if (!$this->tableview_id) {
166  $ilCtrl->redirectByClass(ilDclRecordListGUI::class, "listRecords");
167  }
168 
169  // see ilObjDataCollectionGUI->executeCommand about instantiation
170  $pageObj = new ilDclDetailedViewDefinitionGUI($this->tableview_id);
171  $pageObj->setOutputMode($pageObj::PRESENTATION);
172 
173  $html = $pageObj->showPage();
174  $rctpl->addCss("./assets/css/content.css");
175  $rctpl->fillCssFiles();
176  $table = ilDclCache::getTableCache($this->record_obj->getTableId());
177  foreach ($table->getRecordFields() as $field) {
178  //ILIAS_Ref_Links
179  $pattern = '/\[dcliln field="' . preg_quote($field->getTitle(), "/") . '"\](.*?)\[\/dcliln\]/';
180  if (preg_match($pattern, $html)) {
181  $html = preg_replace(
182  $pattern,
183  $this->record_obj->getRecordFieldSingleHTML($field->getId(), $this->setOptions("$1")),
184  $html
185  );
186  }
187 
188  //DataCollection Ref Links
189  $pattern = '/\[dclrefln field="' . preg_quote($field->getTitle(), "/") . '"\](.*?)\[\/dclrefln\]/';
190  if (preg_match($pattern, $html)) {
191  $this->currentField = $field;
192  $html = preg_replace_callback($pattern, [$this, "doReplace"], $html);
193  }
194 
195  $pattern = '/\[ext tableOf="' . preg_quote($field->getTitle(), "/") . '" field="(.*?)"\]/';
196  if (preg_match($pattern, $html)) {
197  $this->currentField = $field;
198  $html = preg_replace_callback($pattern, [$this, "doExtReplace"], $html);
199  }
200 
201  $html = str_ireplace(
202  "[" . $field->getTitle() . "]",
203  $this->record_obj->getRecordFieldSingleHTML($field->getId(), ['tableview_id' => $this->tableview_id]),
204  $html
205  );
206  }
207  foreach ($table->getStandardFields() as $field) {
208  $html = str_ireplace(
209  "[" . $field->getId() . "]",
210  $this->record_obj->getRecordFieldSingleHTML($field->getId(), ['tableview_id' => $this->tableview_id]),
211  $html
212  );
213  }
214  $rctpl->setVariable("CONTENT", $html);
215 
216  //Permanent Link
217  $tpl->setPermanentLink(
218  'dcl',
219  filter_input(INPUT_GET, 'ref_id', FILTER_VALIDATE_INT),
220  $this->table->getId() . '_' . $this->tableview_id . '_' . $this->record_obj->getId()
221  );
222 
223  // Buttons for previous/next records
224 
225  if ($this->is_enabled_paging) {
226  $record_ids = ilSession::get('dcl_record_ids');
227  if ($record_ids !== [] && array_search($this->record_id, $record_ids) !== false) {
228  $DIC->toolbar()->addComponent(
229  $DIC->ui()->factory()->viewControl()->pagination()
230  ->withTargetURL($this->ctrl->getLinkTargetByClass(self::class, 'jumpToRecord'), 'record_pos')
231  ->withPageSize(1)
232  ->withDropdownAt(5)
233  ->withDropdownLabel($this->lng->txt('entry_of'))
234  ->withCurrentPage(array_search($this->record_id, $record_ids))
235  ->withTotalEntries(count($record_ids))
236  ->withMaxPaginationButtons(20)
237  );
238  }
239  }
240 
241  if ($this->record_obj->hasPermissionToEdit($this->dcl_gui_object->getRefId())) {
242  $DIC->toolbar()->addSpacer('100%');
243  $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, 'table_id', $this->table->getId());
244  $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, 'tableview_id', $this->tableview_id);
245  $this->ctrl->setParameterByClass(ilDclRecordEditGUI::class, 'redirect', ilDclRecordEditGUI::REDIRECT_DETAIL);
246  $this->ctrl->saveParameterByClass(ilDclRecordEditGUI::class, 'record_id');
247  $DIC->toolbar()->addComponent($DIC->ui()->factory()->button()->standard(
248  $this->lng->txt('dcl_edit_record'),
249  $this->ctrl->getLinkTargetByClass(ilDclRecordEditGUI::class, 'edit')
250  ));
251  }
252 
253  // Comments
254  if ($this->table->getPublicCommentsEnabled()) {
255  $rctpl->setVariable('COMMENTS', $this->renderComments($editComments));
256  }
257 
258  $tpl->setContent($rctpl->get());
259  }
260 
261  public function doReplace(array $found): string
262  {
263  return $this->record_obj->getRecordFieldSingleHTML($this->currentField->getId(), $this->setOptions($found[1]));
264  }
265 
266  public function doExtReplace(array $found): ?string
267  {
268  $ref_rec_ids = $this->record_obj->getRecordFieldValue($this->currentField->getId());
269  if (!is_array($ref_rec_ids)) {
270  $ref_rec_ids = [$ref_rec_ids];
271  }
272  if (!count($ref_rec_ids) || !$ref_rec_ids) {
273  return null;
274  }
275  $ref_recs = [];
276  foreach ($ref_rec_ids as $ref_rec_id) {
277  $ref_recs[] = ilDclCache::getRecordCache($ref_rec_id);
278  }
279  $field = $ref_recs[0]->getTable()->getFieldByTitle($found[1]);
280 
281  $tpl = new ilTemplate("tpl.reference_list.html", true, true, "components/ILIAS/DataCollection");
282  $tpl->setCurrentBlock("reference_list");
283 
284  if (!$field) {
285  if (ilObjDataCollectionAccess::hasWriteAccess($this->dcl_gui_object->getRefId())) {
286  $this->main_tpl->setOnScreenMessage(
287  'info',
288  "Bad Viewdefinition at [ext tableOf=\"" . $found[1] . "\" ...]",
289  true
290  );
291  }
292 
293  return null;
294  }
295 
296  foreach ($ref_recs as $ref_record) {
297  $tpl->setCurrentBlock("reference");
298  $tpl->setVariable("CONTENT", $ref_record->getRecordFieldHTML($field->getId()));
299  $tpl->parseCurrentBlock();
300  }
301  return $tpl->get();
302  }
303 
304  protected function renderComments(bool $edit = false): string
305  {
306  if (!$edit) {
307  return $this->commentGUI->getListHTML();
308  } else {
309  return $this->commentGUI->addNoteForm();
310  }
311  }
312 
313  private function setOptions(string $link_name): array
314  {
315  $options = [];
316  $options['link']['display'] = true;
317  $options['link']['name'] = $link_name;
318 
319  return $options;
320  }
321 
325  private function loadSession(): void
326  {
327  $list = new ilDclRecordListTableGUI(
328  new ilDclRecordListGUI($this->dcl_gui_object, $this->table->getId(), $this->tableview_id),
329  "listRecords",
330  $this->table,
331  $this->tableview_id
332  );
333  $list->initFilter();
334  $list->determineOffsetAndOrder();
335  $this->table->getPartialRecords(
336  (string) $this->table->getId(),
337  $list->getOrderField(),
338  $list->getOrderDirection(),
339  $list->getLimit(),
340  $list->getOffset(),
341  $list->getFilter()
342  );
343  }
344 
345  protected function checkAccess(): bool
346  {
347  $has_accass = ilObjDataCollectionAccess::hasAccessTo($this->dcl_gui_object->getRefId(), $this->table->getId(), $this->tableview_id);
348  $is_active = ilDclDetailedViewDefinition::isActive($this->tableview_id);
349  return $has_accass && $is_active;
350  }
351 }
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.
ilGlobalTemplateInterface $main_tpl
ilDclDetailedViewDefinitionGUI: ilPageEditorGUI, ilEditClipboardGUI, ilMediaPoolTargetSelector ilDcl...
$objId
Definition: xapitoken.php:55
ilDclBaseFieldModel $currentField
ilDclBaseRecordModel $record_obj
ilObjDataCollectionGUI: ilInfoScreenGUI, ilNoteGUI, ilCommonActionDispatcherGUI ilObjDataCollectionG...
static http()
Fetches the global http state from ILIAS.
static getTableCache(int $table_id=null)
ilObjDataCollectionGUI $dcl_gui_object
global $DIC
Definition: shib_login.php:25
getRecordFields()
Returns all fields of this table which are NOT standard fields.
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.
__construct(ilObjDataCollectionGUI $a_dcl_object, int $tableview_id)
ilDclDetailedViewGUI: ilDclDetailedViewDefinitionGUI, ilEditClipboardGUI, ilCommentGUI ...