[ Index ]

PHP Cross Reference of WP-Table Reloaded 1.4.2a

title

Body

[close]

/ -> wp-table-reloaded-frontend.php (source)

   1  <?php
   2  /*
   3  File Name: WP-Table Reloaded - Frontend Class (see main file wp-table-reloaded.php)
   4  Plugin URI: http://tobias.baethge.com/wordpress-plugins/wp-table-reloaded-english/
   5  Description: Description: This plugin allows you to create and easily manage tables in the admin-area of WordPress. A comfortable backend allows an easy manipulation of table data. You can then include the tables into your posts, on your pages or in text widgets by using a shortcode or a template tag function. Tables can be imported and exported from/to CSV, XML and HTML.
   6  Version: 1.4.2
   7  Author: Tobias B&auml;thge
   8  Author URI: http://tobias.baethge.com/
   9  Donate URI: http://tobias.baethge.com/donate/
  10  */
  11  
  12  class WP_Table_Reloaded_Frontend {
  13  
  14      // ###################################################################################################################
  15      // plugin variables
  16      var $options = array();
  17      var $tables = array();
  18  
  19      var $optionname = array(
  20          'tables' => 'wp_table_reloaded_tables',
  21          'options' => 'wp_table_reloaded_options',
  22          'table' => 'wp_table_reloaded_data'
  23      );
  24      // shortcodes
  25      var $shortcode_table = 'table';
  26      var $shortcode_table_info = 'table-info';
  27      
  28      var $shown_tables = array();
  29      var $tablesorter_tables = array();
  30  
  31      // ###################################################################################################################
  32      function WP_Table_Reloaded_Frontend() {
  33          // load options and table information from database, if not available: default
  34          $this->options = get_option( $this->optionname['options'], false );
  35          $this->tables = get_option( $this->optionname['tables'], false );
  36  
  37          if ( false === $this->options || false === $this->tables )
  38              return '';
  39  
  40          // front-end function, shortcode for the_content, manual filter for widget_text
  41          // shortcode "table-info" needs to be declared before "table"! Otherwise it will not be recognized!
  42          add_shortcode( $this->shortcode_table_info, array( &$this, 'handle_content_shortcode_table_info' ) );
  43          add_shortcode( $this->shortcode_table, array( &$this, 'handle_content_shortcode_table' ) );
  44  
  45          add_filter( 'widget_text', array( &$this, 'handle_widget_filter_table_info' ) );
  46          add_filter( 'widget_text', array( &$this, 'handle_widget_filter_table' ) );
  47  
  48          // if tablesorter enabled (globally) include javascript
  49          if ( true == $this->options['enable_tablesorter'] ) {
  50              $this->add_head_jquery_js(); // jquery needed in any case (it's too late to do this, when shortcode is executed
  51              add_action( 'wp_footer', array( &$this, 'output_tablesorter_js' ) ); // but if we actually need the tablesorter script can be determined in the footer
  52          }
  53  
  54          // if global css shall be used
  55          if ( true == $this->options['use_custom_css'] )
  56              add_action( 'wp_head', array( &$this, 'add_custom_css' ) );
  57      }
  58  
  59      // ###################################################################################################################
  60      // handle [table-info id=<the_table_id> field=<name> /] in the_content()
  61      function handle_content_shortcode_table_info( $atts ) {
  62  
  63          // parse shortcode attributs, only allow those specified
  64          $default_atts = array(
  65                  'id' => 0,
  66                  'field' => '',
  67                  'format' => ''
  68          );
  69            $atts = shortcode_atts( $default_atts, $atts );
  70  
  71          // check if table exists
  72          $table_id = $atts['id'];
  73          if ( !is_numeric( $table_id ) || 1 > $table_id || false == $this->table_exists( $table_id ) )
  74              return "[table \"{$table_id}\" not found /]<br />\n";
  75  
  76          $field = $atts['field'];
  77          $format = $atts['format'];
  78          
  79          $table = $this->load_table( $table_id );
  80  
  81          switch ( $field ) {
  82              case 'name':
  83              case 'description':
  84                  $output = $table[ $field ];
  85                  break;
  86              case 'last_modified':
  87                  $output = ( 'raw' == $format ) ?  $table['last_modified'] : $this->format_datetime( $table['last_modified'] );
  88                  break;
  89              case 'last_editor':
  90                  $output = $this->get_last_editor( $table['last_editor_id'] );
  91                  break;
  92              default:
  93                  if ( isset( $table['custom_fields'][ $field ] ) ) {
  94                      $output = $table['custom_fields'][ $field ];
  95                  } else {
  96                      $output = "[table-info field &quot;{$field}&quot; not found in table {$table_id} /]<br />\n";
  97                  }
  98          }
  99  
 100          return $output;
 101      }
 102  
 103      // ###################################################################################################################
 104      // handle [table id=<the_table_id> /] in the_content()
 105      function handle_content_shortcode_table( $atts ) {
 106          // parse shortcode attributs, only allow those specified
 107          $default_atts = array(
 108                  'id' => 0,
 109                  'column_widths' => '',
 110                  'alternating_row_colors' => -1,
 111                  'first_row_th' => -1,
 112                  'print_name' => -1,
 113                  'print_description' => -1,
 114                  'use_tablesorter' => -1,
 115                  'row_offset' => 1, // ATTENTION: MIGHT BE DROPPED IN FUTURE VERSIONS!
 116                  'row_count' => null, // ATTENTION: MIGHT BE DROPPED IN FUTURE VERSIONS!
 117                  'show_rows' => '',
 118                  'show_columns' => '',
 119                  'hide_rows' => '',
 120                  'hide_columns' => '',
 121                  'cellspacing' => 1,
 122                  'cellpadding' => 0,
 123                  'border' => 0
 124                  
 125          );
 126            $atts = shortcode_atts( $default_atts, $atts );
 127  
 128          // check if table exists
 129          $table_id = $atts['id'];
 130          if ( !is_numeric( $table_id ) || 1 > $table_id || false == $this->table_exists( $table_id ) )
 131              return "[table &quot;{$table_id}&quot; not found /]<br />\n";
 132  
 133          // explode from string to array
 134          $atts['column_widths'] = explode( '|', $atts['column_widths'] );
 135  
 136          // rows/columns are indexed from 0 internally
 137          $atts['show_rows'] = ( !empty( $atts['show_rows'] ) ) ? explode( ',', $atts['show_rows'] ) : array();
 138          foreach ( $atts['show_rows'] as $key => $value )
 139              $atts['show_rows'][$key] = (string) ( $value - 1 );
 140          $atts['show_columns'] = ( !empty( $atts['show_columns'] ) ) ? explode( ',', $atts['show_columns'] ) : array();
 141          foreach ( $atts['show_columns'] as $key => $value )
 142              $atts['show_columns'][$key] = (string) ( $value - 1 );
 143          $atts['hide_rows'] = ( !empty( $atts['hide_rows'] ) ) ? explode( ',', $atts['hide_rows'] ) : array();
 144          foreach ( $atts['hide_rows'] as $key => $value )
 145              $atts['hide_rows'][$key] = (string) ( $value - 1 );
 146          $atts['hide_columns'] = ( !empty( $atts['hide_columns'] ) ) ? explode( ',', $atts['hide_columns'] ) : array();
 147          foreach ( $atts['hide_columns'] as $key => $value )
 148              $atts['hide_columns'][$key] = (string) ( $value - 1 );
 149  
 150          $table = $this->load_table( $table_id );
 151  
 152          // check for table data
 153          if ( !isset( $table['data'] ) || empty( $table['data'] ) )
 154              return "[table &quot;{$table_id}&quot; seems to be empty /]<br />\n";
 155  
 156          // determine options to use (if set in shortcode, use those, otherwise use options from "Edit Table" screen)
 157          $output_options = array();
 158          foreach ( $atts as $key => $value ) {
 159              // have to check this, because strings 'true' or 'false' are not recognized as boolean!
 160              if ( is_array( $value ) )
 161                  $output_options[ $key ] = $value;
 162              elseif ( 'true' == strtolower( $value ) )
 163                  $output_options[ $key ] = true;
 164              elseif ( 'false' == strtolower( $value ) )
 165                  $output_options[ $key ] = false;
 166              else
 167                  $output_options[ $key ] = ( -1 !== $value ) ? $value : $table['options'][ $key ] ;
 168          }
 169          
 170          // how often was table displayed on this page yet? get its HTML ID
 171          $count = ( isset( $this->shown_tables[ $table_id ] ) ) ? $this->shown_tables[ $table['id'] ] : 0;
 172          $count = $count + 1;
 173          $this->shown_tables[ $table_id ] = $count;
 174          $output_options['html_id'] = "wp-table-reloaded-id-{$table_id}-no-{$count}";
 175          
 176          $output = $this->render_table( $table, $output_options );
 177  
 178          return $output;
 179      }
 180  
 181      // ###################################################################################################################
 182      // handle [table-info id=<the_table_id> field="name" /] in widget texts
 183      function handle_widget_filter_table_info( $text ) {
 184          // pattern to search for in widget text (only our plugin's shortcode!)
 185          if ( version_compare( $GLOBALS['wp_version'], '2.8alpha', '>=') ) {
 186              $pattern = '(.?)\[(' . preg_quote( $this->shortcode_table_info ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
 187          } else {
 188              $pattern = '\[(' . preg_quote( $this->shortcode_table_info ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
 189          }
 190          // search for it, if found, handle as if it were a shortcode
 191          return preg_replace_callback( '/'.$pattern.'/s', 'do_shortcode_tag', $text );
 192      }
 193  
 194      // handle [table id=<the_table_id> /] in widget texts
 195      function handle_widget_filter_table( $text ) {
 196          // pattern to search for in widget text (only our plugin's shortcode!)
 197          if ( version_compare( $GLOBALS['wp_version'], '2.8alpha', '>=') ) {
 198              $pattern = '(.?)\[(' . preg_quote( $this->shortcode_table ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
 199          } else {
 200              $pattern = '\[(' . preg_quote( $this->shortcode_table ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
 201          }
 202          // search for it, if found, handle as if it were a shortcode
 203          return preg_replace_callback( '/'.$pattern.'/s', 'do_shortcode_tag', $text );
 204      }
 205  
 206      // ###################################################################################################################
 207      // check, if given table id really exists
 208      function table_exists( $table_id ) {
 209          return isset( $this->tables[ $table_id ] );
 210      }
 211  
 212      // ###################################################################################################################
 213      function load_table( $table_id ) {
 214          $this->tables[ $table_id ] = ( isset( $this->tables[ $table_id ] ) ) ? $this->tables[ $table_id ] : $this->optionname['table'] . '_' . $table_id;
 215          $table = get_option( $this->tables[ $table_id ], array() );
 216          return $table;
 217      }
 218  
 219      // ###################################################################################################################
 220      // echo content of array
 221      function render_table( $table, $output_options ) {
 222          // classes that will be added to <table class=...>, can be used for css-styling
 223          $cssclasses = array( 'wp-table-reloaded', "wp-table-reloaded-id-{$table['id']}" );
 224          $cssclasses = implode( ' ', $cssclasses );
 225  
 226          // if row_offset or row_count were given, we cut that part from the table and show just that
 227          // ATTENTION: MIGHT BE DROPPED IN FUTURE VERSIONS!
 228          if ( null === $output_options['row_count'] )
 229              $table['data'] = array_slice( $table['data'], $output_options['row_offset'] - 1 ); // -1 because we start from 1
 230          else
 231              $table['data'] = array_slice( $table['data'], $output_options['row_offset'] - 1, $output_options['row_count'] ); // -1 because we start from 1
 232  
 233          // load information about hidden rows and columns
 234          $hidden_rows = isset( $table['visibility']['rows'] ) ? array_keys( $table['visibility']['rows'], true ) : array();
 235          $hidden_rows = array_merge( $hidden_rows, $output_options['hide_rows'] );
 236          $hidden_rows = array_diff( $hidden_rows, $output_options['show_rows'] );
 237          sort( $hidden_rows, SORT_NUMERIC );
 238          $hidden_columns = isset( $table['visibility']['columns'] ) ? array_keys( $table['visibility']['columns'], true ) : array();
 239          $hidden_columns = array_merge( $hidden_columns, $output_options['hide_columns'] );
 240          $hidden_columns = array_merge( array_diff( $hidden_columns, $output_options['show_columns'] ) );
 241          sort( $hidden_columns, SORT_NUMERIC );
 242  
 243          // remove hidden rows and re-index
 244          foreach( $hidden_rows as $row_idx ) {
 245              unset( $table['data'][$row_idx] );
 246          }
 247          $table['data'] = array_merge( $table['data'] );
 248          // remove hidden columns and re-index
 249          foreach( $table['data'] as $row_idx => $row ) {
 250              foreach( $hidden_columns as $col_idx ) {
 251                  unset( $row[$col_idx] );
 252              }
 253              $table['data'][$row_idx] = array_merge( $row );
 254          }
 255  
 256          $rows = count( $table['data'] );
 257          $cols = (0 < $rows) ? count( $table['data'][0] ) : 0;
 258  
 259          // make array $shortcode_atts['column_widths'] have $cols entries
 260          $output_options['column_widths'] = array_pad( $output_options['column_widths'], $cols, '' );
 261  
 262          $output = '';
 263  
 264          if ( 0 < $rows && 0 < $cols) {
 265          
 266              if ( true == $output_options['print_name'] )
 267                  $output .= '<h2 class="wp-table-reloaded-table-name">' . $this->safe_output( $table['name'] ) . "</h2>\n";
 268          
 269              $output .= "<table id=\"{$output_options['html_id']}\" class=\"{$cssclasses}\" cellspacing=\"{$output_options['cellspacing']}\" cellpadding=\"{$output_options['cellpadding']}\" border=\"{$output_options['border']}\">\n";
 270  
 271              foreach( $table['data'] as $row_idx => $row ) {
 272                  if ( true == $output_options['alternating_row_colors'] )
 273                      $row_class = ( 1 == ($row_idx % 2) ) ? ' class="even row-' . ( $row_idx + 1 ) . '"' : ' class="odd row-' . ( $row_idx + 1 ) . '"';
 274                  else
 275                      $row_class = ' class="row-' . ( $row_idx + 1 ) . '"';
 276                      
 277                  if( 0 == $row_idx ) {
 278                      if ( true == $output_options['first_row_th'] ) {
 279                          $output .= "<thead>\n";
 280                          $output .= "\t<tr{$row_class}>\n\t\t";
 281                          foreach( $row as $col_idx => $cell_content ) {
 282                              $col_class = ' class="column-' . ( $col_idx + 1 ) . '"';
 283                              $width_style = ( !empty( $output_options['column_widths'][$col_idx] ) ) ? " style=\"width:{$output_options['column_widths'][$col_idx]};\"" : '';
 284                              $cell_content = do_shortcode( $this->safe_output( $cell_content ) );
 285                              $output .= "<th{$col_class}{$width_style}>" . "{$cell_content}" . "</th>";
 286                          }
 287                          $output .= "\n\t</tr>\n";
 288                          $output .= "</thead>\n";
 289                          $output .= "<tbody>\n";
 290                      } else {
 291                          $output .= "<tbody>\n";
 292                          $output .= "\t<tr{$row_class}>\n\t\t";
 293                          foreach( $row as $col_idx => $cell_content ) {
 294                              $col_class = ' class="column-' . ( $col_idx + 1 ) . '"';
 295                              $width_style = ( !empty( $output_options['column_widths'][$col_idx] ) ) ? " style=\"width:{$output_options['column_widths'][$col_idx]};\"" : '';
 296                              $cell_content = do_shortcode( $this->safe_output( $cell_content ) );
 297                              $output .= "<td{$col_class}{$width_style}>" . "{$cell_content}" . "</td>";
 298                          }
 299                          $output .= "\n\t</tr>\n";
 300                      }
 301                  } else {
 302                      $output .= "\t<tr{$row_class}>\n\t\t";
 303                      foreach( $row as $col_idx => $cell_content ) {
 304                          $col_class = ' class="column-' . ( $col_idx + 1 ) . '"';
 305                          $cell_content = do_shortcode( $this->safe_output( $cell_content ) );
 306                          $output .= "<td{$col_class}>" . "{$cell_content}" . "</td>";
 307                      }
 308                      $output .= "\n\t</tr>\n";
 309                  }
 310              }
 311              $output .= "</tbody>\n";
 312              $output .= "</table>\n";
 313  
 314              if ( true == $output_options['print_description'] )
 315                  $output .= '<span class="wp-table-reloaded-table-description">' . $this->safe_output( $table['description'] ) . "</span>\n";
 316  
 317              // if alternating row colors, we want to keep those when sorting
 318              $widgets = ( true == $output_options['alternating_row_colors'] ) ? "{widgets: ['zebra']}" : '';
 319  
 320              // eventually add this table to list of tables which will be tablesorted and thus be included in the script call in wp_footer
 321              if ( true == $output_options['use_tablesorter'] && true == $output_options['first_row_th'] ) {
 322                  // check if tablesorter is generally enabled already done
 323                  $this->tablesorter_tables[ $output_options['html_id'] ] = $widgets;
 324              }
 325              
 326          } // endif rows and cols exist
 327          
 328          return $output;
 329      }
 330  
 331      // ###################################################################################################################
 332      function safe_output( $string ) {
 333          // replace any & with &amp; that is not already an encoded entity (from function htmlentities2 in WP 2.8)
 334          $string = preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;", $string );
 335          // then we only remove slashes and change line breaks, htmlspecialchars would encode <HTML> tags which we don't want
 336          return nl2br( stripslashes( $string ) );
 337      }
 338  
 339      // ###################################################################################################################
 340      function format_datetime( $last_modified ) {
 341          return mysql2date( get_option('date_format'), $last_modified ) . ' ' . mysql2date( get_option('time_format'), $last_modified );
 342      }
 343  
 344      // ###################################################################################################################
 345      function get_last_editor( $last_editor_id ) {
 346          $user = get_userdata( $last_editor_id );
 347          return $user->nickname;
 348      }
 349  
 350      // ###################################################################################################################
 351      // enqueue jquery-js-file
 352      function add_head_jquery_js() {
 353          wp_enqueue_script( 'jquery' );
 354      }
 355  
 356      // ###################################################################################################################
 357      // load and print css-style, (only called if enabled, by wp_head-action)
 358      function add_custom_css() {
 359          // load css filename from options, if option doesnt exist, use default
 360          $css = ( isset( $this->options['custom_css'] ) ) ? $this->options['custom_css'] : '';
 361          $css = stripslashes( $css );
 362  
 363          if ( !empty( $css ) ) {
 364              echo <<<CSSSTYLE
 365  <style type="text/css" media="all">
 366  /* <![CDATA[ */
 367  {$css}
 368  /* ]]> */
 369  </style>
 370  CSSSTYLE;
 371          }
 372      }
 373  
 374      // ###################################################################################################################
 375      // output tablesorter execution js for all tables in wp_footer
 376      function output_tablesorter_js() {
 377          if ( isset( $this->options['use_tablesorter_extended'] ) && true == $this->options['use_tablesorter_extended'] )
 378              $jsfile =  'jquery.tablesorter.extended.js'; // filename of the tablesorter extended script
 379          else
 380              $jsfile =  'jquery.tablesorter.min.js'; // filename of the tablesorter script
 381  
 382          if ( 0 < count( $this->tablesorter_tables ) && file_exists( WP_TABLE_RELOADED_ABSPATH . 'js/' . $jsfile ) ) {
 383          
 384              // we have tables that shall be sortable, so we load the js
 385              wp_register_script( 'wp-table-reloaded-tablesorter-js', WP_TABLE_RELOADED_URL . 'js/' . $jsfile, array( 'jquery' ) );
 386              wp_print_scripts( 'wp-table-reloaded-tablesorter-js' );
 387  
 388              // generate the commands to make them sortable
 389              $commands = "\n";
 390              foreach ( $this->tablesorter_tables as $html_id => $widgets )
 391                  $commands .= "\t$(\"#{$html_id}\").tablesorter({$widgets});\n";
 392  
 393              // and echo the commands
 394              echo <<<JSSCRIPT
 395  <script type="text/javascript">
 396  /* <![CDATA[ */
 397  jQuery(document).ready(function($){{$commands}});
 398  /* ]]> */
 399  </script>
 400  JSSCRIPT;
 401          }
 402      }
 403  
 404  } // class WP_Table_Reloaded_Frontend
 405  
 406  ?>


Generated: Sat Dec 12 18:29:45 2009 Cross-referenced by PHPXref 0.7