Player list basic EOS functionality

This commit is contained in:
ThemeBoy
2013-12-02 05:04:29 +11:00
parent 21cf90be9c
commit 59f0d80277
5 changed files with 219 additions and 123 deletions

View File

@@ -75,45 +75,67 @@ function sp_event_team_meta( $post ) {
function sp_event_players_meta( $post ) { function sp_event_players_meta( $post ) {
$limit = get_option( 'sp_event_team_count' ); $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 ); $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 // Teams
foreach ( $teams as $key => $team_id ): if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ):
// 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' ), 'Teams' ); ?></strong></p>
<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>
<?php <?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 ) { function sp_event_results_meta( $post ) {
$limit = get_option( 'sp_event_team_count' ); $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 ); $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 else:
$data = sp_array_combine( $teams, $results );
?> $results = (array)get_post_meta( $post->ID, 'sp_results', true );
<div>
<?php sp_event_results_table( $columns, $data ); ?> // Get columns from result variables
</div> $columns = sp_get_var_labels( 'sp_result' );
<?php
// 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 ) { function sp_event_article_meta( $post ) {

View File

@@ -73,41 +73,104 @@ function sp_list_player_meta( $post ) {
} }
function sp_list_stats_meta( $post ) { function sp_list_stats_meta( $post ) {
$players = (array)get_post_meta( $post->ID, 'sp_player', false ); $div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
$division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
$team_id = get_post_meta( $post->ID, 'sp_team', true ); $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 // Equation Operating System
$placeholders = array(); $eos = new eqEOS();
foreach ( $players as $player ):
$args = array( // Get labels from result variables
'post_type' => 'sp_event', $metric_labels = (array)sp_get_var_labels( 'sp_metric' );
'meta_query' => array(
array( // Get all divisions populated with stats where available
'key' => 'sp_player', $data = sp_array_combine( $player_ids, $stats );
'value' => $player
) // Get equations from statistics variables
), $equations = sp_get_var_equations( 'sp_metric' );
'tax_query' => array(
array( // Create entry for each player in totals
'taxonomy' => 'sp_div', $totals = array();
'field' => 'id', foreach ( $player_ids as $player_id ):
'terms' => $division_id if ( ! $player_id )
) continue;
) $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
);
$placeholders[ $player ] = sp_get_stats_row( 'sp_player', $args, true ); foreach ( $metric_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
endforeach;
endforeach; endforeach;
// Get column names from settings $args = array(
$stats_settings = get_option( 'sportspress_stats' ); 'post_type' => 'sp_event',
$columns = sp_get_eos_keys( $stats_settings['player'] ); '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 // Event loop
array_unshift( $columns, __( 'Player', 'sportspress' ) ); 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();
} }
?> ?>

View File

@@ -59,6 +59,13 @@ function sp_player_stats_meta( $post ) {
$div_ids[] = $value->term_id; $div_ids[] = $value->term_id;
endforeach; 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 // Loop through statistics for each team
foreach ( $team_ids as $team_id ): foreach ( $team_ids as $team_id ):
if ( !$team_id ) continue; if ( !$team_id ) continue;

View File

@@ -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() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
break; break;
case ( 'sp_staff' ): case ( 'sp_staff' ):
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
break; break;
case ( 'sp_table' ): 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' ); 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() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
break; break;
case ( 'sp_list' ): 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() ) ); 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' ); 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() ) ); sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) );

View File

@@ -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' ) ) { if ( !function_exists( 'sp_team_stats_table' ) ) {
function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) { function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) {
?> ?>
@@ -799,24 +839,32 @@ if ( !function_exists( 'sp_team_stats_table' ) ) {
<tbody> <tbody>
<?php <?php
$i = 0; $i = 0;
foreach ( $data as $div_id => $div_stats ): if ( empty( $data ) ):
if ( !$div_id ) continue;
$div = get_term( $div_id, 'sp_div' );
?> ?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>"> <tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td> <td><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Division', 'sportspress' ) ); ?></strong></td>
<?php echo $div->name; ?> </tr>
</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 <?php
$i++; else:
endforeach; 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> </tbody>
</table> </table>
@@ -840,6 +888,12 @@ if ( !function_exists( 'sp_player_metrics_table' ) ) {
<?php <?php
$i = 0; $i = 0;
foreach ( $data as $team_id => $team_stats ): 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 ): foreach ( $team_stats as $div_id => $div_stats ):
if ( !$div_id ) continue; if ( !$div_id ) continue;
$div = get_term( $div_id, 'sp_div' ); $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' ) ) { if ( !function_exists( 'sp_post_adder' ) ) {
function sp_post_adder( $meta = 'post' ) { function sp_post_adder( $meta = 'post' ) {
$obj = get_post_type_object( $meta ); $obj = get_post_type_object( $meta );