00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00037 class ilTableGUI
00038 {
00039 var $title;
00040 var $icon;
00041 var $icon_alt;
00042
00043 var $help_page;
00044 var $help_icon;
00045 var $help_icon_alt;
00046
00047 var $header_names;
00048 var $header_vars;
00049 var $linkbar_vars;
00050
00051 var $data;
00052
00053 var $column_count;
00054 var $column_width;
00055
00056 var $max_count;
00057 var $limit;
00058 var $max_limit = false;
00059 var $offset;
00060 var $order_column;
00061 var $order_direction;
00062
00063 var $footer_style;
00064 var $footer_previous;
00065 var $footer_next;
00066
00067 var $lang_support = true;
00068 var $global_tpl;
00069 var $form_name;
00070 var $select_all_checkbox;
00071 var $action_buttons;
00072
00073 var $prefix;
00074 var $base = "";
00075
00076
00077 var $enabled = array( "table" => true,
00078 "title" => true,
00079 "icon" => true,
00080 "help" => false,
00081 "content" => true,
00082 "action" => false,
00083 "header" => true,
00084 "footer" => true,
00085 "linkbar" => true,
00086 "numinfo" => true,
00087 "numinfo_header" => false,
00088 "sort" => true,
00089 "hits" => false,
00090 "auto_sort" => true,
00091 "select_all" => false
00092 );
00093
00094
00095 var $styles = array(
00096 "table" => "fullwidth"
00097 );
00098
00106 function ilTableGUI($a_data = 0,$a_global_tpl = true)
00107 {
00108 global $ilias, $tpl, $lng;
00109
00110 $this->global_tpl = $a_global_tpl;
00111 $this->ilias =& $ilias;
00112 $this->header_vars = array();
00113 $this->header_params = array();
00114 $this->enabled["form"] = true;
00115 $this->action_buttons = array();
00116 if ($this->global_tpl)
00117 {
00118 $this->tpl =& $tpl;
00119 }
00120 else
00121 {
00122 $this->tpl = new ilTemplate("tpl.table.html", true, true, "Services/Table");
00123 }
00124
00125 $this->lng =& $lng;
00126
00127 if (!$this->lng)
00128 {
00129 $this->lang_support = false;
00130 }
00131
00132 $this->setData($a_data);
00133 }
00134
00135
00141 function setTemplate(&$a_tpl)
00142 {
00143 $this->tpl =& $a_tpl;
00144 }
00145
00146 function &getTemplateObject()
00147 {
00148 return $this->tpl;
00149 }
00150
00156 function setData($a_data)
00157 {
00158 if (is_array($a_data))
00159 {
00160 $this->data = $a_data;
00161 }
00162 }
00163
00164 function getData()
00165 {
00166 return $this->data;
00167 }
00168
00176 function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
00177 {
00178 $this->title = $a_title;
00179 $this->icon = $a_icon;
00180 $this->icon_alt = $a_icon_alt;
00181
00182 if (!$this->icon)
00183 {
00184 $this->enabled["icon"] = false;
00185
00186 return;
00187 }
00188
00189 if (!$this->icon_alt)
00190 {
00191 $this->icon_alt = $this->icon;
00192 }
00193 $this->enabled["icon"] = true;
00194 }
00195
00203 function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
00204 {
00205 $this->help_page = $a_help_page;
00206 $this->help_icon = $a_help_icon;
00207 $this->help_icon_alt = $a_help_icon_alt;
00208
00209 if (!$this->help_icon_alt)
00210 {
00211 $this->help_icon_alt = $this->help_icon;
00212 }
00213 }
00214
00220 function setHeaderNames($a_header_names)
00221 {
00222 $this->header_names = $a_header_names;
00223 $this->column_count = count($this->header_names);
00224 }
00225
00231 function getColumnCount()
00232 {
00233 return $this->column_count;
00234 }
00235
00242 function setHeaderVars($a_header_vars,$a_header_params = 0)
00243 {
00244 $this->header_vars = $a_header_vars;
00245
00246 if ($a_header_params == 0 or !is_array($a_header_params))
00247 {
00248 $this->link_params = "";
00249 }
00250 else
00251 {
00252 $this->header_params = $a_header_params;
00253
00254 foreach ($a_header_params as $key => $val)
00255 {
00256 $this->link_params .= $key."=".$val."&";
00257 }
00258 }
00259 }
00260
00266 function setColumnWidth($a_column_width)
00267 {
00268 $this->column_width = $a_column_width;
00269 }
00270
00277 function setOneColumnWidth($a_column_width,$a_column_number)
00278 {
00279 $this->column_width[$a_column_number] = $a_column_width;
00280 }
00281
00289 function setMaxCount($a_max_count)
00290 {
00291 $this->max_count = $a_max_count;
00292
00293 if ($this->max_limit)
00294 {
00295 $this->limit = $this->max_count;
00296 }
00297 }
00298
00305 function setLimit($a_limit = 0, $a_default_limit = 0)
00306 {
00307 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
00308
00309 if ($this->limit == 0)
00310 {
00311 $this->max_limit = true;
00312 }
00313 }
00314
00318 function getLimit()
00319 {
00320 return $this->limit;
00321 }
00322
00323
00329 function setPrefix($a_prefix)
00330 {
00331 $this->prefix = ($a_prefix) ? $a_prefix : "";
00332 }
00333
00339 function setOffset($a_offset)
00340 {
00341 $this->offset = ($a_offset) ? $a_offset : 0;
00342 }
00343
00347 function getOffset()
00348 {
00349 return $this->offset;
00350 }
00351
00358 function setOrderColumn($a_order_column = 0,$a_default_column = 0)
00359 {
00360
00361 if (empty($a_order_column))
00362 {
00363 if (!empty($a_default_column))
00364 {
00365 $this->order_column = array_search($a_default_column,$this->header_vars);
00366 }
00367 else
00368 {
00369 $this->order_column = 0;
00370 return;
00371 }
00372 }
00373 else
00374 {
00375 $this->order_column = array_search($a_order_column,$this->header_vars);
00376 }
00377
00378 if ($this->order_column === false)
00379 {
00380
00381 $this->order_column = 0;
00382 }
00383 }
00384
00388 function getOrderColumn()
00389 {
00390 return $this->order_column;
00391 }
00392
00398 function setOrderDirection($a_order_direction)
00399 {
00400 if ($a_order_direction == "desc")
00401 {
00402 $this->order_direction = "desc";
00403 $this->sort_order = "asc";
00404 }
00405 else
00406 {
00407 $this->order_direction = "asc";
00408 $this->sort_order = "desc";
00409 }
00410 }
00411
00415 function getOrderDirection()
00416 {
00417 return $this->order_direction;
00418 }
00419
00427 function setFooter($a_style,$a_previous = 0,$a_next = 0)
00428 {
00429 $this->footer_style = $a_style;
00430
00431 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
00432 $this->footer_next = ($a_next) ? $a_next : ">>>";
00433 }
00434
00440 function enable($a_module_name)
00441 {
00442 if (!in_array($a_module_name,array_keys($this->enabled)))
00443 {
00444 return false;
00445 }
00446
00447 $this->enabled[$a_module_name] = true;
00448 }
00449
00455 function disable($a_module_name)
00456 {
00457 if (!in_array($a_module_name,array_keys($this->enabled)))
00458 {
00459 return false;
00460 }
00461
00462 $this->enabled[$a_module_name] = false;
00463 }
00464
00465
00466 function sortData()
00467 {
00468 if($this->enabled["sort"])
00469 {
00470 $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
00471 }
00472 $this->data = array_slice($this->data,$this->offset,$this->limit);
00473 }
00474
00479 function render()
00480 {
00481 if($this->enabled['table'])
00482 {
00483 $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
00484 }
00485
00486
00487 if ($this->enabled["icon"] && $this->enabled["title"])
00488 {
00489 $this->tpl->setCurrentBlock("tbl_header_title_icon");
00490 $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
00491 $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
00492 $this->tpl->parseCurrentBlock();
00493 }
00494
00495 if ($this->enabled["help"] && $this->enabled["title"])
00496 {
00497 $this->tpl->setCurrentBlock("tbl_header_title_help");
00498 $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
00499 $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
00500 $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
00501 $this->tpl->parseCurrentBlock();
00502 }
00503
00504
00505 if ($this->enabled["hits"] && $this->enabled["title"])
00506 {
00507 $this->tpl->setCurrentBlock("tbl_header_hits_page");
00508 $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
00509 $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
00510 $this->tpl->parseCurrentBlock();
00511 }
00512
00513
00514 if ($this->enabled["title"])
00515 {
00516 $this->tpl->setCurrentBlock("tbl_header_title");
00517 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00518 $this->tpl->setVariable("TBL_TITLE",$this->title);
00519 $this->tpl->parseCurrentBlock();
00520 }
00521
00522
00523 if ($this->enabled["header"])
00524 {
00525 $this->renderHeader();
00526 }
00527
00528
00529
00530
00531
00532
00533 if ($this->enabled["content"] && is_array($this->data))
00534 {
00535 if($this->enabled['auto_sort'])
00536 {
00537 $this->setMaxCount(count($this->data));
00538 $this->sortData();
00539 }
00540 $count = 0;
00541
00542 foreach ($this->data as $tbl_content_row)
00543 {
00544 foreach ($tbl_content_row as $key => $tbl_content_cell)
00545 {
00546 if (is_array($tbl_content_cell))
00547 {
00548 $this->tpl->setCurrentBlock("tbl_cell_subtitle");
00549 $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
00550 $this->tpl->parseCurrentBlock();
00551 $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
00552 }
00553
00554 $this->tpl->setCurrentBlock("tbl_content_cell");
00555 $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
00556 $this->tpl->parseCurrentBlock();
00557 }
00558
00559 $this->tpl->setCurrentBlock("tbl_content_row");
00560 $rowcolor = ilUtil::switchColor($count,"tblrow1","tblrow2");
00561 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
00562 $this->tpl->parseCurrentBlock();
00563
00564 $count++;
00565 }
00566 }
00567
00568 if ($this->enabled["select_all"])
00569 {
00570 if ((strlen($this->getFormName())) && (strlen($this->getSelectAllCheckbox())))
00571 {
00572 $this->tpl->setVariable('SELECT_PREFIX',$this->prefix);
00573 $this->tpl->setVariable("SELECT_ALL_TXT_SELECT_ALL", $this->lng->txt("select_all"));
00574 $this->tpl->setVariable("SELECT_ALL_CHECKBOX_NAME", $this->getSelectAllCheckbox());
00575 $this->tpl->setVariable("SELECT_ALL_FORM_NAME", $this->getFormName());
00576 if (!($this->enabled["numinfo"] && $this->enabled["footer"]))
00577 {
00578 $this->tpl->setVariable("COLUMN_COUNT", $this->getColumnCount());
00579 }
00580 }
00581 }
00582
00583
00584 if ($this->enabled["numinfo_header"])
00585 {
00586 $start = $this->offset + 1;
00587 $end = $this->offset + $this->limit;
00588
00589 if ($end > $this->max_count or $this->limit == 0)
00590 {
00591 $end = $this->max_count;
00592 }
00593
00594 if ($this->lang_support)
00595 {
00596 $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
00597 }
00598 else
00599 {
00600 $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
00601 }
00602 if ($this->max_count > 0)
00603 {
00604
00605 $this->tpl->setCurrentBlock("tbl_header_numinfo");
00606 $this->tpl->setVariable("NUMINFO_HEADER", $numinfo);
00607 $this->tpl->setVariable("COLUMN_COUNT_HEADER", $this->getColumnCount());
00608 $this->tpl->parseCurrentBlock();
00609 }
00610 }
00611
00612 if ($this->enabled["numinfo"] && $this->enabled["footer"])
00613 {
00614 $start = $this->offset + 1;
00615 $end = $this->offset + $this->limit;
00616
00617 if ($end > $this->max_count or $this->limit == 0)
00618 {
00619 $end = $this->max_count;
00620 }
00621
00622 if ($this->lang_support)
00623 {
00624 $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
00625 }
00626 else
00627 {
00628 $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
00629 }
00630 if ($this->max_count > 0)
00631 {
00632
00633 $this->tpl->setCurrentBlock("tbl_footer_numinfo");
00634 $this->tpl->setVariable("NUMINFO", $numinfo);
00635 $this->tpl->parseCurrentBlock();
00636 }
00637 }
00638
00639 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
00640 && $this->max_count > 0)
00641 {
00642 $params = array(
00643 $this->prefix."sort_by" => $this->header_vars[$this->order_column],
00644 $this->prefix."sort_order" => $this->order_direction
00645 );
00646 $params = array_merge($this->header_params,$params);
00647
00648 $layout = array(
00649 "link" => $this->footer_style,
00650 "prev" => $this->footer_previous,
00651 "next" => $this->footer_next,
00652 );
00653
00654 $base = ($this->getBase() == "")
00655 ? basename($_SERVER["PHP_SELF"])
00656 : $this->getBase();
00657
00658 $linkbar = ilUtil::Linkbar($base,$this->max_count,$this->limit,$this->offset,$params,$layout);
00659 $this->tpl->setCurrentBlock("tbl_footer_linkbar");
00660 $this->tpl->setVariable("LINKBAR", $linkbar);
00661 $this->tpl->parseCurrentBlock();
00662 }
00663
00664
00665 if ($this->enabled["footer"] && $this->max_count > 0)
00666 {
00667 $this->tpl->setCurrentBlock("tbl_footer");
00668 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00669 $this->tpl->parseCurrentBlock();
00670 }
00671
00672
00673 if ($this->enabled["action"])
00674 {
00675 foreach ($this->action_buttons as $button)
00676 {
00677 $this->tpl->setCurrentBlock("tbl_action_btn");
00678 $this->tpl->setVariable("BTN_NAME", $button["name"]);
00679 $this->tpl->setVariable("BTN_VALUE", $button["value"]);
00680 $this->tpl->parseCurrentBlock();
00681 }
00682 $this->tpl->setCurrentBlock("tbl_action_row");
00683 $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00684 $this->tpl->setVariable("ALT_ARROW", $this->lng->txt("arrow_downright.gif"));
00685 $this->tpl->setVariable("COLUMN_COUNTS", $this->getColumnCount());
00686 $this->tpl->parseCurrentBlock();
00687 }
00688
00689 if ($this->enabled["form"])
00690 {
00691 $this->tpl->touchBlock("tbl_form_footer");
00692 }
00693
00694 if($this->enabled['table'])
00695 {
00696 $this->tpl->touchBlock("tbl_table_end");
00697 }
00698
00699 if (!$this->global_tpl)
00700 {
00701 return $this->tpl->get();
00702 }
00703 }
00704
00705 function renderHeader()
00706 {
00707 foreach ($this->header_names as $key => $tbl_header_cell)
00708 {
00709 if (!$this->enabled["sort"])
00710 {
00711 $this->tpl->setCurrentBlock("tbl_header_no_link");
00712 if ($this->column_width[$key])
00713 {
00714 $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
00715 }
00716 $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
00717 $this->tpl->parseCurrentBlock();
00718 continue;
00719 }
00720 if (($key == $this->order_column) && ($this->order_direction != ""))
00721 {
00722 if (strcmp($this->header_vars[$key], "") != 0)
00723 {
00724 $this->tpl->setCurrentBlock("tbl_order_image");
00725 $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.gif"));
00726 $this->tpl->parseCurrentBlock();
00727 }
00728 }
00729
00730 $this->tpl->setCurrentBlock("tbl_header_cell");
00731 $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
00732
00733
00734 if ($this->column_width[$key])
00735 {
00736 $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
00737 }
00738
00739 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
00740 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
00741
00742 $order_dir = "asc";
00743
00744 if ($key == $this->order_column)
00745 {
00746 $order_dir = $this->sort_order;
00747
00748 $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
00749 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
00750 }
00751
00752 $this->setOrderLink($key, $order_dir);
00753 $this->tpl->parseCurrentBlock();
00754 }
00755
00756 $this->tpl->setCurrentBlock("tbl_header");
00757 $this->tpl->parseCurrentBlock();
00758 }
00759
00760 function setOrderLink($key, $order_dir)
00761 {
00762 $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);
00763 }
00764
00765
00766
00767
00768
00769
00770
00771 function setStyle($a_element,$a_style)
00772 {
00773 $this->styles[$a_element] = $a_style;
00774 }
00775
00776
00777
00778
00779
00780
00781 function getStyle($a_element)
00782 {
00783 return $this->styles[$a_element];
00784 }
00785
00791 function setBase($a_base)
00792 {
00793 $this->base = $a_base;
00794 }
00795
00801 function getBase()
00802 {
00803 return $this->base;
00804 }
00805
00806
00807
00808
00809
00810
00811 function getFormName()
00812 {
00813 return $this->form_name;
00814 }
00815
00816
00817
00818
00819
00820
00821 function setFormName($a_name = "cmd")
00822 {
00823 $this->form_name = $a_name;
00824 }
00825
00826
00827
00828
00829
00830
00831 function getSelectAllCheckbox()
00832 {
00833 return $this->select_all_checkbox;
00834 }
00835
00836
00837
00838
00839
00840
00841 function setSelectAllCheckbox($a_select_all_checkbox)
00842 {
00843 $this->select_all_checkbox = $a_select_all_checkbox;
00844 }
00845
00846
00847
00848
00849
00850
00851 function clearActionButtons()
00852 {
00853 $this->action_buttons = array();
00854 }
00855
00856
00857
00858
00859
00860
00861
00862
00863 function addActionButton($btn_name, $btn_value)
00864 {
00865 array_push($this->action_buttons,
00866 array(
00867 "name" => $btn_name,
00868 "value" => $btn_value
00869 )
00870 );
00871 }
00872 }
00873 ?>