From 9fee1468c31f591f50d4e2bf9cc1e933881ed3cb Mon Sep 17 00:00:00 2001 From: Takumi Date: Fri, 23 Aug 2013 00:53:31 +1000 Subject: [PATCH] Let us reference event vars in team stats --- actions.php | 29 +++++++++++++++++++++++------ event.php | 36 +++++++++++++++++++++++++++++++++++- helpers.php | 34 ++++++++++++++++++++++++++++++---- sportspress-admin.css | 4 +++- table.php | 9 ++++++++- 5 files changed, 99 insertions(+), 13 deletions(-) diff --git a/actions.php b/actions.php index cd755c2c..4090caa8 100644 --- a/actions.php +++ b/actions.php @@ -105,16 +105,33 @@ function sp_save_post( $post_id ) { update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); break; case ( 'sp_event' ): - update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + + // Get results + $results = (array)sp_array_value( $_POST, 'sp_results', array() ); + + // Update results + update_post_meta( $post_id, 'sp_results', $results ); + + // Delete result values delete_post_meta( $post_id, 'sp_result'); - $stats = (array)sp_array_value( $_POST, 'sp_stats', array() ); - foreach ( $stats as $table ) { - $total = sp_array_value( sp_array_value( $table, 0, null ), 0, null ); - add_post_meta( $post_id, 'sp_result', $total ); - } + + // Add result values for each team (first column of results table) + $teams = (array)sp_array_value( $results, 0, null ); + foreach ( $teams as $team ): + add_post_meta( $post_id, 'sp_result', sp_array_value( $team, 0, null ) ); + endforeach; + + // Update stats + update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + + // Update team array sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); + + // Update player array sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) ); + break; + case ( 'sp_player' ): update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); diff --git a/event.php b/event.php index 51bf7c2e..b338bc0a 100644 --- a/event.php +++ b/event.php @@ -31,6 +31,7 @@ function sp_event_meta_init() { add_meta_box( 'submitdiv', __( 'Event', 'sportspress' ), 'post_submit_meta_box', 'sp_event', 'side', 'high' ); add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sp_event_team_meta', 'sp_event', 'side', 'high' ); add_meta_box( 'sp_articlediv', __( 'Article', 'sportspress' ), 'sp_event_article_meta', 'sp_event', 'normal', 'high' ); + add_meta_box( 'sp_resultsdiv', __( 'Results', 'sportspress' ), 'sp_event_results_meta', 'sp_event', 'normal', 'high' ); add_meta_box( 'sp_statsdiv', __( 'Statistics', 'sportspress' ), 'sp_event_stats_meta', 'sp_event', 'normal', 'high' ); } @@ -73,21 +74,54 @@ function sp_event_article_meta( $post ) { wp_editor( $post->post_content, 'content' ); } +function sp_event_results_meta( $post ) { + $limit = get_option( 'sp_event_team_count' ); + $teams = array_pad( array_slice( (array)get_post_meta( $post->ID, 'sp_team', false ), 0, $limit ), $limit, 0 ); + $results = (array)get_post_meta( $post->ID, 'sp_results', true ); + + // Get results for all teams + $data = sp_array_combine( $teams, sp_array_value( $results, 0, array() ) ); + + // Get column names from settings + $stats_settings = get_option( 'sportspress_stats' ); + $columns = sp_get_eos_keys( $stats_settings['event'] ); + + // Add first column label + array_unshift( $columns, __( 'Team', 'sportspress' ) ); + + ?> +
+ +
+ ID, 'sp_team', false ), 0, $limit ), $limit, 0 ); $stats = (array)get_post_meta( $post->ID, 'sp_stats', true ); + // Get column names from settings + $stats_settings = get_option( 'sportspress_stats' ); + $columns = sp_get_eos_keys( $stats_settings['player'] ); + + // Add first column label + array_unshift( $columns, __( 'Player', 'sportspress' ) ); + // Teams foreach ( $teams as $key => $value ): + + // Get results for players in the team $players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key ); $data = sp_array_combine( $players, sp_array_value( $stats, $value, array() ) ); + ?>

- +
$value ): + $column = explode( ':', $value ); + $var_name = preg_replace("/[^A-Za-z0-9 ]/", '', sp_array_value( $column, 1 ) ); + $results[] = $var_name; + $vars[ $var_name ] = 0; + endforeach; + // Add object properties needed for retreiving event stats foreach( $posts as $post ): $post->sp_team = get_post_meta( $post->ID, 'sp_team', false ); $post->sp_team_index = array_search( $args['meta_query'][0]['value'], $post->sp_team ); $post->sp_result = get_post_meta( $post->ID, 'sp_result', false ); + $post->sp_results = sp_array_value( sp_array_value( sp_array_value( get_post_meta( $post->ID, 'sp_results', false ), 0, array() ), 0, array() ), $args['meta_query'][0]['value'], array() ); + foreach( $results as $key => $value ): + if ( !array_key_exists( $value, $vars ) ) $vars[ $value ] = 0; + $vars[ $value ] += sp_array_value( $post->sp_results, $key, 0 ); + endforeach; endforeach; // Get team stats from all attended events $vars['appearances'] = sizeof( $posts ); - $vars['wins'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); - $vars['ties'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) == 1; } ) ); - $vars['losses'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); + $vars['greater'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); + $vars['equal'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) == 1; } ) ); + $vars['less'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); $vars['for'] = 0; foreach( $posts as $post ): $vars['for'] += $post->sp_result[ $post->sp_team_index ]; endforeach; $vars['against'] = 0; foreach( $posts as $post ): $result = $post->sp_result; unset( $result[ $post->sp_team_index ] ); $vars['against'] += array_sum( $result ); endforeach; - + // Get EOS array $rows = sp_get_eos_array( $stats_settings['team'] ); break; case 'sp_player': + // Get stats settings keys + $keys = sp_get_eos_keys( $stats_settings['player'] ); + + // 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_array( get_option( 'sp_event_stats_columns' ) ); foreach ( $columns as $key => $value ): diff --git a/sportspress-admin.css b/sportspress-admin.css index bef8f67a..21d870e1 100644 --- a/sportspress-admin.css +++ b/sportspress-admin.css @@ -98,7 +98,9 @@ white-space: nowrap; } #sp_statsdiv .widefat input[type="text"], -#sp_statsdiv .widefat input[type="number"] { +#sp_statsdiv .widefat input[type="number"], +#sp_resultsdiv .widefat input[type="text"], +#sp_resultsdiv .widefat input[type="number"] { min-width: 14px; width: 100%; } diff --git a/table.php b/table.php index 17fff960..c0716cab 100644 --- a/table.php +++ b/table.php @@ -85,6 +85,13 @@ function sp_table_stats_meta( $post ) { $placeholders[ $team ] = sp_get_stats_row( 'sp_team', $args, true ); endforeach; - sp_stats_table( $data, $placeholders, $league_id, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false ); + // Get column names from settings + $stats_settings = get_option( 'sportspress_stats' ); + $columns = sp_get_eos_keys( $stats_settings['team'] ); + + // Add first column label + array_unshift( $columns, __( 'Team', 'sportspress' ) ); + + sp_stats_table( $data, $placeholders, $league_id, $columns, false ); } ?> \ No newline at end of file