ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilTableGUI.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
19{
23 protected $tpl;
24
28 protected $lng;
29
30 public $title; // table title name
31 public $icon; // table title icon
32 public $icon_alt; // table title icon alt text
33
34 public $help_page; // table help name
35 public $help_icon; // table help icon
36 public $help_icon_alt; // table help icon alt text
37
38 public $header_names; // titles of header columns
39 public $header_vars; // var names of header columns (i.e. for database order column)
40 public $linkbar_vars; // additional variables for linkbar
41
42 public $data; // table content
43
44 public $column_count; // no. of columns (based on element count of $this->header array)
45 public $column_width; // column width of each column (used in order until max. column from column count is reached)
46 // any exceeding values are ignored
47 public $max_count; // max. count of database query
48 public $limit; // max. count of dataset per page
49 public $max_limit = false;
50 public $offset; // dataset offset
51 public $order_column; // order column
52 public $order_direction; // order direction
53
54 public $footer_style; // css format for links
55 public $footer_previous; // value of previous link
56 public $footer_next; // value of next link
57
58 public $lang_support = true; // if a lang object is included
59 public $global_tpl; // uses global tpl (true) or a local one (false)
60 public $form_name; // the name of the parent form of the table
61 public $select_all_checkbox; // the name (or the first characters if unique) of a checkbox the should be toggled with a select all button
62 public $action_buttons; // action buttons in the table footer
63
64 public $prefix; // prefix for sort and offset fields if you have two or more tables on a page that you want to sort separately
65 public $base = ""; // base script (deprecated)
66
67 // default settings for enabled/disabled table modules
68 public $enabled = array( "table" => true,
69 "title" => true,
70 "icon" => true,
71 "help" => false,
72 "content" => true,
73 "action" => false,
74 "header" => true,
75 "footer" => true,
76 "linkbar" => true,
77 "numinfo" => true,
78 "numinfo_header" => false,
79 "sort" => true,
80 "hits" => false,
81 "auto_sort" => true,
82 "select_all" => false
83 );
84
85 // tpl styles (only one so far)
86 public $styles = array(
87 "table" => "fullwidth"
88 );
89
97 public function __construct($a_data = 0, $a_global_tpl = true)
98 {
99 global $DIC;
100
101 $tpl = $DIC["tpl"];
102 $lng = $DIC->language();
103
104 $this->global_tpl = $a_global_tpl;
105 $this->header_vars = array();
106 $this->header_params = array();
107 $this->enabled["form"] = true;
108 $this->action_buttons = array();
109 if ($this->global_tpl) {
110 $this->tpl = $tpl;
111 } else {
112 $this->tpl = new ilTemplate("tpl.table.html", true, true, "Services/Table");
113 }
114
115 $this->lng = $lng;
116
117 if (!$this->lng) {
118 $this->lang_support = false;
119 }
120
121 $this->setData($a_data);
122 }
123
124
130 public function setTemplate($a_tpl)
131 {
132 $this->tpl = $a_tpl;
133 }
134
135 public function &getTemplateObject()
136 {
137 return $this->tpl;
138 }
139
145 public function setData($a_data)
146 {
147 if (is_array($a_data)) {
148 $this->data = $a_data;
149 }
150 }
151
152 public function getData()
153 {
154 return $this->data;
155 }
156
164 public function setTitle($a_title, $a_icon = 0, $a_icon_alt = 0)
165 {
166 $this->title = $a_title;
167 $this->icon = $a_icon;
168 $this->icon_alt = $a_icon_alt;
169
170 if (!$this->icon) {
171 $this->enabled["icon"] = false;
172
173 return;
174 }
175
176 if (!$this->icon_alt) {
177 $this->icon_alt = $this->icon;
178 }
179 $this->enabled["icon"] = true;
180 }
181
189 public function setHelp($a_help_page, $a_help_icon, $a_help_icon_alt = 0)
190 {
191 $this->help_page = $a_help_page;
192 $this->help_icon = $a_help_icon;
193 $this->help_icon_alt = $a_help_icon_alt;
194
195 if (!$this->help_icon_alt) {
196 $this->help_icon_alt = $this->help_icon;
197 }
198 }
199
205 public function setHeaderNames($a_header_names)
206 {
207 $this->header_names = $a_header_names;
208 $this->column_count = count($this->header_names);
209 }
210
216 public function getColumnCount()
217 {
218 return $this->column_count;
219 }
220
227 public function setHeaderVars($a_header_vars, $a_header_params = 0)
228 {
229 $this->header_vars = $a_header_vars;
230
231 if ($a_header_params == 0 or !is_array($a_header_params)) {
232 $this->link_params = "";
233 } else {
234 $this->header_params = $a_header_params; // temp. solution for linkbar
235
236 foreach ($a_header_params as $key => $val) {
237 $this->link_params .= $key . "=" . $val . "&";
238 }
239 }
240 }
241
247 public function setColumnWidth($a_column_width)
248 {
249 $this->column_width = $a_column_width;
250 }
251
258 public function setOneColumnWidth($a_column_width, $a_column_number)
259 {
260 $this->column_width[$a_column_number] = $a_column_width;
261 }
262
270 public function setMaxCount($a_max_count)
271 {
272 $this->max_count = $a_max_count;
273
274 if ($this->max_limit) {
275 $this->limit = $this->max_count;
276 }
277 }
278
285 public function setLimit($a_limit = 0, $a_default_limit = 0)
286 {
287 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
288
289 if ($this->limit == 0) {
290 $this->max_limit = true;
291 }
292 }
293
297 public function getLimit()
298 {
299 return $this->limit;
300 }
301
302
308 public function setPrefix($a_prefix)
309 {
310 $this->prefix = ($a_prefix) ? $a_prefix : "";
311 }
312
318 public function setOffset($a_offset)
319 {
320 $this->offset = ($a_offset) ? $a_offset : 0;
321 }
322
326 public function getOffset()
327 {
328 return $this->offset;
329 }
330
337 public function setOrderColumn($a_order_column = 0, $a_default_column = 0)
338 {
339 // set default sort column to first column
340 if (empty($a_order_column)) {
341 if (!empty($a_default_column)) {
342 $this->order_column = array_search($a_default_column, $this->header_vars);
343 } else {
344 $this->order_column = 0;
345 return;
346 }
347 } else {
348 $this->order_column = array_search($a_order_column, $this->header_vars);
349 }
350
351 if ($this->order_column === false) {
352 // if not found, set default sort column to first column
353 $this->order_column = 0;
354 }
355 }
356
360 public function getOrderColumn()
361 {
362 return $this->order_column;
363 }
364
370 public function setOrderDirection($a_order_direction)
371 {
372 if (strtolower($a_order_direction) == "desc") {
373 $this->order_direction = "desc";
374 $this->sort_order = "asc";
375 } else {
376 $this->order_direction = "asc"; // set default sort order to "ASC"
377 $this->sort_order = "desc";
378 }
379 }
380
384 public function getOrderDirection()
385 {
387 }
388
396 public function setFooter($a_style, $a_previous = 0, $a_next = 0)
397 {
398 $this->footer_style = $a_style;
399
400 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
401 $this->footer_next = ($a_next) ? $a_next : ">>>";
402 }
403
412 public function enable($a_module_name)
413 {
414 if (!in_array($a_module_name, array_keys($this->enabled))) {
415 return false;
416 }
417
418 $this->enabled[$a_module_name] = true;
419 }
420
429 public function disable($a_module_name)
430 {
431 if (!in_array($a_module_name, array_keys($this->enabled))) {
432 return false;
433 }
434
435 $this->enabled[$a_module_name] = false;
436 }
437
438
439 public function sortData()
440 {
441 if ($this->enabled["sort"]) {
442 $this->data = ilUtil::sortArray($this->data, $this->order_column, $this->order_direction);
443 }
444 $this->data = array_slice($this->data, $this->offset, $this->limit);
445 }
446
451 public function render()
452 {
453 if ($this->enabled['table']) {
454 $this->tpl->setVariable("CSS_TABLE", "table table-striped" /* $this->getStyle("table") */);
455 }
456
457 // table title icon
458 if ($this->enabled["icon"] && $this->enabled["title"]) {
459 $this->tpl->setCurrentBlock("tbl_header_title_icon");
460 $this->tpl->setVariable("TBL_TITLE_IMG", ilUtil::getImagePath($this->icon));
461 $this->tpl->setVariable("TBL_TITLE_IMG_ALT", $this->icon_alt);
462 $this->tpl->parseCurrentBlock();
463 }
464 // table title help
465 if ($this->enabled["help"] && $this->enabled["title"]) {
466 $this->tpl->setCurrentBlock("tbl_header_title_help");
467 $this->tpl->setVariable("TBL_HELP_IMG", ilUtil::getImagePath($this->help_icon));
468 $this->tpl->setVariable("TBL_HELP_LINK", $this->help_page);
469 $this->tpl->setVariable("TBL_HELP_IMG_ALT", $this->help_icon_alt);
470 $this->tpl->parseCurrentBlock();
471 }
472
473 // hits per page selector
474 if ($this->enabled["hits"] && $this->enabled["title"]) {
475 $this->tpl->setCurrentBlock("tbl_header_hits_page");
476 $this->tpl->setVariable("LIMIT", $_SESSION["tbl_limit"]);
477 $this->tpl->setVariable("HITS_PER_PAGE", $this->lng->txt("hits_per_page"));
478 $this->tpl->parseCurrentBlock();
479 }
480
481 // table title
482 if ($this->enabled["title"]) {
483 $this->tpl->setCurrentBlock("tbl_header_title");
484 $this->tpl->setVariable("COLUMN_COUNT", $this->column_count);
485 $this->tpl->setVariable("TBL_TITLE", $this->title);
486 $this->tpl->parseCurrentBlock();
487 }
488
489 // table header
490 if ($this->enabled["header"]) {
491 $this->renderHeader();
492 }
493
494 // table data
495 // the table content may be skipped to use an individual template blockfile
496 // To do so don't set $this->data and parse your table content by yourself
497 // The template block name for the blockfile MUST be 'TBL_CONTENT'
498
499 if ($this->enabled["content"] && is_array($this->data)) {
500 if ($this->enabled['auto_sort']) {
501 $this->setMaxCount(count($this->data));
502 $this->sortData();
503 }
504 $count = 0;
505
506 foreach ($this->data as $tbl_content_row) {
507 foreach ($tbl_content_row as $key => $tbl_content_cell) {
508 if (is_array($tbl_content_cell)) {
509 $this->tpl->setCurrentBlock("tbl_cell_subtitle");
510 $this->tpl->setVariable("TBL_CELL_SUBTITLE", $tbl_content_cell[1]);
511 $this->tpl->parseCurrentBlock();
512 $tbl_content_cell = "<b>" . $tbl_content_cell[0] . "</b>";
513 }
514
515 $this->tpl->setCurrentBlock("tbl_content_cell");
516 $this->tpl->setVariable("TBL_CONTENT_CELL", $tbl_content_cell);
517 $this->tpl->parseCurrentBlock();
518 }
519
520 $this->tpl->setCurrentBlock("tbl_content_row");
521 $rowcolor = ilUtil::switchColor($count, "tblrow1", "tblrow2");
522 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
523 $this->tpl->parseCurrentBlock();
524
525 $count++;
526 }
527 }
528 // select all checkbox
529 if ($this->enabled["select_all"]) {
530 if ((strlen($this->getFormName())) && (strlen($this->getSelectAllCheckbox()))) {
531 $this->tpl->setVariable('SELECT_PREFIX', $this->prefix);
532 $this->tpl->setVariable("SELECT_ALL_TXT_SELECT_ALL", $this->lng->txt("select_all"));
533 $this->tpl->setVariable("SELECT_ALL_CHECKBOX_NAME", $this->getSelectAllCheckbox());
534 $this->tpl->setVariable("SELECT_ALL_FORM_NAME", $this->getFormName());
535 if (!($this->enabled["numinfo"] && $this->enabled["footer"])) {
536 $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
537 }
538 }
539 }
540
541 // table header numinfo
542 if ($this->enabled["numinfo_header"]) {
543 $start = $this->offset + 1; // compute num info
544 $end = $this->offset + $this->limit;
545
546 if ($end > $this->max_count or $this->limit == 0) {
547 $end = $this->max_count;
548 }
549
550 if ($this->lang_support) {
551 $numinfo = "(" . $this->lng->txt("dataset") . " " . $start . " - " . $end . " " . strtolower($this->lng->txt("of")) . " " . $this->max_count . ")";
552 } else {
553 $numinfo = "(Dataset " . $start . " - " . $end . " of " . $this->max_count . ")";
554 }
555 if ($this->max_count > 0) {
556 //$numinfo = $this->lng->txt("no_datasets");
557 $this->tpl->setCurrentBlock("tbl_header_numinfo");
558 $this->tpl->setVariable("NUMINFO_HEADER", $numinfo);
559 $this->tpl->setVariable("COLUMN_COUNT_HEADER", $this->getColumnCount());
560 $this->tpl->parseCurrentBlock();
561 }
562 }
563 // table footer numinfo
564 if ($this->enabled["numinfo"] && $this->enabled["footer"]) {
565 $start = $this->offset + 1; // compute num info
566 $end = $this->offset + $this->limit;
567
568 if ($end > $this->max_count or $this->limit == 0) {
569 $end = $this->max_count;
570 }
571
572 if ($this->lang_support) {
573 $numinfo = "(" . $this->lng->txt("dataset") . " " . $start . " - " . $end . " " . strtolower($this->lng->txt("of")) . " " . $this->max_count . ")";
574 } else {
575 $numinfo = "(Dataset " . $start . " - " . $end . " of " . $this->max_count . ")";
576 }
577 if ($this->max_count > 0) {
578 //$numinfo = $this->lng->txt("no_datasets");
579 $this->tpl->setCurrentBlock("tbl_footer_numinfo");
580 $this->tpl->setVariable("NUMINFO", $numinfo);
581 $this->tpl->parseCurrentBlock();
582 }
583 }
584 // table footer linkbar
585 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
586 && $this->max_count > 0) {
587 $params = array(
588 $this->prefix . "sort_by" => $this->header_vars[$this->order_column],
589 $this->prefix . "sort_order" => $this->order_direction
590 );
591 $params = array_merge($this->header_params, $params);
592
593 $layout = array(
594 "link" => $this->footer_style,
595 "prev" => $this->footer_previous,
596 "next" => $this->footer_next,
597 );
598
599 $base = ($this->getBase() == "")
600 ? basename($_SERVER["PHP_SELF"])
601 : $this->getBase();
602
603 $linkbar = ilUtil::Linkbar($base, $this->max_count, $this->limit, $this->offset, $params, $layout, $this->prefix);
604 $this->tpl->setCurrentBlock("tbl_footer_linkbar");
605 $this->tpl->setVariable("LINKBAR", $linkbar);
606 $this->tpl->parseCurrentBlock();
607 }
608
609 // table footer
610 if ($this->enabled["footer"] && $this->max_count > 0) {
611 $this->tpl->setCurrentBlock("tbl_footer");
612 $this->tpl->setVariable("COLUMN_COUNT", $this->column_count);
613 $this->tpl->parseCurrentBlock();
614 }
615
616 // action buttons
617 if ($this->enabled["action"] && is_array($this->action_buttons)) {
618 foreach ($this->action_buttons as $button) {
619 $this->tpl->setCurrentBlock("tbl_action_btn");
620 $this->tpl->setVariable("BTN_NAME", $button["name"]);
621 $this->tpl->setVariable("BTN_VALUE", $button["value"]);
622 $this->tpl->parseCurrentBlock();
623 }
624 $this->tpl->setCurrentBlock("tbl_action_row");
625 $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.svg"));
626 $this->tpl->setVariable("ALT_ARROW", $this->lng->txt("arrow_downright.svg"));
627 $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
628 $this->tpl->parseCurrentBlock();
629 }
630
631 if ($this->enabled["form"]) {
632 $this->tpl->touchBlock("tbl_form_footer");
633 }
634
635 if ($this->enabled['table']) {
636 $this->tpl->touchBlock("tbl_table_end");
637 }
638
639 if (!$this->global_tpl) {
640 return $this->tpl->get();
641 }
642 }
643
644 public function renderHeader()
645 {
646 foreach ($this->header_names as $key => $tbl_header_cell) {
647 if (!$this->enabled["sort"]) {
648 $this->tpl->setCurrentBlock("tbl_header_no_link");
649 if ($this->column_width[$key]) {
650 $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK", " width=\"" . $this->column_width[$key] . "\"");
651 }
652 $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK", $tbl_header_cell);
653 $this->tpl->parseCurrentBlock();
654 continue;
655 }
656 if (($key == $this->order_column) && ($this->order_direction != "")) {
657 if (strcmp($this->header_vars[$key], "") != 0) {
658 $this->tpl->setCurrentBlock("tbl_order_image");
659 $this->tpl->parseCurrentBlock();
660 }
661 }
662
663 $this->tpl->setCurrentBlock("tbl_header_cell");
664 $this->tpl->setVariable("TBL_HEADER_CELL", $tbl_header_cell);
665
666 // only set width if a value is given for that column
667 if ($this->column_width[$key]) {
668 $this->tpl->setVariable("TBL_COLUMN_WIDTH", " width=\"" . $this->column_width[$key] . "\"");
669 }
670
671 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
672 $this->tpl->setVariable("TBL_ORDER_ALT", $lng_sort_column);
673
674 $order_dir = "asc";
675
676 if ($key == $this->order_column) {
677 $order_dir = $this->sort_order;
678
679 $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
680 $this->tpl->setVariable("TBL_ORDER_ALT", $lng_change_sort);
681 }
682
683 $this->setOrderLink($key, $order_dir);
684 $this->tpl->parseCurrentBlock();
685 }
686
687 $this->tpl->setCurrentBlock("tbl_header");
688 $this->tpl->parseCurrentBlock();
689 }
690
691 public function setOrderLink($key, $order_dir)
692 {
693 $this->tpl->setVariable("TBL_ORDER_LINK", basename($_SERVER["PHP_SELF"]) . "?" . $this->link_params . $this->prefix . "sort_by=" . $this->header_vars[$key] . "&" . $this->prefix . "sort_order=" . $order_dir . "&" . $this->prefix . "offset=" . $this->offset);
694 }
695
696 /*
697 * set a tpl stylesheet
698 * @access public
699 * @param string table element
700 * @param string CSS definition
701 */
702 public function setStyle($a_element, $a_style)
703 {
704 $this->styles[$a_element] = $a_style;
705 }
706
707 /*
708 * get a tpl stylesheet
709 * @access public
710 * @param string table element
711 */
712 public function getStyle($a_element)
713 {
714 return $this->styles[$a_element];
715 }
716
722 public function setBase($a_base)
723 {
724 $this->base = $a_base;
725 }
726
732 public function getBase()
733 {
734 return $this->base;
735 }
736
737 /*
738 * get the name of the parent form
739 * @access public
740 * @return string name of the parent form
741 */
742 public function getFormName()
743 {
744 return $this->form_name;
745 }
746
747 /*
748 * set the name of the parent form
749 * @access public
750 * @param string $a_name name of the parent form
751 */
752 public function setFormName($a_name = "cmd")
753 {
754 $this->form_name = $a_name;
755 }
756
757 /*
758 * get the name of the checkbox that should be toggled with a select all button
759 * @access public
760 * @return string name of the checkbox
761 */
762 public function getSelectAllCheckbox()
763 {
765 }
766
767 /*
768 * set the name of the checkbox that should be toggled with a select all button
769 * @access public
770 * @param string $a_select_all_checkbox name of the checkbox
771 */
772 public function setSelectAllCheckbox($a_select_all_checkbox)
773 {
774 $this->select_all_checkbox = $a_select_all_checkbox;
775 }
776
777 /*
778 * Removes all action buttons from the table
779 *
780 * @access public
781 */
782 public function clearActionButtons()
783 {
784 $this->action_buttons = array();
785 }
786
787 /*
788 * Adds an action button to the table
789 *
790 * @param string $btn_name Name of the action button
791 * @param string $btn_value Value of the action button
792 * @access public
793 */
794 public function addActionButton($btn_name, $btn_value)
795 {
796 array_push(
797 $this->action_buttons,
798 array(
799 "name" => $btn_name,
800 "value" => $btn_value
801 )
802 );
803 }
804}
base()
Definition: base.php:4
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class ilTableGUI.
getBase()
Get Base script name (deprecated, only use this for workarounds).
getOrderDirection()
Get order direction.
setOrderLink($key, $order_dir)
setTitle($a_title, $a_icon=0, $a_icon_alt=0)
set table title @access public
setMaxCount($a_max_count)
set max.
getStyle($a_element)
setHeaderVars($a_header_vars, $a_header_params=0)
set table header vars @access public
setBase($a_base)
Set Base script name (deprecated, only use this for workarounds).
setHeaderNames($a_header_names)
set table header names @access public
setTemplate($a_tpl)
set template @access public
getLimit()
Get limit.
setOrderColumn($a_order_column=0, $a_default_column=0)
set order column @access public
disable($a_module_name)
diesables particular modules of table
setSelectAllCheckbox($a_select_all_checkbox)
setStyle($a_element, $a_style)
setPrefix($a_prefix)
set prefix for sort and offset fields (if you have two or more tables on a page that you want to sort...
__construct($a_data=0, $a_global_tpl=true)
Constructor.
setOrderDirection($a_order_direction)
set order direction @access public
setLimit($a_limit=0, $a_default_limit=0)
set max.
setFormName($a_name="cmd")
setColumnWidth($a_column_width)
set table column widths @access public
enable($a_module_name)
enables particular modules of table
getColumnCount()
Returns the column count based on the number of the header row columns @access public.
addActionButton($btn_name, $btn_value)
setData($a_data)
set table data @access public
getOrderColumn()
Get order column.
render()
render table @access public
setFooter($a_style, $a_previous=0, $a_next=0)
set order direction @access public
getOffset()
Get offset.
setOffset($a_offset)
set dataset offset @access public
setHelp($a_help_page, $a_help_icon, $a_help_icon_alt=0)
set table help page @access public
setOneColumnWidth($a_column_width, $a_column_number)
set one table column width @access public
special template class to simplify handling of ITX/PEAR
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
static Linkbar($AScript, $AHits, $ALimit, $AOffset, $AParams=array(), $ALayout=array(), $prefix='')
Linkbar Diese Funktion erzeugt einen typischen Navigationsbalken mit "Previous"- und "Next"-Links und...
static switchColor($a_num, $a_css1, $a_css2)
switches style sheets for each even $a_num (used for changing colors of different result rows)
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
$DIC
Definition: xapitoken.php:46