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
00069 var $enabled = array(
00070 "title" => true,
00071 "icon" => true,
00072 "help" => false,
00073 "content" => true,
00074 "action" => false,
00075 "header" => true,
00076 "footer" => true,
00077 "linkbar" => true,
00078 "numinfo" => true,
00079 "sort" => true,
00080 "hits" => false
00081 );
00082
00083
00084 var $styles = array(
00085 "table" => "fullwidth"
00086 );
00087
00095 function ilTableGUI($a_data = 0,$a_global_tpl = true)
00096 {
00097 global $ilias, $tpl, $lng;
00098
00099 $this->global_tpl = $a_global_tpl;
00100 $this->ilias =& $ilias;
00101 $this->header_vars = array();
00102 $this->header_params = array();
00103 if ($this->global_tpl)
00104 {
00105 $this->tpl =& $tpl;
00106 }
00107 else
00108 {
00109 $this->tpl = new ilTemplate("tpl.table.html",true,true);
00110 }
00111
00112
00113
00114 $this->lng =& $lng;
00115
00116 if (!$this->lng)
00117 {
00118 $this->lang_support = false;
00119 }
00120
00121 $this->setData($a_data);
00122 }
00123
00124
00130 function setTemplate(&$a_tpl)
00131 {
00132 $this->tpl =& $a_tpl;
00133 }
00134
00135 function &getTemplateObject()
00136 {
00137 return $this->tpl;
00138 }
00139
00145 function setData($a_data)
00146 {
00147 if (is_array($a_data))
00148 {
00149 $this->data = $a_data;
00150 }
00151 }
00152
00153 function getData()
00154 {
00155 return $this->data;
00156 }
00157
00165 function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
00166 {
00167 $this->title = $a_title;
00168 $this->icon = $a_icon;
00169 $this->icon_alt = $a_icon_alt;
00170
00171 if (!$this->icon)
00172 {
00173 $this->enabled["icon"] = false;
00174
00175 return;
00176 }
00177
00178 if (!$this->icon_alt)
00179 {
00180 $this->icon_alt = $this->icon;
00181 }
00182 }
00183
00191 function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
00192 {
00193 $this->help_page = $a_help_page;
00194 $this->help_icon = $a_help_icon;
00195 $this->help_icon_alt = $a_help_icon_alt;
00196
00197 if (!$this->help_icon_alt)
00198 {
00199 $this->help_icon_alt = $this->help_icon;
00200 }
00201 }
00202
00208 function setHeaderNames($a_header_names)
00209 {
00210 $this->header_names = $a_header_names;
00211 $this->column_count = count($this->header_names);
00212 }
00213
00220 function setHeaderVars($a_header_vars,$a_header_params = 0)
00221 {
00222 $this->header_vars = $a_header_vars;
00223
00224 if ($a_header_params == 0 or !is_array($a_header_params))
00225 {
00226 $this->link_params = "";
00227 }
00228 else
00229 {
00230 $this->header_params = $a_header_params;
00231
00232 foreach ($a_header_params as $key => $val)
00233 {
00234 $this->link_params .= $key."=".$val."&";
00235 }
00236 }
00237 }
00238
00244 function setColumnWidth($a_column_width)
00245 {
00246 $this->column_width = $a_column_width;
00247 }
00248
00255 function setOneColumnWidth($a_column_width,$a_column_number)
00256 {
00257 $this->column_width[$a_column_number] = $a_column_width;
00258 }
00259
00267 function setMaxCount($a_max_count)
00268 {
00269 $this->max_count = $a_max_count;
00270
00271 if ($this->max_limit)
00272 {
00273 $this->limit = $this->max_count;
00274 }
00275 }
00276
00283 function setLimit($a_limit = 0, $a_default_limit = 0)
00284 {
00285 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
00286
00287 if ($this->limit == 0)
00288 {
00289 $this->max_limit = true;
00290 }
00291 }
00292
00298 function setOffset($a_offset)
00299 {
00300 $this->offset = ($a_offset) ? $a_offset : 0;
00301 }
00302
00309 function setOrderColumn($a_order_column = 0,$a_default_column = 0)
00310 {
00311
00312 if (empty($a_order_column))
00313 {
00314 if (!empty($a_default_column))
00315 {
00316 $this->order_column = array_search($a_default_column,$this->header_vars);
00317 }
00318 else
00319 {
00320 $this->order_column = 0;
00321 return;
00322 }
00323 }
00324 else
00325 {
00326 $this->order_column = array_search($a_order_column,$this->header_vars);
00327 }
00328
00329 if ($this->order_column === false)
00330 {
00331
00332 $this->order_column = 0;
00333 }
00334 }
00335
00341 function setOrderDirection($a_order_direction)
00342 {
00343 if ($a_order_direction == "desc")
00344 {
00345 $this->order_direction = "desc";
00346 $this->sort_order = "asc";
00347 }
00348 else
00349 {
00350 $this->order_direction = "asc";
00351 $this->sort_order = "desc";
00352 }
00353 }
00354
00362 function setFooter($a_style,$a_previous = 0,$a_next = 0)
00363 {
00364 $this->footer_style = $a_style;
00365
00366 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
00367 $this->footer_next = ($a_next) ? $a_next : ">>>";
00368 }
00369
00375 function enable($a_module_name)
00376 {
00377 if (!in_array($a_module_name,array_keys($this->enabled)))
00378 {
00379 return false;
00380 }
00381
00382 $this->enabled[$a_module_name] = true;
00383 }
00384
00390 function disable($a_module_name)
00391 {
00392 if (!in_array($a_module_name,array_keys($this->enabled)))
00393 {
00394 return false;
00395 }
00396
00397 $this->enabled[$a_module_name] = false;
00398 }
00399
00400
00401 function sortData()
00402 {
00403 if($this->enabled["sort"])
00404 {
00405 $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
00406 }
00407 $this->data = array_slice($this->data,$this->offset,$this->limit);
00408 }
00409
00414 function render()
00415 {
00416 $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
00417
00418
00419 if ($this->enabled["icon"] && $this->enabled["title"])
00420 {
00421 $this->tpl->setCurrentBlock("tbl_header_title_icon");
00422 $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
00423 $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
00424 $this->tpl->parseCurrentBlock();
00425 }
00426
00427 if ($this->enabled["help"] && $this->enabled["title"])
00428 {
00429 $this->tpl->setCurrentBlock("tbl_header_title_help");
00430 $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
00431 $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
00432 $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
00433 $this->tpl->parseCurrentBlock();
00434 }
00435
00436
00437 if ($this->enabled["hits"] && $this->enabled["title"])
00438 {
00439 $this->tpl->setCurrentBlock("tbl_header_hits_page");
00440 $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
00441 $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
00442 $this->tpl->parseCurrentBlock();
00443 }
00444
00445
00446 if ($this->enabled["title"])
00447 {
00448 $this->tpl->setCurrentBlock("tbl_header_title");
00449 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00450 $this->tpl->setVariable("TBL_TITLE",$this->title);
00451 $this->tpl->parseCurrentBlock();
00452 }
00453
00454
00455 if ($this->enabled["header"])
00456 {
00457 foreach ($this->header_names as $key => $tbl_header_cell)
00458 {
00459 if (!$this->enabled["sort"])
00460 {
00461 $this->tpl->setCurrentBlock("tbl_header_no_link");
00462 $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
00463 $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
00464 $this->tpl->parseCurrentBlock();
00465 continue;
00466 }
00467 if (($key == $this->order_column) && ($this->order_direction != ""))
00468 {
00469 if (strcmp($this->header_vars[$key], "") != 0)
00470 {
00471 $this->tpl->setCurrentBlock("tbl_order_image");
00472 $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.png"));
00473 $this->tpl->parseCurrentBlock();
00474 }
00475 }
00476
00477 $this->tpl->setCurrentBlock("tbl_header_cell");
00478 $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
00479
00480
00481 if ($this->column_width[$key])
00482 {
00483 $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
00484 }
00485
00486 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
00487 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
00488
00489 $order_dir = "asc";
00490
00491 if ($key == $this->order_column)
00492 {
00493 $order_dir = $this->sort_order;
00494
00495 $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
00496 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
00497 }
00498
00499 $this->tpl->setVariable("TBL_ORDER_LINK",basename($_SERVER["PHP_SELF"])."?".$this->link_params."sort_by=".$this->header_vars[$key]."&sort_order=".$order_dir."&offset=".$this->offset);
00500 $this->tpl->parseCurrentBlock();
00501 }
00502 }
00503
00504
00505
00506
00507
00508
00509 if ($this->enabled["content"] && is_array($this->data))
00510 {
00511 $this->setMaxCount(count($this->data));
00512
00513 $this->sortData();
00514 $count = 0;
00515
00516 foreach ($this->data as $tbl_content_row)
00517 {
00518 foreach ($tbl_content_row as $key => $tbl_content_cell)
00519 {
00520 if (is_array($tbl_content_cell))
00521 {
00522 $this->tpl->setCurrentBlock("tbl_cell_subtitle");
00523 $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
00524 $this->tpl->parseCurrentBlock();
00525 $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
00526 }
00527
00528 $this->tpl->setCurrentBlock("tbl_content_cell");
00529 $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
00530 $this->tpl->parseCurrentBlock();
00531 }
00532
00533 $this->tpl->setCurrentBlock("tbl_content_row");
00534 $rowcolor = ilUtil::switchColor($count,"tblrow2","tblrow1");
00535 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
00536 $this->tpl->parseCurrentBlock();
00537
00538 $count++;
00539 }
00540 }
00541
00542 if ($this->enabled["numinfo"] && $this->enabled["footer"])
00543 {
00544 $start = $this->offset + 1;
00545 $end = $this->offset + $this->limit;
00546
00547 if ($end > $this->max_count or $this->limit == 0)
00548 {
00549 $end = $this->max_count;
00550 }
00551
00552 if ($this->lang_support)
00553 {
00554 $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
00555 }
00556 else
00557 {
00558 $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
00559 }
00560 if ($this->max_count == 0)
00561 {
00562 $numinfo = $this->lng->txt("no_datasets");
00563 }
00564
00565 $this->tpl->setCurrentBlock("tbl_footer_numinfo");
00566 $this->tpl->setVariable("NUMINFO", $numinfo);
00567 $this->tpl->parseCurrentBlock();
00568 }
00569
00570 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0)
00571 {
00572 $params = array(
00573 "sort_by" => $this->header_vars[$this->order_column],
00574 "sort_order" => $this->order_direction
00575 );
00576 $params = array_merge($this->header_params,$params);
00577
00578 $layout = array(
00579 "link" => $this->footer_style,
00580 "prev" => $this->footer_previous,
00581 "next" => $this->footer_next,
00582 );
00583
00584 $linkbar = ilUtil::Linkbar(basename($_SERVER["PHP_SELF"]),$this->max_count,$this->limit,$this->offset,$params,$layout);
00585
00586 $this->tpl->setCurrentBlock("tbl_footer_linkbar");
00587 $this->tpl->setVariable("LINKBAR", $linkbar);
00588 $this->tpl->parseCurrentBlock();
00589 }
00590
00591
00592 if ($this->enabled["footer"])
00593 {
00594 $this->tpl->setCurrentBlock("tbl_footer");
00595 $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00596 $this->tpl->parseCurrentBlock();
00597 }
00598
00599 $this->tpl->touchBlock("tbl_form_footer");
00600
00601 if (!$this->global_tpl)
00602 {
00603 return $this->tpl->get();
00604 }
00605 }
00606
00607
00608
00609
00610
00611
00612
00613 function setStyle($a_element,$a_style)
00614 {
00615 $this->styles[$a_element] = $a_style;
00616 }
00617
00618
00619
00620
00621
00622
00623 function getStyle($a_element)
00624 {
00625 return $this->styles[$a_element];
00626 }
00627 }
00628 ?>