Add player roster output

This commit is contained in:
Brian Miyaji
2014-03-14 16:35:42 +11:00
parent aeda2a69c7
commit 6ea06c6fea
7 changed files with 388 additions and 12 deletions

View File

@@ -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;

View File

@@ -61,9 +61,9 @@ function sportspress_list_format_meta( $post ) {
$format = get_post_meta( $post->ID, 'sp_format', true );
?>
<div id="post-formats-select">
<input type="radio" name="sp_format" class="post-format" id="post-format-list" value="list" <?php checked( true, ! $format || $format == 'list' ); ?>> <label for="post-format-list" class="post-format-icon post-format-list">List</label>
<br><input type="radio" name="sp_format" class="post-format" id="post-format-positions" value="positions" <?php checked( 'positions', $format ); ?>> <label for="post-format-positions" class="post-format-icon post-format-positions">Positions</label>
<br><input type="radio" name="sp_format" class="post-format" id="post-format-gallery" value="gallery" <?php checked( 'gallery', $format ); ?>> <label for="post-format-gallery" class="post-format-icon post-format-gallery">Gallery</label>
<input type="radio" name="sp_format" class="post-format" id="post-format-list" value="list" <?php checked( true, ! $format || $format == 'list' ); ?>> <label for="post-format-list" class="post-format-icon post-format-list"><?php _e( 'List', 'sportspress' ); ?></label>
<br><input type="radio" name="sp_format" class="post-format" id="post-format-roster" value="roster" <?php checked( 'roster', $format ); ?>> <label for="post-format-roster" class="post-format-icon post-format-roster"><?php _e( 'Roster', 'sportspress' ); ?></label>
<br><input type="radio" name="sp_format" class="post-format" id="post-format-gallery" value="gallery" <?php checked( 'gallery', $format ); ?>> <label for="post-format-gallery" class="post-format-icon post-format-gallery"><?php _e( 'Gallery', 'sportspress' ); ?></label>
<br>
</div>
<?php

View File

@@ -1,6 +1,6 @@
<?php
if ( !function_exists( 'sportspress_player_list' ) ) {
function sportspress_player_list( $id = null, $args = '' ) {
if ( !function_exists( 'sportspress_player_gallery' ) ) {
function sportspress_player_gallery( $id = null, $args = '' ) {
if ( ! $id )
$id = get_the_ID();
@@ -87,7 +87,7 @@ if ( !function_exists( 'sportspress_player_list' ) ) {
$output .= '</tbody>' . '</table>' . '</div>';
return apply_filters( 'sportspress_player_list', $output );
return apply_filters( 'sportspress_player_gallery', $output );
}
}

View File

@@ -0,0 +1,103 @@
<?php
if ( !function_exists( 'sportspress_player_roster' ) ) {
function sportspress_player_roster( $id = null, $args = '' ) {
if ( ! $id )
$id = get_the_ID();
$defaults = array(
'statistics' => 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 .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
// Rank or number
if ( isset( $r['orderby'] ) && $r['orderby'] != 'number' ):
$rows .= '<td class="data-rank">' . ( $i + 1 ) . '</td>';
else:
$number = get_post_meta( $player_id, 'sp_number', true );
$rows .= '<td class="data-number">' . ( $number ? $number : '&nbsp;' ) . '</td>';
endif;
// Name as link
$permalink = get_post_permalink( $player_id );
$name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', '&nbsp;' ) );
$rows .= '<td class="data-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
foreach( $labels as $key => $value ):
if ( $key == 'name' )
continue;
if ( ! is_array( $statistics ) || in_array( $key, $statistics ) )
$rows .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$rows .= '</tr>';
$i++;
endforeach;
if ( ! empty( $rows ) ):
$output .= '<h4 class="sp-table-caption">' . $position->name . '</h4>';
$output .= '<div class="sp-table-wrapper">' .
'<table class="sp-player-list sp-player-roster sp-data-table sp-responsive-table">' . '<thead>' . '<tr>';
if ( in_array( $r['orderby'], array( 'number', 'name' ) ) ):
$output .= '<th class="data-number">#</th>';
else:
$output .= '<th class="data-rank">' . __( 'Rank', 'sportspress' ) . '</th>';
endif;
foreach( $labels as $key => $label ):
if ( ! is_array( $statistics ) || $key == 'name' || in_array( $key, $statistics ) )
$output .= '<th class="data-' . $key . '">'. $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>' . $rows . '</tbody>' . '</table>' . '</div>';
endif;
endforeach;
return apply_filters( 'sportspress_player_roster', $output );
}
}

View File

@@ -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 {

View File

@@ -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 ) {

View File

@@ -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';