From 6ea06c6feadbfd0be75db32568d28c74b68e87d5 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Fri, 14 Mar 2014 16:35:42 +1100 Subject: [PATCH] Add player roster output --- admin/hooks/the-content.php | 14 +- admin/post-types/list.php | 6 +- admin/templates/player-gallery.php | 6 +- admin/templates/player-roster.php | 103 ++++++++++++ assets/css/admin.css | 8 +- functions.php | 259 +++++++++++++++++++++++++++++ sportspress.php | 4 +- 7 files changed, 388 insertions(+), 12 deletions(-) create mode 100644 admin/templates/player-roster.php diff --git a/admin/hooks/the-content.php b/admin/hooks/the-content.php index 4dfbde0c..fd20a56f 100644 --- a/admin/hooks/the-content.php +++ b/admin/hooks/the-content.php @@ -70,7 +70,19 @@ add_filter( 'the_content', 'sportspress_default_player_content' ); function sportspress_default_list_content( $content ) { if ( is_singular( 'sp_list' ) && in_the_loop() ): - $list = sportspress_player_list( get_the_ID() ); + $id = get_the_ID(); + $format = get_post_meta( $id, 'sp_format', true ); + switch ( $format ): + case 'roster': + $list = sportspress_player_roster( $id ); + break; + case 'gallery': + $list = sportspress_player_gallery( $id ); + break; + default: + $list = sportspress_player_list( $id ); + break; + endswitch; $content = $list . $content; endif; return $content; diff --git a/admin/post-types/list.php b/admin/post-types/list.php index 674dea43..2371a4ef 100644 --- a/admin/post-types/list.php +++ b/admin/post-types/list.php @@ -61,9 +61,9 @@ function sportspress_list_format_meta( $post ) { $format = get_post_meta( $post->ID, 'sp_format', true ); ?>
- > -
> -
> + > +
> +
>
' . '' . ''; - return apply_filters( 'sportspress_player_list', $output ); + return apply_filters( 'sportspress_player_gallery', $output ); } } diff --git a/admin/templates/player-roster.php b/admin/templates/player-roster.php new file mode 100644 index 00000000..406e1dfd --- /dev/null +++ b/admin/templates/player-roster.php @@ -0,0 +1,103 @@ + null, + 'orderby' => 'default', + 'order' => 'ASC', + ); + + $r = wp_parse_args( $args, $defaults ); + + $output = ''; + + $data = sportspress_get_player_roster_data( $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] ); + + $statistics = sportspress_array_value( $r, 'statistics', null ); + + if ( $r['orderby'] == 'default' ): + $r['orderby'] = get_post_meta( $id, 'sp_orderby', true ); + $r['order'] = get_post_meta( $id, 'sp_order', true ); + else: + global $sportspress_statistic_priorities; + $sportspress_statistic_priorities = array( + array( + 'statistic' => $r['orderby'], + 'order' => $r['order'], + ), + ); + uasort( $data, 'sportspress_sort_list_players' ); + endif; + + $positions = get_terms ( 'sp_position' ); + + foreach ( $positions as $position ): + $rows = ''; + $i = 0; + + foreach ( $data as $player_id => $row ): + + if ( ! in_array( $position->term_id, $row['positions']) ) + continue; + + $rows .= ''; + + // Rank or number + if ( isset( $r['orderby'] ) && $r['orderby'] != 'number' ): + $rows .= '' . ( $i + 1 ) . ''; + else: + $number = get_post_meta( $player_id, 'sp_number', true ); + $rows .= '' . ( $number ? $number : ' ' ) . ''; + endif; + + // Name as link + $permalink = get_post_permalink( $player_id ); + $name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', ' ' ) ); + $rows .= '' . '' . $name . ''; + + foreach( $labels as $key => $value ): + if ( $key == 'name' ) + continue; + if ( ! is_array( $statistics ) || in_array( $key, $statistics ) ) + $rows .= '' . sportspress_array_value( $row, $key, '—' ) . ''; + endforeach; + + $rows .= ''; + + $i++; + + endforeach; + + if ( ! empty( $rows ) ): + $output .= '

' . $position->name . '

'; + $output .= '
' . + '' . '' . ''; + if ( in_array( $r['orderby'], array( 'number', 'name' ) ) ): + $output .= ''; + else: + $output .= ''; + endif; + + foreach( $labels as $key => $label ): + if ( ! is_array( $statistics ) || $key == 'name' || in_array( $key, $statistics ) ) + $output .= ''; + endforeach; + $output .= '' . '' . '' . $rows . '' . '
#' . __( 'Rank', 'sportspress' ) . ''. $label . '
' . '
'; + endif; + + endforeach; + + return apply_filters( 'sportspress_player_roster', $output ); + + } +} diff --git a/assets/css/admin.css b/assets/css/admin.css index 04098c73..f53f9ca5 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -74,12 +74,12 @@ content: "\f328"; } -.post-state-format.post-format-list:before, .post-format-icon.post-format-list:before, a.post-state-format.format-list:before { - content: "\f163"; +.post-state-format.post-format-roster:before, .post-format-icon.post-format-roster:before, a.post-state-format.format-roster:before { + content: "\f307"; } -.post-state-format.post-format-positions:before, .post-format-icon.post-format-positions:before, a.post-state-format.format-positions:before { - content: "\f164"; +.post-state-format.post-format-list:before, .post-format-icon.post-format-list:before, a.post-state-format.format-list:before { + content: "\f163"; } .widefat .column-sp_team strong { diff --git a/functions.php b/functions.php index 8fa20a3d..cd18a25a 100644 --- a/functions.php +++ b/functions.php @@ -2217,6 +2217,265 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { } } +if ( !function_exists( 'sportspress_get_player_roster_data' ) ) { + function sportspress_get_player_roster_data( $post_id, $admin = false ) { + $league_id = sportspress_get_the_term_id( $post_id, 'sp_league', 0 ); + $div_id = sportspress_get_the_term_id( $post_id, 'sp_season', 0 ); + $team_id = get_post_meta( $post_id, 'sp_team', true ); + $player_ids = (array)get_post_meta( $post_id, 'sp_player', false ); + $stats = (array)get_post_meta( $post_id, 'sp_players', true ); + $orderby = get_post_meta( $post_id, 'sp_orderby', true ); + $order = get_post_meta( $post_id, 'sp_order', true ); + + // Get labels from result variables + $columns = (array)sportspress_get_var_labels( 'sp_statistic' ); + + // Get all leagues populated with stats where available + $tempdata = sportspress_array_combine( $player_ids, $stats ); + + // Create entry for each player in totals + $totals = array(); + $placeholders = array(); + + foreach ( $player_ids as $player_id ): + if ( ! $player_id ) + continue; + + $positions = get_the_terms( $player_id, 'sp_position' ); + $position_ids = array(); + foreach ( $positions as $position ): + $position_ids[] = $position->term_id; + endforeach; + + $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); + + foreach ( $columns as $key => $value ): + $totals[ $player_id ][ $key ] = 0; + endforeach; + + // Get static statistics + $static = get_post_meta( $player_id, 'sp_statistics', true ); + + // Create placeholders entry for the player + $placeholders[ $player_id ] = array( 'eventsplayed' => 0, 'positions' => $position_ids ); + + // Add static statistics to placeholders + if ( is_array( $static ) && array_key_exists( $league_id, $static ) && array_key_exists( $div_id, $static[ $league_id ] ) ): + $placeholders[ $player_id ] = array_merge( $placeholders[ $player_id ], $static[ $league_id ][ $div_id ] ); + endif; + endforeach; + + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ), + ), + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_team', + 'compare' => 'EXISTS', + ), + ), + ); + + if ( $team_id ): + $args['meta_query'] = array( + array( + 'key' => 'sp_team', + 'value' => $team_id, + ), + ); + endif; + + $events = get_posts( $args ); + + // Event loop + foreach( $events as $event ): + + $teams = (array)get_post_meta( $event->ID, 'sp_players', true ); + + if ( $team_id ): + + if ( ! array_key_exists( $team_id, $teams ) ) + continue; + + $players = sportspress_array_value( $teams, $team_id, array() ); + + foreach ( $players as $player_id => $player_statistics ): + + if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) + continue; + + // Increment events played + if ( sportspress_array_value( $player_statistics, 'status' ) != 'sub' || sportspress_array_value( $player_statistics, 'sub', 0 ) ): + $totals[ $player_id ]['eventsplayed']++; + endif; + + foreach ( $player_statistics as $key => $value ): + + if ( array_key_exists( $key, $totals[ $player_id ] ) ): + $totals[ $player_id ][ $key ] += $value; + endif; + + endforeach; + + endforeach; + + else: + + foreach ( $teams as $players ): + + foreach ( $players as $player_id => $player_statistics ): + + if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) + continue; + + // Increment events played + if ( sportspress_array_value( $player_statistics, 'status' ) != 'sub' || sportspress_array_value( $player_statistics, 'sub', 0 ) ): + $totals[ $player_id ]['eventsplayed']++; + endif; + + foreach ( $player_statistics as $key => $value ): + + if ( array_key_exists( $key, $totals[ $player_id ] ) ): + $totals[ $player_id ][ $key ] += $value; + endif; + + endforeach; + + endforeach; + + endforeach; + + endif; + + endforeach; + + $args = array( + 'post_type' => 'sp_statistic', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC', + ); + $statistics = get_posts( $args ); + + $columns = array( 'eventsplayed' => __( 'Played', 'sportspress' ) ); + + foreach ( $statistics as $statistic ): + + // Get post meta + $meta = get_post_meta( $statistic->ID ); + + // Add equation to object + $statistic->equation = sportspress_array_value( sportspress_array_value( $meta, 'sp_equation', array() ), 0, 0 ); + + // Add column name to columns + $columns[ $statistic->post_name ] = $statistic->post_title; + + endforeach; + + // Fill in empty placeholder values for each player + foreach ( $player_ids as $player_id ): + + if ( ! $player_id ) + continue; + + // Add events played as an object to statistics for placeholder calculations + $epstat = new stdClass(); + $epstat->post_name = 'eventsplayed'; + array_unshift( $statistics, $epstat ); + + foreach ( $statistics as $statistic ): + if ( sportspress_array_value( $placeholders[ $player_id ], $statistic->post_name, '' ) == '' ): + + if ( $statistic->post_name == 'eventsplayed' ): + $calculate = 'total'; + else: + $calculate = get_post_meta( $statistic->ID, 'sp_calculate', true ); + endif; + + if ( $calculate && $calculate == 'average' ): + + // Reflect average + $eventsplayed = (int)sportspress_array_value( $totals[ $player_id ], 'eventsplayed', 0 ); + if ( ! $eventsplayed ): + $placeholders[ $player_id ][ $statistic->post_name ] = 0; + else: + $placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ) / $eventsplayed; + endif; + + else: + + // Reflect total + $placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ); + + endif; + + endif; + endforeach; + endforeach; + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $player_id => $player_data ): + + // Add player name to row + $merged[ $player_id ] = array( 'number' => get_post_meta( $player_id, 'sp_number', true ), 'name' => get_the_title( $player_id ), 'eventsplayed' => 0 ); + + foreach( $player_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ): + $merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ]; + else: + $merged[ $player_id ][ $key ] = $value; + endif; + + endforeach; + endforeach; + + if ( $orderby != 'number' || $order != 'ASC' ): + global $sportspress_statistic_priorities; + $sportspress_statistic_priorities = array( + array( + 'statistic' => $orderby, + 'order' => $order, + ), + ); + uasort( $merged, 'sportspress_sort_list_players' ); + endif; + + // Rearrange data array to reflect statistics + $data = array(); + foreach( $merged as $key => $value ): + $data[ $key ] = $tempdata[ $key ]; + endforeach; + + if ( $admin ): + return array( $columns, $data, $placeholders, $merged ); + else: + $labels = array_merge( array( 'name' => __( 'Player', 'sportspress' ) ), $columns ); + $merged[0] = $labels; + return $merged; + endif; + } +} + if ( !function_exists( 'sportspress_sort_list_players' ) ) { function sportspress_sort_list_players ( $a, $b ) { diff --git a/sportspress.php b/sportspress.php index c3fdac46..68666a59 100644 --- a/sportspress.php +++ b/sportspress.php @@ -46,6 +46,8 @@ require_once dirname( __FILE__ ) . '/admin/templates/events-calendar.php'; require_once dirname( __FILE__ ) . '/admin/templates/league-table.php'; require_once dirname( __FILE__ ) . '/admin/templates/player-league-statistics.php'; require_once dirname( __FILE__ ) . '/admin/templates/player-list.php'; +require_once dirname( __FILE__ ) . '/admin/templates/player-roster.php'; +require_once dirname( __FILE__ ) . '/admin/templates/player-gallery.php'; require_once dirname( __FILE__ ) . '/admin/templates/player-metrics.php'; require_once dirname( __FILE__ ) . '/admin/templates/player-statistics.php'; require_once dirname( __FILE__ ) . '/admin/templates/team-columns.php'; @@ -70,7 +72,7 @@ require_once dirname( __FILE__ ) . '/admin/post-types/table.php'; require_once dirname( __FILE__ ) . '/admin/post-types/player.php'; require_once dirname( __FILE__ ) . '/admin/post-types/list.php'; require_once dirname( __FILE__ ) . '/admin/post-types/staff.php'; -//require_once dirname( __FILE__ ) . '/admin/post-types/sponsor.php'; +require_once dirname( __FILE__ ) . '/admin/post-types/sponsor.php'; // Terms require_once dirname( __FILE__ ) . '/admin/terms/league.php';