ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.arIndexTableGUI.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Table/classes/class.ilTable2GUI.php');
3require_once('./Services/ActiveRecord/class.ActiveRecordList.php');
4require_once('./Services/ActiveRecord/Views/Index/class.arIndexTableField.php');
5require_once('./Services/ActiveRecord/Views/Index/class.arIndexTableFields.php');
6require_once('./Services/ActiveRecord/Views/Index/class.arIndexTableAction.php');
7require_once('./Services/ActiveRecord/Views/Index/class.arIndexTableActions.php');
8require_once('./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php');
9
10
19
23 protected $ctrl;
27 protected $tabs;
31 protected $access;
35 protected $actions;
39 protected $multi_item_actions = array();
43 protected $toolbar = NULL;
47 protected $table_title = '';
51 protected $fields = NULL;
55 protected $active_record_list = NULL;
59 protected $parent_obj = NULL;
60
61
67 public function __construct(arGUI $a_parent_obj, $a_parent_cmd, ActiveRecordList $active_record_list) {
68 global $ilCtrl, $ilTabs, $ilAccess;
69 $this->ctrl = $ilCtrl;
70 $this->tabs = $ilTabs;
71 $this->access = $ilAccess;
72
73 $this->parent_obj = $a_parent_obj;
74
75 $this->active_record_list = $active_record_list;
76
77 $this->setId(strtolower(get_class($this->active_record_list->getAR()) . "_index"));
78
80
81 parent::__construct($a_parent_obj, $a_parent_cmd);
82
84 }
85
86
87 protected function initBeforeParentConstructor() {
88 $this->initFields();
89 $this->initActions();
90 $this->initMultiItemActions();
92 }
93
94
95 protected function initAfterParentConstructor() {
96 $this->initFormAction();
97 $this->initCommandButtons();
98 $this->initToolbar();
99
100 $this->initTableFilter();
101 $this->initRowSelector();
102
103 $this->initTableRowTemplate();
104 $this->initTableColumns();
105 $this->initTableData();
106 }
107
108
109 protected function initTitle() {
110 $this->setTableTitle($this->getId());
111 }
112
113
114 protected function initFields() {
115 $this->fields = new arIndexTableFields($this->active_record_list->getAR());
116 $this->customizeFields();
117 $this->fields->sortFields();
118 }
119
120
124 protected function customizeFields() {
125 }
126
127
128 protected function initActions() {
129 global $lng;
130
131 $this->addAction(new arIndexTableAction('view', $lng->txt('view'), get_class($this->parent_obj), 'view', 'view'));
132 $this->addAction(new arIndexTableAction('edit', $lng->txt('edit'), get_class($this->parent_obj), 'edit', 'write'));
133 $this->addAction(new arIndexTableAction('delete', $lng->txt('delete'), get_class($this->parent_obj), 'delete', 'write'));
134 }
135
136
140 protected function addAction(arIndexTableAction $action) {
141 if (!$this->getActions()) {
142 $this->setActions(new arIndexTableActions());
143 }
144 $this->actions->addAction($action);
145 }
146
147
148 protected function initFormAction() {
149 $this->setFormAction($this->ctrl->getFormAction($this->parent_obj));
150 }
151
152
153 protected function initCommandButtons() {
154 }
155
156
157 protected function initToolbar() {
158 if ($this->getActions() && $this->getActions()->getAction("edit")) {
159 $toolbar = new ilToolbarGUI();
160 $toolbar->addButton($this->getAddButtonTxt(), $this->ctrl->getLinkTarget($this->parent_obj, "add"));
161 $this->setToolbar($toolbar);
162 }
163 }
164
165
169 protected function getAddButtonTxt() {
170 return $this->txt("add_item");
171 }
172
173
174 protected function initMultiItemActions() {
175 if ($this->getActions() && $this->getActions()->getAction("delete")) {
176 $this->addMutliItemAction(new arIndexTableAction('delete', $this->getMultiDeleteTxt(), get_class($this->parent_obj), 'delete'));
177 }
178 }
179
180
184 protected function getMultiDeleteTxt() {
185 return $this->txt("delete", false);
186 }
187
188
192 public function addMutliItemAction(arIndexTableAction $action) {
193 if (!$this->getMultiItemActions()) {
195 }
196 $this->multi_item_actions->addAction($action);
197 }
198
199
200 protected function initMultiItemActionsButton() {
201 if ($this->getMultiItemActions()) {
202 $this->addMultiItemSelectionButton("index_table_multi_action", $this->multi_item_actions->getActionsAsKeyTextArray(), "multiAction", $this->txt('execute', false));
203 $this->setSelectAllCheckbox("id[]");
204 }
205 }
206
207
213 public function getSelectableColumns() {
214 return $this->getFields()->getSelectableColumns($this);
215 }
216
217
221 protected function initTableFilter() {
222 $this->setFilterCols(7);
223 $this->setFilterCommand("applyFilter");
224 $this->setResetCommand("resetFilter");
225
226 $fields = $this->getFieldsAsArray();
227
228 foreach ($fields as $field) {
232 if ($field->getHasFilter()) {
233 $this->addFilterField($field);
234 }
235 }
236 }
237
238
242 protected function addFilterField(arIndexTableField $field) {
243 switch ($field->getFieldType()) {
244 case 'integer':
245 case 'float':
246 $this->addFilterItemByMetaType($field->getName(), self::FILTER_NUMBER_RANGE, false, $this->txt($field->getTxt()));
247 break;
248 case 'text':
249 case 'clob':
250 $this->addFilterItemByMetaType($field->getName(), self::FILTER_TEXT, false, $this->txt($field->getTxt()));
251 break;
252 case 'date':
253 $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATE_RANGE, false, $this->txt($field->getTxt()));
254 break;
255 case 'time':
256 case 'timestamp':
257 $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATETIME_RANGE, false, $this->txt($field->getTxt()));
258 break;
259 }
260 }
261
262
267 protected function addFilterItemToForm(ilFormPropertyGUI $item, $optional = false) {
271 $this->addFilterItem($item, $optional);
272 $item->readFromSession();
273 $this->filter_array[$item->getPostVar()] = $item->getValue();
274 }
275
276
277 protected function initRowSelector() {
278 $this->setShowRowsSelector(true);
279 }
280
281
286 protected function initTableRowTemplate() {
287 $this->setRowTemplate('tpl.record_row.html', './Services/ActiveRecord/');
288 }
289
290
294 protected function initTableColumns() {
296
297 foreach ($this->getFieldsAsArray() as $field) {
301 if ($this->checkColumnVisibile($field)) {
302 if ($field->getSortable()) {
303 $this->addColumn($this->txt($field->getTxt()), $field->getName());
304 } else {
305 $this->addColumn($this->txt($field->getTxt()));
306 }
307 }
308 }
309 if ($this->getActions()) {
310 $this->addColumn($this->txt('actions', false));
311 }
312 }
313
314
315 protected function addMultipleSelectionColumn() {
316 if ($this->getMultiItemActions()) {
317 $this->addColumn("", "", 1);
318 }
319 }
320
321
327 protected function checkColumnVisibile(arIndexTableField $field) {
328 return ($field->getVisible() && !$this->getSelectableColumns()) || $this->isColumnSelected($field->getName());
329 }
330
331
332 protected function initTableData() {
333 $this->active_record_list->getArWhereCollection()->setStatements(NULL);
334 $this->active_record_list->getArJoinCollection()->setStatements(NULL);
335 $this->active_record_list->getArLimitCollection()->setStatements(NULL);
336 $this->active_record_list->getArOrderCollection()->setStatements(NULL);
337
338 $this->filterTableData();
339 $this->beforeGetData();
341 $ar_data = $this->active_record_list->getArray();
342 $data = array();
343
344 foreach ($ar_data as $key => $item) {
345 $data[$key] = array();
346 foreach ($this->getFields()->getFieldsForDisplay() as $field) {
350 if (array_key_exists($field->getName(), $item)) {
351 if (!$item[$field->getName()]) {
352 $data[$key][$field->getName()] = $this->setEmptyFieldData($field, $item);
353 } elseif ($field->getIsCreatedByField()) {
354 $data[$key][$field->getName()] = $this->setArCreatedByField($field, $item, $item[$field->getName()]);
355 } elseif ($field->getIsModifiedByField()) {
356 $data[$key][$field->getName()] = $this->setArModifiedByField($field, $item, $item[$field->getName()]);
357 } else {
358 $data[$key][$field->getName()] = $this->setArFieldData($field, $item, $item[$field->getName()]);
359 }
360 } else {
361 $data[$key][$field->getName()] = $this->setCustomFieldData($field, $item);
362 }
363 }
364 }
365 $this->setData($data);
366 }
367
368
369 protected function filterTableData() {
370 $filters = $this->getFilterItems();
371 if ($filters) {
372 foreach ($filters as $filter) {
376 $this->addFilterWhere($filter, $filter->getPostVar(), $filter->getValue());
377 }
378 }
379 }
380
381
387 protected function addFilterWhere(ilFormPropertyGUI $filter, $name, $value) {
388
389 switch (get_class($filter)) {
390 case 'ilTextInputGUI':
391 $this->addFilterTextWhere($filter, $name, $value);
392
393 return;
394 case 'ilCombinationInputGUI':
395 if (is_object($value["from"]) || is_object($value["to"])) {
396 if (get_class($value["from"]) == "ilDateTime" || get_class($value["to"]) == "ilDateTime") {
397 $this->addFilterDateTimeWhere($filter, $name, $value);
398
399 return;
400 }
401 if (get_class($value["from"]) == "ilDate" || get_class($value["to"]) == "ilDate") {
402 $this->addFilterDateWhere($filter, $name, $value);
403
404 return;
405 }
406 $this->addFilterCustomWhere($filter, $name, $value);
407
408 return;
409 }
410
411 $this->addFilterNumericWhere($filter, $name, $value);
412 break;
413 default:
414 $this->addFilterCustomWhere($filter, $name, $value);
415
416 return;
417 }
418 }
419
420
426 protected function addFilterNumericWhere(ilCombinationInputGUI $filter, $name, $value) {
427
428 if ($value["from"] != "" OR $value["to"] != "") {
429 if ($value["from"] == "") {
430 $value["from"] = 0;
431 }
432 if ($value["to"] == "") {
433 $value["to"] = PHP_INT_MAX;
434 }
435 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " >= " . $value["from"]
436 . " AND " . $this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " <= " . $value["to"]);
437 }
438 }
439
440
446 protected function addFilterTextWhere(ilTextInputGUI $filter, $name, $value) {
447 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " like '%" . $value . "%'");
448 }
449
450
456 protected function addFilterDateWhere(ilCombinationInputGUI $filter, $name, $value) {
457 if ($value["from"] != NULL OR $value["to"] != NULL) {
458 if ($value["from"] == NULL) {
459 $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
460 }
461 if ($value["to"] == NULL) {
462 $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
463 }
464 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
465 . $value["from"]->get(IL_CAL_DATE) . "' AND '" . $value["to"]->get(IL_CAL_DATE) . "'");
466 }
467 }
468
469
475 protected function addFilterDateTimeWhere(ilCombinationInputGUI $filter, $name, $value) {
476 if ($value["from"] != NULL OR $value["to"] != NULL) {
477 if ($value["from"] == NULL) {
478 $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
479 }
480 if ($value["to"] == NULL) {
481 $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
482 }
483 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
484 . $value["from"]->get(IL_CAL_DATETIME) . "' AND '" . $value["to"]->get(IL_CAL_DATETIME) . "'");
485 }
486 }
487
488
489 protected function addFilterCustomWhere() {
490 }
491
492
493 protected function beforeGetData() {
494 }
495
496
497 protected function setOrderAndSegmentation() {
498 $this->setExternalSorting(true);
499 $this->setExternalSegmentation(true);
500 if (!$this->getDefaultOrderField()) {
501 $this->setDefaultOrderField($this->active_record_list->getAR()->getArFieldList()->getPrimaryField()->getName());
502 }
503 $this->determineLimit();
505 $this->setMaxCount($this->active_record_list->count());
506 $this->active_record_list->orderBy($this->getOrderField(), $this->getOrderDirection());
507 $this->active_record_list->limit($this->getOffset(), $this->getLimit());
508 }
509
510
517 protected function setEmptyFieldData(arIndexTableField $field, $item) {
518 return "";
519 }
520
521
529 protected function setArModifiedByField(arIndexTableField $field, $item, $value) {
530 $user = new ilObjUser($value);
531
532 return $user->getPublicName();
533 }
534
535
543 protected function setArCreatedByField(arIndexTableField $field, $item, $value) {
544 $user = new ilObjUser($value);
545
546 return $user->getPublicName();
547 }
548
549
557 protected function setArFieldData(arIndexTableField $field, $item, $value) {
558 switch ($field->getFieldType()) {
559 case 'integer':
560 case 'float':
561 case 'text':
562 case 'clob':
563 return $value;
564 case 'date':
565 case 'time':
566 case 'timestamp':
567 return $this->setDateFieldData($field, $item, $value);
568 }
569
570 return "";
571 }
572
573
581 protected function setDateFieldData(arIndexTableField $field, $item, $value) {
582 $datetime = new ilDateTime($value, IL_CAL_DATETIME);
583
585 }
586
587
594 protected function setCustomFieldData(arIndexTableField $field, $item) {
595 return "CUSTOM-OVERRIDE: setCustomFieldData";
596 }
597
598
605 final function fillRow($a_set) {
606 $this->setCtrlParametersForRow($a_set);
607 $this->addMultiItemActionCheckboxToRow($a_set);
608 $this->parseRow($a_set);
609 $this->addActionsToRow($a_set);
610 }
611
612
616 protected function setCtrlParametersForRow($a_set) {
617 $this->ctrl->setParameterByClass(get_class($this->parent_obj), 'ar_id', self::domid_encode($a_set[$this->getFields()->getPrimaryField()
618 ->getName()]));
619 }
620
621
625 protected function addMultiItemActionCheckboxToRow($a_set) {
626 if ($this->getMultiItemActions()) {
627 $this->tpl->setVariable('ID', self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
628 }
629 }
630
631
635 protected function parseRow($a_set) {
636 foreach ($a_set as $key => $value) {
637 $field = $this->getField($key);
638 if ($this->checkColumnVisibile($field)) {
639 $this->parseEntry($field, $value);
640 }
641 }
642 }
643
644
649 protected function parseEntry(arIndexTableField $field, $value) {
650 $this->tpl->setCurrentBlock('entry');
651 $this->tpl->setVariable('ENTRY_CONTENT', $value);
652 $this->tpl->parseCurrentBlock('entry');
653 }
654
655
659 protected function addActionsToRow($a_set) {
660 if ($this->getActions()) {
661 $alist = new ilAdvancedSelectionListGUI();
662 $alist->setId(self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
663 $alist->setListTitle($this->txt('actions', false));
664
665 foreach ($this->getActionsAsArray() as $action) {
669 $access = true;
670 if ($action->getAccess()) {
671 $access = $this->access->checkAccess($action->getAccess(), '', $_GET['ref_id']);
672 }
673 if ($access) {
674 $alist->addItem($action->getTitle(), $action->getId(), $this->ctrl->getLinkTargetByClass($action->getTargetClass(), $action->getTargetCmd()));
675 }
676 }
677
678 $this->tpl->setVariable('ACTION', $alist->getHTML());
679 }
680 }
681
682
686 public function render() {
687
688 $index_table_tpl = new ilTemplate("tpl.index_table.html", true, true, "./Services/ActiveRecord/");
689 if ($this->getToolbar()) {
690 $index_table_tpl->setVariable("TOOLBAR", $this->getToolbar()->getHTML());
691 }
692
693 $index_table_tpl->setVariable("TABLE", parent::render());
694
695 return $index_table_tpl->get();
696 }
697
698
705 public function txt($txt, $plugin_txt = true) {
706 return $this->parent_obj->txt($txt, $plugin_txt);
707 }
708
709
710 public function applyFilter() {
711 $this->writeFilterToSession();
712 $this->resetOffset();
713 $this->initTableData();
714 }
715
716
717 public function resetFilter() {
718 parent::resetFilter();
719 $this->resetOffset();
720 $this->initTableData();
721 }
722
723
731 public static function domid_encode($id_to_encode) {
732 $encoded_id = NULL;
733 if (!empty($id_to_encode)) {
734 $encoded_id = preg_replace_callback('/([^a-zA-Z0-9])/', function ($matches) {
735 return "__idstart_" . strtolower(dechex(ord($matches[0]))) . "_idend__";
736 }, $id_to_encode);
737 }
738
739 return $encoded_id;
740 }
741
742
749 public static function domid_decode($id_to_decode) {
750 $decoded_id = "";
751 if (!empty($id_to_decode)) {
752 $decoded_id = preg_replace_callback('/__idstart_(.{2})_idend__/', function ($matches) {
753 return chr(hexdec($matches[1]));
754 }, $id_to_decode);
755 }
756
757 return $decoded_id;
758 }
759
760
765 $this->fields = $fields;
766 }
767
768
772 public function getFields() {
773 return $this->fields;
774 }
775
776
780 public function getFieldsAsArray() {
781 return $this->getFields()->getFields();
782 }
783
784
790 public function getField($field_name) {
791 return $this->getFields()->getField($field_name);
792 }
793
794
798 public function addField(arIndexTableField $field) {
799 $this->getFields()->addField($field);
800 }
801
802
806 public function setTableTitle($table_title) {
807 $this->table_title = $table_title;
808 }
809
810
814 public function getTableTitle() {
815 return $this->table_title;
816 }
817
818
822 public function setToolbar($toolbar) {
823 $this->toolbar = $toolbar;
824 }
825
826
830 public function getToolbar() {
831 return $this->toolbar;
832 }
833
834
838 public function setActions($actions) {
839 $this->actions = $actions;
840 }
841
842
846 public function getActions() {
847 return $this->actions;
848 }
849
850
854 public function getActionsAsArray() {
855 return $this->actions->getActions();
856 }
857
858
863 $this->multi_item_actions = $multi_item_actions;
864 }
865
866
870 public function getMultiItemActions() {
872 }
873
874
878 public function getMultiItemActionsAsArray() {
880 }
881}
$_GET["client_id"]
Class ActiveRecordList.
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
GUI-Class arIndexTableAction.
GUI-Class arIndexTableActions.
GUI-Class arIndexTableField.
GUI-Class arIndexTableFields.
GUI-Class arIndexTableGUI.
$fields
arIndexTableFields
setArFieldData(arIndexTableField $field, $item, $value)
addFilterWhere(ilFormPropertyGUI $filter, $name, $value)
addMultiItemActionCheckboxToRow($a_set)
setMultiItemActions($multi_item_actions)
getSelectableColumns()
Get selectable columns.
addAction(arIndexTableAction $action)
parseEntry(arIndexTableField $field, $value)
addFilterTextWhere(ilTextInputGUI $filter, $name, $value)
setCustomFieldData(arIndexTableField $field, $item)
setDateFieldData(arIndexTableField $field, $item, $value)
addFilterDateTimeWhere(ilCombinationInputGUI $filter, $name, $value)
setTableTitle($table_title)
setEmptyFieldData(arIndexTableField $field, $item)
static domid_decode($id_to_decode)
setArModifiedByField(arIndexTableField $field, $item, $value)
resetFilter()
Reset filter.
setFields(arIndexTableFields $fields)
customizeFields()
@description To be overridden
__construct(arGUI $a_parent_obj, $a_parent_cmd, ActiveRecordList $active_record_list)
setArCreatedByField(arIndexTableField $field, $item, $value)
addFilterNumericWhere(ilCombinationInputGUI $filter, $name, $value)
txt($txt, $plugin_txt=true)
checkColumnVisibile(arIndexTableField $field)
addField(arIndexTableField $field)
addFilterField(arIndexTableField $field)
addMutliItemAction(arIndexTableAction $action)
static domid_encode($id_to_encode)
addFilterDateWhere(ilCombinationInputGUI $filter, $name, $value)
User interface class for advanced drop-down selection lists.
This class represents a number property in a property form.
static formatDate(ilDateTime $date)
Format a date @access public.
@classDescription Date and time handling
This class represents a property in a property form.
readFromSession()
Read from session.
getPostVar()
Get Post Variable.
Class ilTable2GUI.
getHTML()
Get HTML.
setExternalSorting($a_val)
Set external sorting.
addColumn($a_text, $a_sort_field="", $a_width="", $a_is_checkbox_action_column=false, $a_class="", $a_tooltip="", $a_tooltip_with_html=false)
Add a column to the header.
setShowRowsSelector($a_value)
Toggle rows-per-page selector.
getFilterItems($a_optionals=false)
Get filter items.
determineOffsetAndOrder($a_omit_offset=false)
Determine offset and order.
getId()
Get element id.
setData($a_data)
set table data @access public
setResetCommand($a_val, $a_caption=null)
Set reset filter command.
getLimit()
Get limit.
setRowTemplate($a_template, $a_template_dir="")
Set row template.
resetOffset($a_in_determination=false)
Reset offset.
addFilterItem($a_input_item, $a_optional=false)
Add filter item.
setDefaultOrderField($a_defaultorderfield)
Set Default order field.
setSelectAllCheckbox($a_select_all_checkbox)
Set the name of the checkbox that should be toggled with a select all button.
getOffset()
Get offset.
setExternalSegmentation($a_val)
Set external segmentation.
setId($a_val)
Set id.
writeFilterToSession()
Write filter values to session.
setFilterCols($a_val)
Set filter columns.
setFormAction($a_form_action, $a_multipart=false)
Set Form action parameter.
addFilterItemByMetaType($id, $type=self::FILTER_TEXT, $a_optional=false, $caption=NULL)
Add filter by standard type.
addMultiItemSelectionButton($a_sel_var, $a_options, $a_cmd, $a_text, $a_default_selection='')
Add Selection List + Command button for selected items.
determineLimit()
Determine the limit.
setFilterCommand($a_val, $a_caption=null)
Set filter command.
getDefaultOrderField()
Get Default order field.
isColumnSelected($a_col)
Is given column selected?
getOrderDirection()
Get order direction.
setMaxCount($a_max_count)
set max.
special template class to simplify handling of ITX/PEAR
This class represents a text property in a property form.
$txt
Definition: error.php:12
global $ilCtrl
Definition: ilias.php:18
global $lng
Definition: privfeed.php:17
$errors fields
Definition: imgupload.php:52