ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTableGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
38 {
39  var $title; // table title name
40  var $icon; // table title icon
41  var $icon_alt; // table title icon alt text
42 
43  var $help_page; // table help name
44  var $help_icon; // table help icon
45  var $help_icon_alt; // table help icon alt text
46 
47  var $header_names; // titles of header columns
48  var $header_vars; // var names of header columns (i.e. for database order column)
49  var $linkbar_vars; // additional variables for linkbar
50 
51  var $data; // table content
52 
53  var $column_count; // no. of columns (based on element count of $this->header array)
54  var $column_width; // column width of each column (used in order until max. column from column count is reached)
55  // any exceeding values are ignored
56  var $max_count; // max. count of database query
57  var $limit; // max. count of dataset per page
58  var $max_limit = false;
59  var $offset; // dataset offset
60  var $order_column; // order column
61  var $order_direction; // order direction
62 
63  var $footer_style; // css format for links
64  var $footer_previous; // value of previous link
65  var $footer_next; // value of next link
66 
67  var $lang_support = true; // if a lang object is included
68  var $global_tpl; // uses global tpl (true) or a local one (false)
69  var $form_name; // the name of the parent form of the table
70  var $select_all_checkbox; // the name (or the first characters if unique) of a checkbox the should be toggled with a select all button
71  var $action_buttons; // action buttons in the table footer
72 
73  var $prefix; // prefix for sort and offset fields if you have two or more tables on a page that you want to sort separately
74  var $base = ""; // base script (deprecated)
75 
76  // default settings for enabled/disabled table modules
77  var $enabled = array( "table" => true,
78  "title" => true,
79  "icon" => true,
80  "help" => false,
81  "content" => true,
82  "action" => false,
83  "header" => true,
84  "footer" => true,
85  "linkbar" => true,
86  "numinfo" => true,
87  "numinfo_header" => false,
88  "sort" => true,
89  "hits" => false,
90  "auto_sort" => true,
91  "select_all" => false
92  );
93 
94  // tpl styles (only one so far)
95  var $styles = array(
96  "table" => "fullwidth"
97  );
98 
106  function ilTableGUI($a_data = 0,$a_global_tpl = true)
107  {
108  global $ilias, $tpl, $lng;
109 
110  $this->global_tpl = $a_global_tpl;
111  $this->ilias =& $ilias;
112  $this->header_vars = array();
113  $this->header_params = array();
114  $this->enabled["form"] = true;
115  $this->action_buttons = array();
116  if ($this->global_tpl)
117  {
118  $this->tpl =& $tpl;
119  }
120  else
121  {
122  $this->tpl = new ilTemplate("tpl.table.html", true, true, "Services/Table");
123  }
124 
125  $this->lng =& $lng;
126 
127  if (!$this->lng)
128  {
129  $this->lang_support = false;
130  }
131 
132  $this->setData($a_data);
133  }
134 
135 
141  function setTemplate(&$a_tpl)
142  {
143  $this->tpl =& $a_tpl;
144  }
145 
146  function &getTemplateObject()
147  {
148  return $this->tpl;
149  }
150 
156  function setData($a_data)
157  {
158  if (is_array($a_data))
159  {
160  $this->data = $a_data;
161  }
162  }
163 
164  function getData()
165  {
166  return $this->data;
167  }
168 
176  function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
177  {
178  $this->title = $a_title;
179  $this->icon = $a_icon;
180  $this->icon_alt = $a_icon_alt;
181 
182  if (!$this->icon)
183  {
184  $this->enabled["icon"] = false;
185 
186  return;
187  }
188 
189  if (!$this->icon_alt)
190  {
191  $this->icon_alt = $this->icon;
192  }
193  $this->enabled["icon"] = true;
194  }
195 
203  function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
204  {
205  $this->help_page = $a_help_page;
206  $this->help_icon = $a_help_icon;
207  $this->help_icon_alt = $a_help_icon_alt;
208 
209  if (!$this->help_icon_alt)
210  {
211  $this->help_icon_alt = $this->help_icon;
212  }
213  }
214 
220  function setHeaderNames($a_header_names)
221  {
222  $this->header_names = $a_header_names;
223  $this->column_count = count($this->header_names);
224  }
225 
231  function getColumnCount()
232  {
233  return $this->column_count;
234  }
235 
242  function setHeaderVars($a_header_vars,$a_header_params = 0)
243  {
244  $this->header_vars = $a_header_vars;
245 
246  if ($a_header_params == 0 or !is_array($a_header_params))
247  {
248  $this->link_params = "";
249  }
250  else
251  {
252  $this->header_params = $a_header_params; // temp. solution for linkbar
253 
254  foreach ($a_header_params as $key => $val)
255  {
256  $this->link_params .= $key."=".$val."&";
257  }
258  }
259  }
260 
266  function setColumnWidth($a_column_width)
267  {
268  $this->column_width = $a_column_width;
269  }
270 
277  function setOneColumnWidth($a_column_width,$a_column_number)
278  {
279  $this->column_width[$a_column_number] = $a_column_width;
280  }
281 
289  function setMaxCount($a_max_count)
290  {
291  $this->max_count = $a_max_count;
292 
293  if ($this->max_limit)
294  {
295  $this->limit = $this->max_count;
296  }
297  }
298 
305  function setLimit($a_limit = 0, $a_default_limit = 0)
306  {
307  $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
308 
309  if ($this->limit == 0)
310  {
311  $this->max_limit = true;
312  }
313  }
314 
318  function getLimit()
319  {
320  return $this->limit;
321  }
322 
323 
329  function setPrefix($a_prefix)
330  {
331  $this->prefix = ($a_prefix) ? $a_prefix : "";
332  }
333 
339  function setOffset($a_offset)
340  {
341  $this->offset = ($a_offset) ? $a_offset : 0;
342  }
343 
347  function getOffset()
348  {
349  return $this->offset;
350  }
351 
358  function setOrderColumn($a_order_column = 0,$a_default_column = 0)
359  {
360  // set default sort column to first column
361  if (empty($a_order_column))
362  {
363  if (!empty($a_default_column))
364  {
365  $this->order_column = array_search($a_default_column,$this->header_vars);
366  }
367  else
368  {
369  $this->order_column = 0;
370  return;
371  }
372  }
373  else
374  {
375  $this->order_column = array_search($a_order_column,$this->header_vars);
376  }
377 
378  if ($this->order_column === false)
379  {
380  // if not found, set default sort column to first column
381  $this->order_column = 0;
382  }
383  }
384 
388  function getOrderColumn()
389  {
390  return $this->order_column;
391  }
392 
398  function setOrderDirection($a_order_direction)
399  {
400  if ($a_order_direction == "desc")
401  {
402  $this->order_direction = "desc";
403  $this->sort_order = "asc";
404  }
405  else
406  {
407  $this->order_direction = "asc"; // set default sort order to "ASC"
408  $this->sort_order = "desc";
409  }
410  }
411 
415  function getOrderDirection()
416  {
417  return $this->order_direction;
418  }
419 
427  function setFooter($a_style,$a_previous = 0,$a_next = 0)
428  {
429  $this->footer_style = $a_style;
430 
431  $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
432  $this->footer_next = ($a_next) ? $a_next : ">>>";
433  }
434 
443  function enable($a_module_name)
444  {
445  if (!in_array($a_module_name,array_keys($this->enabled)))
446  {
447  return false;
448  }
449 
450  $this->enabled[$a_module_name] = true;
451  }
452 
461  function disable($a_module_name)
462  {
463  if (!in_array($a_module_name,array_keys($this->enabled)))
464  {
465  return false;
466  }
467 
468  $this->enabled[$a_module_name] = false;
469  }
470 
471 
472  function sortData()
473  {
474  if($this->enabled["sort"])
475  {
476  $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
477  }
478  $this->data = array_slice($this->data,$this->offset,$this->limit);
479  }
480 
485  function render()
486  {
487  if($this->enabled['table'])
488  {
489  $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
490  }
491 
492  // table title icon
493  if ($this->enabled["icon"] && $this->enabled["title"])
494  {
495  $this->tpl->setCurrentBlock("tbl_header_title_icon");
496  $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
497  $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
498  $this->tpl->parseCurrentBlock();
499  }
500  // table title help
501  if ($this->enabled["help"] && $this->enabled["title"])
502  {
503  $this->tpl->setCurrentBlock("tbl_header_title_help");
504  $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
505  $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
506  $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
507  $this->tpl->parseCurrentBlock();
508  }
509 
510  // hits per page selector
511  if ($this->enabled["hits"] && $this->enabled["title"])
512  {
513  $this->tpl->setCurrentBlock("tbl_header_hits_page");
514  $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
515  $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
516  $this->tpl->parseCurrentBlock();
517  }
518 
519  // table title
520  if ($this->enabled["title"])
521  {
522  $this->tpl->setCurrentBlock("tbl_header_title");
523  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
524  $this->tpl->setVariable("TBL_TITLE",$this->title);
525  $this->tpl->parseCurrentBlock();
526  }
527 
528  // table header
529  if ($this->enabled["header"])
530  {
531  $this->renderHeader();
532  }
533 
534  // table data
535  // the table content may be skipped to use an individual template blockfile
536  // To do so don't set $this->data and parse your table content by yourself
537  // The template block name for the blockfile MUST be 'TBL_CONTENT'
538 
539  if ($this->enabled["content"] && is_array($this->data))
540  {
541  if($this->enabled['auto_sort'])
542  {
543  $this->setMaxCount(count($this->data));
544  $this->sortData();
545  }
546  $count = 0;
547 
548  foreach ($this->data as $tbl_content_row)
549  {
550  foreach ($tbl_content_row as $key => $tbl_content_cell)
551  {
552  if (is_array($tbl_content_cell))
553  {
554  $this->tpl->setCurrentBlock("tbl_cell_subtitle");
555  $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
556  $this->tpl->parseCurrentBlock();
557  $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
558  }
559 
560  $this->tpl->setCurrentBlock("tbl_content_cell");
561  $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
562  $this->tpl->parseCurrentBlock();
563  }
564 
565  $this->tpl->setCurrentBlock("tbl_content_row");
566  $rowcolor = ilUtil::switchColor($count,"tblrow1","tblrow2");
567  $this->tpl->setVariable("ROWCOLOR", $rowcolor);
568  $this->tpl->parseCurrentBlock();
569 
570  $count++;
571  }
572  }
573  // select all checkbox
574  if ($this->enabled["select_all"])
575  {
576  if ((strlen($this->getFormName())) && (strlen($this->getSelectAllCheckbox())))
577  {
578  $this->tpl->setVariable('SELECT_PREFIX',$this->prefix);
579  $this->tpl->setVariable("SELECT_ALL_TXT_SELECT_ALL", $this->lng->txt("select_all"));
580  $this->tpl->setVariable("SELECT_ALL_CHECKBOX_NAME", $this->getSelectAllCheckbox());
581  $this->tpl->setVariable("SELECT_ALL_FORM_NAME", $this->getFormName());
582  if (!($this->enabled["numinfo"] && $this->enabled["footer"]))
583  {
584  $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
585  }
586  }
587  }
588 
589  // table header numinfo
590  if ($this->enabled["numinfo_header"])
591  {
592  $start = $this->offset + 1; // compute num info
593  $end = $this->offset + $this->limit;
594 
595  if ($end > $this->max_count or $this->limit == 0)
596  {
597  $end = $this->max_count;
598  }
599 
600  if ($this->lang_support)
601  {
602  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
603  }
604  else
605  {
606  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
607  }
608  if ($this->max_count > 0)
609  {
610  //$numinfo = $this->lng->txt("no_datasets");
611  $this->tpl->setCurrentBlock("tbl_header_numinfo");
612  $this->tpl->setVariable("NUMINFO_HEADER", $numinfo);
613  $this->tpl->setVariable("COLUMN_COUNT_HEADER", $this->getColumnCount());
614  $this->tpl->parseCurrentBlock();
615  }
616  }
617  // table footer numinfo
618  if ($this->enabled["numinfo"] && $this->enabled["footer"])
619  {
620  $start = $this->offset + 1; // compute num info
621  $end = $this->offset + $this->limit;
622 
623  if ($end > $this->max_count or $this->limit == 0)
624  {
625  $end = $this->max_count;
626  }
627 
628  if ($this->lang_support)
629  {
630  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
631  }
632  else
633  {
634  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
635  }
636  if ($this->max_count > 0)
637  {
638  //$numinfo = $this->lng->txt("no_datasets");
639  $this->tpl->setCurrentBlock("tbl_footer_numinfo");
640  $this->tpl->setVariable("NUMINFO", $numinfo);
641  $this->tpl->parseCurrentBlock();
642  }
643  }
644  // table footer linkbar
645  if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
646  && $this->max_count > 0)
647  {
648  $params = array(
649  $this->prefix."sort_by" => $this->header_vars[$this->order_column],
650  $this->prefix."sort_order" => $this->order_direction
651  );
652  $params = array_merge($this->header_params,$params);
653 
654  $layout = array(
655  "link" => $this->footer_style,
656  "prev" => $this->footer_previous,
657  "next" => $this->footer_next,
658  );
659 
660  $base = ($this->getBase() == "")
661  ? basename($_SERVER["PHP_SELF"])
662  : $this->getBase();
663 
664  $linkbar = ilUtil::Linkbar($base,$this->max_count,$this->limit,$this->offset,$params,$layout, $this->prefix);
665  $this->tpl->setCurrentBlock("tbl_footer_linkbar");
666  $this->tpl->setVariable("LINKBAR", $linkbar);
667  $this->tpl->parseCurrentBlock();
668  }
669 
670  // table footer
671  if ($this->enabled["footer"] && $this->max_count > 0)
672  {
673  $this->tpl->setCurrentBlock("tbl_footer");
674  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
675  $this->tpl->parseCurrentBlock();
676  }
677 
678  // action buttons
679  if ($this->enabled["action"])
680  {
681  foreach ($this->action_buttons as $button)
682  {
683  $this->tpl->setCurrentBlock("tbl_action_btn");
684  $this->tpl->setVariable("BTN_NAME", $button["name"]);
685  $this->tpl->setVariable("BTN_VALUE", $button["value"]);
686  $this->tpl->parseCurrentBlock();
687  }
688  $this->tpl->setCurrentBlock("tbl_action_row");
689  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
690  $this->tpl->setVariable("ALT_ARROW", $this->lng->txt("arrow_downright.gif"));
691  $this->tpl->setVariable("COLUMN_COUNTS", $this->getColumnCount());
692  $this->tpl->parseCurrentBlock();
693  }
694 
695  if ($this->enabled["form"])
696  {
697  $this->tpl->touchBlock("tbl_form_footer");
698  }
699 
700  if($this->enabled['table'])
701  {
702  $this->tpl->touchBlock("tbl_table_end");
703  }
704 
705  if (!$this->global_tpl)
706  {
707  return $this->tpl->get();
708  }
709  }
710 
711  function renderHeader()
712  {
713  foreach ($this->header_names as $key => $tbl_header_cell)
714  {
715  if (!$this->enabled["sort"])
716  {
717  $this->tpl->setCurrentBlock("tbl_header_no_link");
718  if ($this->column_width[$key])
719  {
720  $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
721  }
722  $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
723  $this->tpl->parseCurrentBlock();
724  continue;
725  }
726  if (($key == $this->order_column) && ($this->order_direction != ""))
727  {
728  if (strcmp($this->header_vars[$key], "") != 0)
729  {
730  $this->tpl->setCurrentBlock("tbl_order_image");
731  $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.gif"));
732  $this->tpl->parseCurrentBlock();
733  }
734  }
735 
736  $this->tpl->setCurrentBlock("tbl_header_cell");
737  $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
738 
739  // only set width if a value is given for that column
740  if ($this->column_width[$key])
741  {
742  $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
743  }
744 
745  $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
746  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
747 
748  $order_dir = "asc";
749 
750  if ($key == $this->order_column)
751  {
752  $order_dir = $this->sort_order;
753 
754  $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
755  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
756  }
757 
758  $this->setOrderLink($key, $order_dir);
759  $this->tpl->parseCurrentBlock();
760  }
761 
762  $this->tpl->setCurrentBlock("tbl_header");
763  $this->tpl->parseCurrentBlock();
764  }
765 
766  function setOrderLink($key, $order_dir)
767  {
768  $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);
769  }
770 
771  /*
772  * set a tpl stylesheet
773  * @access public
774  * @param string table element
775  * @param string CSS definition
776  */
777  function setStyle($a_element,$a_style)
778  {
779  $this->styles[$a_element] = $a_style;
780  }
781 
782  /*
783  * get a tpl stylesheet
784  * @access public
785  * @param string table element
786  */
787  function getStyle($a_element)
788  {
789  return $this->styles[$a_element];
790  }
791 
797  function setBase($a_base)
798  {
799  $this->base = $a_base;
800  }
801 
807  function getBase()
808  {
809  return $this->base;
810  }
811 
812  /*
813  * get the name of the parent form
814  * @access public
815  * @return string name of the parent form
816  */
817  function getFormName()
818  {
819  return $this->form_name;
820  }
821 
822  /*
823  * set the name of the parent form
824  * @access public
825  * @param string $a_name name of the parent form
826  */
827  function setFormName($a_name = "cmd")
828  {
829  $this->form_name = $a_name;
830  }
831 
832  /*
833  * get the name of the checkbox that should be toggled with a select all button
834  * @access public
835  * @return string name of the checkbox
836  */
838  {
840  }
841 
842  /*
843  * set the name of the checkbox that should be toggled with a select all button
844  * @access public
845  * @param string $a_select_all_checkbox name of the checkbox
846  */
847  function setSelectAllCheckbox($a_select_all_checkbox)
848  {
849  $this->select_all_checkbox = $a_select_all_checkbox;
850  }
851 
852  /*
853  * Removes all action buttons from the table
854  *
855  * @access public
856  */
858  {
859  $this->action_buttons = array();
860  }
861 
862  /*
863  * Adds an action button to the table
864  *
865  * @param string $btn_name Name of the action button
866  * @param string $btn_value Value of the action button
867  * @access public
868  */
869  function addActionButton($btn_name, $btn_value)
870  {
871  array_push($this->action_buttons,
872  array(
873  "name" => $btn_name,
874  "value" => $btn_value
875  )
876  );
877  }
878 }
879 ?>