00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00035 class ilTableGUI
00036 {
00037 var $title;
00038 var $icon;
00039 var $icon_alt;
00040
00041 var $help_page;
00042 var $help_icon;
00043 var $help_icon_alt;
00044
00045 var $header_names;
00046 var $header_vars;
00047 var $linkbar_vars;
00048
00049 var $data;
00050
00051 var $column_count;
00052 var $column_width;
00053
00054 var $max_count;
00055 var $limit;
00056 var $max_limit = false;
00057 var $offset;
00058 var $order_column;
00059 var $order_direction;
00060
00061 var $footer_style;
00062 var $footer_previous;
00063 var $footer_next;
00064
00065 var $lang_support = true;
00066 var $global_tpl;
00067
00068 var $prefix;
00069
00070
00071 var $enabled = array( "table" => true,
00072 "title" => true,
00073 "icon" => true,
00074 "help" => false,
00075 "content" => true,
00076 "action" => false,
00077 "header" => true,
00078 "footer" => true,
00079 "linkbar" => true,
00080 "numinfo" => true,
00081 "sort" => true,
00082 "hits" => false,
00083 "auto_sort" => true
00084 );
00085
00086
00087 var $styles = array(
00088 "table" => "fullwidth"
00089 );
00090
00098 function ilTableGUI($a_data = 0,$a_global_tpl = true)
00099 {
00100 global $ilias, $tpl, $lng;
00101
00102 $this->global_tpl = $a_global_tpl;
00103 $this->ilias =& $ilias;
00104 $this->header_vars = array();
00105 $this->header_params = array();
00106 $this->enabled["form"] = true;
00107 if ($this->global_tpl)
00108 {
00109 $this->tpl =& $tpl;
00110 }
00111 else
00112 {
00113 $this->tpl = new ilTemplate("tpl.table.html",true,true);
00114 }
00115
00116
00117
00118 $this->lng =& $lng;
00119
00120 if (!$this->lng)
00121 {
00122 $this->lang_support = false;
00123 }
00124
00125 $this->setData($a_data);
00126 }
00127
00128
00134 function setTemplate(&$a_tpl)
00135 {
00136 $this->tpl =& $a_tpl;
00137 }
00138
00139 function &getTemplateObject()
00140 {
00141 return $this->tpl;
00142 }
00143
00149 function setData($a_data)
00150 {
00151 if (is_array($a_data))
00152 {
00153 $this->data = $a_data;
00154 }
00155 }
00156
00157 function getData()
00158 {
00159 return $this->data;
00160 }
00161
00169 function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
00170 {
00171 $this->title = $a_title;
00172 $this->icon = $a_icon;
00173 $this->icon_alt = $a_icon_alt;
00174
00175 if (!$this->icon)
00176 {
00177 $this->enabled["icon"] = false;
00178
00179 return;
00180 }
00181
00182 if (!$this->icon_alt)
00183 {
00184 $this->icon_alt = $this->icon;
00185 }
00186 }
00187
00195 function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
00196 {
00197 $this->help_page = $a_help_page;
00198 $this->help_icon = $a_help_icon;
00199 $this->help_icon_alt = $a_help_icon_alt;
00200
00201 if (!$this->help_icon_alt)
00202 {
00203 $this->help_icon_alt = $this->help_icon;
00204 }
00205 }
00206
00212 function setHeaderNames($a_header_names)
00213 {
00214 $this->header_names = $a_header_names;
00215 $this->column_count = count($this->header_names);
00216 }
00217
00224 function setHeaderVars($a_header_vars,$a_header_params = 0)
00225 {
00226 $this->header_vars = $a_header_vars;
00227
00228 if ($a_header_params == 0 or !is_array($a_header_params))
00229 {
00230 $this->link_params = "";
00231 }
00232 else
00233 {
00234 $this->header_params = $a_header_params;
00235
00236 foreach ($a_header_params as $key => $val)
00237 {
00238 $this->link_params .= $key."=".$val."&";
00239 }
00240 }
00241 }
00242
00248 function setColumnWidth($a_column_width)
00249 {
00250 $this->column_width = $a_column_width;
00251 }
00252
00259 function setOneColumnWidth($a_column_width,$a_column_number)
00260 {
00261 $this->column_width[$a_column_number] = $a_column_width;
00262 }
00263
00271 function setMaxCount($a_max_count)
00272 {
00273 $this->max_count = $a_max_count;
00274
00275 if ($this->max_limit)
00276 {
00277 $this->limit = $this->max_count;
00278 }
00279 }
00280
00287 function setLimit($a_limit = 0, $a_default_limit = 0)
00288 {
00289 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
00290
00291 if ($this->limit == 0)
00292 {
00293 $this->max_limit = true;
00294 }
00295 }
00296
00302 function setPrefix($a_prefix)
00303 {
00304 $this->prefix = ($a_prefix) ? $a_prefix : "";
00305 }
00306
00312 function setOffset($a_offset)
00313 {
00314 $this->offset = ($a_offset) ? $a_offset : 0;
00315 }
00316
00323 function setOrderColumn($a_order_column = 0,$a_default_column = 0)
00324 {
00325
00326 if (empty($a_order_column))
00327 {
00328 if (!empty($a_default_column))
00329 {
00330 $this->order_column = array_search($a_default_column,$this->header_vars);
00331 }
00332 else
00333 {
00334 $this->order_column = 0;
00335 return;
00336 }
00337 }
00338 else
00339 {
00340 $this->order_column = array_search($a_order_column,$this->header_vars);
00341 }
00342
00343 if ($this->order_column === false)
00344 {
00345
00346 $this->order_column = 0;
00347 }
00348 }
00349
00355 function setOrderDirection($a_order_direction)
00356 {
00357 if ($a_order_direction == "desc")
00358 {
00359 $this->order_direction = "desc";
00360 $this->sort_order = "asc";
00361 }
00362 else
00363 {
00364 $this->order_direction = "asc";
00365 $this->sort_order = "desc";
00366 }
00367 }
00368
00376 function setFooter($a_style,$a_previous = 0,$a_next = 0)
00377 {
00378 $this->footer_style = $a_style;
00379
00380 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
00381 $this->footer_next = ($a_next) ? $a_next : ">>>";
00382 }
00383
00389 function enable($a_module_name)
00390 {
00391 if (!in_array($a_module_name,array_keys($this->enabled)))
00392 {
00393 return false;
00394 }
00395
00396 $this->enabled[$a_module_name] = true;
00397 }
00398
00404 function disable($a_module_name)
00405 {
00406 if (!in_array($a_module_name,array_keys($this->enabled)))
00407 {
00408 return false;
00409 }
00410
00411 $this->enabled[$a_module_name] = false;
00412 }
00413
00414
00415 function sortData()
00416 {
00417 if($this->enabled["sort"])
00418 {
00419 $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
00420 }
00421 $this->data = array_slice($this->data,$this->offset,$this->limit);
00422 }
00423
00428 function render()
00429 {
00430 if($this->enabled['table'])
00431 {
00432 $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
00433 }
00434
00435
00436 if ($this->enabled["icon"] && $this->enabled["title"])
00437 {
00438 $this->tpl->setCurrentBlock("tbl_header_title_icon");
00439 $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
00440 $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
00441 $this->tpl->parseCurrentBlock();
00442 }
00443
00444 if ($this->enabled["help"] && $this->enabled["title"])
00445 {
00446 $this->tpl->setCurrentBlock("tbl_header_title_help");
00447 $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
00448 $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
00449 $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
00450 $this->tpl->parseCurrentBlock();
00451 }
00452
00453
00454 if ($this->enabled["hits"] && $this->enabled["title"])
00455 {
00456 $this->tpl->setCurrentBlock("tbl_header_hits_page");
00457 $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
00458 $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
00459 $this->tpl->parseCurrentBlock();
00460 }
00461
00462
00463 if ($this->enabled["title"])
00464 {
00465 $this->tpl->setCurrentBlock("tbl_header_title");
00466 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00467 $this->tpl->setVariable("TBL_TITLE",$this->title);
00468 $this->tpl->parseCurrentBlock();
00469 }
00470
00471
00472 if ($this->enabled["header"])
00473 {
00474 foreach ($this->header_names as $key => $tbl_header_cell)
00475 {
00476 if (!$this->enabled["sort"])
00477 {
00478 $this->tpl->setCurrentBlock("tbl_header_no_link");
00479 if ($this->column_width[$key])
00480 {
00481 $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
00482 }
00483 $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
00484 $this->tpl->parseCurrentBlock();
00485 continue;
00486 }
00487 if (($key == $this->order_column) && ($this->order_direction != ""))
00488 {
00489 if (strcmp($this->header_vars[$key], "") != 0)
00490 {
00491 $this->tpl->setCurrentBlock("tbl_order_image");
00492 $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.gif"));
00493 $this->tpl->parseCurrentBlock();
00494 }
00495 }
00496
00497 $this->tpl->setCurrentBlock("tbl_header_cell");
00498 $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
00499
00500
00501 if ($this->column_width[$key])
00502 {
00503 $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
00504 }
00505
00506 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
00507 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
00508
00509 $order_dir = "asc";
00510
00511 if ($key == $this->order_column)
00512 {
00513 $order_dir = $this->sort_order;
00514
00515 $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
00516 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
00517 }
00518
00519 $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);
00520 $this->tpl->parseCurrentBlock();
00521 }
00522
00523 $this->tpl->setCurrentBlock("tbl_header");
00524 $this->tpl->parseCurrentBlock();
00525 }
00526
00527
00528
00529
00530
00531
00532 if ($this->enabled["content"] && is_array($this->data))
00533 {
00534 if($this->enabled['auto_sort'])
00535 {
00536 $this->setMaxCount(count($this->data));
00537 $this->sortData();
00538 }
00539 $count = 0;
00540
00541 foreach ($this->data as $tbl_content_row)
00542 {
00543 foreach ($tbl_content_row as $key => $tbl_content_cell)
00544 {
00545 if (is_array($tbl_content_cell))
00546 {
00547 $this->tpl->setCurrentBlock("tbl_cell_subtitle");
00548 $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
00549 $this->tpl->parseCurrentBlock();
00550 $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
00551 }
00552
00553 $this->tpl->setCurrentBlock("tbl_content_cell");
00554 $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
00555 $this->tpl->parseCurrentBlock();
00556 }
00557
00558 $this->tpl->setCurrentBlock("tbl_content_row");
00559 $rowcolor = ilUtil::switchColor($count,"tblrow1","tblrow2");
00560 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
00561 $this->tpl->parseCurrentBlock();
00562
00563 $count++;
00564 }
00565 }
00566
00567 if ($this->enabled["numinfo"] && $this->enabled["footer"])
00568 {
00569 $start = $this->offset + 1;
00570 $end = $this->offset + $this->limit;
00571
00572 if ($end > $this->max_count or $this->limit == 0)
00573 {
00574 $end = $this->max_count;
00575 }
00576
00577 if ($this->lang_support)
00578 {
00579 $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
00580 }
00581 else
00582 {
00583 $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
00584 }
00585 if ($this->max_count > 0)
00586 {
00587
00588 $this->tpl->setCurrentBlock("tbl_footer_numinfo");
00589 $this->tpl->setVariable("NUMINFO", $numinfo);
00590 $this->tpl->parseCurrentBlock();
00591 }
00592 }
00593
00594 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0
00595 && $this->max_count > 0)
00596 {
00597 $params = array(
00598 "sort_by" => $this->header_vars[$this->order_column],
00599 "sort_order" => $this->order_direction
00600 );
00601 $params = array_merge($this->header_params,$params);
00602
00603 $layout = array(
00604 "link" => $this->footer_style,
00605 "prev" => $this->footer_previous,
00606 "next" => $this->footer_next,
00607 );
00608 $linkbar = ilUtil::Linkbar(basename($_SERVER["PHP_SELF"]),$this->max_count,$this->limit,$this->offset,$params,$layout);
00609 $this->tpl->setCurrentBlock("tbl_footer_linkbar");
00610 $this->tpl->setVariable("LINKBAR", $linkbar);
00611 $this->tpl->parseCurrentBlock();
00612 }
00613
00614
00615 if ($this->enabled["footer"] && $this->max_count > 0)
00616 {
00617 $this->tpl->setCurrentBlock("tbl_footer");
00618 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00619 $this->tpl->parseCurrentBlock();
00620 }
00621
00622 if ($this->enabled["form"])
00623 {
00624 $this->tpl->touchBlock("tbl_form_footer");
00625 }
00626
00627 if($this->enabled['table'])
00628 {
00629 $this->tpl->touchBlock("tbl_table_end");
00630 }
00631
00632 if (!$this->global_tpl)
00633 {
00634 return $this->tpl->get();
00635 }
00636 }
00637
00638
00639
00640
00641
00642
00643
00644 function setStyle($a_element,$a_style)
00645 {
00646 $this->styles[$a_element] = $a_style;
00647 }
00648
00649
00650
00651
00652
00653
00654 function getStyle($a_element)
00655 {
00656 return $this->styles[$a_element];
00657 }
00658 }
00659 ?>