• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilTableGUI.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 class ilTableGUI
00036 {
00037         var $title;                                     // table title name
00038         var $icon;                                      // table title icon
00039         var $icon_alt;                          // table title icon alt text
00040 
00041         var $help_page;                         // table help name
00042         var $help_icon;                         // table help icon
00043         var $help_icon_alt;                     // table help icon alt text
00044 
00045         var $header_names;                      // titles of header columns
00046         var $header_vars;                       // var names of header columns (i.e. for database order column)
00047         var $linkbar_vars;                      // additional variables for linkbar
00048 
00049         var $data;                                      // table content
00050         
00051         var $column_count;                      // no. of columns (based on element count of $this->header array)
00052         var $column_width;                      // column width of each column (used in order until max. column from column count is reached)
00053                                                                 // any exceeding values are ignored
00054         var $max_count;                         // max. count of database query
00055         var $limit;                                     // max. count of dataset per page
00056         var $offset;                            // dataset offset
00057         var $order_column;                      // order column
00058         var $order_direction;           // order direction
00059         
00060         var $footer_style;                      // css format for links
00061         var     $footer_previous;               // value of previous link
00062         var     $footer_next;                   // value of next link
00063         
00064         var $lang_support = true;       // if a lang object is included
00065         var $global_tpl;                        // uses global tpl (true) or a local one (false)
00066         
00067         // default settings for enabled/disabled table modules 
00068         var $enabled = array(
00069                                                         "title"                 =>      true,
00070                                                         "icon"                  =>      true,
00071                                                         "help"                  =>      false,
00072                                                         "content"               =>      true,
00073                                                         "action"                =>      false,
00074                                                         "header"        =>  true,
00075                                                         "footer"                =>      true,
00076                                                         "linkbar"               =>      true,
00077                                                         "numinfo"               =>      true,
00078                                                         "sort"                  =>  true,
00079                                                         "hits"          =>  false
00080                                                 );
00081                                                 
00082         // tpl styles (only one so far)
00083         var $styles = array(
00084                                                         "table"         => "fullwidth"
00085                                                 );
00086         
00094         function ilTableGUI($a_data = 0,$a_global_tpl = true)
00095         {
00096                 global $ilias, $tpl, $lng;
00097 
00098                 $this->global_tpl = $a_global_tpl;
00099                 $this->ilias =& $ilias;
00100                 $this->header_vars = array();
00101                 $this->header_params = array();
00102                 if ($this->global_tpl)
00103                 {
00104                         $this->tpl =& $tpl;
00105                 }
00106                 else
00107                 {
00108                         $this->tpl = new ilTemplate("tpl.table.html",true,true);
00109                 }
00110 
00111                 //$this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00112 
00113                 $this->lng =& $lng;
00114 
00115                 if (!$this->lng)
00116                 {
00117                         $this->lang_support = false;
00118                 }
00119 
00120                 $this->setData($a_data);
00121         }
00122 
00123 
00129         function setTemplate(&$a_tpl)
00130         {
00131                 $this->tpl =& $a_tpl;
00132         }
00133 
00134         function &getTemplateObject()
00135         {
00136                 return $this->tpl;
00137         }
00138 
00144         function setData($a_data)
00145         {
00146                 if (is_array($a_data))
00147                 {
00148                         $this->data = $a_data;
00149                 }
00150         }
00151 
00152         function getData()
00153         {
00154                 return $this->data;
00155         }
00156 
00164         function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
00165         {
00166                 $this->title = $a_title;
00167                 $this->icon = $a_icon;
00168                 $this->icon_alt = $a_icon_alt;
00169 
00170                 if (!$this->icon)
00171                 {
00172                         $this->enabled["icon"] = false;
00173 
00174                         return;
00175                 }
00176 
00177                 if (!$this->icon_alt)
00178                 {
00179                         $this->icon_alt = $this->icon;
00180                 }
00181         }
00182         
00190         function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
00191         {
00192                 $this->help_page = $a_help_page;
00193                 $this->help_icon = $a_help_icon;
00194                 $this->help_icon_alt = $a_help_icon_alt;
00195 
00196                 if (!$this->help_icon_alt)
00197                 {
00198                         $this->help_icon_alt = $this->help_icon;
00199                 }
00200         }
00201         
00207         function setHeaderNames($a_header_names)
00208         {
00209                 $this->header_names = $a_header_names;
00210                 $this->column_count = count($this->header_names);
00211         }
00212 
00219         function setHeaderVars($a_header_vars,$a_header_params = 0)
00220         {
00221                 $this->header_vars = $a_header_vars;
00222                 
00223                 if ($a_header_params == 0 or !is_array($a_header_params))
00224                 {
00225                         $this->link_params = "";
00226                 }
00227                 else
00228                 {
00229                         $this->header_params = $a_header_params;        // temp. solution for linkbar
00230 
00231                         foreach ($a_header_params as $key => $val)
00232                         {
00233                                 $this->link_params .= $key."=".$val."&";
00234                         }
00235                 }
00236         }
00237         
00243         function setColumnWidth($a_column_width)
00244         {
00245                 $this->column_width = $a_column_width;
00246         }
00247         
00254         function setOneColumnWidth($a_column_width,$a_column_number)
00255         {
00256                 $this->column_width[$a_column_number] = $a_column_width;
00257         }
00258 
00266         function setMaxCount($a_max_count)
00267         {
00268                 $this->max_count = $a_max_count;
00269 
00270                 if ($this->max_limit)
00271                 { 
00272                         $this->limit = $this->max_count;
00273                 }
00274         }
00275         
00282         function setLimit($a_limit = 0, $a_default_limit = 0)
00283         {
00284                 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
00285                 
00286                 if ($this->limit == 0)
00287                 {
00288                         $this->max_limit = true;
00289                 }
00290         }
00291 
00297         function setOffset($a_offset)
00298         {
00299                 $this->offset = ($a_offset) ? $a_offset : 0;
00300         }
00301         
00308         function setOrderColumn($a_order_column = 0,$a_default_column = 0)
00309         {
00310                 // set default sort column to first column
00311                 if (empty($a_order_column))
00312                 {
00313                         if (!empty($a_default_column))
00314                         {
00315                                 $this->order_column = array_search($a_default_column,$this->header_vars);       
00316                         }
00317                         else
00318                         {
00319                                 $this->order_column = 0;
00320                                 return;
00321                         }
00322                 }
00323                 else
00324                 {
00325                         $this->order_column = array_search($a_order_column,$this->header_vars);
00326                 }
00327 
00328                 if ($this->order_column === false)
00329                 {
00330                         // if not found, set default sort column to first column
00331                         $this->order_column = 0;
00332                 }
00333         }
00334 
00340         function setOrderDirection($a_order_direction)
00341         {
00342                 if ($a_order_direction == "desc")
00343                 {
00344                         $this->order_direction = "desc";
00345                         $this->sort_order = "asc";
00346                 }
00347                 else
00348                 {
00349                         $this->order_direction = "asc"; // set default sort order to "ASC"
00350                         $this->sort_order = "desc";
00351                 }
00352         }
00353 
00361         function setFooter($a_style,$a_previous = 0,$a_next = 0)
00362         {
00363                 $this->footer_style = $a_style;
00364 
00365                 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
00366                 $this->footer_next = ($a_next) ? $a_next : ">>>";
00367         }
00368 
00374         function enable($a_module_name)
00375         {
00376                 if (!in_array($a_module_name,array_keys($this->enabled)))
00377                 {
00378                         return false;
00379                 }
00380 
00381                 $this->enabled[$a_module_name] = true;
00382         } 
00383 
00389         function disable($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] = false;
00397         }
00398         
00399 
00400         function sortData()
00401         {
00402                 if($this->enabled["sort"])
00403                 {
00404                         $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
00405                 }
00406                 $this->data = array_slice($this->data,$this->offset,$this->limit);
00407         }
00408 
00413         function render()
00414         {
00415                 $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
00416 
00417                 // table title icon
00418                 if ($this->enabled["icon"] && $this->enabled["title"])
00419                 {
00420                         $this->tpl->setCurrentBlock("tbl_header_title_icon");
00421                         $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
00422                         $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
00423                         $this->tpl->parseCurrentBlock();
00424                 }
00425                 // table title help
00426                 if ($this->enabled["help"] && $this->enabled["title"])
00427                 {
00428                         $this->tpl->setCurrentBlock("tbl_header_title_help");
00429                         $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
00430                         $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
00431                         $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
00432                         $this->tpl->parseCurrentBlock();
00433                 }
00434 
00435                 // hits per page selector
00436                 if ($this->enabled["hits"] && $this->enabled["title"])
00437                 {
00438                         $this->tpl->setCurrentBlock("tbl_header_hits_page");
00439                         $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
00440                         $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
00441                         $this->tpl->parseCurrentBlock();
00442                 }
00443                 
00444                 // table title
00445                 if ($this->enabled["title"])
00446                 {
00447                         $this->tpl->setCurrentBlock("tbl_header_title");
00448                         $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00449                         $this->tpl->setVariable("TBL_TITLE",$this->title);
00450                         $this->tpl->parseCurrentBlock();
00451                 }
00452                 
00453                 // table header
00454                 if ($this->enabled["header"])
00455                 {
00456                         foreach ($this->header_names as $key => $tbl_header_cell)
00457                         {
00458                                 if (!$this->enabled["sort"])
00459                                 {
00460                                         $this->tpl->setCurrentBlock("tbl_header_no_link");
00461                                         $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
00462                                         $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
00463                                         $this->tpl->parseCurrentBlock();
00464                                         continue;
00465                                 }
00466                                 if (($key == $this->order_column) && ($this->order_direction != ""))
00467                                 {
00468                                         if (strcmp($this->header_vars[$key], "") != 0)
00469                                         {
00470                                                 $this->tpl->setCurrentBlock("tbl_order_image");
00471                                                 $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.png"));
00472                                                 $this->tpl->parseCurrentBlock();
00473                                         }
00474                                 }
00475         
00476                                 $this->tpl->setCurrentBlock("tbl_header_cell");
00477                                 $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
00478                                 
00479                                 // only set width if a value is given for that column
00480                                 if ($this->column_width[$key])
00481                                 {
00482                                         $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
00483                                 }
00484         
00485                                 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
00486                                 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
00487                         
00488                                 $order_dir = "asc";
00489                         
00490                                 if ($key == $this->order_column)
00491                                 { 
00492                                         $order_dir = $this->sort_order;
00493         
00494                                         $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
00495                                         $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
00496                                 }
00497                         
00498                                 $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);
00499                                 $this->tpl->parseCurrentBlock();
00500                         }
00501                 }
00502 
00503                 // table data
00504                 // the table content may be skipped to use an individual template blockfile
00505                 // To do so don't set $this->data and parse your table content by yourself
00506                 // The template block name for the blockfile MUST be 'TBL_CONTENT'
00507 
00508                 if ($this->enabled["content"] && is_array($this->data))
00509                 {
00510                         $this->setMaxCount(count($this->data));
00511                         
00512                         $this->sortData();
00513                         $count = 0;
00514                         
00515                         foreach ($this->data as $tbl_content_row)
00516                         {
00517                                 foreach ($tbl_content_row as $key => $tbl_content_cell)
00518                                 {
00519                     if (is_array($tbl_content_cell))
00520                     {
00521                         $this->tpl->setCurrentBlock("tbl_cell_subtitle");
00522                                             $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
00523                                             $this->tpl->parseCurrentBlock();
00524                                             $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
00525                     }
00526                     
00527                     $this->tpl->setCurrentBlock("tbl_content_cell");
00528                                         $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
00529                                         $this->tpl->parseCurrentBlock();
00530                                 }
00531 
00532                                 $this->tpl->setCurrentBlock("tbl_content_row");
00533                                 $rowcolor = ilUtil::switchColor($count,"tblrow2","tblrow1");
00534                                 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
00535                                 $this->tpl->parseCurrentBlock();
00536                         
00537                                 $count++;
00538                         }
00539                 }
00540                 // table footer numinfo
00541                 if ($this->enabled["numinfo"] && $this->enabled["footer"])
00542                 {
00543 
00544                         $start = $this->offset + 1;                             // compute num info
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                 // table footer linkbar
00570 
00571                 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0)
00572                 {
00573                         $params = array(
00574                                                         "sort_by"               => $this->header_vars[$this->order_column],
00575                                                         "sort_order"    => $this->order_direction
00576                                                         );
00577                         $params = array_merge($this->header_params,$params);
00578                         
00579                         $layout = array(
00580                                                         "link"  => $this->footer_style,
00581                                                         "prev"  => $this->footer_previous,
00582                                                         "next"  => $this->footer_next,
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                 // table footer
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         * set a tpl stylesheet
00609         * @access       public
00610         * @param        string  table element
00611         * @param        string  CSS definition
00612         */
00613         function setStyle($a_element,$a_style)
00614         {
00615                 $this->styles[$a_element] = $a_style;
00616         }
00617 
00618         /*
00619         * get a tpl stylesheet
00620         * @access       public
00621         * @param        string  table element
00622         */
00623         function getStyle($a_element)
00624         {
00625                 return $this->styles[$a_element];
00626         }
00627 }
00628 ?>

Generated on Fri Dec 13 2013 09:06:35 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1