ILIAS  release_7 Revision v7.30-3-g800a261c036
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');
8
17{
18
22 protected $ctrl;
26 protected $tabs;
30 protected $access;
34 protected $actions;
38 protected $multi_item_actions = array();
42 protected $toolbar = null;
46 protected $table_title = '';
50 protected $fields = null;
54 protected $active_record_list = null;
58 protected $parent_obj = null;
59
60
66 public function __construct(arGUI $a_parent_obj, $a_parent_cmd, ActiveRecordList $active_record_list)
67 {
68 global $DIC;
69 $ilCtrl = $DIC['ilCtrl'];
70 $ilTabs = $DIC['ilTabs'];
71 $ilAccess = $DIC['ilAccess'];
72 $this->ctrl = $ilCtrl;
73 $this->tabs = $ilTabs;
74 $this->access = $ilAccess;
75
76 $this->parent_obj = $a_parent_obj;
77
78 $this->active_record_list = $active_record_list;
79
80 $this->setId(strtolower(get_class($this->active_record_list->getAR()) . "_index"));
81
83
84 parent::__construct($a_parent_obj, $a_parent_cmd);
85
87 }
88
89
90 protected function initBeforeParentConstructor()
91 {
92 $this->initFields();
93 $this->initActions();
94 $this->initMultiItemActions();
96 }
97
98
99 protected function initAfterParentConstructor()
100 {
101 $this->initFormAction();
102 $this->initCommandButtons();
103 $this->initToolbar();
104
105 $this->initTableFilter();
106 $this->initRowSelector();
107
108 $this->initTableRowTemplate();
109 $this->initTableColumns();
110 $this->initTableData();
111 }
112
113
114 protected function initTitle()
115 {
116 $this->setTableTitle($this->getId());
117 }
118
119
120 protected function initFields()
121 {
122 $this->fields = new arIndexTableFields($this->active_record_list->getAR());
123 $this->customizeFields();
124 $this->fields->sortFields();
125 }
126
127
131 protected function customizeFields()
132 {
133 }
134
135
136 protected function initActions()
137 {
138 global $DIC;
139 $lng = $DIC['lng'];
140
141 $this->addAction(new arIndexTableAction('view', $lng->txt('view'), get_class($this->parent_obj), 'view', 'view'));
142 $this->addAction(new arIndexTableAction('edit', $lng->txt('edit'), get_class($this->parent_obj), 'edit', 'write'));
143 $this->addAction(new arIndexTableAction('delete', $lng->txt('delete'), get_class($this->parent_obj), 'delete', 'write'));
144 }
145
146
150 protected function addAction(arIndexTableAction $action)
151 {
152 if (!$this->getActions()) {
153 $this->setActions(new arIndexTableActions());
154 }
155 $this->actions->addAction($action);
156 }
157
158
159 protected function initFormAction()
160 {
161 $this->setFormAction($this->ctrl->getFormAction($this->parent_obj));
162 }
163
164
165 protected function initCommandButtons()
166 {
167 }
168
169
170 protected function initToolbar()
171 {
172 if ($this->getActions() && $this->getActions()->getAction("edit")) {
173 $toolbar = new ilToolbarGUI();
174 $toolbar->addButton($this->getAddButtonTxt(), $this->ctrl->getLinkTarget($this->parent_obj, "add"));
175 $this->setToolbar($toolbar);
176 }
177 }
178
179
183 protected function getAddButtonTxt()
184 {
185 return $this->txt("add_item");
186 }
187
188
189 protected function initMultiItemActions()
190 {
191 if ($this->getActions() && $this->getActions()->getAction("delete")) {
192 $this->addMutliItemAction(new arIndexTableAction('delete', $this->getMultiDeleteTxt(), get_class($this->parent_obj), 'delete'));
193 }
194 }
195
196
200 protected function getMultiDeleteTxt()
201 {
202 return $this->txt("delete", false);
203 }
204
205
209 public function addMutliItemAction(arIndexTableAction $action)
210 {
211 if (!$this->getMultiItemActions()) {
213 }
214 $this->multi_item_actions->addAction($action);
215 }
216
217
218 protected function initMultiItemActionsButton()
219 {
220 if ($this->getMultiItemActions()) {
221 $this->addMultiItemSelectionButton("index_table_multi_action", $this->multi_item_actions->getActionsAsKeyTextArray(), "multiAction", $this->txt('execute', false));
222 $this->setSelectAllCheckbox("id[]");
223 }
224 }
225
226
232 public function getSelectableColumns()
233 {
234 return $this->getFields()->getSelectableColumns($this);
235 }
236
237
241 protected function initTableFilter()
242 {
243 $this->setFilterCols(7);
244 $this->setFilterCommand("applyFilter");
245 $this->setResetCommand("resetFilter");
246
247 $fields = $this->getFieldsAsArray();
248
249 foreach ($fields as $field) {
253 if ($field->getHasFilter()) {
254 $this->addFilterField($field);
255 }
256 }
257 }
258
259
263 protected function addFilterField(arIndexTableField $field)
264 {
265 switch ($field->getFieldType()) {
266 case 'integer':
267 case 'float':
268 $this->addFilterItemByMetaType($field->getName(), self::FILTER_NUMBER_RANGE, false, $this->txt($field->getTxt()));
269 break;
270 case 'text':
271 case 'clob':
272 $this->addFilterItemByMetaType($field->getName(), self::FILTER_TEXT, false, $this->txt($field->getTxt()));
273 break;
274 case 'date':
275 $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATE_RANGE, false, $this->txt($field->getTxt()));
276 break;
277 case 'time':
278 case 'timestamp':
279 $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATETIME_RANGE, false, $this->txt($field->getTxt()));
280 break;
281 }
282 }
283
284
289 protected function addFilterItemToForm(ilFormPropertyGUI $item, $optional = false)
290 {
294 $this->addFilterItem($item, $optional);
295 $item->readFromSession();
296 $this->filter_array[$item->getPostVar()] = $item->getValue();
297 }
298
299
300 protected function initRowSelector()
301 {
302 $this->setShowRowsSelector(true);
303 }
304
305
310 protected function initTableRowTemplate()
311 {
312 $this->setRowTemplate('tpl.record_row.html', './Services/ActiveRecord/');
313 }
314
315
319 protected function initTableColumns()
320 {
322
323 foreach ($this->getFieldsAsArray() as $field) {
327 if ($this->checkColumnVisibile($field)) {
328 if ($field->getSortable()) {
329 $this->addColumn($this->txt($field->getTxt()), $field->getName());
330 } else {
331 $this->addColumn($this->txt($field->getTxt()));
332 }
333 }
334 }
335 if ($this->getActions()) {
336 $this->addColumn($this->txt('actions', false));
337 }
338 }
339
340
341 protected function addMultipleSelectionColumn()
342 {
343 if ($this->getMultiItemActions()) {
344 $this->addColumn("", "", 1);
345 }
346 }
347
348
354 protected function checkColumnVisibile(arIndexTableField $field)
355 {
356 return ($field->getVisible() && !$this->getSelectableColumns()) || $this->isColumnSelected($field->getName());
357 }
358
359
360 protected function initTableData()
361 {
362 $this->active_record_list->getArWhereCollection()->setStatements(null);
363 $this->active_record_list->getArJoinCollection()->setStatements(null);
364 $this->active_record_list->getArLimitCollection()->setStatements(null);
365 $this->active_record_list->getArOrderCollection()->setStatements(null);
366
367 $this->filterTableData();
368 $this->beforeGetData();
370 $ar_data = $this->active_record_list->getArray();
371 $data = array();
372
373 foreach ($ar_data as $key => $item) {
374 $data[$key] = array();
375 foreach ($this->getFields()->getFieldsForDisplay() as $field) {
379 if (array_key_exists($field->getName(), $item)) {
380 if (!$item[$field->getName()]) {
381 $data[$key][$field->getName()] = $this->setEmptyFieldData($field, $item);
382 } elseif ($field->getIsCreatedByField()) {
383 $data[$key][$field->getName()] = $this->setArCreatedByField($field, $item, $item[$field->getName()]);
384 } elseif ($field->getIsModifiedByField()) {
385 $data[$key][$field->getName()] = $this->setArModifiedByField($field, $item, $item[$field->getName()]);
386 } else {
387 $data[$key][$field->getName()] = $this->setArFieldData($field, $item, $item[$field->getName()]);
388 }
389 } else {
390 $data[$key][$field->getName()] = $this->setCustomFieldData($field, $item);
391 }
392 }
393 }
394 $this->setData($data);
395 }
396
397
398 protected function filterTableData()
399 {
400 $filters = $this->getFilterItems();
401 if ($filters) {
402 foreach ($filters as $filter) {
406 $this->addFilterWhere($filter, $filter->getPostVar(), $filter->getValue());
407 }
408 }
409 }
410
411
417 protected function addFilterWhere(ilFormPropertyGUI $filter, $name, $value)
418 {
419 switch (get_class($filter)) {
420 case 'ilTextInputGUI':
421 $this->addFilterTextWhere($filter, $name, $value);
422
423 return;
424 case 'ilCombinationInputGUI':
425 if (is_object($value["from"]) || is_object($value["to"])) {
426 if (get_class($value["from"]) == "ilDateTime" || get_class($value["to"]) == "ilDateTime") {
427 $this->addFilterDateTimeWhere($filter, $name, $value);
428
429 return;
430 }
431 if (get_class($value["from"]) == "ilDate" || get_class($value["to"]) == "ilDate") {
432 $this->addFilterDateWhere($filter, $name, $value);
433
434 return;
435 }
436 $this->addFilterCustomWhere($filter, $name, $value);
437
438 return;
439 }
440
441 $this->addFilterNumericWhere($filter, $name, $value);
442 break;
443 default:
444 $this->addFilterCustomWhere($filter, $name, $value);
445
446 return;
447 }
448 }
449
450
456 protected function addFilterNumericWhere(ilCombinationInputGUI $filter, $name, $value)
457 {
458 if ($value["from"] != "" or $value["to"] != "") {
459 if ($value["from"] == "") {
460 $value["from"] = 0;
461 }
462 if ($value["to"] == "") {
463 $value["to"] = PHP_INT_MAX;
464 }
465 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " >= " . $value["from"]
466 . " AND " . $this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " <= " . $value["to"]);
467 }
468 }
469
470
476 protected function addFilterTextWhere(ilTextInputGUI $filter, $name, $value)
477 {
478 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " like '%" . $value . "%'");
479 }
480
481
487 protected function addFilterDateWhere(ilCombinationInputGUI $filter, $name, $value)
488 {
489 if ($value["from"] != null or $value["to"] != null) {
490 if ($value["from"] == null) {
491 $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
492 }
493 if ($value["to"] == null) {
494 $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
495 }
496 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
497 . $value["from"]->get(IL_CAL_DATE) . "' AND '" . $value["to"]->get(IL_CAL_DATE) . "'");
498 }
499 }
500
501
507 protected function addFilterDateTimeWhere(ilCombinationInputGUI $filter, $name, $value)
508 {
509 if ($value["from"] != null or $value["to"] != null) {
510 if ($value["from"] == null) {
511 $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
512 }
513 if ($value["to"] == null) {
514 $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
515 }
516 $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
517 . $value["from"]->get(IL_CAL_DATETIME) . "' AND '" . $value["to"]->get(IL_CAL_DATETIME) . "'");
518 }
519 }
520
521
522 protected function addFilterCustomWhere()
523 {
524 }
525
526
527 protected function beforeGetData()
528 {
529 }
530
531
532 protected function setOrderAndSegmentation()
533 {
534 $this->setExternalSorting(true);
535 $this->setExternalSegmentation(true);
536 if (!$this->getDefaultOrderField()) {
537 $this->setDefaultOrderField($this->active_record_list->getAR()->getArFieldList()->getPrimaryField()->getName());
538 }
539 $this->determineLimit();
541 $this->setMaxCount($this->active_record_list->count());
542 $this->active_record_list->orderBy($this->getOrderField(), $this->getOrderDirection());
543 $this->active_record_list->limit($this->getOffset(), $this->getLimit());
544 }
545
546
553 protected function setEmptyFieldData(arIndexTableField $field, $item)
554 {
555 return "";
556 }
557
558
566 protected function setArModifiedByField(arIndexTableField $field, $item, $value)
567 {
568 $user = new ilObjUser($value);
569
570 return $user->getPublicName();
571 }
572
573
581 protected function setArCreatedByField(arIndexTableField $field, $item, $value)
582 {
583 $user = new ilObjUser($value);
584
585 return $user->getPublicName();
586 }
587
588
596 protected function setArFieldData(arIndexTableField $field, $item, $value)
597 {
598 switch ($field->getFieldType()) {
599 case 'integer':
600 case 'float':
601 case 'text':
602 case 'clob':
603 return $value;
604 case 'date':
605 case 'time':
606 case 'timestamp':
607 return $this->setDateFieldData($field, $item, $value);
608 }
609
610 return "";
611 }
612
613
621 protected function setDateFieldData(arIndexTableField $field, $item, $value)
622 {
623 $datetime = new ilDateTime($value, IL_CAL_DATETIME);
624
626 }
627
628
635 protected function setCustomFieldData(arIndexTableField $field, $item)
636 {
637 return "CUSTOM-OVERRIDE: setCustomFieldData";
638 }
639
640
647 final public function fillRow($a_set)
648 {
649 $this->setCtrlParametersForRow($a_set);
650 $this->addMultiItemActionCheckboxToRow($a_set);
651 $this->parseRow($a_set);
652 $this->addActionsToRow($a_set);
653 }
654
655
659 protected function setCtrlParametersForRow($a_set)
660 {
661 $this->ctrl->setParameterByClass(get_class($this->parent_obj), 'ar_id', self::domid_encode($a_set[$this->getFields()->getPrimaryField()
662 ->getName()]));
663 }
664
665
669 protected function addMultiItemActionCheckboxToRow($a_set)
670 {
671 if ($this->getMultiItemActions()) {
672 $this->tpl->setVariable('ID', self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
673 }
674 }
675
676
680 protected function parseRow($a_set)
681 {
682 foreach ($a_set as $key => $value) {
683 $field = $this->getField($key);
684 if ($this->checkColumnVisibile($field)) {
685 $this->parseEntry($field, $value);
686 }
687 }
688 }
689
690
695 protected function parseEntry(arIndexTableField $field, $value)
696 {
697 $this->tpl->setCurrentBlock('entry');
698 $this->tpl->setVariable('ENTRY_CONTENT', $value);
699 $this->tpl->parseCurrentBlock('entry');
700 }
701
702
706 protected function addActionsToRow($a_set)
707 {
708 if ($this->getActions()) {
709 $alist = new ilAdvancedSelectionListGUI();
710 $alist->setId(self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
711 $alist->setListTitle($this->txt('actions', false));
712
713 foreach ($this->getActionsAsArray() as $action) {
717 $access = true;
718 if ($action->getAccess()) {
719 $access = $this->access->checkAccess($action->getAccess(), '', $_GET['ref_id']);
720 }
721 if ($access) {
722 $alist->addItem($action->getTitle(), $action->getId(), $this->ctrl->getLinkTargetByClass($action->getTargetClass(), $action->getTargetCmd()));
723 }
724 }
725
726 $this->tpl->setVariable('ACTION', $alist->getHTML());
727 }
728 }
729
730
734 public function render()
735 {
736 $index_table_tpl = new ilTemplate("tpl.index_table.html", true, true, "./Services/ActiveRecord/");
737 if ($this->getToolbar()) {
738 $index_table_tpl->setVariable("TOOLBAR", $this->getToolbar()->getHTML());
739 }
740
741 $index_table_tpl->setVariable("TABLE", parent::render());
742
743 return $index_table_tpl->get();
744 }
745
746
753 public function txt($txt, $plugin_txt = true)
754 {
755 return $this->parent_obj->txt($txt, $plugin_txt);
756 }
757
758
759 public function applyFilter()
760 {
761 $this->writeFilterToSession();
762 $this->resetOffset();
763 $this->initTableData();
764 }
765
766
767 public function resetFilter()
768 {
769 parent::resetFilter();
770 $this->resetOffset();
771 $this->initTableData();
772 }
773
774
782 public static function domid_encode($id_to_encode)
783 {
784 $encoded_id = null;
785 if (!empty($id_to_encode)) {
786 $encoded_id = preg_replace_callback('/([^a-zA-Z0-9])/', function ($matches) {
787 return "__idstart_" . strtolower(dechex(ord($matches[0]))) . "_idend__";
788 }, $id_to_encode);
789 }
790
791 return $encoded_id;
792 }
793
794
801 public static function domid_decode($id_to_decode)
802 {
803 $decoded_id = "";
804 if (!empty($id_to_decode)) {
805 $decoded_id = preg_replace_callback('/__idstart_(.{2})_idend__/', function ($matches) {
806 return chr(hexdec($matches[1]));
807 }, $id_to_decode);
808 }
809
810 return $decoded_id;
811 }
812
813
818 {
819 $this->fields = $fields;
820 }
821
822
826 public function getFields()
827 {
828 return $this->fields;
829 }
830
831
835 public function getFieldsAsArray()
836 {
837 return $this->getFields()->getFields();
838 }
839
840
846 public function getField($field_name)
847 {
848 return $this->getFields()->getField($field_name);
849 }
850
851
855 public function addField(arIndexTableField $field)
856 {
857 $this->getFields()->addField($field);
858 }
859
860
865 {
866 $this->table_title = $table_title;
867 }
868
869
873 public function getTableTitle()
874 {
875 return $this->table_title;
876 }
877
878
882 public function setToolbar($toolbar)
883 {
884 $this->toolbar = $toolbar;
885 }
886
887
891 public function getToolbar()
892 {
893 return $this->toolbar;
894 }
895
896
900 public function setActions($actions)
901 {
902 $this->actions = $actions;
903 }
904
905
909 public function getActions()
910 {
911 return $this->actions;
912 }
913
914
918 public function getActionsAsArray()
919 {
920 return $this->actions->getActions();
921 }
922
923
928 {
929 $this->multi_item_actions = $multi_item_actions;
930 }
931
932
936 public function getMultiItemActions()
937 {
939 }
940
941
946 {
948 }
949}
$_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, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
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.
setShowRowsSelector($a_value)
Toggle rows-per-page selector.
getFilterItems($a_optionals=false)
Get filter items.
determineOffsetAndOrder($a_omit_offset=false)
Determine offset and order.
setSelectAllCheckbox($a_select_all_checkbox, $a_select_all_on_top=false)
Set the name of the checkbox that should be toggled with a select all button.
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.
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.
addFilterItemByMetaType($id, $type=self::FILTER_TEXT, $a_optional=false, $caption=null)
Add filter by standard type.
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.
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:13
global $DIC
Definition: goto.php:24
$errors fields
Definition: imgupload.php:51
if($format !==null) $name
Definition: metadata.php:230
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc