• 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-2005 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 $max_limit = false;
00057         var $offset;                            // dataset offset
00058         var $order_column;                      // order column
00059         var $order_direction;           // order direction
00060         
00061         var $footer_style;                      // css format for links
00062         var     $footer_previous;               // value of previous link
00063         var     $footer_next;                   // value of next link
00064         
00065         var $lang_support = true;       // if a lang object is included
00066         var $global_tpl;                        // uses global tpl (true) or a local one (false)
00067         
00068         // default settings for enabled/disabled table modules 
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                                                         "auto_sort"     =>  true
00082                                                 );
00083 
00084         // tpl styles (only one so far)
00085         var $styles = array(
00086                                                         "table"         => "fullwidth"
00087                                                 );
00088         
00096         function ilTableGUI($a_data = 0,$a_global_tpl = true)
00097         {
00098                 global $ilias, $tpl, $lng;
00099 
00100                 $this->global_tpl = $a_global_tpl;
00101                 $this->ilias =& $ilias;
00102                 $this->header_vars = array();
00103                 $this->header_params = array();
00104                 $this->enabled["form"] = true;
00105                 if ($this->global_tpl)
00106                 {
00107                         $this->tpl =& $tpl;
00108                 }
00109                 else
00110                 {
00111                         $this->tpl = new ilTemplate("tpl.table.html",true,true);
00112                 }
00113 
00114                 //$this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00115 
00116                 $this->lng =& $lng;
00117 
00118                 if (!$this->lng)
00119                 {
00120                         $this->lang_support = false;
00121                 }
00122 
00123                 $this->setData($a_data);
00124         }
00125 
00126 
00132         function setTemplate(&$a_tpl)
00133         {
00134                 $this->tpl =& $a_tpl;
00135         }
00136 
00137         function &getTemplateObject()
00138         {
00139                 return $this->tpl;
00140         }
00141 
00147         function setData($a_data)
00148         {
00149                 if (is_array($a_data))
00150                 {
00151                         $this->data = $a_data;
00152                 }
00153         }
00154 
00155         function getData()
00156         {
00157                 return $this->data;
00158         }
00159 
00167         function setTitle($a_title,$a_icon = 0,$a_icon_alt = 0)
00168         {
00169                 $this->title = $a_title;
00170                 $this->icon = $a_icon;
00171                 $this->icon_alt = $a_icon_alt;
00172 
00173                 if (!$this->icon)
00174                 {
00175                         $this->enabled["icon"] = false;
00176 
00177                         return;
00178                 }
00179 
00180                 if (!$this->icon_alt)
00181                 {
00182                         $this->icon_alt = $this->icon;
00183                 }
00184         }
00185         
00193         function setHelp($a_help_page,$a_help_icon,$a_help_icon_alt = 0)
00194         {
00195                 $this->help_page = $a_help_page;
00196                 $this->help_icon = $a_help_icon;
00197                 $this->help_icon_alt = $a_help_icon_alt;
00198 
00199                 if (!$this->help_icon_alt)
00200                 {
00201                         $this->help_icon_alt = $this->help_icon;
00202                 }
00203         }
00204         
00210         function setHeaderNames($a_header_names)
00211         {
00212                 $this->header_names = $a_header_names;
00213                 $this->column_count = count($this->header_names);
00214         }
00215 
00222         function setHeaderVars($a_header_vars,$a_header_params = 0)
00223         {
00224                 $this->header_vars = $a_header_vars;
00225                 
00226                 if ($a_header_params == 0 or !is_array($a_header_params))
00227                 {
00228                         $this->link_params = "";
00229                 }
00230                 else
00231                 {
00232                         $this->header_params = $a_header_params;        // temp. solution for linkbar
00233 
00234                         foreach ($a_header_params as $key => $val)
00235                         {
00236                                 $this->link_params .= $key."=".$val."&";
00237                         }
00238                 }
00239         }
00240         
00246         function setColumnWidth($a_column_width)
00247         {
00248                 $this->column_width = $a_column_width;
00249         }
00250         
00257         function setOneColumnWidth($a_column_width,$a_column_number)
00258         {
00259                 $this->column_width[$a_column_number] = $a_column_width;
00260         }
00261 
00269         function setMaxCount($a_max_count)
00270         {
00271                 $this->max_count = $a_max_count;
00272 
00273                 if ($this->max_limit)
00274                 {
00275                         $this->limit = $this->max_count;
00276                 }
00277         }
00278         
00285         function setLimit($a_limit = 0, $a_default_limit = 0)
00286         {
00287                 $this->limit = ($a_limit) ? $a_limit : $a_default_limit;
00288 
00289                 if ($this->limit == 0)
00290                 {
00291                         $this->max_limit = true;
00292                 }
00293         }
00294 
00300         function setOffset($a_offset)
00301         {
00302                 $this->offset = ($a_offset) ? $a_offset : 0;
00303         }
00304         
00311         function setOrderColumn($a_order_column = 0,$a_default_column = 0)
00312         {
00313                 // set default sort column to first column
00314                 if (empty($a_order_column))
00315                 {
00316                         if (!empty($a_default_column))
00317                         {
00318                                 $this->order_column = array_search($a_default_column,$this->header_vars);       
00319                         }
00320                         else
00321                         {
00322                                 $this->order_column = 0;
00323                                 return;
00324                         }
00325                 }
00326                 else
00327                 {
00328                         $this->order_column = array_search($a_order_column,$this->header_vars);
00329                 }
00330 
00331                 if ($this->order_column === false)
00332                 {
00333                         // if not found, set default sort column to first column
00334                         $this->order_column = 0;
00335                 }
00336         }
00337 
00343         function setOrderDirection($a_order_direction)
00344         {
00345                 if ($a_order_direction == "desc")
00346                 {
00347                         $this->order_direction = "desc";
00348                         $this->sort_order = "asc";
00349                 }
00350                 else
00351                 {
00352                         $this->order_direction = "asc"; // set default sort order to "ASC"
00353                         $this->sort_order = "desc";
00354                 }
00355         }
00356 
00364         function setFooter($a_style,$a_previous = 0,$a_next = 0)
00365         {
00366                 $this->footer_style = $a_style;
00367 
00368                 $this->footer_previous = ($a_previous) ? $a_previous : "<<<";
00369                 $this->footer_next = ($a_next) ? $a_next : ">>>";
00370         }
00371 
00377         function enable($a_module_name)
00378         {
00379                 if (!in_array($a_module_name,array_keys($this->enabled)))
00380                 {
00381                         return false;
00382                 }
00383 
00384                 $this->enabled[$a_module_name] = true;
00385         } 
00386 
00392         function disable($a_module_name)
00393         {
00394                 if (!in_array($a_module_name,array_keys($this->enabled)))
00395                 {
00396                         return false;
00397                 }
00398 
00399                 $this->enabled[$a_module_name] = false;
00400         }
00401         
00402 
00403         function sortData()
00404         {
00405                 if($this->enabled["sort"])
00406                 {
00407                         $this->data = ilUtil::sortArray($this->data,$this->order_column,$this->order_direction);
00408                 }
00409                 $this->data = array_slice($this->data,$this->offset,$this->limit);
00410         }
00411 
00416         function render()
00417         {
00418                 $this->tpl->setVariable("CSS_TABLE",$this->getStyle("table"));
00419 
00420                 // table title icon
00421                 if ($this->enabled["icon"] && $this->enabled["title"])
00422                 {
00423                         $this->tpl->setCurrentBlock("tbl_header_title_icon");
00424                         $this->tpl->setVariable("TBL_TITLE_IMG",ilUtil::getImagePath($this->icon));
00425                         $this->tpl->setVariable("TBL_TITLE_IMG_ALT",$this->icon_alt);
00426                         $this->tpl->parseCurrentBlock();
00427                 }
00428                 // table title help
00429                 if ($this->enabled["help"] && $this->enabled["title"])
00430                 {
00431                         $this->tpl->setCurrentBlock("tbl_header_title_help");
00432                         $this->tpl->setVariable("TBL_HELP_IMG",ilUtil::getImagePath($this->help_icon));
00433                         $this->tpl->setVariable("TBL_HELP_LINK",$this->help_page);
00434                         $this->tpl->setVariable("TBL_HELP_IMG_ALT",$this->help_icon_alt);
00435                         $this->tpl->parseCurrentBlock();
00436                 }
00437 
00438                 // hits per page selector
00439                 if ($this->enabled["hits"] && $this->enabled["title"])
00440                 {
00441                         $this->tpl->setCurrentBlock("tbl_header_hits_page");
00442                         $this->tpl->setVariable("LIMIT",$_SESSION["tbl_limit"]);
00443                         $this->tpl->setVariable("HITS_PER_PAGE",$this->lng->txt("hits_per_page"));
00444                         $this->tpl->parseCurrentBlock();
00445                 }
00446                 
00447                 // table title
00448                 if ($this->enabled["title"])
00449                 {
00450                         $this->tpl->setCurrentBlock("tbl_header_title");
00451                         $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00452                         $this->tpl->setVariable("TBL_TITLE",$this->title);
00453                         $this->tpl->parseCurrentBlock();
00454                 }
00455                 
00456                 // table header
00457                 if ($this->enabled["header"])
00458                 {
00459                         foreach ($this->header_names as $key => $tbl_header_cell)
00460                         {
00461                                 if (!$this->enabled["sort"])
00462                                 {
00463                                         $this->tpl->setCurrentBlock("tbl_header_no_link");
00464                                         $this->tpl->setVariable("TBL_COLUMN_WIDTH_NO_LINK"," width=\"".$this->column_width[$key]."\"");
00465                                         $this->tpl->setVariable("TBL_HEADER_CELL_NO_LINK",$tbl_header_cell);
00466                                         $this->tpl->parseCurrentBlock();
00467                                         continue;
00468                                 }
00469                                 if (($key == $this->order_column) && ($this->order_direction != ""))
00470                                 {
00471                                         if (strcmp($this->header_vars[$key], "") != 0)
00472                                         {
00473                                                 $this->tpl->setCurrentBlock("tbl_order_image");
00474                                                 $this->tpl->setVariable("IMG_ORDER_DIR",ilUtil::getImagePath($this->order_direction."_order.png"));
00475                                                 $this->tpl->parseCurrentBlock();
00476                                         }
00477                                 }
00478         
00479                                 $this->tpl->setCurrentBlock("tbl_header_cell");
00480                                 $this->tpl->setVariable("TBL_HEADER_CELL",$tbl_header_cell);
00481                                 
00482                                 // only set width if a value is given for that column
00483                                 if ($this->column_width[$key])
00484                                 {
00485                                         $this->tpl->setVariable("TBL_COLUMN_WIDTH"," width=\"".$this->column_width[$key]."\"");
00486                                 }
00487         
00488                                 $lng_sort_column = ($this->lang_support) ? $this->lng->txt("sort_by_this_column") : "Sort by this column";
00489                                 $this->tpl->setVariable("TBL_ORDER_ALT",$lng_sort_column);
00490                         
00491                                 $order_dir = "asc";
00492                         
00493                                 if ($key == $this->order_column)
00494                                 { 
00495                                         $order_dir = $this->sort_order;
00496         
00497                                         $lng_change_sort = ($this->lang_support) ? $this->lng->txt("change_sort_direction") : "Change sort direction";
00498                                         $this->tpl->setVariable("TBL_ORDER_ALT",$lng_change_sort);
00499                                 }
00500                         
00501                                 $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);
00502                                 $this->tpl->parseCurrentBlock();
00503                         }
00504                         
00505                         $this->tpl->setCurrentBlock("tbl_header");
00506                         $this->tpl->parseCurrentBlock();
00507                 }
00508 
00509                 // table data
00510                 // the table content may be skipped to use an individual template blockfile
00511                 // To do so don't set $this->data and parse your table content by yourself
00512                 // The template block name for the blockfile MUST be 'TBL_CONTENT'
00513 
00514                 if ($this->enabled["content"] && is_array($this->data))
00515                 {
00516                         if($this->enabled['auto_sort'])
00517                         {
00518                                 $this->setMaxCount(count($this->data));
00519                                 $this->sortData();
00520                         }
00521                         $count = 0;
00522                         
00523                         foreach ($this->data as $tbl_content_row)
00524                         {
00525                                 foreach ($tbl_content_row as $key => $tbl_content_cell)
00526                                 {
00527                     if (is_array($tbl_content_cell))
00528                     {
00529                         $this->tpl->setCurrentBlock("tbl_cell_subtitle");
00530                                             $this->tpl->setVariable("TBL_CELL_SUBTITLE",$tbl_content_cell[1]);
00531                                             $this->tpl->parseCurrentBlock();
00532                                             $tbl_content_cell = "<b>".$tbl_content_cell[0]."</b>";
00533                     }
00534                     
00535                     $this->tpl->setCurrentBlock("tbl_content_cell");
00536                                         $this->tpl->setVariable("TBL_CONTENT_CELL",$tbl_content_cell);
00537                                         $this->tpl->parseCurrentBlock();
00538                                 }
00539 
00540                                 $this->tpl->setCurrentBlock("tbl_content_row");
00541                                 $rowcolor = ilUtil::switchColor($count,"tblrow2","tblrow1");
00542                                 $this->tpl->setVariable("ROWCOLOR", $rowcolor);
00543                                 $this->tpl->parseCurrentBlock();
00544                         
00545                                 $count++;
00546                         }
00547                 }
00548                 // table footer numinfo
00549                 if ($this->enabled["numinfo"] && $this->enabled["footer"])
00550                 {
00551                         $start = $this->offset + 1;                             // compute num info
00552                         $end = $this->offset + $this->limit;
00553                         
00554                         if ($end > $this->max_count or $this->limit == 0)
00555                         {
00556                                 $end = $this->max_count;
00557                         }
00558                         
00559                         if ($this->lang_support)
00560                         {
00561                                 $numinfo = "(".$this->lng->txt("dataset")." ".$start." - ".$end." ".strtolower($this->lng->txt("of"))." ".$this->max_count.")";
00562                         }
00563                         else
00564                         {
00565                                 $numinfo = "(Dataset ".$start." - ".$end." of ".$this->max_count.")";
00566                         }
00567                         if ($this->max_count == 0)
00568                         {
00569                                 $numinfo = $this->lng->txt("no_datasets");
00570                         }
00571         
00572                         $this->tpl->setCurrentBlock("tbl_footer_numinfo");
00573                         $this->tpl->setVariable("NUMINFO", $numinfo);
00574                         $this->tpl->parseCurrentBlock();
00575                 }
00576                 // table footer linkbar
00577                 if ($this->enabled["linkbar"] && $this->enabled["footer"] && $this->limit != 0)
00578                 {
00579                         $params = array(
00580                                                         "sort_by"               => $this->header_vars[$this->order_column],
00581                                                         "sort_order"    => $this->order_direction
00582                                                         );
00583                         $params = array_merge($this->header_params,$params);
00584                         
00585                         $layout = array(
00586                                                         "link"  => $this->footer_style,
00587                                                         "prev"  => $this->footer_previous,
00588                                                         "next"  => $this->footer_next,
00589                                                         );
00590                         $linkbar = ilUtil::Linkbar(basename($_SERVER["PHP_SELF"]),$this->max_count,$this->limit,$this->offset,$params,$layout);
00591                         $this->tpl->setCurrentBlock("tbl_footer_linkbar");
00592                         $this->tpl->setVariable("LINKBAR", $linkbar);
00593                         $this->tpl->parseCurrentBlock();
00594                 }
00595                                                 
00596                 // table footer
00597                 if ($this->enabled["footer"])
00598                 {
00599                         $this->tpl->setCurrentBlock("tbl_footer");
00600                         $this->tpl->setVariable("COLUMN_COUNT",$this->column_count);
00601                         $this->tpl->parseCurrentBlock();
00602                 }
00603 
00604                 if ($this->enabled["form"])
00605                 {
00606                         $this->tpl->touchBlock("tbl_form_footer");
00607                 }
00608 
00609                 if (!$this->global_tpl)
00610                 {
00611                         return $this->tpl->get();
00612                 }
00613         }
00614         
00615         /*
00616         * set a tpl stylesheet
00617         * @access       public
00618         * @param        string  table element
00619         * @param        string  CSS definition
00620         */
00621         function setStyle($a_element,$a_style)
00622         {
00623                 $this->styles[$a_element] = $a_style;
00624         }
00625 
00626         /*
00627         * get a tpl stylesheet
00628         * @access       public
00629         * @param        string  table element
00630         */
00631         function getStyle($a_element)
00632         {
00633                 return $this->styles[$a_element];
00634         }
00635 }
00636 ?>

Generated on Fri Dec 13 2013 11:57:55 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1