From 21e4719db194c8d9e1feba990773d242a020d48d Mon Sep 17 00:00:00 2001 From: ThemeBoy Date: Tue, 3 Dec 2013 01:59:27 +1100 Subject: [PATCH] Add statistic order and priority sorting to league table --- admin/post-types/stat.php | 37 ++- admin/post-types/table.php | 83 ++++- assets/js/admin.js | 9 + sportspress-actions.php | 18 ++ ...s-helpers.php => sportspress-functions.php | 304 ------------------ sportspress.php | 4 +- 6 files changed, 130 insertions(+), 325 deletions(-) rename sportspress-helpers.php => sportspress-functions.php (73%) diff --git a/admin/post-types/stat.php b/admin/post-types/stat.php index 75476fb1..abaed77c 100644 --- a/admin/post-types/stat.php +++ b/admin/post-types/stat.php @@ -25,27 +25,48 @@ function sp_stat_edit_columns() { 'cb' => '', 'title' => __( 'Label', 'sportspress' ), 'sp_equation' => __( 'Equation', 'sportspress' ), + 'sp_order' => __( 'Sort Order', 'sportspress' ) ); return $columns; } add_filter( 'manage_edit-sp_stat_columns', 'sp_stat_edit_columns' ); function sp_stat_meta_init() { - add_meta_box( 'sp_equationdiv', __( 'Equation', 'sportspress' ), 'sp_stat_equation_meta', 'sp_stat', 'normal', 'high' ); + add_meta_box( 'sp_detailsdiv', __( 'Details', 'sportspress' ), 'sp_stat_details_meta', 'sp_stat', 'normal', 'high' ); } -function sp_stat_equation_meta( $post ) { +function sp_stat_details_meta( $post ) { $equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) ); + $order = get_post_meta( $post->ID, 'sp_order', true ); + $priority = get_post_meta( $post->ID, 'sp_priority', true ); ?> -
-

+

+

+ ID, $piece, array( 'team_event', 'result', 'outcome' ) ); + endforeach; + ?> +

+

+

+ + +

ID, 'sp_div', 0 ); $team_ids = (array)get_post_meta( $post->ID, 'sp_team', false ); - $stats = (array)get_post_meta( $post->ID, 'sp_teams', true ); + $table_stats = (array)get_post_meta( $post->ID, 'sp_teams', true ); // Equation Operating System $eos = new eqEOS(); @@ -73,10 +73,7 @@ function sp_table_stats_meta( $post ) { $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); // Get all divisions populated with stats where available - $data = sp_array_combine( $team_ids, $stats ); - - // Get equations from statistics variables - $equations = sp_get_var_equations( 'sp_stat' ); + $tempdata = sp_array_combine( $team_ids, $table_stats ); // Create entry for each team in totals $totals = array(); @@ -152,20 +149,84 @@ function sp_table_stats_meta( $post ) { endforeach; + $args = array( + 'post_type' => 'sp_stat', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC' + ); + $stats = get_posts( $args ); + + $columns = array(); + $priorities = array(); + + foreach ( $stats as $stat ): + + // Get post meta + $meta = get_post_meta( $stat->ID ); + + // Add equation to object + $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); + + // Add column name to columns + $columns[ $stat->post_name ] = $stat->post_title; + + // Add order to priorities if priority is set and does not exist in array already + $priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 ); + if ( $priority && ! array_key_exists( $priorities, $priority ) ): + $priorities[ $priority ] = array( + 'column' => $stat->post_name, + 'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' ) + ); + endif; + + endforeach; + + // Sort priorities in descending order + ksort( $priorities ); + // Fill in empty placeholder values for each team foreach ( $team_ids as $team_id ): if ( ! $team_id ) continue; - - foreach ( $equations as $key => $value ): - if ( sp_array_value( $placeholders[ $team_id ], $key, '' ) == '' ): - $placeholders[ $team_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals[ $team_id ] ); + + foreach ( $stats as $stat ): + if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ): + $placeholders[ $team_id ][ $stat->post_name ] = $eos->solveIF( str_replace( ' ', '', $stat->equation ), $totals[ $team_id ] ); endif; endforeach; endforeach; - // Get columns from statistics variables - $columns = sp_get_var_labels( 'sp_stat' ); + // Merge the data and placeholders arrays + $merged = array(); + foreach( $tempdata as $team_id => $team_data ): + foreach( $team_data as $key => $value ): + if ( $value != '' ): + $merged[ $team_id ][ $key ] = $value; + elseif ( array_key_exists( $team_id, $placeholders ) && array_key_exists( $key, $placeholders[ $team_id ] ) ): + $merged[ $team_id ][ $key ] = $placeholders[ $team_id ][ $key ]; + else: + endif; + endforeach; + endforeach; + + uasort( $merged, function( $a, $b ) use ( $priorities ) { + foreach( $priorities as $priority ): + if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ): + $output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 ); + if ( $priority['order'] == 'DESC' ) $output = 0 - $output; + return $output; + endif; + endforeach; + return 0; + }); + + // Rearrange data array to reflect statistics + $data = array(); + foreach( $merged as $key => $value ): + $data[ $key ] = $tempdata[ $key ]; + endforeach; sp_league_table( $columns, $data, $placeholders ); sp_nonce(); diff --git a/assets/js/admin.js b/assets/js/admin.js index 60b8b6b1..e3515d42 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -73,6 +73,15 @@ jQuery(document).ready(function($){ } }); + // Equation selector + $('.sp-order-selector select:first').change(function() { + if($(this).val() == '0') { + $(this).siblings().prop( 'disabled', true ); + } else { + $(this).siblings().prop( 'disabled', false ) + } + }); + // Trigger equation selector $('.sp-equation-selector select:last').change().siblings().change(); diff --git a/sportspress-actions.php b/sportspress-actions.php index 62e3fe94..1802546f 100644 --- a/sportspress-actions.php +++ b/sportspress-actions.php @@ -53,6 +53,18 @@ function sp_manage_posts_custom_column( $column, $post_id ) { get_post_meta ( $post_id, 'sp_equation', true ) ); break; + case 'sp_order': + $priority = get_post_meta ( $post_id, 'sp_priority', true ); + if ( $priority ): + echo $priority . ' ' . str_replace( + array( 'DESC', 'ASC' ), + array( '↓', '↑' ), + get_post_meta ( $post_id, 'sp_order', true ) + ); + else: + echo '—'; + endif; + break; case 'sp_player': echo sp_the_posts( $post_id, 'sp_player' ); break; @@ -166,6 +178,12 @@ function sp_save_post( $post_id ) { // Update equation as string update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); + // Update sort order as string + update_post_meta( $post_id, 'sp_priority', sp_array_value( $_POST, 'sp_priority', '0' ) ); + + // Update sort order as string + update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', 'DESC' ) ); + break; case ( 'sp_metric' ): diff --git a/sportspress-helpers.php b/sportspress-functions.php similarity index 73% rename from sportspress-helpers.php rename to sportspress-functions.php index d21e8c7e..0a8ff0fd 100644 --- a/sportspress-helpers.php +++ b/sportspress-functions.php @@ -1,22 +1,4 @@ $max_depth ) - $max_depth = $depth; - endif; - endforeach; - return $max_depth; - else: - return 0; - endif; - } -} - if ( !function_exists( 'sp_array_between' ) ) { function sp_array_between ( $array = array(), $delimiter = 0, $index = 0 ) { $keys = array_keys( $array, $delimiter ); @@ -148,16 +130,6 @@ if ( !function_exists( 'sp_dropdown_taxonomies' ) ) { } if ( !function_exists( 'sp_dropdown_pages' ) ) { - - -/* - -*/ function sp_dropdown_pages( $args = array() ) { $defaults = array( 'show_option_all' => false, @@ -417,27 +389,6 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) { } } -if ( !function_exists( 'sp_get_eos_rows' ) ) { - function sp_get_eos_rows( $raw ) { - $raw = str_replace( array( "\r\n", ' ' ), array( "\n", '' ), $raw ); - $output = explode( "\n", $raw ); - return $output; - } -} - -if ( !function_exists( 'sp_get_eos_keys' ) ) { - function sp_get_eos_keys( $raw ) { - $raw = str_replace( array( "\r\n", ' :' ), array( "\n", ':' ), $raw ); - $arr = explode( "\n", $raw ); - $output = array(); - foreach ( $arr as $value ): - $output[] = substr( $value, 0, strpos( $value, ':') ); - endforeach; - return $output; - } -} - - if ( !function_exists( 'sp_get_var_labels' ) ) { function sp_get_var_labels( $post_type, $independent = false ) { $args = array( @@ -489,261 +440,6 @@ if ( !function_exists( 'sp_get_var_equations' ) ) { } } -if ( !function_exists( 'sp_get_stats_row' ) ) { - function sp_get_stats_row( $post_id, $post_type = 'post', $args = array(), $static = false ) { - $args = array_merge( - array( - 'posts_per_page' => -1 - ), - (array)$args - ); - $posts = (array)get_posts( $args ); - - // Equation Operating System - $eos = new eqEOS(); - - $vars = array(); - - $stats_settings = get_option( 'sportspress_stats' ); - - // Get dynamic stats - switch ( $post_type ): - case 'sp_team': - - // All events attended by the team - $vars['eventsattended'] = $vars['eventsplayed'] = sizeof( $posts ); - - // Get result variables - $args = array( - 'post_type' => 'sp_result', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC' - ); - $result_vars = (array)get_posts( $args ); - - // Get outcome variables - $args = array( - 'post_type' => 'sp_outcome', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC' - ); - $outcome_vars = (array)get_posts( $args ); - - // Initialize outcome variables - foreach( $outcome_vars as $outcome_var ): - $vars[ $outcome_var->post_name ] = 0; - $vars[ $outcome_var->post_name . '_max' ] = 0; - $vars[ $outcome_var->post_name . '_min' ] = 0; - endforeach; - - // Populate each result variable - foreach( $result_vars as $result_var ): - - // Initialize and add for element to array - if ( ! array_key_exists( $result_var->post_name, $vars . '_for' ) ): - $vars[ $result_var->post_name . '_for' ] = 0; - endif; - - // Initialize and add against element to array - if ( ! array_key_exists( $result_var->post_name, $vars . '_against' ) ): - $vars[ $result_var->post_name . '_against' ] = 0; - endif; - - foreach( $posts as $event ): - - // Get match statistics - $stats = get_post_meta( $event->ID, 'sp_stats', true ); - - // Get value for the team in this match - $value = (double) sp_array_value( $stats[ $post_id ][0], $result_var->post_name, 0 ); - - // Add value for - $vars[ $result_var->post_name . '_for' ] += $value; - - // Add values against - foreach ( $stats as $team_post_id => $stat_array ): - if ( $team_post_id != $post_id ): - $vars[ $result_var->post_name . '_against' ] += sp_array_value( $stat_array[0], $result_var->post_name, 0 ); - endif; - endforeach; - - // Calculate outcome - // TODO - - // Check if max or min, and replace if it is -// if ( $value > $vars[ $result->post_name . '_max' ] ) $vars[ $result->post_name . '_max' ] = $value; -// elseif ( $value < $vars[ $result->post_name . '_min' ] ) $vars[ $result->post_name . '_min' ] = $value; - - endforeach; - - endforeach; - - // Get stats columns - $args = array( - 'post_type' => 'sp_stat', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC' - ); - $columns = (array)get_posts( $args ); - - break; - - case 'sp_player': - - // Get stats settings keys - $keys = sp_get_eos_keys( get_option( 'sp_player_stats_columns' ) ); - - // Add object properties needed for retreiving event stats - foreach( $posts as $post ): - $post->sp_player = get_post_meta( $post->ID, 'sp_team', false ); - $post->sp_player_index = array_search( $args['meta_query'][0]['value'], $post->sp_player ); - endforeach; - - // Create array of event stats columns - $columns = sp_get_eos_rows( get_option( 'sp_event_stats_columns' ) ); - foreach ( $columns as $key => $value ): - $row = explode( ':', $value ); - $var_name = strtolower( preg_replace( '~[^\p{L}]++~u', '', end( $row ) ) ); - $vars[ $var_name ] = 0; - $stats_keys[ $key ] = $var_name; - endforeach; - - // Populate columns with player stats from events - foreach ( $posts as $post ): - $team_stats = get_post_meta( $post->ID, 'sp_stats', true ); - foreach ( $team_stats as $team_id => $stat ): - if ( array_key_exists( 1, $args['meta_query'] ) && $team_id != sp_array_value( $args['meta_query'][1], 'value', 0 ) ) continue; - $player_id = sp_array_value( $args['meta_query'][0], 'value', 0 ); - if ( !array_key_exists( $player_id, $stat ) ) continue; - foreach ( $stat[ $player_id ] as $key => $value ): - if ( !array_key_exists( $key, $stats_keys ) || !array_key_exists( $stats_keys[ $key ], $vars ) ) continue; - $vars[ $stats_keys[ $key ] ] += $value; - endforeach; - endforeach; - endforeach; - - // Add appearances event count to vars - $vars['appearances'] = sizeof( $posts ); - - // Get EOS array - $rows = sp_get_eos_rows( get_option( 'sp_player_stats_columns' ) ); - - break; - - default: - - $columns = array(); - break; - - endswitch; - - // Get dynamic stats - $dynamic = array(); - foreach ( $columns as $column ): - $equation = get_post_meta( $column->ID, 'sp_equation', true ); - //$dynamic[ $column->post_name ] = $eos->solveIF( $equation, $vars ); - endforeach; - - echo '
';
-		print_r( $vars );
-		echo '
'; - - if ( $static || true ): - - // Get static stats - $static = (array)get_post_meta( $args['meta_query'][0]['value'], 'sp_stats', true ); - $table = sp_array_value( $static, 0, array() ); - if ( array_key_exists( 'tax_query', $args ) ) - $row_id = $args['tax_query'][0]['terms']; - else - $row_id = 0; - $static = sp_array_value( $table, $row_id, array() ); - - // Combine static and dynamic stats - $output = array_filter( $static ) + $dynamic; - ksort( $output ); - - else: - - $output = $dynamic; - - endif; - - return $output; - } -} - -if ( !function_exists( 'sp_stats_table' ) ) { - function sp_stats_table( $stats = array(), $placeholders = array(), $index = 0, $columns = array(), $total = true, $rowtype = 'post', $slug = 'sp_stats' ) { - global $pagenow; - if ( !is_array( $stats ) ) - $stats = array( __( 'Name', 'sportspress' ) ); - ?> - - - - - - - - - - $values ): - if ( !$key ) continue; - ?> - - - - - - - - - - - - - - - -
- name; - break; - endswitch; - if ( empty( $title ) ) - $title = __( '(no title)' ); - echo $title; - ?> -
- diff --git a/sportspress.php b/sportspress.php index 1f54875e..9e5646bb 100644 --- a/sportspress.php +++ b/sportspress.php @@ -27,8 +27,8 @@ include dirname( __FILE__ ) . '/lib/classes/eos.class.php' ; // Globals include dirname( __FILE__ ) . '/sportspress-globals.php' ; -// Helpers -require_once dirname( __FILE__ ) . '/sportspress-helpers.php'; +// Functions +require_once dirname( __FILE__ ) . '/sportspress-functions.php'; // Settings include dirname( __FILE__ ) . '/sportspress-settings.php' ;