Player list basic EOS functionality
This commit is contained in:
@@ -75,45 +75,67 @@ function sp_event_team_meta( $post ) {
|
||||
function sp_event_players_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 );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
|
||||
|
||||
// Get columns from result variables
|
||||
$columns = sp_get_var_labels( 'sp_metric', true );
|
||||
|
||||
// Teams
|
||||
foreach ( $teams as $key => $team_id ):
|
||||
|
||||
// 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, $team_id, array() ) );
|
||||
if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ):
|
||||
|
||||
?>
|
||||
<div>
|
||||
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
|
||||
<?php sp_event_players_table( $columns, $data, $team_id ); ?>
|
||||
</div>
|
||||
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Teams' ); ?></strong></p>
|
||||
<?php
|
||||
|
||||
endforeach;
|
||||
else:
|
||||
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
|
||||
|
||||
// Get columns from result variables
|
||||
$columns = sp_get_var_labels( 'sp_metric', true );
|
||||
|
||||
foreach ( $teams as $key => $team_id ):
|
||||
|
||||
// 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, $team_id, array() ) );
|
||||
|
||||
?>
|
||||
<div>
|
||||
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
|
||||
<?php if ( $team_id ) sp_event_players_table( $columns, $data, $team_id ); ?>
|
||||
</div>
|
||||
<?php
|
||||
|
||||
endforeach;
|
||||
endif;
|
||||
|
||||
}
|
||||
|
||||
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 );
|
||||
|
||||
// Teams
|
||||
if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ):
|
||||
|
||||
// Get columns from result variables
|
||||
$columns = sp_get_var_labels( 'sp_result' );
|
||||
?>
|
||||
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Teams' ); ?></strong></p>
|
||||
<?php
|
||||
|
||||
// Get results for all teams
|
||||
$data = sp_array_combine( $teams, $results );
|
||||
else:
|
||||
|
||||
?>
|
||||
<div>
|
||||
<?php sp_event_results_table( $columns, $data ); ?>
|
||||
</div>
|
||||
<?php
|
||||
$results = (array)get_post_meta( $post->ID, 'sp_results', true );
|
||||
|
||||
// Get columns from result variables
|
||||
$columns = sp_get_var_labels( 'sp_result' );
|
||||
|
||||
// Get results for all teams
|
||||
$data = sp_array_combine( $teams, $results );
|
||||
|
||||
?>
|
||||
<div>
|
||||
<?php sp_event_results_table( $columns, $data ); ?>
|
||||
</div>
|
||||
<?php
|
||||
|
||||
endif;
|
||||
}
|
||||
|
||||
function sp_event_article_meta( $post ) {
|
||||
|
||||
@@ -73,41 +73,104 @@ function sp_list_player_meta( $post ) {
|
||||
}
|
||||
|
||||
function sp_list_stats_meta( $post ) {
|
||||
$players = (array)get_post_meta( $post->ID, 'sp_player', false );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
$division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
|
||||
$div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
|
||||
$team_id = get_post_meta( $post->ID, 'sp_team', true );
|
||||
$data = sp_array_combine( $players, sp_array_value( $stats, $division_id, array() ) );
|
||||
$player_ids = (array)get_post_meta( $post->ID, 'sp_player', false );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
|
||||
|
||||
// Generate array of placeholder values for each player
|
||||
$placeholders = array();
|
||||
foreach ( $players as $player ):
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'sp_player',
|
||||
'value' => $player
|
||||
)
|
||||
),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $division_id
|
||||
)
|
||||
)
|
||||
);
|
||||
$placeholders[ $player ] = sp_get_stats_row( 'sp_player', $args, true );
|
||||
// Equation Operating System
|
||||
$eos = new eqEOS();
|
||||
|
||||
// Get labels from result variables
|
||||
$metric_labels = (array)sp_get_var_labels( 'sp_metric' );
|
||||
|
||||
// Get all divisions populated with stats where available
|
||||
$data = sp_array_combine( $player_ids, $stats );
|
||||
|
||||
// Get equations from statistics variables
|
||||
$equations = sp_get_var_equations( 'sp_metric' );
|
||||
|
||||
// Create entry for each player in totals
|
||||
$totals = array();
|
||||
foreach ( $player_ids as $player_id ):
|
||||
if ( ! $player_id )
|
||||
continue;
|
||||
$totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
|
||||
|
||||
foreach ( $metric_labels as $key => $value ):
|
||||
$totals[ $player_id ][ $key ] = 0;
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
// Get column names from settings
|
||||
$stats_settings = get_option( 'sportspress_stats' );
|
||||
$columns = sp_get_eos_keys( $stats_settings['player'] );
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
'posts_per_page' => -1,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $div_id
|
||||
)
|
||||
),
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'sp_team',
|
||||
'value' => $team_id,
|
||||
)
|
||||
)
|
||||
);
|
||||
$events = get_posts( $args );
|
||||
|
||||
// Add first column label
|
||||
array_unshift( $columns, __( 'Player', 'sportspress' ) );
|
||||
// Event loop
|
||||
foreach( $events as $event ):
|
||||
|
||||
sp_stats_table( $data, $placeholders, $division_id, $columns, false );
|
||||
$teams = (array)get_post_meta( $event->ID, 'sp_players', true );
|
||||
|
||||
if ( ! array_key_exists( $team_id, $teams ) )
|
||||
continue;
|
||||
|
||||
$players = sp_array_value( $teams, $team_id, array() );
|
||||
|
||||
foreach ( $players as $player_id => $player_metrics ):
|
||||
|
||||
// Increment events played
|
||||
$totals[ $player_id ]['eventsplayed']++;
|
||||
|
||||
foreach ( $player_metrics as $key => $value ):
|
||||
|
||||
if ( array_key_exists( $key, $totals[ $player_id ] ) ):
|
||||
$totals[ $player_id ][ $key ] += $value;
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
|
||||
endforeach;
|
||||
|
||||
endforeach;
|
||||
|
||||
// Generate placeholder values for each team
|
||||
$placeholders = array();
|
||||
foreach ( $player_ids as $player_id ):
|
||||
$placeholders[ $player_id ] = array();
|
||||
foreach ( $equations as $key => $value ):
|
||||
|
||||
if ( empty( $value ) ):
|
||||
|
||||
// Reflect totals
|
||||
$placeholders[ $player_id ][ $key ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $key, 0 );
|
||||
|
||||
else:
|
||||
|
||||
// Calculate value
|
||||
$placeholders[ $player_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), sp_array_value( $totals, $player_id, array() ) );
|
||||
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
sp_player_table( $metric_labels, $data, $placeholders );
|
||||
sp_nonce();
|
||||
}
|
||||
?>
|
||||
@@ -59,6 +59,13 @@ function sp_player_stats_meta( $post ) {
|
||||
$div_ids[] = $value->term_id;
|
||||
endforeach;
|
||||
|
||||
if ( empty( $team_ids ) || $team_ids == array(0) ):
|
||||
?>
|
||||
<p><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ); ?></strong></p>
|
||||
<?php
|
||||
return;
|
||||
endif;
|
||||
|
||||
// Loop through statistics for each team
|
||||
foreach ( $team_ids as $team_id ):
|
||||
if ( !$team_id ) continue;
|
||||
|
||||
@@ -184,17 +184,17 @@ function sp_save_post( $post_id ) {
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case ( 'sp_staff' ):
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
break;
|
||||
case ( 'sp_table' ):
|
||||
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
|
||||
update_post_meta( $post_id, 'sp_teams', sp_array_value( $_POST, 'sp_teams', array() ) );
|
||||
wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_div', 0 ), 'sp_div' );
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
break;
|
||||
case ( 'sp_list' ):
|
||||
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
|
||||
update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) );
|
||||
update_post_meta( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_div', 0 ), 'sp_div' );
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) );
|
||||
|
||||
@@ -784,6 +784,46 @@ if ( !function_exists( 'sp_league_table' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_player_table' ) ) {
|
||||
function sp_player_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Player', 'sportspress' ); ?></th>
|
||||
<?php foreach ( $columns as $label ): ?>
|
||||
<th><?php echo $label; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ( $data as $player_id => $player_stats ):
|
||||
if ( !$player_id ) continue;
|
||||
$div = get_term( $player_id, 'sp_div' );
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo get_the_title( $player_id ); ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $player_stats, $column, '' );
|
||||
$placeholder = sp_array_value( sp_array_value( $placeholders, $player_id, array() ), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_players[<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach;
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_team_stats_table' ) ) {
|
||||
function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
@@ -799,24 +839,32 @@ if ( !function_exists( 'sp_team_stats_table' ) ) {
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ( $data as $div_id => $div_stats ):
|
||||
if ( !$div_id ) continue;
|
||||
$div = get_term( $div_id, 'sp_div' );
|
||||
if ( empty( $data ) ):
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo $div->name; ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $div_stats, $column, '' );
|
||||
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $div_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Division', 'sportspress' ) ); ?></strong></td>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach;
|
||||
else:
|
||||
foreach ( $data as $div_id => $div_stats ):
|
||||
if ( !$div_id ) continue;
|
||||
$div = get_term( $div_id, 'sp_div' );
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo $div->name; ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $div_stats, $column, '' );
|
||||
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $div_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach;
|
||||
endif;
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -840,6 +888,12 @@ if ( !function_exists( 'sp_player_metrics_table' ) ) {
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ( $data as $team_id => $team_stats ):
|
||||
if ( empty( $team_stats ) ):
|
||||
?>
|
||||
<td><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Team', 'sportspress' ) ); ?></strong></td>
|
||||
<?php
|
||||
continue;
|
||||
endif;
|
||||
foreach ( $team_stats as $div_id => $div_stats ):
|
||||
if ( !$div_id ) continue;
|
||||
$div = get_term( $div_id, 'sp_div' );
|
||||
@@ -968,56 +1022,6 @@ if ( !function_exists( 'sp_event_players_table' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_someother_table' ) ) {
|
||||
function sp_someother_table( $columns = array(), $data = array(), $placeholders = array(), $team_id ) {
|
||||
?>
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Player', 'sportspress' ); ?></th>
|
||||
<?php foreach ( $columns as $label ): ?>
|
||||
<th><?php echo $label; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ( $data as $player_id => $player_metrics ):
|
||||
if ( !$player_id ) continue;
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo get_the_title( $player_id ); ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $player_metrics, $column, '' );
|
||||
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $player_id, 0), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach;
|
||||
$total_results = sp_array_value( $data, 0, array() );
|
||||
?>
|
||||
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$player_id = 0;
|
||||
$value = sp_array_value( $player_metrics, $column, '' );
|
||||
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $player_id, 0), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_post_adder' ) ) {
|
||||
function sp_post_adder( $meta = 'post' ) {
|
||||
$obj = get_post_type_object( $meta );
|
||||
|
||||
Reference in New Issue
Block a user