ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.arIndexTableGUI.php
Go to the documentation of this file.
1 <?php
2 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/class.srModelObjectTableGUI.php');
3 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/class.ActiveRecordList.php');
4 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Index/class.arIndexTableField.php');
5 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Index/class.arIndexTableFields.php');
6 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Index/class.arIndexTableAction.php');
7 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Index/class.arIndexTableActions.php');
8 
17 
21  protected $ctrl;
25  protected $tabs;
29  protected $access;
33  protected $actions;
37  protected $multi_item_actions = array();
41  protected $toolbar = NULL;
45  protected $table_title = '';
49  protected $fields = NULL;
53  protected $active_record_list = NULL;
57  protected $parent_obj = NULL;
58 
59 
65  public function __construct(arGUI $a_parent_obj, $a_parent_cmd, ActiveRecordList $active_record_list) {
66  global $ilCtrl, $ilTabs, $ilAccess;
67  $this->ctrl = $ilCtrl;
68  $this->tabs = $ilTabs;
69  $this->access = $ilAccess;
70 
71  $this->parent_obj = $a_parent_obj;
72 
73  $this->active_record_list = $active_record_list;
74 
75  $this->setId(strtolower(get_class($this->active_record_list->getAR()) . "_index"));
76 
78 
79  parent::__construct($a_parent_obj, $a_parent_cmd);
80 
82  }
83 
84 
85  protected function initBeforeParentConstructor() {
86  $this->initFields();
87  $this->initActions();
88  $this->initMultiItemActions();
90  }
91 
92 
93  protected function initAfterParentConstructor() {
94  $this->initFormAction();
95  $this->initCommandButtons();
96  $this->initToolbar();
97 
98  $this->initTableFilter();
99  $this->initRowSelector();
100 
101  $this->initTableRowTemplate();
102  $this->initTableColumns();
103  $this->initTableData();
104  }
105 
106 
107  protected function initTitle() {
108  $this->setTableTitle($this->getId());
109  }
110 
111 
112  protected function initFields() {
113  $this->fields = new arIndexTableFields($this->active_record_list->getAR());
114  $this->customizeFields();
115  $this->fields->sortFields();
116  }
117 
118 
122  protected function customizeFields() {
123  }
124 
125 
126  protected function initActions() {
127  global $lng;
128 
129  $this->addAction(new arIndexTableAction('view', $lng->txt('view'), get_class($this->parent_obj), 'view', 'view'));
130  $this->addAction(new arIndexTableAction('edit', $lng->txt('edit'), get_class($this->parent_obj), 'edit', 'write'));
131  $this->addAction(new arIndexTableAction('delete', $lng->txt('delete'), get_class($this->parent_obj), 'delete', 'write'));
132  }
133 
134 
138  protected function addAction(arIndexTableAction $action) {
139  if (!$this->getActions()) {
140  $this->setActions(new arIndexTableActions());
141  }
142  $this->actions->addAction($action);
143  }
144 
145 
146  protected function initFormAction() {
147  $this->setFormAction($this->ctrl->getFormAction($this->parent_obj));
148  }
149 
150 
151  protected function initCommandButtons() {
152  }
153 
154 
155  protected function initToolbar() {
156  if ($this->getActions() && $this->getActions()->getAction("edit")) {
157  $toolbar = new ilToolbarGUI();
158  $toolbar->addButton($this->getAddButtonTxt(), $this->ctrl->getLinkTarget($this->parent_obj, "add"));
159  $this->setToolbar($toolbar);
160  }
161  }
162 
163 
167  protected function getAddButtonTxt() {
168  return $this->txt("add_item");
169  }
170 
171 
172  protected function initMultiItemActions() {
173  if ($this->getActions() && $this->getActions()->getAction("delete")) {
174  $this->addMutliItemAction(new arIndexTableAction('delete', $this->getMultiDeleteTxt(), get_class($this->parent_obj), 'delete'));
175  }
176  }
177 
178 
182  protected function getMultiDeleteTxt() {
183  return $this->txt("delete", false);
184  }
185 
186 
190  public function addMutliItemAction(arIndexTableAction $action) {
191  if (!$this->getMultiItemActions()) {
193  }
194  $this->multi_item_actions->addAction($action);
195  }
196 
197 
198  protected function initMultiItemActionsButton() {
199  if ($this->getMultiItemActions()) {
200  $this->addMultiItemSelectionButton("index_table_multi_action", $this->multi_item_actions->getActionsAsKeyTextArray(), "multiAction", $this->txt('execute', false));
201  $this->setSelectAllCheckbox("id[]");
202  }
203  }
204 
205 
211  public function getSelectableColumns() {
212  return $this->getFields()->getSelectableColumns($this);
213  }
214 
215 
219  protected function initTableFilter() {
220  $this->setFilterCols(7);
221  $this->setFilterCommand("applyFilter");
222  $this->setResetCommand("resetFilter");
223 
224  $fields = $this->getFieldsAsArray();
225 
226  foreach ($fields as $field) {
230  if ($field->getHasFilter()) {
231  $this->addFilterField($field);
232  }
233  }
234  }
235 
236 
240  protected function addFilterField(arIndexTableField $field) {
241  switch ($field->getFieldType()) {
242  case 'integer':
243  case 'float':
244  $this->addFilterItemByMetaType($field->getName(), self::FILTER_NUMBER_RANGE, false, $this->txt($field->getTxt()));
245  break;
246  case 'text':
247  case 'clob':
248  $this->addFilterItemByMetaType($field->getName(), self::FILTER_TEXT, false, $this->txt($field->getTxt()));
249  break;
250  case 'date':
251  $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATE_RANGE, false, $this->txt($field->getTxt()));
252  break;
253  case 'time':
254  case 'timestamp':
255  $this->addFilterItemByMetaType($field->getName(), self::FILTER_DATETIME_RANGE, false, $this->txt($field->getTxt()));
256  break;
257  }
258  }
259 
260 
265  protected function addFilterItemToForm(ilFormPropertyGUI $item, $optional = false) {
269  $this->addFilterItem($item, $optional);
270  $item->readFromSession();
271  $this->filter_array[$item->getPostVar()] = $item->getValue();
272  }
273 
274 
275  protected function initRowSelector() {
276  $this->setShowRowsSelector(true);
277  }
278 
279 
284  protected function initTableRowTemplate() {
285  $this->setRowTemplate('tpl.record_row.html', './Customizing/global/plugins/Libraries/ActiveRecord/');
286  }
287 
288 
292  protected function initTableColumns() {
294 
295  foreach ($this->getFieldsAsArray() as $field) {
299  if ($this->checkColumnVisibile($field)) {
300  if ($field->getSortable()) {
301  $this->addColumn($this->txt($field->getTxt()), $field->getName());
302  } else {
303  $this->addColumn($this->txt($field->getTxt()));
304  }
305  }
306  }
307  if ($this->getActions()) {
308  $this->addColumn($this->txt('actions', false));
309  }
310  }
311 
312 
313  protected function addMultipleSelectionColumn() {
314  if ($this->getMultiItemActions()) {
315  $this->addColumn("", "", 1);
316  }
317  }
318 
319 
325  protected function checkColumnVisibile(arIndexTableField $field) {
326  return ($field->getVisible() && !$this->getSelectableColumns()) || $this->isColumnSelected($field->getName());
327  }
328 
329 
330  protected function initTableData() {
331  $this->active_record_list->getArWhereCollection()->setStatements(NULL);
332  $this->active_record_list->getArJoinCollection()->setStatements(NULL);
333  $this->active_record_list->getArLimitCollection()->setStatements(NULL);
334  $this->active_record_list->getArOrderCollection()->setStatements(NULL);
335 
336  $this->filterTableData();
337  $this->beforeGetData();
338  $this->setOrderAndSegmentation();
339  $ar_data = $this->active_record_list->getArray();
340  $data = array();
341 
342  foreach ($ar_data as $key => $item) {
343  $data[$key] = array();
344  foreach ($this->getFields()->getFieldsForDisplay() as $field) {
348  if (array_key_exists($field->getName(), $item)) {
349  if (!$item[$field->getName()]) {
350  $data[$key][$field->getName()] = $this->setEmptyFieldData($field, $item);
351  } elseif ($field->getIsCreatedByField()) {
352  $data[$key][$field->getName()] = $this->setArCreatedByField($field, $item, $item[$field->getName()]);
353  } elseif ($field->getIsModifiedByField()) {
354  $data[$key][$field->getName()] = $this->setArModifiedByField($field, $item, $item[$field->getName()]);
355  } else {
356  $data[$key][$field->getName()] = $this->setArFieldData($field, $item, $item[$field->getName()]);
357  }
358  } else {
359  $data[$key][$field->getName()] = $this->setCustomFieldData($field, $item);
360  }
361  }
362  }
363  $this->setData($data);
364  }
365 
366 
367  protected function filterTableData() {
368  $filters = $this->getFilterItems();
369  if ($filters) {
370  foreach ($filters as $filter) {
374  $this->addFilterWhere($filter, $filter->getPostVar(), $filter->getValue());
375  }
376  }
377  }
378 
379 
385  protected function addFilterWhere(ilFormPropertyGUI $filter, $name, $value) {
386 
387  switch (get_class($filter)) {
388  case 'ilTextInputGUI':
389  $this->addFilterTextWhere($filter, $name, $value);
390 
391  return;
392  case 'ilCombinationInputGUI':
393  if (is_object($value["from"]) || is_object($value["to"])) {
394  if (get_class($value["from"]) == "ilDateTime" || get_class($value["to"]) == "ilDateTime") {
395  $this->addFilterDateTimeWhere($filter, $name, $value);
396 
397  return;
398  }
399  if (get_class($value["from"]) == "ilDate" || get_class($value["to"]) == "ilDate") {
400  $this->addFilterDateWhere($filter, $name, $value);
401 
402  return;
403  }
404  $this->addFilterCustomWhere($filter, $name, $value);
405 
406  return;
407  }
408 
409  $this->addFilterNumericWhere($filter, $name, $value);
410  break;
411  default:
412  $this->addFilterCustomWhere($filter, $name, $value);
413 
414  return;
415  }
416  }
417 
418 
424  protected function addFilterNumericWhere(ilCombinationInputGUI $filter, $name, $value) {
425 
426  if ($value["from"] != "" OR $value["to"] != "") {
427  if ($value["from"] == "") {
428  $value["from"] = 0;
429  }
430  if ($value["to"] == "") {
431  $value["to"] = PHP_INT_MAX;
432  }
433  $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " >= " . $value["from"]
434  . " AND " . $this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " <= " . $value["to"]);
435  }
436  }
437 
438 
444  protected function addFilterTextWhere(ilTextInputGUI $filter, $name, $value) {
445  $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " like '%" . $value . "%'");
446  }
447 
448 
454  protected function addFilterDateWhere(ilCombinationInputGUI $filter, $name, $value) {
455  if ($value["from"] != NULL OR $value["to"] != NULL) {
456  if ($value["from"] == NULL) {
457  $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
458  }
459  if ($value["to"] == NULL) {
460  $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
461  }
462  $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
463  . $value["from"]->get(IL_CAL_DATE) . "' AND '" . $value["to"]->get(IL_CAL_DATE) . "'");
464  }
465  }
466 
467 
473  protected function addFilterDateTimeWhere(ilCombinationInputGUI $filter, $name, $value) {
474  if ($value["from"] != NULL OR $value["to"] != NULL) {
475  if ($value["from"] == NULL) {
476  $value["from"] = new ilDateTime("0001-01-01", IL_CAL_DATE);
477  }
478  if ($value["to"] == NULL) {
479  $value["to"] = new ilDateTime("9999-01-01", IL_CAL_DATE);
480  }
481  $this->active_record_list->where($this->active_record_list->getAR()->getConnectorContainerName() . "." . $name . " BETWEEN '"
482  . $value["from"]->get(IL_CAL_DATETIME) . "' AND '" . $value["to"]->get(IL_CAL_DATETIME) . "'");
483  }
484  }
485 
486 
487  protected function addFilterCustomWhere() {
488  }
489 
490 
491  protected function beforeGetData() {
492  }
493 
494 
495  protected function setOrderAndSegmentation() {
496  $this->setExternalSorting(true);
497  $this->setExternalSegmentation(true);
498  if (!$this->getDefaultOrderField()) {
499  $this->setDefaultOrderField($this->active_record_list->getAR()->getArFieldList()->getPrimaryField()->getName());
500  }
501  $this->determineLimit();
502  $this->determineOffsetAndOrder();
503  $this->setMaxCount($this->active_record_list->count());
504  $this->active_record_list->orderBy($this->getOrderField(), $this->getOrderDirection());
505  $this->active_record_list->limit($this->getOffset(), $this->getLimit());
506  }
507 
508 
515  protected function setEmptyFieldData(arIndexTableField $field, $item) {
516  return "";
517  }
518 
519 
527  protected function setArModifiedByField(arIndexTableField $field, $item, $value) {
528  $user = new ilObjUser($value);
529 
530  return $user->getPublicName();
531  }
532 
533 
541  protected function setArCreatedByField(arIndexTableField $field, $item, $value) {
542  $user = new ilObjUser($value);
543 
544  return $user->getPublicName();
545  }
546 
547 
555  protected function setArFieldData(arIndexTableField $field, $item, $value) {
556  switch ($field->getFieldType()) {
557  case 'integer':
558  case 'float':
559  case 'text':
560  case 'clob':
561  return $value;
562  case 'date':
563  case 'time':
564  case 'timestamp':
565  return $this->setDateFieldData($field, $item, $value);
566  }
567 
568  return "";
569  }
570 
571 
579  protected function setDateFieldData(arIndexTableField $field, $item, $value) {
580  $datetime = new ilDateTime($value, IL_CAL_DATETIME);
581 
582  return ilDatePresentation::formatDate($datetime, IL_CAL_UNIX);
583  }
584 
585 
592  protected function setCustomFieldData(arIndexTableField $field, $item) {
593  return "CUSTOM-OVERRIDE: setCustomFieldData";
594  }
595 
596 
603  final function fillRow($a_set) {
604  $this->setCtrlParametersForRow($a_set);
605  $this->addMultiItemActionCheckboxToRow($a_set);
606  $this->parseRow($a_set);
607  $this->addActionsToRow($a_set);
608  }
609 
610 
614  protected function setCtrlParametersForRow($a_set) {
615  $this->ctrl->setParameterByClass(get_class($this->parent_obj), 'ar_id', self::domid_encode($a_set[$this->getFields()->getPrimaryField()
616  ->getName()]));
617  }
618 
619 
623  protected function addMultiItemActionCheckboxToRow($a_set) {
624  if ($this->getMultiItemActions()) {
625  $this->tpl->setVariable('ID', self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
626  }
627  }
628 
629 
633  protected function parseRow($a_set) {
634  foreach ($a_set as $key => $value) {
635  $field = $this->getField($key);
636  if ($this->checkColumnVisibile($field)) {
637  $this->parseEntry($field, $value);
638  }
639  }
640  }
641 
642 
647  protected function parseEntry(arIndexTableField $field, $value) {
648  $this->tpl->setCurrentBlock('entry');
649  $this->tpl->setVariable('ENTRY_CONTENT', $value);
650  $this->tpl->parseCurrentBlock('entry');
651  }
652 
653 
657  protected function addActionsToRow($a_set) {
658  if ($this->getActions()) {
659  $alist = new ilAdvancedSelectionListGUI();
660  $alist->setId(self::domid_encode($a_set[$this->getFields()->getPrimaryField()->getName()]));
661  $alist->setListTitle($this->txt('actions', false));
662 
663  foreach ($this->getActionsAsArray() as $action) {
667  $access = true;
668  if ($action->getAccess()) {
669  $access = $this->access->checkAccess($action->getAccess(), '', $_GET['ref_id']);
670  }
671  if ($access) {
672  $alist->addItem($action->getTitle(), $action->getId(), $this->ctrl->getLinkTargetByClass($action->getTargetClass(), $action->getTargetCmd()));
673  }
674  }
675 
676  $this->tpl->setVariable('ACTION', $alist->getHTML());
677  }
678  }
679 
680 
684  public function render() {
685 
686  $index_table_tpl = new ilTemplate("tpl.index_table.html", true, true, "./Customizing/global/plugins/Libraries/ActiveRecord/");
687  if ($this->getToolbar()) {
688  $index_table_tpl->setVariable("TOOLBAR", $this->getToolbar()->getHTML());
689  }
690 
691  $index_table_tpl->setVariable("TABLE", parent::render());
692 
693  return $index_table_tpl->get();
694  }
695 
696 
703  public function txt($txt, $plugin_txt = true) {
704  return $this->parent_obj->txt($txt, $plugin_txt);
705  }
706 
707 
708  public function applyFilter() {
709  $this->writeFilterToSession();
710  $this->resetOffset();
711  $this->initTableData();
712  }
713 
714 
715  public function resetFilter() {
717  $this->resetOffset();
718  $this->initTableData();
719  }
720 
721 
729  public static function domid_encode($id_to_encode) {
730  $encoded_id = NULL;
731  if (!empty($id_to_encode)) {
732  $encoded_id = preg_replace_callback('/([^a-zA-Z0-9])/', function ($matches) {
733  return "__idstart_" . strtolower(dechex(ord($matches[0]))) . "_idend__";
734  }, $id_to_encode);
735  }
736 
737  return $encoded_id;
738  }
739 
740 
747  public static function domid_decode($id_to_decode) {
748  $decoded_id = "";
749  if (!empty($id_to_decode)) {
750  $decoded_id = preg_replace_callback('/__idstart_(.{2})_idend__/', function ($matches) {
751  return chr(hexdec($matches[1]));
752  }, $id_to_decode);
753  }
754 
755  return $decoded_id;
756  }
757 
758 
763  $this->fields = $fields;
764  }
765 
766 
770  public function getFields() {
771  return $this->fields;
772  }
773 
774 
778  public function getFieldsAsArray() {
779  return $this->getFields()->getFields();
780  }
781 
782 
788  public function getField($field_name) {
789  return $this->getFields()->getField($field_name);
790  }
791 
792 
796  public function addField(arIndexTableField $field) {
797  $this->getFields()->addField($field);
798  }
799 
800 
804  public function setTableTitle($table_title) {
805  $this->table_title = $table_title;
806  }
807 
808 
812  public function getTableTitle() {
813  return $this->table_title;
814  }
815 
816 
820  public function setToolbar($toolbar) {
821  $this->toolbar = $toolbar;
822  }
823 
824 
828  public function getToolbar() {
829  return $this->toolbar;
830  }
831 
832 
836  public function setActions($actions) {
837  $this->actions = $actions;
838  }
839 
840 
844  public function getActions() {
845  return $this->actions;
846  }
847 
848 
852  public function getActionsAsArray() {
853  return $this->actions->getActions();
854  }
855 
856 
861  $this->multi_item_actions = $multi_item_actions;
862  }
863 
864 
868  public function getMultiItemActions() {
870  }
871 
872 
876  public function getMultiItemActionsAsArray() {
878  }
879 }