diff --git a/admin-functions.php b/admin-functions.php index fe1ea111..78f06b02 100644 --- a/admin-functions.php +++ b/admin-functions.php @@ -535,8 +535,8 @@ if ( !function_exists( 'sportspress_edit_league_table' ) ) { } } -if ( !function_exists( 'sportspress_edit_player_table' ) ) { - function sportspress_edit_player_table( $columns = array(), $data = array(), $placeholders = array() ) { +if ( !function_exists( 'sportspress_edit_player_list_table' ) ) { + function sportspress_edit_player_list_table( $columns = array(), $data = array(), $placeholders = array() ) { ?>
@@ -628,7 +628,7 @@ if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) { } if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { - function sportspress_edit_player_statistics_table( $columns = array(), $data = array(), $placeholders = array() ) { + function sportspress_edit_player_statistics_table( $team_id, $columns = array(), $data = array(), $placeholders = array() ) { ?>
@@ -643,31 +643,23 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { $team_stats ): - if ( empty( $team_stats ) ): - ?> - - $div_stats ): - if ( !$div_id ) continue; - $div = get_term( $div_id, 'sp_season' ); - ?> - - - $label ): - $value = sportspress_array_value( $div_stats, $column, '' ); - $placeholder = sportspress_array_value( sportspress_array_value( sportspress_array_value( $placeholders, $team_id, array() ), $div_id, array() ), $column, 0 ); - ?> - - - - $div_stats ): + if ( !$div_id ) continue; + $div = get_term( $div_id, 'sp_season' ); + ?> + + + $label ): + $value = sportspress_array_value( $div_stats, $column, '' ); + $placeholder = sportspress_array_value( sportspress_array_value( $placeholders, $div_id, array() ), $column, 0 ); + ?> + + + + @@ -1232,8 +1224,8 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) { if ( $breakdown ): return array( $columns, $data, $placeholders, $merged ); else: - array_unshift( $columns, __( 'Team', 'sportspress' ) ); - $merged[0] = $columns; + $labels = array_merge( array( 'name' => __( 'Team', 'sportspress' ) ), $columns ); + $merged[0] = $labels; return $merged; endif; } @@ -1440,13 +1432,148 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { if ( $breakdown ): return array( $columns, $data, $placeholders, $merged ); else: - array_unshift( $columns, __( 'Player', 'sportspress' ) ); - $merged[0] = $columns; + $labels = array_merge( array( 'name' => __( 'Player', 'sportspress' ) ), $columns ); + $merged[0] = $labels; return $merged; endif; } } +if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { + function sportspress_get_player_statistics_data( $post_id, $team_id, $breakdown = false ) { + + $seasons = (array)get_the_terms( $post_id, 'sp_season' ); + $stats = (array)get_post_meta( $post_id, 'sp_statistics', true ); + + // Get labels from statistic variables + $statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' ); + + // Generate array of all season ids and season names + $div_ids = array(); + $season_names = array(); + foreach ( $seasons as $season ): + if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): + $div_ids[] = $season->term_id; + $season_names[ $season->term_id ] = $season->name; + endif; + endforeach; + + $tempdata = array(); + + // Get all seasons populated with stats where available + $tempdata = sportspress_array_combine( $div_ids, sportspress_array_value( $stats, $team_id, array() ) ); + + // Get equations from statistics variables + $equations = sportspress_get_var_equations( 'sp_statistic' ); + + foreach ( $div_ids as $div_id ): + + $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); + + foreach ( $statistic_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_team', + 'value' => $team_id + ), + array( + 'key' => 'sp_player', + 'value' => $post_id + ) + ), + 'tax_query' => array( + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ) + ) + ); + $events = get_posts( $args ); + foreach( $events as $event ): + $totals['eventsattended']++; + $totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar + $team_statistics = (array)get_post_meta( $event->ID, 'sp_players', true ); + if ( array_key_exists( $team_id, $team_statistics ) ): + $players = sportspress_array_value( $team_statistics, $team_id, array() ); + if ( array_key_exists( $post_id, $players ) ): + $player_statistics = sportspress_array_value( $players, $post_id, array() ); + foreach ( $player_statistics as $key => $value ): + if ( array_key_exists( $key, $totals ) ): + $totals[ $key ] += $value; + endif; + endforeach; + endif; + endif; + endforeach; + + // Generate array of placeholder values for each league + $placeholders[ $div_id ] = array(); + foreach ( $equations as $key => $value ): + + if ( empty( $value ) ): + + // Reflect totals + $placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ); + + else: + + // Calculate value + if ( sizeof( $events ) > 0 ): + $placeholders[ $div_id ][ $key ] = sportspress_solve( $value, $totals ); + else: + $placeholders[ $div_id ][ $key ] = 0; + endif; + + endif; + + endforeach; + + endforeach; + + // Get columns from statistics variables + $columns = sportspress_get_var_labels( 'sp_statistic' ); + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $season_id => $season_data ): + + // Add season name to row + $merged[ $season_id ] = array( 'name' => sportspress_array_value( $season_names, $season_id, ' ' ) ); + + foreach( $season_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $season_id, $tempdata ) && array_key_exists( $key, $tempdata[ $season_id ] ) && $tempdata[ $season_id ][ $key ] != '' ): + $merged[ $season_id ][ $key ] = $tempdata[ $season_id ][ $key ]; + else: + $merged[ $season_id ][ $key ] = $value; + endif; + + endforeach; + + endforeach; + + if ( $breakdown ): + return array( $columns, $tempdata, $placeholders, $merged ); + else: + $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ) ), $columns ); + $merged[0] = $labels; + return $merged; + endif; + + } +} + if ( !function_exists( 'sportspress_highlight_admin_menu' ) ) { function sportspress_highlight_admin_menu() { global $parent_file, $submenu_file; diff --git a/admin/hooks/the-content.php b/admin/hooks/the-content.php index d99927bd..9128ad03 100644 --- a/admin/hooks/the-content.php +++ b/admin/hooks/the-content.php @@ -7,14 +7,22 @@ function sportspress_the_content( $content ) { global $post; // Display league table - $content .= sportspress_league_table( $post->ID ); + $content = sportspress_league_table( $post->ID ) . $content; elseif ( is_singular( 'sp_list' ) && in_the_loop() ): - global $post; + global $post; // Display player list - $content .= sportspress_player_list( $post->ID ); + $content = sportspress_player_list( $post->ID ) . $content; + + + elseif ( is_singular( 'sp_player' ) && in_the_loop() ): + + global $post; + + // Display player list + $content = sportspress_player_statistics( $post->ID ) . $content; endif; diff --git a/admin/post-types/list.php b/admin/post-types/list.php index ffe6365b..cbc9d20f 100644 --- a/admin/post-types/list.php +++ b/admin/post-types/list.php @@ -85,6 +85,6 @@ function sportspress_list_stats_meta( $post ) { list( $columns, $data, $placeholders, $merged ) = sportspress_get_player_list_data( $post->ID, true ); - sportspress_edit_player_table( $columns, $data, $placeholders ); + sportspress_edit_player_list_table( $columns, $data, $placeholders ); sportspress_nonce(); } diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 810acd68..e1b89f03 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -35,13 +35,16 @@ function sportspress_player_meta_init( $post ) { $teams = (array)get_post_meta( $post->ID, 'sp_team', false ); $seasons = (array)get_the_terms( $post->ID, 'sp_season' ); + // First one is empty + unset( $teams[0] ); + remove_meta_box( 'submitdiv', 'sp_player', 'side' ); add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sp_player', 'side', 'high' ); remove_meta_box( 'postimagediv', 'sp_player', 'side' ); add_meta_box( 'postimagediv', __( 'Photo', 'sportspress' ), 'post_thumbnail_meta_box', 'sp_player', 'side', 'high' ); add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sportspress_player_team_meta', 'sp_player', 'side', 'high' ); - if ( $teams && $teams != array(0) && $seasons && is_array( $seasons ) && is_object( $seasons[0] ) ): + if ( $teams && ! empty( $teams ) && $seasons && is_array( $seasons ) && is_object( $seasons[0] ) ): add_meta_box( 'sp_statsdiv', __( 'Player Statistics', 'sportspress' ), 'sportspress_player_stats_meta', 'sp_player', 'normal', 'high' ); endif; @@ -57,31 +60,10 @@ function sportspress_player_team_meta( $post ) { function sportspress_player_stats_meta( $post ) { $team_ids = (array)get_post_meta( $post->ID, 'sp_team', false ); - $seasons = (array)get_the_terms( $post->ID, 'sp_season' ); - $stats = (array)get_post_meta( $post->ID, 'sp_statistics', true ); - - // Equation Operating System - $eos = new eqEOS(); - - // Get labels from statistic variables - $statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' ); - - // Generate array of all league ids - $div_ids = array(); - foreach ( $seasons as $key => $value ): - if ( is_object( $value ) && property_exists( $value, 'term_id' ) ) - $div_ids[] = $value->term_id; - endforeach; + // First one is empty unset( $team_ids[0] ); - if ( empty( $team_ids ) ): - ?> -

- 0, 'eventsplayed' => 0 ); - - foreach ( $statistic_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_team', - 'value' => $team_id - ), - array( - 'key' => 'sp_player', - 'value' => $post->ID - ) - ), - 'tax_query' => array( - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ) - ) - ); - $events = get_posts( $args ); - foreach( $events as $event ): - $totals['eventsattended']++; - $totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar - $team_statistics = (array)get_post_meta( $event->ID, 'sp_players', true ); - if ( array_key_exists( $team_id, $team_statistics ) ): - $players = sportspress_array_value( $team_statistics, $team_id, array() ); - if ( array_key_exists( $post->ID, $players ) ): - $player_statistics = sportspress_array_value( $players, $post->ID, array() ); - foreach ( $player_statistics as $key => $value ): - if ( array_key_exists( $key, $totals ) ): - $totals[ $key ] += $value; - endif; - endforeach; - endif; - endif; - endforeach; - - // Generate array of placeholder values for each league - $placeholders[ $team_id ][ $div_id ] = array(); - foreach ( $equations as $key => $value ): - - if ( empty( $value ) ): - - // Reflect totals - $placeholders[ $team_id ][ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ); - - else: - - // Calculate value - if ( sizeof( $events ) > 0 ): - $placeholders[ $team_id ][ $div_id ][ $key ] = sportspress_solve( $value, $totals ); - else: - $placeholders[ $team_id ][ $div_id ][ $key ] = 0; - endif; - - endif; - - endforeach; - - endforeach; - - // Get columns from statistics variables - $columns = sportspress_get_var_labels( 'sp_statistic' ); - if ( $team_num > 1 ): ?>

ID, $team_id, true ); + + sportspress_edit_player_statistics_table( $team_id, $columns, $data, $placeholders ); endforeach; } diff --git a/assets/js/sportspress.js b/assets/js/sportspress.js index faf17346..6e0e08f3 100644 --- a/assets/js/sportspress.js +++ b/assets/js/sportspress.js @@ -1,7 +1,7 @@ (function($) { - // Data tables - $(".sp-data-table").dataTable({ + // League Table Sorting + $(".sp-league-table").dataTable({ "aaSorting": [], "bAutoWidth": false, "bFilter": false, @@ -15,8 +15,43 @@ } }, "aoColumnDefs": [ - { "asSorting": [ "asc" ], "aTargets": [ 0 ] } + { "asSorting": [ "asc" ], "aTargets": [ 0 ] }, ] }); + // Player List Sorting + $(".sp-player-list").dataTable({ + "aaSorting": [], + "bAutoWidth": false, + "bFilter": false, + "bInfo": false, + "bPaginate": false, + "bSort": true, + "oLanguage": { + "oAria": { + "sSortAscending": "", + "sSortDescending": "" + } + }, + "aoColumnDefs": [ + { "asSorting": [ "asc" ], "aTargets": [ 0 ] }, + ] + }); + + // Player Statistics Sorting + $(".sp-player-statistics").dataTable({ + "aaSorting": [], + "bAutoWidth": false, + "bFilter": false, + "bInfo": false, + "bPaginate": false, + "bSort": true, + "oLanguage": { + "oAria": { + "sSortAscending": "", + "sSortDescending": "" + } + } + }); + })(jQuery); \ No newline at end of file diff --git a/functions.php b/functions.php index 7f6c45c2..0182b6c0 100644 --- a/functions.php +++ b/functions.php @@ -14,10 +14,10 @@ if ( !function_exists( 'sportspress_league_table' ) ) { $output .= ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; - $output .= '' . '' . '' . ''; + $output .= '' . '' . ''; $i = 1; @@ -68,10 +68,10 @@ if ( !function_exists( 'sportspress_player_list' ) ) { $output .= ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; - $output .= '' . '' . '' . ''; + $output .= '' . '' . ''; $i = 1; @@ -103,4 +103,55 @@ if ( !function_exists( 'sportspress_player_list' ) ) { return $output; } -} \ No newline at end of file +} + +if ( !function_exists( 'sportspress_player_statistics' ) ) { + function sportspress_player_statistics( $id ) { + + $team_ids = (array)get_post_meta( $id, 'sp_team', false ); + + // First one is empty + unset( $team_ids[0] ); + + // Loop through statistics for each team + foreach ( $team_ids as $team_id ): + + $data = sportspress_get_player_statistics_data( $id, $team_id ); + + // The first row should be column labels + $labels = $data[0]; + + // Remove the first row to leave us with the actual data + unset( $data[0] ); + + $output = '
- name; ?> -
+ name; ?> +
' . __( '#', 'sportspress' ) . '' . $label . '' . $label . '
' . __( '#', 'sportspress' ) . '' . $label . '' . $label . '
' . '' . ''; + + foreach( $labels as $key => $label ): + $output .= ''; + endforeach; + + $output .= '' . '' . ''; + + $i = 1; + + foreach( $data as $season_id => $row ): + + $output .= ''; + + foreach( $labels as $key => $value ): + $output .= ''; + endforeach; + + $output .= ''; + + endforeach; + + $output .= '' . '
' . $label . '
' . sportspress_array_value( $row, $key, '—' ) . '
'; + + + endforeach; + + return $output; + + } +}