ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 {
20  var $title; // table title name
21  var $icon; // table title icon
22  var $icon_alt; // table title icon alt text
23 
24  var $help_page; // table help name
25  var $help_icon; // table help icon
26  var $help_icon_alt; // table help icon alt text
27 
28  var $header_names; // titles of header columns
29  var $header_vars; // var names of header columns (i.e. for database order column)
30  var $linkbar_vars; // additional variables for linkbar
31 
32  var $data; // table content
33 
34  var $column_count; // no. of columns (based on element count of $this->header array)
35  var $column_width; // column width of each column (used in order until max. column from column count is reached)
36  // any exceeding values are ignored
37  var $max_count; // max. count of database query
38  var $limit; // max. count of dataset per page
39  var $max_limit = false;
40  var $offset; // dataset offset
41  var $order_column; // order column
42  var $order_direction; // order direction
43 
44  var $footer_style; // css format for links
45  var $footer_previous; // value of previous link
46  var $footer_next; // value of next link
47 
48  var $lang_support = true; // if a lang object is included
49  var $global_tpl; // uses global tpl (true) or a local one (false)
50  var $form_name; // the name of the parent form of the table
51  var $select_all_checkbox; // the name (or the first characters if unique) of a checkbox the should be toggled with a select all button
52  var $action_buttons; // action buttons in the table footer
53 
54  var $prefix; // prefix for sort and offset fields if you have two or more tables on a page that you want to sort separately
55  var $base = ""; // base script (deprecated)
56 
57  // default settings for enabled/disabled table modules
58  var $enabled = array( "table" => true,
59  "title" => true,
60  "icon" => true,
61  "help" => false,
62  "content" => true,
63  "action" => false,
64  "header" => true,
65  "footer" => true,
66  "linkbar" => true,
67  "numinfo" => true,
68  "numinfo_header" => false,
69  "sort" => true,
70  "hits" => false,
71  "auto_sort" => true,
72  "select_all" => false
73  );
74 
75  // tpl styles (only one so far)
76  var $styles = array(
77  "table" => "fullwidth"
78  );
79 
87  function ilTableGUI($a_data = 0,$a_global_tpl = true)
88  {
89  global $ilias, $tpl, $lng;
90 
91  $this->global_tpl = $a_global_tpl;
92  $this->ilias =& $ilias;
93  $this->header_vars = array();
94  $this->header_params = array();
95  $this->enabled["form"] = true;
96  $this->action_buttons = array();
97  if ($this->global_tpl)
98  {
99  $this->tpl =& $tpl;
100  }
101  else
102  {
103  $this->tpl = new ilTemplate("tpl.table.html", true, true, "Services/Table");
104  }
105 
106  $this->lng =& $lng;
107 
108  if (!$this->lng)
109  {
110  $this->lang_support = false;
111  }
112 
113  $this->setData($a_data);
114  }
115 
116 
122  function setTemplate(&$a_tpl)
123  {
124  $this->tpl =& $a_tpl;
125  }
126 
127  function &getTemplateObject()
128  {
129  return $this->tpl;
130  }
131 
137  function setData($a_data)
138  {
139  if (is_array($a_data))
140  {
141  $this->data = $a_data;
142  }
143  }
144 
145  function getData()
146  {
147  return $this->data;
148  }
149 
157  function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
158  {
159  $this->title = $a_title;
160  $this->icon = $a_icon;
161  $this->icon_alt = $a_icon_alt;
162 
163  if (!$this->icon)
164  {
165  $this->enabled["icon"] = false;
166 
167  return;
168  }
169 
170  if (!$this->icon_alt)
171  {
172  $this->icon_alt = $this->icon;
173  }
174  $this->enabled["icon"] = true;
175  }
176 
184  function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
185  {
186  $this->help_page = $a_help_page;
187  $this->help_icon = $a_help_icon;
188  $this->help_icon_alt = $a_help_icon_alt;
189 
190  if (!$this->help_icon_alt)
191  {
192  $this->help_icon_alt = $this->help_icon;
193  }
194  }
195 
201  function setHeaderNames($a_header_names)
202  {
203  $this->header_names = $a_header_names;
204  $this->column_count = count($this->header_names);
205  }
206 
212  function getColumnCount()
213  {
214  return $this->column_count;
215  }
216 
223  function setHeaderVars($a_header_vars,$a_header_params = 0)
224  {
225  $this->header_vars = $a_header_vars;
226 
227  if ($a_header_params == 0 or !is_array($a_header_params))
228  {
229  $this->link_params = "";
230  }
231  else
232  {
233  $this->header_params = $a_header_params; // temp. solution for linkbar
234 
235  foreach ($a_header_params as $key => $val)
236  {
237  $this->link_params .= $key."=".$val."&";
238  }
239  }
240  }
241 
247  function setColumnWidth($a_column_width)
248  {
249  $this->column_width = $a_column_width;
250  }
251 
258  function setOneColumnWidth($a_column_width,$a_column_number)
259  {
260  $this->column_width[$a_column_number] = $a_column_width;
261  }
262 
270  function setMaxCount($a_max_count)
271  {
272  $this->max_count = $a_max_count;
273 
274  if ($this->max_limit)
275  {
276  $this->limit = $this->max_count;
277  }
278  }
279 
286  function setLimit($a_limit = 0, $a_default_limit = 0)
287  {
288  $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
289 
290  if ($this->limit == 0)
291  {
292  $this->max_limit = true;
293  }
294  }
295 
299  function getLimit()
300  {
301  return $this->limit;
302  }
303 
304 
310  function setPrefix($a_prefix)
311  {
312  $this->prefix = ($a_prefix) ? $a_prefix : "";
313  }
314 
320  function setOffset($a_offset)
321  {
322  $this->offset = ($a_offset) ? $a_offset : 0;
323  }
324 
328  function getOffset()
329  {
330  return $this->offset;
331  }
332 
339  function setOrderColumn($a_order_column = 0,$a_default_column = 0)
340  {
341  // set default sort column to first column
342  if (empty($a_order_column))
343  {
344  if (!empty($a_default_column))
345  {
346  $this->order_column = array_search($a_default_column,$this->header_vars);
347  }
348  else
349  {
350  $this->order_column = 0;
351  return;
352  }
353  }
354  else
355  {
356  $this->order_column = array_search($a_order_column,$this->header_vars);
357  }
358 
359  if ($this->order_column === false)
360  {
361  // if not found, set default sort column to first column
362  $this->order_column = 0;
363  }
364  }
365 
369  function getOrderColumn()
370  {
371  return $this->order_column;
372  }
373 
379  function setOrderDirection($a_order_direction)
380  {
381  if ($a_order_direction == "desc")
382  {
383  $this->order_direction = "desc";
384  $this->sort_order = "asc";
385  }
386  else
387  {
388  $this->order_direction = "asc"; // set default sort order to "ASC"
389  $this->sort_order = "desc";
390  }
391  }
392 
396  function getOrderDirection()
397  {
398  return $this->order_direction;
399  }
400 
408  function setFooter($a_style,$a_previous = 0,$a_next = 0)
409  {
410  $this->footer_style = $a_style;
411 
412  $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
413  $this->footer_next = ($a_next) ? $a_next : ">>>";
414  }
415 
424  function enable($a_module_name)
425  {
426  if (!in_array($a_module_name,array_keys($this->enabled)))
427  {
428  return false;
429  }
430 
431  $this->enabled[$a_module_name] = true;
432  }
433 
442  function disable($a_module_name)
443  {
444  if (!in_array($a_module_name,array_keys($this->enabled)))
445  {
446  return false;
447  }
448 
449  $this->enabled[$a_module_name] = false;
450  }
451 
452 
453  function sortData()
454  {
455  if($this->enabled["sort"])
456  {
457  $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
458  }
459  $this->data = array_slice($this->data,$this->offset,$this->limit);
460  }
461 
466  function render()
467  {
468  if($this->enabled['table'])
469  {
470  $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
471  }
472 
473  // table title icon
474  if ($this->enabled["icon"] && $this->enabled["title"])
475  {
476  $this->tpl->setCurrentBlock("tbl_header_title_icon");
477  $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
478  $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
479  $this->tpl->parseCurrentBlock();
480  }
481  // table title help
482  if ($this->enabled["help"] && $this->enabled["title"])
483  {
484  $this->tpl->setCurrentBlock("tbl_header_title_help");
485  $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
486  $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
487  $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
488  $this->tpl->parseCurrentBlock();
489  }
490 
491  // hits per page selector
492  if ($this->enabled["hits"] && $this->enabled["title"])
493  {
494  $this->tpl->setCurrentBlock("tbl_header_hits_page");
495  $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
496  $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
497  $this->tpl->parseCurrentBlock();
498  }
499 
500  // table title
501  if ($this->enabled["title"])
502  {
503  $this->tpl->setCurrentBlock("tbl_header_title");
504  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
505  $this->tpl->setVariable("TBL_TITLE",$this->title);
506  $this->tpl->parseCurrentBlock();
507  }
508 
509  // table header
510  if ($this->enabled["header"])
511  {
512  $this->renderHeader();
513  }
514 
515  // table data
516  // the table content may be skipped to use an individual template blockfile
517  // To do so don't set $this->data and parse your table content by yourself
518  // The template block name for the blockfile MUST be 'TBL_CONTENT'
519 
520  if ($this->enabled["content"] && is_array($this->data))
521  {
522  if($this->enabled['auto_sort'])
523  {
524  $this->setMaxCount(count($this->data));
525  $this->sortData();
526  }
527  $count = 0;
528 
529  foreach ($this->data as $tbl_content_row)
530  {
531  foreach ($tbl_content_row as $key => $tbl_content_cell)
532  {
533  if (is_array($tbl_content_cell))
534  {
535  $this->tpl->setCurrentBlock("tbl_cell_subtitle");
536  $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
537  $this->tpl->parseCurrentBlock();
538  $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
539  }
540 
541  $this->tpl->setCurrentBlock("tbl_content_cell");
542  $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
543  $this->tpl->parseCurrentBlock();
544  }
545 
546  $this->tpl->setCurrentBlock("tbl_content_row");
547  $rowcolor = ilUtil::switchColor($count,"tblrow1","tblrow2");
548  $this->tpl->setVariable("ROWCOLOR", $rowcolor);
549  $this->tpl->parseCurrentBlock();
550 
551  $count++;
552  }
553  }
554  // select all checkbox
555  if ($this->enabled["select_all"])
556  {
557  if ((strlen($this->getFormName())) && (strlen($this->getSelectAllCheckbox())))
558  {
559  $this->tpl->setVariable('SELECT_PREFIX',$this->prefix);
560  $this->tpl->setVariable("SELECT_ALL_TXT_SELECT_ALL", $this->lng->txt("select_all"));
561  $this->tpl->setVariable("SELECT_ALL_CHECKBOX_NAME", $this->getSelectAllCheckbox());
562  $this->tpl->setVariable("SELECT_ALL_FORM_NAME", $this->getFormName());
563  if (!($this->enabled["numinfo"] && $this->enabled["footer"]))
564  {
565  $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
566  }
567  }
568  }
569 
570  // table header numinfo
571  if ($this->enabled["numinfo_header"])
572  {
573  $start = $this->offset + 1; // compute num info
574  $end = $this->offset + $this->limit;
575 
576  if ($end > $this->max_count or $this->limit == 0)
577  {
578  $end = $this->max_count;
579  }
580 
581  if ($this->lang_support)
582  {
583  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
584  }
585  else
586  {
587  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
588  }
589  if ($this->max_count > 0)
590  {
591  //$numinfo = $this->lng->txt("no_datasets");
592  $this->tpl->setCurrentBlock("tbl_header_numinfo");
593  $this->tpl->setVariable("NUMINFO_HEADER", $numinfo);
594  $this->tpl->setVariable("COLUMN_COUNT_HEADER", $this->getColumnCount());
595  $this->tpl->parseCurrentBlock();
596  }
597  }
598  // table footer numinfo
599  if ($this->enabled["numinfo"] && $this->enabled["footer"])
600  {
601  $start = $this->offset + 1; // compute num info
602  $end = $this->offset + $this->limit;
603 
604  if ($end > $this->max_count or $this->limit == 0)
605  {
606  $end = $this->max_count;
607  }
608 
609  if ($this->lang_support)
610  {
611  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
612  }
613  else
614  {
615  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
616  }
617  if ($this->max_count > 0)
618  {
619  //$numinfo = $this->lng->txt("no_datasets");
620  $this->tpl->setCurrentBlock("tbl_footer_numinfo");
621  $this->tpl->setVariable("NUMINFO", $numinfo);
622  $this->tpl->parseCurrentBlock();
623  }
624  }
625  // table footer linkbar
626  if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
627  && $this->max_count > 0)
628  {
629  $params = array(
630  $this->prefix."sort_by" => $this->header_vars[$this->order_column],
631  $this->prefix."sort_order" => $this->order_direction
632  );
633  $params = array_merge($this->header_params,$params);
634 
635  $layout = array(
636  "link" => $this->footer_style,
637  "prev" => $this->footer_previous,
638  "next" => $this->footer_next,
639  );
640 
641  $base = ($this->getBase() == "")
642  ? basename($_SERVER["PHP_SELF"])
643  : $this->getBase();
644 
645  $linkbar = ilUtil::Linkbar($base,$this->max_count,$this->limit,$this->offset,$params,$layout, $this->prefix);
646  $this->tpl->setCurrentBlock("tbl_footer_linkbar");
647  $this->tpl->setVariable("LINKBAR", $linkbar);
648  $this->tpl->parseCurrentBlock();
649  }
650 
651  // table footer
652  if ($this->enabled["footer"] && $this->max_count > 0)
653  {
654  $this->tpl->setCurrentBlock("tbl_footer");
655  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
656  $this->tpl->parseCurrentBlock();
657  }
658 
659  // action buttons
660  if ($this->enabled["action"])
661  {
662  foreach ($this->action_buttons as $button)
663  {
664  $this->tpl->setCurrentBlock("tbl_action_btn");
665  $this->tpl->setVariable("BTN_NAME", $button["name"]);
666  $this->tpl->setVariable("BTN_VALUE", $button["value"]);
667  $this->tpl->parseCurrentBlock();
668  }
669  $this->tpl->setCurrentBlock("tbl_action_row");
670  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
671  $this->tpl->setVariable("ALT_ARROW", $this->lng->txt("arrow_downright.gif"));
672  $this->tpl->setVariable("COLUMN_COUNTS", $this->getColumnCount());
673  $this->tpl->parseCurrentBlock();
674  }
675 
676  if ($this->enabled["form"])
677  {
678  $this->tpl->touchBlock("tbl_form_footer");
679  }
680 
681  if($this->enabled['table'])
682  {
683  $this->tpl->touchBlock("tbl_table_end");
684  }
685 
686  if (!$this->global_tpl)
687  {
688  return $this->tpl->get();
689  }
690  }
691 
692  function renderHeader()
693  {
694  foreach ($this->header_names as $key => $tbl_header_cell)
695  {
696  if (!$this->enabled["sort"])
697  {
698  $this->tpl->setCurrentBlock("tbl_header_no_link");
699  if ($this->column_width[$key])
700  {
701  $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
702  }
703  $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
704  $this->tpl->parseCurrentBlock();
705  continue;
706  }
707  if (($key == $this->order_column) && ($this->order_direction != ""))
708  {
709  if (strcmp($this->header_vars[$key], "") != 0)
710  {
711  $this->tpl->setCurrentBlock("tbl_order_image");
712  $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.gif"));
713  $this->tpl->parseCurrentBlock();
714  }
715  }
716 
717  $this->tpl->setCurrentBlock("tbl_header_cell");
718  $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
719 
720  // only set width if a value is given for that column
721  if ($this->column_width[$key])
722  {
723  $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
724  }
725 
726  $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
727  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
728 
729  $order_dir = "asc";
730 
731  if ($key == $this->order_column)
732  {
733  $order_dir = $this->sort_order;
734 
735  $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
736  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
737  }
738 
739  $this->setOrderLink($key, $order_dir);
740  $this->tpl->parseCurrentBlock();
741  }
742 
743  $this->tpl->setCurrentBlock("tbl_header");
744  $this->tpl->parseCurrentBlock();
745  }
746 
747  function setOrderLink($key, $order_dir)
748  {
749  $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);
750  }
751 
752  /*
753  * set a tpl stylesheet
754  * @access public
755  * @param string table element
756  * @param string CSS definition
757  */
758  function setStyle($a_element,$a_style)
759  {
760  $this->styles[$a_element] = $a_style;
761  }
762 
763  /*
764  * get a tpl stylesheet
765  * @access public
766  * @param string table element
767  */
768  function getStyle($a_element)
769  {
770  return $this->styles[$a_element];
771  }
772 
778  function setBase($a_base)
779  {
780  $this->base = $a_base;
781  }
782 
788  function getBase()
789  {
790  return $this->base;
791  }
792 
793  /*
794  * get the name of the parent form
795  * @access public
796  * @return string name of the parent form
797  */
798  function getFormName()
799  {
800  return $this->form_name;
801  }
802 
803  /*
804  * set the name of the parent form
805  * @access public
806  * @param string $a_name name of the parent form
807  */
808  function setFormName($a_name = "cmd")
809  {
810  $this->form_name = $a_name;
811  }
812 
813  /*
814  * get the name of the checkbox that should be toggled with a select all button
815  * @access public
816  * @return string name of the checkbox
817  */
819  {
821  }
822 
823  /*
824  * set the name of the checkbox that should be toggled with a select all button
825  * @access public
826  * @param string $a_select_all_checkbox name of the checkbox
827  */
828  function setSelectAllCheckbox($a_select_all_checkbox)
829  {
830  $this->select_all_checkbox = $a_select_all_checkbox;
831  }
832 
833  /*
834  * Removes all action buttons from the table
835  *
836  * @access public
837  */
839  {
840  $this->action_buttons = array();
841  }
842 
843  /*
844  * Adds an action button to the table
845  *
846  * @param string $btn_name Name of the action button
847  * @param string $btn_value Value of the action button
848  * @access public
849  */
850  function addActionButton($btn_name, $btn_value)
851  {
852  array_push($this->action_buttons,
853  array(
854  "name" => $btn_name,
855  "value" => $btn_value
856  )
857  );
858  }
859 }
860 ?>