Dokeos PHP Cross Reference Learning Management Systems

Source: /main/inc/lib/sortabletable.class.php - 1081 lines - 40432 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /*
   4  ==============================================================================
   5      Dokeos - elearning and course management software
   6  
   7      Copyright (c) 2005-2008 Dokeos S.A.
   8      Copyright (c) Bart Mollet (bart.mollet@hogent.be)
   9  
  10      For a full list of contributors, see "credits.txt".
  11      The full license can be read in "license.txt".
  12  
  13      This program is free software; you can redistribute it and/or
  14      modify it under the terms of the GNU General Public License
  15      as published by the Free Software Foundation; either version 2
  16      of the License, or (at your option) any later version.
  17  
  18      See the GNU General Public License for more details.
  19  
  20      Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  21      Mail: info@dokeos.com
  22  ==============================================================================
  23  */
  24  require_once "HTML/Table.php"; //See http://pear.php.net/package/HTML_Table
  25  require_once "Pager/Pager.php"; //See http://pear.php.net/package/Pager
  26  require_once  'tablesort.lib.php';
  27  require_once  'main_api.lib.php';
  28  /**
  29   * This class allows you to display a sortable data-table. It is possible to
  30   * split the data in several pages.
  31   * Using this class you can:
  32   * - automatically create checkboxes of the first table column
  33   *     - a "select all" and "deselect all" link is added
  34   *     - only if you provide a list of actions for the selected items
  35   * - click on the table header to sort the data
  36   * - choose how many items you see per page
  37   * - navigate through all data-pages
  38   */
  39  class SortableTable extends HTML_Table {
  40      /**
  41       * A name for this table
  42       */
  43      public $table_name;
  44      /**
  45       * The page to display
  46       */
  47      public $page_nr;
  48      /**
  49       * The column to sort the data
  50       */
  51      public $column;
  52      /**
  53       * The sorting direction (ASC or DESC)
  54       */
  55      public $direction;
  56      /**
  57       * Number of items to display per page
  58       */
  59      public $per_page;
  60      /**
  61       * The default number of items to display per page
  62       */
  63      public $default_items_per_page;
  64      /**
  65       * A prefix for the URL-parameters, can be used on pages with multiple
  66       * SortableTables
  67       */
  68      public $param_prefix;
  69      /**
  70       * The pager object to split the data in several pages
  71       */
  72      public $pager;
  73      /**
  74       * The total number of items in the table
  75       */
  76      public $total_number_of_items;
  77      /**
  78       * The function to get the total number of items
  79       */
  80      public $get_total_number_function;
  81      /**
  82       * The function to the the data to display
  83       */
  84      public $get_data_function;
  85      /**
  86       * An array with defined column-filters
  87       */
  88      public $column_filters;
  89      /**
  90       * A list of actions which will be available through a select list
  91       */
  92      public $form_actions;
  93      /**
  94       * Additional parameters to pass in the URL
  95       */
  96      public $additional_parameters;
  97      /**
  98       * Additional attributes for the th-tags
  99       */
 100      public $th_attributes;
 101      /**
 102       * Additional attributes for the td-tags
 103       */
 104      public $td_attributes;
 105      /**
 106       * Array with names of the other tables defined on the same page of this
 107       * table
 108       */
 109      public $other_tables;
 110  
 111  
 112      /**
 113       * Create a new SortableTable
 114       * @param string $table_name A name for the table (default = 'table')
 115       * @param string $get_total_number_function A user defined function to get
 116       * the total number of items in the table
 117       * @param string $get_data_function A function to get the data to display on
 118       * the current page
 119       * @param int $default_column The default column on which the data should be
 120       * sorted
 121       * @param int $default_items_per_page The default number of items to show
 122       * on one page
 123       * @param string $default_order_direction The default order direction;
 124       * either the constant 'ASC' or 'DESC'
 125       */
 126  	public function __construct ($table_name = 'table', $get_total_number_function = null, $get_data_function = null, $default_column = 1, $default_items_per_page = 20, $default_order_direction = 'ASC') {
 127          parent :: __construct (array ('class' => 'data_table'));
 128          $this->table_name = $table_name;
 129          $this->additional_parameters = array ();
 130          $this->param_prefix = $table_name.'_';
 131  
 132          $this->page_nr = isset ($_SESSION[$this->param_prefix.'page_nr']) ? intval($_SESSION[$this->param_prefix.'page_nr']) : 1;
 133          $this->page_nr = isset ($_GET[$this->param_prefix.'page_nr'])       ? intval($_GET[$this->param_prefix.'page_nr']) : $this->page_nr;
 134          $this->column  = isset ($_SESSION[$this->param_prefix.'column'])  ? intval($_SESSION[$this->param_prefix.'column']) : $default_column;
 135          $this->column  = isset ($_GET[$this->param_prefix.'column'])       ? intval($_GET[$this->param_prefix.'column']) : $this->column;
 136          $this->direction = $default_order_direction;
 137  
 138          //$this->direction = isset ($_SESSION[$this->param_prefix.'direction']) ? $_SESSION[$this->param_prefix.'direction'] : $default_order_direction;
 139  
 140  
 141          if (isset($_SESSION[$this->param_prefix.'direction'])) {
 142              $my_session_direction = $_SESSION[$this->param_prefix.'direction'];
 143              if(!in_array($my_session_direction, array('ASC','DESC'))){
 144                  $this->direction = 'ASC';
 145              } else {
 146                  if ($my_session_direction=='ASC') {
 147                      $this->direction = 'ASC';
 148                  } elseif ($my_session_direction=='DESC') {
 149                      $this->direction = 'DESC';
 150                  }
 151              }
 152          }
 153  
 154          if (isset($_GET[$this->param_prefix.'direction'])) {
 155              $my_get_direction = $_GET[$this->param_prefix.'direction'];
 156              if(!in_array($my_get_direction, array('ASC','DESC'))){
 157                  $this->direction = 'ASC';
 158              } else {
 159                  if ($my_get_direction=='ASC') {
 160                      $this->direction = 'ASC';
 161                  } elseif ($my_get_direction=='DESC') {
 162                      $this->direction = 'DESC';
 163                  }
 164              }
 165          }
 166  
 167          if(empty($this->direction))
 168          {
 169              $this->direction = 'ASC';
 170          }
 171  
 172          //allow to change paginate in multiples tabs
 173          unset($_SESSION[$this->param_prefix.'per_page']);
 174  
 175          $this->per_page = isset ($_SESSION[$this->param_prefix.'per_page']) ? intval($_SESSION[$this->param_prefix.'per_page']) : $default_items_per_page;
 176          $this->per_page = isset ($_GET[$this->param_prefix.'per_page'])        ? intval($_GET[$this->param_prefix.'per_page']) : $this->per_page;
 177  
 178          $_SESSION[$this->param_prefix.'per_page'] = $this->per_page;
 179          $_SESSION[$this->param_prefix.'direction'] = $this->direction ;
 180          $_SESSION[$this->param_prefix.'page_nr'] = $this->page_nr;
 181          $_SESSION[$this->param_prefix.'column'] = $this->column;
 182          $this->pager = null;
 183          $this->default_items_per_page = $default_items_per_page;
 184          $this->total_number_of_items = -1;
 185          $this->get_total_number_function = $get_total_number_function;
 186          $this->get_data_function = $get_data_function;
 187          $this->column_filters = array ();
 188          $this->form_actions = array ();
 189          $this->checkbox_name = null;
 190          $this->td_attributes = array ();
 191          $this->th_attributes = array ();
 192          $this->other_tables = array();
 193      }
 194      /**
 195       * Get the Pager object to split the showed data in several pages
 196       */
 197  	public function get_pager () {
 198          if (is_null($this->pager))
 199          {
 200              $total_number_of_items = $this->get_total_number_of_items();
 201              $params['mode'] = 'Sliding';
 202              $params['perPage'] = $this->per_page;
 203              $params['totalItems'] = $total_number_of_items;
 204              $params['urlVar'] = $this->param_prefix.'page_nr';
 205              $params['currentPage'] = $this->page_nr;
 206              //$params['prevImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/go-previous.png"  alt="'.get_lang('PreviousPage').'" title="'.get_lang('PreviousPage').'" style="vertical-align: middle;"/>';
 207              $params['prevImg'] = Display::return_icon('pixel.gif',get_lang('PreviousPage'),array('class'=>'actionplaceholdericon actionprev_navigation','style'=>'vertical-align: middle;'));
 208              //$params['nextImg'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/go-next.png"  alt="'.get_lang('NextPage').'" title="'.get_lang('NextPage').'" style="vertical-align: middle;"/>';
 209              $params['nextImg'] = Display::return_icon('pixel.gif',get_lang('NextPage'),array('class'=>'actionplaceholdericon actionnext_navigation','style'=>'vertical-align: middle;'));
 210              //$params['firstPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/go-first.png"  alt="'.get_lang('FirstPage').'" title="'.get_lang('FirstPage').'" style="vertical-align: middle;"/>';
 211              $params['firstPageText'] = Display::return_icon('pixel.gif',get_lang('FirstPage'),array('class'=>'actionplaceholdericon actiongo_first','style'=>'vertical-align: middle;'));
 212              //$params['lastPageText'] = '<img src="'.api_get_path(WEB_CODE_PATH).'img/go-last.png"  alt="'.get_lang('LastPage').'" title="'.get_lang('LastPage').'" style="vertical-align: middle;"/>';
 213              $params['lastPageText'] =Display::return_icon('pixel.gif',get_lang('go-last.png'),array('class'=>'actionplaceholdericon actiongo_last','style'=>'vertical-align: middle;'));
 214              $params['firstPagePre'] = '';
 215              $params['lastPagePre'] = '';
 216              $params['firstPagePost'] = '';
 217              $params['lastPagePost'] = '';
 218              $params['spacesBeforeSeparator'] = '';
 219              $params['spacesAfterSeparator'] = '';
 220              $query_vars = array_keys($_GET);
 221              $query_vars_needed = array ($this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page');
 222              if (count($this->additional_parameters) > 0)
 223              {
 224                  $query_vars_needed = array_merge($query_vars_needed, array_keys($this->additional_parameters));
 225              }
 226              $query_vars_exclude = array_diff($query_vars, $query_vars_needed);
 227              $params['excludeVars'] = $query_vars_exclude;
 228              $this->pager = & Pager :: factory($params);
 229          }
 230          return $this->pager;
 231      }
 232      /**
 233       * Displays the table, complete with navigation buttons to browse through
 234       * the data-pages.
 235       */
 236  	public function display () {
 237          global $charset;
 238          $empty_table = false;
 239          $html = '';
 240          if ($this->get_total_number_of_items() == 0)
 241          {
 242              $cols = $this->getColCount();
 243              $this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
 244              if (api_is_xml_http_request()===true) {
 245                  $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
 246              } else {
 247                  $message_empty=get_lang('TheListIsEmpty');
 248              }
 249              $this->setCellContents(1, 0,$message_empty);
 250  
 251              $empty_table = true;
 252          }
 253          $html='';
 254          if (!$empty_table)
 255          {
 256              $form = $this->get_page_select_form();
 257              $nav = $this->get_navigation_html();
 258              $html = '<table style="width:100%;">';
 259              $html .= '<tr>';
 260              $html .= '<td style="width:25%;">';
 261          //    $html .= $form;
 262              $html .= '</td>';
 263              $html .= '<td style="text-align:center;">';
 264          //    $html .= $this->get_table_title();
 265              $html .= '</td>';
 266              $html .= '<td style="text-align:right;width:25%;">';
 267              $html .= $nav;
 268              $html .= '</td>';
 269              $html .= '</tr>';
 270              $html .= '</table>';
 271              if (count($this->form_actions) > 0)
 272              {
 273                  $html .= '<script language="JavaScript" type="text/javascript">
 274                                                                  /*<![CDATA[*/
 275  																function setCheckbox(value) {
 276                                                                      var thisvalue = $("input[name=select_all_none]").attr("checked");
 277                                                                      if (thisvalue == true){
 278                                                                          value = true;
 279                                                                      } else {
 280                                                                          value = false;
 281                                                                      }
 282  
 283                                                                       d = document.form_'.$this->table_name.';
 284                                                                       for (i = 0; i < d.elements.length; i++) {
 285                                                                             if (d.elements[i].type == "checkbox") {
 286                                                                               d.elements[i].checked = value;
 287                                                                             }
 288                                                                       }
 289                                                                  }
 290                                                                  /*]]>*/
 291                                                              </script>';
 292                  $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
 293  
 294                  $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
 295              }
 296          }
 297          $html .= $this->get_table_html();
 298          if (!$empty_table)
 299          {
 300              $html .= '<table style="width:100%;">';
 301              $html .= '<tr>';
 302              $html .= '<td colspan="2">';
 303              if (count($this->form_actions) > 0)
 304              {
 305                  //$html .= '<br/>';
 306                  //$html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
 307                  //$html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
 308                  // if there is more than one action we display a dropdown list with a submit button
 309                  // if there is only one we simply display the button (with hidden form)
 310                  if (count($this->form_actions)>1){
 311                      $html .= '<select name="action">';
 312                      $html .= '<option value="--">'.get_lang('SelectAction').'</option>';
 313                      foreach ($this->form_actions as $action => $label) {
 314                          $html .= '<option value="'.$action.'">'.$label.'</option>';
 315                      }
 316                      $html .= '</select>';
 317                      $button_text = get_lang('Select');
 318                  } else{
 319                      // we have to loop through the array (with only one element) because we do not know what they key or value of the array is
 320                      foreach ($this->form_actions as $action => $label) {
 321                          $html .= '<input type="hidden" name="action" value="'.$action.'"/>';
 322                          $button_text = $label;
 323                      }
 324                  }
 325                  switch($action)
 326                  {
 327                      case 'delete_courses': 
 328                      case 'delete':    
 329                      $html .= '<button type="submit" class="cancel multiaction" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;">'.$button_text.'</button>'; break;
 330                      default: $html .= '&nbsp;&nbsp;<button type="submit" class="save multiaction" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;">'.$button_text.'</button>'; break;
 331                  }
 332                  
 333  
 334              }
 335              else
 336              {
 337                  $html .= $form;
 338              }
 339              $html .= '</td>';
 340              $html .= '<td style="text-align:right;">';
 341              $html .= $nav;
 342              $html .= '</td>';
 343              $html .= '</tr>';
 344              $html .= '</table>';
 345              if (count($this->form_actions) > 0)
 346              {
 347                  $html .= '</form>';
 348              }
 349          }
 350          echo $html;
 351      }
 352      
 353      /**
 354       * This function shows the content of a table in a grid.
 355       * Should not be use to edit information (edit/delete rows) only.
 356       * */
 357  	public function display_grid () {
 358          global $charset;        
 359          $empty_table = false;
 360          $html = '';
 361          
 362          if ($this->get_total_number_of_items() == 0) {
 363              $cols = $this->getColCount();
 364              //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
 365              if (api_is_xml_http_request()===true) {
 366                  $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
 367              } else {
 368                  $message_empty=get_lang('TheListIsEmpty');
 369              }
 370              $this->setCellContents(1, 0,$message_empty);
 371              $empty_table = true;
 372          }
 373          $html='';
 374          if (!$empty_table) {
 375              $form = $this->get_page_select_form();
 376              $nav = $this->get_navigation_html();
 377              
 378              // @todo This style css must be moved to default.css only for dev         
 379              echo '<style>
 380                      .grid_container { width:100%;}
 381                      .grid_item { height: 120px; width:98px;  border:1px dotted #ccc; float:left; padding:5px; margin:8px;}
 382                      .grid_element_0 { width:100px; float:left; text-align:center; margin-bottom:5px;}
 383                      .grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
 384                      .grid_element_2 { width:150px; float:left;}
 385                      
 386                      .grid_selectbox { width:50%; float:left;}
 387                      .grid_title     { width:30%; float:left;}
 388                      .grid_nav         { float:right;}
 389                          
 390              </style>';
 391          
 392          
 393              //this also must be moved            
 394              $html = '<div class="sub-header">';
 395              $html .= '<div class="grid_selectbox">'.$form.'</div>';            
 396              $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';            
 397              $html .= '<div class="grid_nav">'.$nav.'</div>';            
 398              $html .= '</div>';
 399              
 400              $html .= '<div class="clear"></div>';
 401              if (count($this->form_actions) > 0) {                
 402                  $script= '<script language="JavaScript" type="text/javascript">
 403                                                                  /*<![CDATA[*/
 404  																function setCheckbox(value) {
 405                                                                       d = document.form_'.$this->table_name.';
 406                                                                       for (i = 0; i < d.elements.length; i++) {
 407                                                                             if (d.elements[i].type == "checkbox") {
 408                                                                               d.elements[i].checked = value;
 409                                                                             }
 410                                                                       }
 411                                                                  }
 412                                                                  /*]]>*/
 413                                                              </script>';
 414                  $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
 415                  $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
 416              }
 417          }
 418          $items = $this->get_clean_html(); // getting the items of the table                
 419          // the generating of style classes must be improved. Maybe we need a a table name to create style on the fly:
 420          // i.e: .whoisonline_table_grid_container instead of  .grid_container
 421          // where whoisonline is the table's name like drupal's template engine
 422          
 423          $html .= '<div class="grid_container">';        
 424          if (is_array($items) && count($items)>0 ) {
 425              foreach($items as $row) {                        
 426                  $html.= '<div class="grid_item">';
 427                  $i=0;
 428                  foreach($row as $element) {
 429                      $html.='<div class="grid_element_'.$i.'">'.$element.'</div>';
 430                      $i++;
 431                  }        
 432                  $html.='</div>';
 433              }            
 434              $html .= '</div>';
 435          }
 436          $html .= '<div class="clear"></div>';
 437          /*
 438          if (!$empty_table) {
 439              $html .= '<table style="width:100%;">';
 440              $html .= '<tr>';
 441              $html .= '<td colspan="2">';
 442              if (count($this->form_actions) > 0) {
 443                  $html .= '<br/>';
 444                  $html .= '<a href="?'.$params.'&amp;'.$this->param_prefix.'selectall=1" onclick="javascript:setCheckbox(true);return false;">'.get_lang('SelectAll').'</a> - ';
 445                  $html .= '<a href="?'.$params.'" onclick="javascript:setCheckbox(false);return false;">'.get_lang('UnSelectAll').'</a> ';
 446                  $html .= '<select name="action">';
 447                  foreach ($this->form_actions as $action => $label) {
 448                      $html .= '<option value="'.$action.'">'.$label.'</option>';
 449                  }
 450                  $html .= '</select>';
 451                  $html .= '&nbsp;&nbsp;<button type="submit" class="save" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;">'.get_lang('Select').'</button>';
 452              } else {
 453                  $html .= $form;
 454              }
 455              $html .= '</td>';
 456              $html .= '<td style="text-align:right;">';
 457              $html .= $nav;
 458              $html .= '</td>';
 459              $html .= '</tr>';
 460              $html .= '</div>';
 461              if (count($this->form_actions) > 0) {
 462                  $html .= '</form>';
 463              }
 464          }
 465          */
 466          echo $html;
 467      }
 468      
 469  	public function display_simple_grid($vibility_options, $hide_navigation) {
 470          global $charset;        
 471          $empty_table = false;
 472          $html = '';        
 473          if ($this->get_total_number_of_items() == 0) {
 474              $cols = $this->getColCount();
 475              //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
 476              if (api_is_xml_http_request()===true) {
 477                  $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
 478              } else {
 479                  $message_empty=get_lang('TheListIsEmpty');
 480              }
 481              $this->setCellContents(1, 0,$message_empty);
 482              $empty_table = true;
 483          }
 484          $html='';
 485          
 486          if (!$empty_table) {
 487              //if we show the pagination                    
 488              if ($hide_navigation == false ) {
 489                  $form = $this->get_page_select_form();    
 490                  $nav = $this->get_navigation_html();
 491                  
 492                  //this also must be moved            
 493                  $html = '<div class="sub-header">';
 494                  $html .= '<div class="grid_selectbox">'.$form.'</div>';            
 495                  $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';            
 496                  $html .= '<div class="grid_nav">'.$nav.'</div>';            
 497                  $html .= '</div>';
 498              }
 499              $html .= '<div class="clear"></div>';
 500              if (count($this->form_actions) > 0) {            
 501                  $script= '<script language="javaScript" type="text/javascript">
 502                              /*<![CDATA[*/
 503  							function setCheckbox(value) {
 504                                   d = document.form_'.$this->table_name.';
 505                                   for (i = 0; i < d.elements.length; i++) {
 506                                         if (d.elements[i].type == "checkbox") {
 507                                           d.elements[i].checked = value;
 508                                         }
 509                                   }
 510                              }
 511                              /*]]>*/
 512                          </script>';
 513                  $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
 514                  $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
 515              }
 516          }
 517          
 518          // @todo This style css must be moved to default.css only for dev         
 519          echo '<style>                
 520                  .search_users_grid_container { width:100%;}
 521                  .search_users_grid_item { width:400px;  height: 90px;  border:1px dotted #ccc; float:left; padding:5px; margin:8px;}
 522                  .search_users_grid_element_0 { width:100px; float:left; text-align:center; margin-bottom:5px;}
 523                  .search_users_grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
 524                  .search_users_grid_element_2 { width:150px; float:left;}
 525                  
 526                  .search_users_grid_selectbox { width:50%; float:left;}
 527                  .search_users_grid_title     { width:30%; float:left;}
 528                  .search_users_grid_nav         { float:right;}
 529                      
 530          </style>';
 531          
 532          if ($hide_navigation == true ) {
 533              $items = $this->table_data; //this is a faster way to get what we want
 534          } else {
 535              //the normal way
 536              $items = $this->get_clean_html(); // getting the items of the table
 537          }
 538          
 539                      
 540          // the generating of style classes must be improved. Maybe we need a a table name to create style on the fly:
 541          // i.e: .whoisonline_table_grid_container instead of  .grid_container
 542          // where whoisonline is the table's name like drupal's template engine
 543          
 544          if (is_array($vibility_options)) {
 545              $filter = false; // the 2nd condition of the if will be loaded             
 546          } else {
 547              if ($vibility_options === false) {
 548                  $filter = false;
 549              } else {
 550                  $filter = true;
 551              }
 552          }
 553          
 554          $html .= '<div class="'.$this->table_name.'_grid_container">';        
 555          if (is_array($items) && count($items)>0 ) {             
 556              foreach($items as $row) {                
 557                  $html.= '<div class="'.$this->table_name.'_grid_item">';
 558                  $i=0;
 559                  foreach($row as $element) {                    
 560                      if ( $filter === true || $vibility_options[$i] == true) {
 561                          $html.='<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
 562                      }
 563                      $i++;
 564                  }        
 565                  $html.='</div>';
 566              }
 567          }
 568          $html.='</div>';
 569          $html .= '<div class="clear"></div>';
 570          echo $html;
 571      }
 572  
 573  	public function return_simple_grid($vibility_options, $hide_navigation) {
 574          global $charset;
 575          $empty_table = false;
 576          $html = '';
 577          if ($this->get_total_number_of_items() == 0) {
 578              $cols = $this->getColCount();
 579              //$this->setCellAttributes(1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols);
 580              if (api_is_xml_http_request()===true) {
 581                  $message_empty=api_utf8_encode(get_lang('TheListIsEmpty'));
 582              } else {
 583                  $message_empty=get_lang('TheListIsEmpty');
 584              }
 585              $this->setCellContents(1, 0,$message_empty);
 586              $empty_table = true;
 587          }
 588          $html='';
 589  
 590          if (!$empty_table) {
 591              //if we show the pagination
 592              if ($hide_navigation == false ) {
 593                  $form = $this->get_page_select_form();
 594                  $nav = $this->get_navigation_html();
 595  
 596                  //this also must be moved
 597                  $html = '<div class="sub-header">';
 598                  $html .= '<div class="grid_selectbox">'.$form.'</div>';
 599                  $html .= '<div class="grid_title">'.$this->get_table_title().'</div>';
 600                  $html .= '<div class="grid_nav">'.$nav.'</div>';
 601                  $html .= '</div>';
 602              }
 603              $html .= '<div class="clear"></div>';
 604              if (count($this->form_actions) > 0) {
 605                  $script= '<script language="javaScript" type="text/javascript">
 606                              /*<![CDATA[*/
 607  							function setCheckbox(value) {
 608                                   d = document.form_'.$this->table_name.';
 609                                   for (i = 0; i < d.elements.length; i++) {
 610                                         if (d.elements[i].type == "checkbox") {
 611                                           d.elements[i].checked = value;
 612                                         }
 613                                   }
 614                              }
 615                              /*]]>*/
 616                          </script>';
 617                  $params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
 618                  $html .= '<form method="post" action="'.api_get_self().'?'.$params.'" name="form_'.$this->table_name.'">';
 619              }
 620          }
 621  
 622          // @todo This style css must be moved to default.css only for dev
 623  
 624                  $html .= '<style>
 625                  .search_users_grid_container { width:100%;}
 626                  .search_users_grid_item { width:400px;  height: 90px;  border:1px dotted #ccc; float:left; padding:5px; margin:8px;}
 627                  .search_users_grid_element_0 { width:100px; float:left; text-align:center; margin-bottom:5px;}
 628                  .search_users_grid_element_1 { width:100px; float:left; text-align:center;margin-bottom:5px;}
 629                  .search_users_grid_element_2 { width:150px; float:left;}
 630  
 631                  .search_users_grid_selectbox { width:50%; float:left;}
 632                  .search_users_grid_title     { width:30%; float:left;}
 633                  .search_users_grid_nav         { float:right;}
 634  
 635          </style>';
 636  
 637          if ($hide_navigation == true ) {
 638              $items = $this->table_data; //this is a faster way to get what we want
 639          } else {
 640              //the normal way
 641              $items = $this->get_clean_html(); // getting the items of the table
 642          }
 643  
 644  
 645          // the generating of style classes must be improved. Maybe we need a a table name to create style on the fly:
 646          // i.e: .whoisonline_table_grid_container instead of  .grid_container
 647          // where whoisonline is the table's name like drupal's template engine
 648  
 649          if (is_array($vibility_options)) {
 650              $filter = false; // the 2nd condition of the if will be loaded
 651          } else {
 652              if ($vibility_options === false) {
 653                  $filter = false;
 654              } else {
 655                  $filter = true;
 656              }
 657          }
 658  
 659          $html .= '<div class="'.$this->table_name.'_grid_container">';
 660          if (is_array($items) && count($items)>0 ) {
 661              foreach($items as $row) {
 662                  $html.= '<div class="'.$this->table_name.'_grid_item">';
 663                  $i=0;
 664                  foreach($row as $element) {
 665                      if ( $filter === true || $vibility_options[$i] == true) {
 666                          $html.='<div class="'.$this->table_name.'_grid_element_'.$i.'">'.$element.'</div>';
 667                      }
 668                      $i++;
 669                  }
 670                  $html.='</div>';
 671              }
 672          }
 673          $html.='</div>';
 674          $html .= '<div class="clear"></div>';
 675          return $html;
 676      }
 677  
 678      /**
 679       * Get the HTML-code with the navigational buttons to browse through the
 680       * data-pages.
 681       */
 682  	public function get_navigation_html () {
 683          $pager = $this->get_pager();
 684          $pager_links = $pager->getLinks();
 685          $showed_items = $pager->getOffsetByPageId();
 686          $nav = $pager_links['first'].' '.$pager_links['back'];
 687          $nav .= ' '.$pager->getCurrentPageId().' / '.$pager->numPages().' ';
 688          $nav .= $pager_links['next'].' '.$pager_links['last'];
 689          return $nav;
 690      }
 691      /**
 692       * Get the HTML-code with the data-table.
 693       */
 694  	public function get_table_html () {
 695          $pager = $this->get_pager();
 696          $val = $pager->getOffsetByPageId();
 697          $offset = $pager->getOffsetByPageId();
 698          $from = $offset[0] - 1;
 699          $table_data = $this->get_table_data($from);
 700          if (is_array($table_data)) {
 701              foreach ($table_data as $index => $row) {
 702                  $row = $this->filter_data($row);
 703                  $this->addRow($row);
 704              }
 705          }
 706          $this->altRowAttributes(0, array ('class' => 'row_odd'), array ('class' => 'row_even'), true);
 707          foreach ($this->th_attributes as $column => $attributes) {
 708              $this->setCellAttributes(0, $column, $attributes);
 709          }
 710          foreach ($this->td_attributes as $column => $attributes) {
 711              $this->setColAttributes($column, $attributes);
 712          }
 713          return $this->toHTML();
 714      }
 715      /**
 716       * This function return the items of the table
 717       * @return array table row items
 718       */
 719  	public function get_clean_html () {
 720          $pager = $this->get_pager();
 721          $val = $pager->getOffsetByPageId();
 722          $offset = $pager->getOffsetByPageId();
 723          $from = $offset[0] - 1;        
 724          $table_data = $this->get_table_data($from);        
 725          $new_table_data = array();
 726          if (is_array($table_data)) {
 727              foreach ($table_data as $index => $row) {
 728                  $row = $this->filter_data($row);
 729                  $new_table_data[] = $row;
 730              }
 731          }        
 732          return $new_table_data;
 733      }
 734      
 735      /**
 736       * Get the HTML-code wich represents a form to select how many items a page
 737       * should contain.
 738       */
 739  	public function get_page_select_form () {
 740          $total_number_of_items = $this->get_total_number_of_items();
 741          if ($total_number_of_items <= $this->default_items_per_page)
 742          {
 743              return '';
 744          }
 745          $result[] = '<form method="get" action="'.api_get_self().'" style="display:inline;">';
 746          $param[$this->param_prefix.'direction'] = $this->direction;
 747          $param[$this->param_prefix.'page_nr'] = $this->page_nr;
 748          $param[$this->param_prefix.'column'] = $this->column;
 749          $param = array_merge($param, $this->additional_parameters);
 750          foreach ($param as $key => $value) {
 751              $result[] = '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
 752          }
 753      // Hide the select box - Bug #6728
 754          $result[] = '<div style="display:none;"><select name="'.$this->param_prefix.'per_page" onchange="javascript:this.form.submit();">';
 755          for ($nr = 10; $nr <= min(50, $total_number_of_items); $nr += 10) {
 756              $result[] = '<option value="'.$nr.'" '. ($nr == $this->per_page ? 'selected="selected"' : '').'>'.$nr.'</option>';
 757          }
 758          if ($total_number_of_items < 500) {
 759              $result[] = '<option value="'.$total_number_of_items.'" '. ($total_number_of_items == $this->per_page ? 'selected="selected"' : '').'>'.api_ucfirst(get_lang('All')).'</option>';
 760          }
 761          $result[] = '</select></div>';
 762          $result[] = '<noscript>';
 763          $result[] = '<button class="save" type="submit">'.get_lang('Save').'</button>';
 764          $result[] = '</noscript>';
 765          $result[] = '</form>';
 766          $result = implode("\n", $result);
 767          return $result;
 768      }
 769      /**
 770       * Get the table title.
 771       */
 772  	public function get_table_title () {
 773          $pager = $this->get_pager();
 774          $showed_items = $pager->getOffsetByPageId();
 775          return $showed_items[0].' - '.$showed_items[1].' / '.$this->get_total_number_of_items();
 776      }
 777      /**
 778       * Set the header-label
 779       * @param int $column The column number
 780       * @param string $label The label
 781       * @param boolean $sortable Is the table sortable by this column? (defatult
 782       * = true)
 783       * @param string $th_attributes Additional attributes for the th-tag of the
 784       * table header
 785       * @param string $td_attributes Additional attributes for the td-tags of the
 786       * column
 787       */
 788  	public function set_header ($column, $label, $sortable = true, $th_attributes = null, $td_attributes = null) {
 789          $param['direction'] = 'ASC';
 790          if ($this->column == $column && $this->direction == 'ASC') {
 791              $param['direction'] = 'DESC';
 792          }
 793          $param['page_nr'] = $this->page_nr;
 794          $param['per_page'] = $this->per_page;
 795          $param['column'] = $column;
 796          if ($sortable) {
 797              $link = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;';
 798              foreach ($param as $key => $value) {
 799                  $link .= $this->param_prefix.$key.'='.urlencode($value).'&amp;';
 800              }
 801              $link .= $this->get_additional_url_paramstring();
 802              $link .= '">'.$label.'</a>';
 803              if ($this->column == $column)
 804              {
 805                  $link .= $this->direction == 'ASC' ? ' &#8595;' : ' &#8593;';
 806              }
 807          }
 808          else
 809          {
 810              $link = $label;
 811          }
 812          $this->setHeaderContents(0, $column, $link);
 813          if (!is_null($td_attributes))
 814          {
 815              $this->td_attributes[$column] = $td_attributes;
 816          }
 817          if (!is_null($th_attributes))
 818          {
 819              $this->th_attributes[$column] = $th_attributes;
 820          }
 821      }
 822      /**
 823       * Get the parameter-string with additional parameters to use in the URLs
 824       * generated by this SortableTable
 825       */
 826  	public function get_additional_url_paramstring () {
 827          $param_string_parts = array ();
 828          if(is_array($this->additional_parameters) && count($this->additional_parameters)>0)
 829          {
 830              foreach ($this->additional_parameters as $key => $value)
 831              {
 832                  $param_string_parts[] = urlencode($key).'='.urlencode($value);
 833              }
 834          }
 835          $result = implode('&amp;', $param_string_parts);
 836          foreach($this->other_tables as $index => $tablename) {
 837              $param = array();
 838              if( isset($_GET[$tablename.'_direction'])) {
 839                  //$param[$tablename.'_direction'] = $_GET[$tablename.'_direction'];
 840                  $my_get_direction = $_GET[$tablename.'_direction'];
 841                  if(!in_array($my_get_direction ,array('ASC','DESC'))){
 842                       $param[$tablename.'_direction'] =  'ASC';
 843                  } else {
 844                      $param[$tablename.'_direction'] = $my_get_direction;
 845                  }
 846              }
 847              if( isset($_GET[$tablename.'_page_nr']))
 848                  $param[$tablename.'_page_nr'] = intval($_GET[$tablename.'_page_nr']);
 849              if( isset($_GET[$tablename.'_per_page']))
 850                  $param[$tablename.'_per_page'] = intval($_GET[$tablename.'_per_page']);
 851              if( isset($_GET[$tablename.'_column']))
 852                  $param[$tablename.'_column'] = intval($_GET[$tablename.'_column']);
 853              $param_string_parts = array ();
 854              foreach ($param as $key => $value) {
 855                  $param_string_parts[] = urlencode($key).'='.urlencode($value);
 856              }
 857              if(count($param_string_parts) > 0)
 858                  $result .= '&amp;'.implode('&amp;', $param_string_parts);
 859          }
 860          return $result;
 861      }
 862      /**
 863       * Get the parameter-string with the SortableTable-related parameters to use
 864       * in URLs
 865       */
 866  	public function get_sortable_table_param_string () {
 867          $param[$this->param_prefix.'direction'] = $this->direction;
 868          $param[$this->param_prefix.'page_nr'] = $this->page_nr;
 869          $param[$this->param_prefix.'per_page'] = $this->per_page;
 870          $param[$this->param_prefix.'column'] = $this->column;
 871          $param_string_parts = array ();
 872          foreach ($param as $key => $value) {
 873              $param_string_parts[] = urlencode($key).'='.urlencode($value);
 874          }
 875          $res = implode('&amp;', $param_string_parts);
 876          return $res;
 877  
 878      }
 879      /**
 880       * Add a filter to a column. If another filter was allready defined for the
 881       * given column, it will be overwritten.
 882       * @param int $column The number of the column
 883       * @param string $function The name of the filter-function. This should be a
 884       * function wich requires 1 parameter and returns the filtered value.
 885       */
 886  	public function set_column_filter ($column, $function) {
 887          $this->column_filters[$column] = $function;
 888      }
 889      /**
 890       * Define a list of actions which can be performed on the table-date.
 891       * If you define a list of actions, the first column of the table will be
 892       * converted into checkboxes.
 893       * @param array $actions A list of actions. The key is the name of the
 894       * action. The value is the label to show in the select-box
 895       * @param string $checkbox_name The name of the generated checkboxes. The
 896       * value of the checkbox will be the value of the first column.
 897       */
 898  	public function set_form_actions ($actions, $checkbox_name = 'id') {
 899          $this->form_actions = $actions;
 900          // if there are multiple actions possible on the items of the sortable table 
 901          // then the first column header should display a checkbox
 902          if (count($this->form_actions) > 0){
 903              $this->set_header(0,'<input type="checkbox" value="select_all_none" name="select_all_none" onclick="javascript:setCheckbox();">',false);
 904          }
 905          $this->checkbox_name = $checkbox_name;
 906      }
 907      /**
 908       * Define a list of additional parameters to use in the generated URLs
 909       * @param array $parameters
 910       */
 911  	public function set_additional_parameters ($parameters) {
 912          $this->additional_parameters = $parameters;
 913      }
 914      /**
 915       * Set other tables on the same page.
 916       * If you have other sortable tables on the page displaying this sortable
 917       * tables, you can define those other tables with this function. If you
 918       * don't define the other tables, there sorting and pagination will return
 919       * to their default state when sorting this table.
 920       * @param array $tablenames An array of table names.
 921       */
 922  	public function set_other_tables ($tablenames) {
 923          $this->other_tables = $tablenames;
 924      }
 925      /**
 926       * Transform all data in a table-row, using the filters defined by the
 927       * function set_column_filter(...) defined elsewhere in this class.
 928       * If you've defined actions, the first element of the given row will be
 929       * converted into a checkbox
 930       * @param array $row A row from the table.
 931       */
 932  	public function filter_data ($row) {
 933          $url_params = $this->get_sortable_table_param_string().'&amp;'.$this->get_additional_url_paramstring();
 934          foreach ($this->column_filters as $column => $function)
 935          {
 936              $row[$column] = call_user_func($function, $row[$column], $url_params, $row);
 937          }
 938          if (count($this->form_actions) > 0) {
 939              if (strlen($row[0]) > 0) {
 940                  $row[0] = '<input type="checkbox" name="'.$this->checkbox_name.'[]" value="'.$row[0].'"';
 941                  if (isset ($_GET[$this->param_prefix.'selectall'])) {
 942                      $row[0] .= ' checked="checked"';
 943                  }
 944                  $row[0] .= '/>';
 945              }
 946          }
 947  
 948          foreach ($row as $index => $value) {
 949              if (strlen($row[$index]) == 0)
 950              {
 951                  $row[$index] = '-';
 952              }
 953          }
 954          return $row;
 955      }
 956      /**
 957       * Get the total number of items. This function calls the function given as
 958       * 2nd argument in the constructor of a SortableTable. Make sure your
 959       * function has the same parameters as defined here.
 960       */
 961  	public function get_total_number_of_items () {
 962          if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
 963              $this->total_number_of_items = call_user_func($this->get_total_number_function);
 964          }
 965          return $this->total_number_of_items;
 966      }
 967      /**
 968       * Get the data to display.  This function calls the function given as
 969       * 2nd argument in the constructor of a SortableTable. Make sure your
 970       * function has the same parameters as defined here.
 971       * @param int $from Index of the first item to return.
 972       * @param int $per_page The number of items to return
 973       * @param int $column The number of the column on which the data should be
 974       * sorted
 975       * @param string $direction In which order should the data be sorted (ASC
 976       * or DESC)
 977       */
 978  	public function get_table_data ($from = null, $per_page = null, $column = null, $direction = null) {
 979          if (!is_null($this->get_data_function))
 980          {
 981              return call_user_func($this->get_data_function, $from, $this->per_page, $this->column, $this->direction);
 982          }
 983          return array ();
 984      }
 985  }
 986  /**
 987   * Sortable table which can be used for data available in an array
 988   */
 989  class SortableTableFromArray extends SortableTable {
 990      /**
 991       * The array containing all data for this table
 992       */
 993      public $table_data;
 994      /**
 995       * Constructor
 996       * @param array $table_data
 997       * @param int $default_column
 998       * @param int $default_items_per_page
 999       */
1000  	public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename') {
1001          parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page);
1002          $this->table_data = $table_data;
1003      }
1004      /**
1005       * Get table data to show on current page
1006       * @see SortableTable#get_table_data
1007       */
1008  	public function get_table_data ($from = 1) {
1009          $content = TableSort :: sort_table($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC);
1010          return array_slice($content, $from, $this->per_page);
1011      }
1012      /**
1013       * Get total number of items
1014       * @see SortableTable#get_total_number_of_items
1015       */
1016  	public function get_total_number_of_items() {
1017          return count($this->table_data);
1018      }
1019  }
1020  
1021  
1022  /**
1023   * Sortable table which can be used for data available in an array
1024   *
1025   * Is a variation of SortableTableFromArray because we add 2 new arrays  $column_show and $column_order
1026   * $column_show is an array that lets us decide which are going to be the columns to show
1027   * $column_order is an array that lets us decide the ordering of the columns
1028   * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
1029   * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
1030   */
1031  
1032  class SortableTableFromArrayConfig extends SortableTable {
1033      /**
1034       * The array containing the columns that will be show i.e $column_show=array('1','0','0'); we will show only the 1st column
1035       */
1036      private $column_show;
1037  
1038      /**
1039       *The array containing the real sort column $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
1040       */
1041      private $column_order;
1042      /**
1043       * The array containing all data for this table
1044       */
1045      private $table_data;
1046  
1047      /**
1048       * Constructor
1049       * @param array $table_data All the information of the table
1050       * @param int $default_column Default column that will be use in the sorts functions
1051       * @param int $default_items_per_page quantity of pages that we are going to see
1052       * @param int $tablename Name of the table
1053       * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
1054       * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
1055       */
1056  	public function __construct ($table_data, $default_column = 1, $default_items_per_page = 20, $tablename = 'tablename',$column_show=null,$column_order=null,$direction='ASC') {
1057          $this->column_show=$column_show;
1058          $this->column_order=$column_order;
1059  
1060          parent :: __construct ($tablename, null, null, $default_column, $default_items_per_page,$direction);
1061  
1062          $this->table_data = $table_data;
1063      }
1064      /**
1065       * Get table data to show on current page
1066       * @see SortableTable#get_table_data
1067       */
1068  	public function get_table_data($from = 1) {
1069          $content = TableSort :: sort_table_config($this->table_data, $this->column, $this->direction == 'ASC' ? SORT_ASC : SORT_DESC ,$this->column_show, $this->column_order);
1070          return array_slice($content, $from, $this->per_page);
1071      }
1072  
1073      /**
1074       * Get total number of items
1075       * @see SortableTable#get_total_number_of_items
1076       */
1077  	public function get_total_number_of_items () {
1078          return count($this->table_data);
1079      }
1080  }
1081  ?>

title

Description

title

Description

title

Description

title

title

Body