[ Index ]

PHP Cross Reference of WP-Table Reloaded 0.9.2

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/
   5  Description: This plugin allows you to create and manage tables in the admin-area of WordPress. You can then show them in your posts or on your pages by using a shortcode. The plugin is greatly influenced by the plugin "WP-Table" by Alex Rabe, but was completely rewritten and uses the state-of-the-art WordPress techniques which makes it faster and lighter than the original plugin.
   6  Version: 0.9.2
   7  Author: Tobias B&auml;thge
   8  Author URI: http://tobias.baethge.com/
   9  */
  10  
  11  class WP_Table_Reloaded_Frontend {
  12  
  13      // ###################################################################################################################
  14      // plugin variables
  15      var $options = array();
  16      var $tables = array();
  17  
  18      var $optionname = array(
  19          'tables' => 'wp_table_reloaded_tables',
  20          'options' => 'wp_table_reloaded_options',
  21          'table' => 'wp_table_reloaded_data'
  22      );
  23      var $shortcode = 'table';
  24  
  25      // ###################################################################################################################
  26      function WP_Table_Reloaded_Frontend() {
  27          // load options and table information from database, if not available: default
  28          $this->options = get_option( $this->optionname['options'], false );
  29          $this->tables = get_option( $this->optionname['tables'], false );
  30  
  31          if ( false === $this->options || false === $this->tables )
  32              return '';
  33  
  34          // front-end function
  35          add_shortcode( $this->shortcode, array( &$this, 'handle_shortcode' ) );
  36  
  37          // if tablesorter enabled (globally) include javascript
  38          if ( true == $this->options['enable_tablesorter'] )
  39              $this->add_head_tablesorter_js();
  40  
  41          // if global css shall be used
  42          if ( true == $this->options['use_global_css'] )
  43              $this->add_head_global_css();
  44      }
  45  
  46      // ###################################################################################################################
  47      // handle [table id=<the_table_id> /]
  48      function handle_shortcode( $attr ) {
  49          $table_id = $attr['id'];
  50  
  51          if ( !is_numeric( $table_id ) || 1 > $table_id)
  52              return '';
  53  
  54          $table = $this->load_table( $table_id );
  55  
  56          $output = $this->render_table( $table );
  57  
  58          return $output;
  59      }
  60  
  61      // ###################################################################################################################
  62      function load_table( $table_id ) {
  63          $this->tables[ $table_id ] = ( isset( $this->tables[ $table_id ] ) ) ? $this->tables[ $table_id ] : $this->optionname['table'] . '_' . $table_id;
  64          $table = get_option( $this->tables[ $table_id ], $this->default_table);
  65          return $table;
  66      }
  67  
  68      // ###################################################################################################################
  69      // echo content of array
  70      function render_table( $table ) {
  71          // classes that will be added to <table class=...>, can be used for css-styling
  72          $cssclasses = array( 'wp-table-reloaded', "wp-table-reloaded-id-{$table['id']}" );
  73          $cssclasses = implode( ' ', $cssclasses );
  74  
  75          $rows = count( $table['data'] );
  76          $cols = (0 < $rows) ? count( $table['data'][0] ) : 0;
  77  
  78          $output = '';
  79  
  80          if ( 0 < $rows && 0 < $cols) {
  81          
  82              if ( true == $table['options']['print_name'] )
  83                  $output .= '<h2 class="wp-table-reloaded-table-name">' . $this->safe_output( $table['name'] ) . "</h2>\n";
  84          
  85              $output .= "<table class=\"{$cssclasses}\" cellspacing=\"1\" cellpadding=\"0\" border=\"0\">\n";
  86  
  87              foreach( $table['data'] as $row_idx => $row ) {
  88                  if ( true == $table['options']['alternating_row_colors'] )
  89                      $row_class = ( 1 == ($row_idx % 2) ) ? ' class="even"' : ' class="odd"';
  90                  if( 0 == $row_idx ) {
  91                      if ( true == $table['options']['first_row_th'] ) {
  92                          $output .= "<thead>\n";
  93                          $output .= "\t<tr{$row_class}>\n\t\t";
  94                          foreach( $row as $col_idx => $cell_content ) {
  95                              $cell_content = $this->safe_output( $cell_content );
  96                              $output .= "<th>" . "{$cell_content}" . "</th>";
  97                          }
  98                          $output .= "\n\t</tr>\n";
  99                          $output .= "</thead>\n";
 100                          $output .= "<tbody>\n";
 101                      } else {
 102                          $output .= "<tbody>\n";
 103                          $output .= "\t<tr{$row_class}>\n\t\t";
 104                          foreach( $row as $col_idx => $cell_content ) {
 105                              $cell_content = $this->safe_output( $cell_content );
 106                              $output .= "<td>" . "{$cell_content}" . "</td>";
 107                          }
 108                          $output .= "\n\t</tr>\n";
 109                      }
 110                  } else {
 111                      $output .= "\t<tr{$row_class}>\n\t\t";
 112                      foreach( $row as $col_idx => $cell_content ) {
 113                          $cell_content = $this->safe_output( $cell_content );
 114                          $output .= "<td>" . "{$cell_content}" . "</td>";
 115                      }
 116                      $output .= "\n\t</tr>\n";
 117                  }
 118              }
 119              $output .= "</tbody>\n";
 120              $output .= "</table>\n";
 121  
 122              if ( true == $table['options']['print_description'] )
 123                  $output .= '<span class="wp-table-reloaded-table-description">' . $this->safe_output( $table['description'] ) . "</span>\n";
 124  
 125              $widgets = ( true == $table['options']['alternating_row_colors'] ) ? "{widgets: ['zebra']}" : '';
 126              if ( true == $table['options']['use_tablesorter'] ) {
 127                  $output .= <<<JSSCRIPT
 128  <script type="text/javascript">
 129  jQuery(document).ready(function($){
 130      $(".wp-table-reloaded-id-{$table['id']}").tablesorter({$widgets});
 131  });
 132  </script>
 133  JSSCRIPT;
 134              }
 135          }
 136          return $output;
 137      }
 138  
 139      // ###################################################################################################################
 140      function safe_output( $string ) {
 141          return stripslashes( $string );
 142      }
 143      
 144      // ###################################################################################################################
 145      // enqueue tablesorter-js-file, if it exists
 146      function add_head_tablesorter_js() {
 147          $jsfile =  'jquery.tablesorter.min.js';
 148          if ( file_exists( dirname ( __FILE__ ) . '/js/' . $jsfile ) ) {
 149              wp_enqueue_script( 'wp-table-reloaded-tablesorter-js', WP_PLUGIN_URL . '/' . basename( dirname( __FILE__ ) ) . '/js/' . $jsfile, array( 'jquery' ) );
 150          }
 151      }
 152      // ###################################################################################################################
 153      // enqueue tablesorter-css-file, if it exists, may be modified by user
 154      function add_head_global_css() {
 155          $cssfile =  'global-frontend-style.css';
 156          if ( file_exists( dirname ( __FILE__ ) . '/css/' . $cssfile ) ) {
 157              wp_enqueue_style( 'wp-table-reloaded-global-css', WP_PLUGIN_URL . '/' . basename( dirname( __FILE__ ) ) . '/css/' . $cssfile );
 158          }
 159      }
 160  
 161  } // class WP_Table_Reloaded_Frontend
 162  
 163  ?>


Generated: Sat Dec 12 18:19:09 2009 Cross-referenced by PHPXref 0.7