| [ Index ] |
PHP Cross Reference of WP-Table Reloaded 1.3 |
[Summary view] [Print] [Text view]
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.3 7 Author: Tobias Bä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 var $shown_tables = array(); 26 var $tablesorter_tables = array(); 27 28 // ################################################################################################################### 29 function WP_Table_Reloaded_Frontend() { 30 // load options and table information from database, if not available: default 31 $this->options = get_option( $this->optionname['options'], false ); 32 $this->tables = get_option( $this->optionname['tables'], false ); 33 34 if ( false === $this->options || false === $this->tables ) 35 return ''; 36 37 // front-end function, shortcode for the_content, manual filter for widget_text 38 add_shortcode( $this->shortcode, array( &$this, 'handle_content_shortcode' ) ); 39 add_filter( 'widget_text', array( &$this, 'handle_widget_filter' ) ); 40 41 // if tablesorter enabled (globally) include javascript 42 if ( true == $this->options['enable_tablesorter'] ) { 43 $this->add_head_jquery_js(); // jquery needed in any case (it's too late to do this, when shortcode is executed 44 add_action( 'wp_footer', array( &$this, 'output_tablesorter_js' ) ); // but if we actually need the tablesorter script can be determined in the footer 45 } 46 47 // if global css shall be used 48 if ( true == $this->options['use_custom_css'] ) 49 add_action( 'wp_head', array( &$this, 'add_custom_css' ) ); 50 } 51 52 // ################################################################################################################### 53 // handle [table id=<the_table_id> /] in the_content() 54 function handle_content_shortcode( $atts ) { 55 // parse shortcode attributs, only allow those specified 56 $default_atts = array( 57 'id' => 0, 58 'column_widths' => '', 59 'alternating_row_colors' => -1, 60 'first_row_th' => -1, 61 'print_name' => -1, 62 'print_description' => -1, 63 'use_tablesorter' => -1, 64 'row_offset' => 1, 65 'row_count' => null 66 ); 67 $atts = shortcode_atts( $default_atts, $atts ); 68 69 // check if table exists 70 $table_id = $atts['id']; 71 if ( !is_numeric( $table_id ) || 1 > $table_id || false == $this->table_exists( $table_id ) ) 72 return "[table \"{$table_id}\" not found /]<br />\n"; 73 74 // explode from string to array 75 $atts['column_widths'] = explode( '|', $atts['column_widths'] ); 76 77 $table = $this->load_table( $table_id ); 78 79 // determine options to use (if set in shortcode, use those, otherwise use options from "Edit Table" screen) 80 $output_options = array(); 81 foreach ( $atts as $key => $value ) { 82 // have to check this, because strings 'true' or 'false' are not recognized as boolean! 83 if ( 'true' == strtolower( $value ) ) 84 $output_options[ $key ] = true; 85 elseif ( 'false' == strtolower( $value ) ) 86 $output_options[ $key ] = false; 87 else 88 $output_options[ $key ] = ( -1 !== $value ) ? $value : $table['options'][ $key ] ; 89 } 90 91 // how often was table displayed on this page yet? get its HTML ID 92 $count = ( isset( $this->shown_tables[ $table_id ] ) ) ? $this->shown_tables[ $table['id'] ] : 0; 93 $count = $count + 1; 94 $this->shown_tables[ $table_id ] = $count; 95 $output_options['html_id'] = "wp-table-reloaded-id-{$table_id}-no-{$count}"; 96 97 $output = $this->render_table( $table, $output_options ); 98 99 return $output; 100 } 101 102 // ################################################################################################################### 103 // handle [table id=<the_table_id> /] in widget texts 104 function handle_widget_filter( $text ) { 105 // pattern to search for in widget text (only our plugin's shortcode!) 106 if ( version_compare( $GLOBALS['wp_version'], '2.8alpha', '>=') ) { 107 $pattern = '(.?)\[(' . preg_quote( $this->shortcode ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)'; 108 } else { 109 $pattern = '\[(' . preg_quote( $this->shortcode ) . ')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?'; 110 } 111 // search for it, if found, handle as if it were a shortcode 112 return preg_replace_callback( '/'.$pattern.'/s', 'do_shortcode_tag', $text ); 113 } 114 115 // ################################################################################################################### 116 // check, if given table id really exists 117 function table_exists( $table_id ) { 118 return isset( $this->tables[ $table_id ] ); 119 } 120 121 // ################################################################################################################### 122 function load_table( $table_id ) { 123 $this->tables[ $table_id ] = ( isset( $this->tables[ $table_id ] ) ) ? $this->tables[ $table_id ] : $this->optionname['table'] . '_' . $table_id; 124 $table = get_option( $this->tables[ $table_id ], array() ); 125 return $table; 126 } 127 128 // ################################################################################################################### 129 // echo content of array 130 function render_table( $table, $output_options ) { 131 // classes that will be added to <table class=...>, can be used for css-styling 132 $cssclasses = array( 'wp-table-reloaded', "wp-table-reloaded-id-{$table['id']}" ); 133 $cssclasses = implode( ' ', $cssclasses ); 134 135 // if row_offset or row_count were given, we cut that part from the table and show just that 136 if ( null === $output_options['row_count'] ) 137 $table['data'] = array_slice( $table['data'], $output_options['row_offset'] - 1 ); // -1 because we start from 1 138 else 139 $table['data'] = array_slice( $table['data'], $output_options['row_offset'] - 1, $output_options['row_count'] ); // -1 because we start from 1 140 141 $rows = count( $table['data'] ); 142 $cols = (0 < $rows) ? count( $table['data'][0] ) : 0; 143 144 // make array $shortcode_atts['column_widths'] have $cols entries 145 $output_options['column_widths'] = array_pad( $output_options['column_widths'], $cols, '' ); 146 147 $output = ''; 148 149 if ( 0 < $rows && 0 < $cols) { 150 151 if ( true == $output_options['print_name'] ) 152 $output .= '<h2 class="wp-table-reloaded-table-name">' . $this->safe_output( $table['name'] ) . "</h2>\n"; 153 154 $output .= "<table id=\"{$output_options['html_id']}\" class=\"{$cssclasses}\" cellspacing=\"1\" cellpadding=\"0\" border=\"0\">\n"; 155 156 foreach( $table['data'] as $row_idx => $row ) { 157 if ( true == $output_options['alternating_row_colors'] ) 158 $row_class = ( 1 == ($row_idx % 2) ) ? ' class="even row-' . ( $row_idx + 1 ) . '"' : ' class="odd row-' . ( $row_idx + 1 ) . '"'; 159 else 160 $row_class = ' class="row-' . ( $row_idx + 1 ) . '"'; 161 162 if( 0 == $row_idx ) { 163 if ( true == $output_options['first_row_th'] ) { 164 $output .= "<thead>\n"; 165 $output .= "\t<tr{$row_class}>\n\t\t"; 166 foreach( $row as $col_idx => $cell_content ) { 167 $col_class = ' class="column-' . ( $col_idx + 1 ) . '"'; 168 $width_style = ( !empty( $output_options['column_widths'][$col_idx] ) ) ? " style=\"width:{$output_options['column_widths'][$col_idx]};\"" : ''; 169 $cell_content = do_shortcode( $this->safe_output( $cell_content ) ); 170 $output .= "<th{$col_class}{$width_style}>" . "{$cell_content}" . "</th>"; 171 } 172 $output .= "\n\t</tr>\n"; 173 $output .= "</thead>\n"; 174 $output .= "<tbody>\n"; 175 } else { 176 $output .= "<tbody>\n"; 177 $output .= "\t<tr{$row_class}>\n\t\t"; 178 foreach( $row as $col_idx => $cell_content ) { 179 $col_class = ' class="column-' . ( $col_idx + 1 ) . '"'; 180 $width_style = ( !empty( $output_options['column_widths'][$col_idx] ) ) ? " style=\"width:{$output_options['column_widths'][$col_idx]};\"" : ''; 181 $cell_content = do_shortcode( $this->safe_output( $cell_content ) ); 182 $output .= "<td{$col_class}{$width_style}>" . "{$cell_content}" . "</td>"; 183 } 184 $output .= "\n\t</tr>\n"; 185 } 186 } else { 187 $output .= "\t<tr{$row_class}>\n\t\t"; 188 foreach( $row as $col_idx => $cell_content ) { 189 $col_class = ' class="column-' . ( $col_idx + 1 ) . '"'; 190 $cell_content = do_shortcode( $this->safe_output( $cell_content ) ); 191 $output .= "<td{$col_class}>" . "{$cell_content}" . "</td>"; 192 } 193 $output .= "\n\t</tr>\n"; 194 } 195 } 196 $output .= "</tbody>\n"; 197 $output .= "</table>\n"; 198 199 if ( true == $output_options['print_description'] ) 200 $output .= '<span class="wp-table-reloaded-table-description">' . $this->safe_output( $table['description'] ) . "</span>\n"; 201 202 // if alternating row colors, we want to keep those when sorting 203 $widgets = ( true == $output_options['alternating_row_colors'] ) ? "{widgets: ['zebra']}" : ''; 204 205 // eventually add this table to list of tables which will be tablesorted and thus be included in the script call in wp_footer 206 if ( true == $output_options['use_tablesorter'] && true == $output_options['first_row_th'] ) { 207 // check if tablesorter is generally enabled already done 208 $this->tablesorter_tables[ $output_options['html_id'] ] = $widgets; 209 } 210 211 } // endif rows and cols exist 212 213 return $output; 214 } 215 216 // ################################################################################################################### 217 function safe_output( $string ) { 218 return nl2br( stripslashes( $string ) ); 219 } 220 221 // ################################################################################################################### 222 // enqueue jquery-js-file 223 function add_head_jquery_js() { 224 wp_enqueue_script( 'jquery' ); 225 } 226 227 // ################################################################################################################### 228 // load and print css-style, (only called if enabled, by wp_head-action) 229 function add_custom_css() { 230 // load css filename from options, if option doesnt exist, use default 231 $css = ( isset( $this->options['custom_css'] ) ) ? $this->options['custom_css'] : ''; 232 $css = stripslashes( $css ); 233 234 if ( !empty( $css ) ) { 235 echo <<<CSSSTYLE 236 <style type="text/css" media="all"> 237 /* <![CDATA[ */ 238 {$css} 239 /* ]]> */ 240 </style> 241 CSSSTYLE; 242 } 243 } 244 245 // ################################################################################################################### 246 // output tablesorter execution js for all tables in wp_footer 247 function output_tablesorter_js() { 248 $jsfile = 'jquery.tablesorter.min.js'; // filename of the tablesorter script 249 250 if ( 0 < count( $this->tablesorter_tables ) && file_exists( WP_TABLE_RELOADED_ABSPATH . 'js/' . $jsfile ) ) { 251 252 // we have tables that shall be sortable, so we load the js 253 wp_register_script( 'wp-table-reloaded-tablesorter-js', WP_TABLE_RELOADED_URL . 'js/' . $jsfile, array( 'jquery' ) ); 254 wp_print_scripts( 'wp-table-reloaded-tablesorter-js' ); 255 256 // generate the commands to make them sortable 257 $commands = "\n"; 258 foreach ( $this->tablesorter_tables as $html_id => $widgets ) 259 $commands .= "\t$(\"#{$html_id}\").tablesorter({$widgets});\n"; 260 261 // and echo the commands 262 echo <<<JSSCRIPT 263 <script type="text/javascript"> 264 /* <![CDATA[ */ 265 jQuery(document).ready(function($){{$commands}}); 266 /* ]]> */ 267 </script> 268 JSSCRIPT; 269 } 270 } 271 272 } // class WP_Table_Reloaded_Frontend 273 274 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Dec 12 18:28:34 2009 | Cross-referenced by PHPXref 0.7 |