ILIAS  Release_3_10_x_branch Revision 61812
 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 
440  function enable($a_module_name)
441  {
442  if (!in_array($a_module_name,array_keys($this->enabled)))
443  {
444  return false;
445  }
446 
447  $this->enabled[$a_module_name] = true;
448  }
449 
455  function disable($a_module_name)
456  {
457  if (!in_array($a_module_name,array_keys($this->enabled)))
458  {
459  return false;
460  }
461 
462  $this->enabled[$a_module_name] = false;
463  }
464 
465 
466  function sortData()
467  {
468  if($this->enabled["sort"])
469  {
470  $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
471  }
472  $this->data = array_slice($this->data,$this->offset,$this->limit);
473  }
474 
479  function render()
480  {
481  if($this->enabled['table'])
482  {
483  $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
484  }
485 
486  // table title icon
487  if ($this->enabled["icon"] && $this->enabled["title"])
488  {
489  $this->tpl->setCurrentBlock("tbl_header_title_icon");
490  $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
491  $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
492  $this->tpl->parseCurrentBlock();
493  }
494  // table title help
495  if ($this->enabled["help"] && $this->enabled["title"])
496  {
497  $this->tpl->setCurrentBlock("tbl_header_title_help");
498  $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
499  $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
500  $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
501  $this->tpl->parseCurrentBlock();
502  }
503 
504  // hits per page selector
505  if ($this->enabled["hits"] && $this->enabled["title"])
506  {
507  $this->tpl->setCurrentBlock("tbl_header_hits_page");
508  $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
509  $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
510  $this->tpl->parseCurrentBlock();
511  }
512 
513  // table title
514  if ($this->enabled["title"])
515  {
516  $this->tpl->setCurrentBlock("tbl_header_title");
517  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
518  $this->tpl->setVariable("TBL_TITLE",$this->title);
519  $this->tpl->parseCurrentBlock();
520  }
521 
522  // table header
523  if ($this->enabled["header"])
524  {
525  $this->renderHeader();
526  }
527 
528  // table data
529  // the table content may be skipped to use an individual template blockfile
530  // To do so don't set $this->data and parse your table content by yourself
531  // The template block name for the blockfile MUST be 'TBL_CONTENT'
532 
533  if ($this->enabled["content"] && is_array($this->data))
534  {
535  if($this->enabled['auto_sort'])
536  {
537  $this->setMaxCount(count($this->data));
538  $this->sortData();
539  }
540  $count = 0;
541 
542  foreach ($this->data as $tbl_content_row)
543  {
544  foreach ($tbl_content_row as $key => $tbl_content_cell)
545  {
546  if (is_array($tbl_content_cell))
547  {
548  $this->tpl->setCurrentBlock("tbl_cell_subtitle");
549  $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
550  $this->tpl->parseCurrentBlock();
551  $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
552  }
553 
554  $this->tpl->setCurrentBlock("tbl_content_cell");
555  $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
556  $this->tpl->parseCurrentBlock();
557  }
558 
559  $this->tpl->setCurrentBlock("tbl_content_row");
560  $rowcolor = ilUtil::switchColor($count,"tblrow1","tblrow2");
561  $this->tpl->setVariable("ROWCOLOR", $rowcolor);
562  $this->tpl->parseCurrentBlock();
563 
564  $count++;
565  }
566  }
567  // select all checkbox
568  if ($this->enabled["select_all"])
569  {
570  if ((strlen($this->getFormName())) && (strlen($this->getSelectAllCheckbox())))
571  {
572  $this->tpl->setVariable('SELECT_PREFIX',$this->prefix);
573  $this->tpl->setVariable("SELECT_ALL_TXT_SELECT_ALL", $this->lng->txt("select_all"));
574  $this->tpl->setVariable("SELECT_ALL_CHECKBOX_NAME", $this->getSelectAllCheckbox());
575  $this->tpl->setVariable("SELECT_ALL_FORM_NAME", $this->getFormName());
576  if (!($this->enabled["numinfo"] && $this->enabled["footer"]))
577  {
578  $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
579  }
580  }
581  }
582 
583  // table header numinfo
584  if ($this->enabled["numinfo_header"])
585  {
586  $start = $this->offset + 1; // compute num info
587  $end = $this->offset + $this->limit;
588 
589  if ($end > $this->max_count or $this->limit == 0)
590  {
591  $end = $this->max_count;
592  }
593 
594  if ($this->lang_support)
595  {
596  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
597  }
598  else
599  {
600  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
601  }
602  if ($this->max_count > 0)
603  {
604  //$numinfo = $this->lng->txt("no_datasets");
605  $this->tpl->setCurrentBlock("tbl_header_numinfo");
606  $this->tpl->setVariable("NUMINFO_HEADER", $numinfo);
607  $this->tpl->setVariable("COLUMN_COUNT_HEADER", $this->getColumnCount());
608  $this->tpl->parseCurrentBlock();
609  }
610  }
611  // table footer numinfo
612  if ($this->enabled["numinfo"] && $this->enabled["footer"])
613  {
614  $start = $this->offset + 1; // compute num info
615  $end = $this->offset + $this->limit;
616 
617  if ($end > $this->max_count or $this->limit == 0)
618  {
619  $end = $this->max_count;
620  }
621 
622  if ($this->lang_support)
623  {
624  $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
625  }
626  else
627  {
628  $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
629  }
630  if ($this->max_count > 0)
631  {
632  //$numinfo = $this->lng->txt("no_datasets");
633  $this->tpl->setCurrentBlock("tbl_footer_numinfo");
634  $this->tpl->setVariable("NUMINFO", $numinfo);
635  $this->tpl->parseCurrentBlock();
636  }
637  }
638  // table footer linkbar
639  if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
640  && $this->max_count > 0)
641  {
642  $params = array(
643  $this->prefix."sort_by" => $this->header_vars[$this->order_column],
644  $this->prefix."sort_order" => $this->order_direction
645  );
646  $params = array_merge($this->header_params,$params);
647 
648  $layout = array(
649  "link" => $this->footer_style,
650  "prev" => $this->footer_previous,
651  "next" => $this->footer_next,
652  );
653 
654  $base = ($this->getBase() == "")
655  ? basename($_SERVER["PHP_SELF"])
656  : $this->getBase();
657 
658  $linkbar = ilUtil::Linkbar($base,$this->max_count,$this->limit,$this->offset,$params,$layout, $this->prefix);
659  $this->tpl->setCurrentBlock("tbl_footer_linkbar");
660  $this->tpl->setVariable("LINKBAR", $linkbar);
661  $this->tpl->parseCurrentBlock();
662  }
663 
664  // table footer
665  if ($this->enabled["footer"] && $this->max_count > 0)
666  {
667  $this->tpl->setCurrentBlock("tbl_footer");
668  $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
669  $this->tpl->parseCurrentBlock();
670  }
671 
672  // action buttons
673  if ($this->enabled["action"])
674  {
675  foreach ($this->action_buttons as $button)
676  {
677  $this->tpl->setCurrentBlock("tbl_action_btn");
678  $this->tpl->setVariable("BTN_NAME", $button["name"]);
679  $this->tpl->setVariable("BTN_VALUE", $button["value"]);
680  $this->tpl->parseCurrentBlock();
681  }
682  $this->tpl->setCurrentBlock("tbl_action_row");
683  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
684  $this->tpl->setVariable("ALT_ARROW", $this->lng->txt("arrow_downright.gif"));
685  $this->tpl->setVariable("COLUMN_COUNTS", $this->getColumnCount());
686  $this->tpl->parseCurrentBlock();
687  }
688 
689  if ($this->enabled["form"])
690  {
691  $this->tpl->touchBlock("tbl_form_footer");
692  }
693 
694  if($this->enabled['table'])
695  {
696  $this->tpl->touchBlock("tbl_table_end");
697  }
698 
699  if (!$this->global_tpl)
700  {
701  return $this->tpl->get();
702  }
703  }
704 
705  function renderHeader()
706  {
707  foreach ($this->header_names as $key => $tbl_header_cell)
708  {
709  if (!$this->enabled["sort"])
710  {
711  $this->tpl->setCurrentBlock("tbl_header_no_link");
712  if ($this->column_width[$key])
713  {
714  $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
715  }
716  $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
717  $this->tpl->parseCurrentBlock();
718  continue;
719  }
720  if (($key == $this->order_column) && ($this->order_direction != ""))
721  {
722  if (strcmp($this->header_vars[$key], "") != 0)
723  {
724  $this->tpl->setCurrentBlock("tbl_order_image");
725  $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.gif"));
726  $this->tpl->parseCurrentBlock();
727  }
728  }
729 
730  $this->tpl->setCurrentBlock("tbl_header_cell");
731  $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
732 
733  // only set width if a value is given for that column
734  if ($this->column_width[$key])
735  {
736  $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
737  }
738 
739  $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
740  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
741 
742  $order_dir = "asc";
743 
744  if ($key == $this->order_column)
745  {
746  $order_dir = $this->sort_order;
747 
748  $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
749  $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
750  }
751 
752  $this->setOrderLink($key, $order_dir);
753  $this->tpl->parseCurrentBlock();
754  }
755 
756  $this->tpl->setCurrentBlock("tbl_header");
757  $this->tpl->parseCurrentBlock();
758  }
759 
760  function setOrderLink($key, $order_dir)
761  {
762  $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);
763  }
764 
765  /*
766  * set a tpl stylesheet
767  * @access public
768  * @param string table element
769  * @param string CSS definition
770  */
771  function setStyle($a_element,$a_style)
772  {
773  $this->styles[$a_element] = $a_style;
774  }
775 
776  /*
777  * get a tpl stylesheet
778  * @access public
779  * @param string table element
780  */
781  function getStyle($a_element)
782  {
783  return $this->styles[$a_element];
784  }
785 
791  function setBase($a_base)
792  {
793  $this->base = $a_base;
794  }
795 
801  function getBase()
802  {
803  return $this->base;
804  }
805 
806  /*
807  * get the name of the parent form
808  * @access public
809  * @return string name of the parent form
810  */
811  function getFormName()
812  {
813  return $this->form_name;
814  }
815 
816  /*
817  * set the name of the parent form
818  * @access public
819  * @param string $a_name name of the parent form
820  */
821  function setFormName($a_name = "cmd")
822  {
823  $this->form_name = $a_name;
824  }
825 
826  /*
827  * get the name of the checkbox that should be toggled with a select all button
828  * @access public
829  * @return string name of the checkbox
830  */
832  {
834  }
835 
836  /*
837  * set the name of the checkbox that should be toggled with a select all button
838  * @access public
839  * @param string $a_select_all_checkbox name of the checkbox
840  */
841  function setSelectAllCheckbox($a_select_all_checkbox)
842  {
843  $this->select_all_checkbox = $a_select_all_checkbox;
844  }
845 
846  /*
847  * Removes all action buttons from the table
848  *
849  * @access public
850  */
852  {
853  $this->action_buttons = array();
854  }
855 
856  /*
857  * Adds an action button to the table
858  *
859  * @param string $btn_name Name of the action button
860  * @param string $btn_value Value of the action button
861  * @access public
862  */
863  function addActionButton($btn_name, $btn_value)
864  {
865  array_push($this->action_buttons,
866  array(
867  "name" => $btn_name,
868  "value" => $btn_value
869  )
870  );
871  }
872 }
873 ?>