Generate event result based on first column value in total row

This commit is contained in:
Takumi
2013-08-03 01:06:14 +10:00
parent a50000ad1a
commit 54d31c7023
4 changed files with 90 additions and 50 deletions

View File

@@ -20,8 +20,8 @@ function sp_manage_posts_custom_column( $column, $post_id ) {
echo get_the_terms ( $post_id, 'sp_position' ) ? the_terms( $post_id, 'sp_position' ) : '—'; echo get_the_terms ( $post_id, 'sp_position' ) ? the_terms( $post_id, 'sp_position' ) : '—';
break; break;
case 'sp_team': case 'sp_team':
$score = get_post_meta( $post_id, 'sp_score', false ); $result = get_post_meta( $post_id, 'sp_result', false );
echo get_post_meta ( $post_id, 'sp_team' ) ? sp_the_posts( $post_id, 'sp_team', '', '<br />', $score, ( empty( $score ) ? ' — ' : ' ' ) ) : '—'; echo get_post_meta ( $post_id, 'sp_team' ) ? sp_the_posts( $post_id, 'sp_team', '', '<br />', $result, ( empty( $result ) ? ' — ' : ' ' ) ) : '—';
break; break;
case 'sp_event': case 'sp_event':
echo get_post_meta ( $post_id, 'sp_event' ) ? sizeof( get_post_meta ( $post_id, 'sp_event' ) ) : '—'; echo get_post_meta ( $post_id, 'sp_event' ) ? sizeof( get_post_meta ( $post_id, 'sp_event' ) ) : '—';
@@ -99,24 +99,46 @@ function sp_save_post( $post_id ) {
if ( !isset( $_POST['sportspress_nonce'] ) || ! wp_verify_nonce( $_POST['sportspress_nonce'], plugin_basename( __FILE__ ) ) ) return $post_id; if ( !isset( $_POST['sportspress_nonce'] ) || ! wp_verify_nonce( $_POST['sportspress_nonce'], plugin_basename( __FILE__ ) ) ) return $post_id;
switch ( $_POST['post_type'] ): switch ( $_POST['post_type'] ):
case ( 'sp_team' ): case ( 'sp_team' ):
update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
break; break;
case ( 'sp_event' ): case ( 'sp_event' ):
update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); delete_post_meta( $post_id, 'sp_result');
sp_update_post_meta_recursive( $post_id, 'sp_player', $_POST['sp_player'] ); $stats = (array)sp_array_value( $_POST, 'sp_stats', array() );
foreach ( $stats as $table ) {
// Add values from each row where total is blank
$total = sp_array_value( sp_array_value( $table, 0, null ), 0, null );
if ( $total == null ):
$values = array();
foreach ( $table as $row ):
$value = sp_array_value( $row, 0, null );
if ( is_numeric( $value ) ):
$values[] = $value;
endif;
endforeach;
if ( sizeof( $values ) ):
$total = array_sum( $values );
endif;
endif;
// Save first column of total row as result
add_post_meta( $post_id, 'sp_result', $total );
}
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) );
break; break;
case ( 'sp_player' ): case ( 'sp_player' ):
update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); 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', $_POST['sp_team'] ); 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', $_POST['sp_stats'] ); update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
wp_set_post_terms( $post_id, $_POST['sp_league'], 'sp_league' ); wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_league', 0 ), 'sp_league' );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
break; break;
endswitch; endswitch;

View File

@@ -76,6 +76,8 @@ function sp_event_stats_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_stats', true ); $stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
$result = (array)get_post_meta( $post->ID, 'sp_result', false );
print_r( $result );
// Teams // Teams
foreach ( $teams as $key => $value ): foreach ( $teams as $key => $value ):

View File

@@ -167,10 +167,13 @@ if ( !function_exists( 'sp_the_posts' ) ) {
$title = __( '(no title)' ); $title = __( '(no title)' );
edit_post_link( $title, '', '', $id ); edit_post_link( $title, '', '', $id );
if ( !empty( $after ) ): if ( !empty( $after ) ):
if ( is_array( $after ) && array_key_exists( $i, $after ) ) if ( is_array( $after ) ):
echo ' - ' . $after[ $i ]; if ( array_key_exists( $i, $after ) && $after[ $i ] != '' ):
else echo ' - ' . $after[ $i ];
endif;
else:
echo $after; echo $after;
endif;
endif; endif;
if ( ++$i !== $count ) if ( ++$i !== $count )
echo $sep; echo $sep;
@@ -233,29 +236,37 @@ if ( !function_exists( 'sp_post_checklist' ) ) {
} }
if ( !function_exists( 'sp_get_stats_row' ) ) { if ( !function_exists( 'sp_get_stats_row' ) ) {
function sp_get_stats_row( $args ) { function sp_get_stats_row( $args = array() ) {
extract( $args ); $args = array_merge(
$args = array( array(
'post_type' => $post_type, 'posts_per_page' => -1
'posts_per_page' => -1, ),
'meta_query' => array( (array)$args
array(
'key' => $meta_key,
'value' => $meta_value,
)
)
); );
if ( isset( $terms ) ):
$args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'terms' => $terms
)
);
endif;
$posts = (array)get_posts( $args ); $posts = (array)get_posts( $args );
$row = array(); foreach( $posts as $post ):
$row[] = sizeof( $posts ); $post->sp_stats = get_post_meta( $post->ID, 'sp_stats', true );
endforeach;
$row = array(
sizeof( $posts ),
sizeof(
array_filter(
$posts,
function( $var ) {
echo '<pre>';
print_r( $var->sp_stats );
echo '</pre>';
return true;
}
)
),
99,
99,
93,
99,
99,
99
);
return $row; return $row;
} }
} }
@@ -268,7 +279,7 @@ if ( !function_exists( 'sp_get_stats' ) ) {
$subset = sp_array_value( $set, $subset_id, array() ); $subset = sp_array_value( $set, $subset_id, array() );
// If empty columns exist in subset // If empty columns exist in subset
if ( in_array( '', $subset ) ): if ( empty( $subset ) || in_array( '', $subset ) ):
// If subset is total row // If subset is total row
if ( $subset_id == 0 ): if ( $subset_id == 0 ):
@@ -297,8 +308,9 @@ if ( !function_exists( 'sp_get_stats' ) ) {
// Get fallback values // Get fallback values
switch ( $post_type ): switch ( $post_type ):
// Teams: all events attended in the league
case 'sp_team': case 'sp_team':
// Get all events attended in that league
$args = array( $args = array(
'post_type' => 'sp_event', 'post_type' => 'sp_event',
'meta_key' => 'sp_team', 'meta_key' => 'sp_team',
@@ -308,13 +320,11 @@ if ( !function_exists( 'sp_get_stats' ) ) {
); );
$fallback = sp_get_stats_row( $args ); $fallback = sp_get_stats_row( $args );
break; break;
endswitch; endswitch;
// Add values from fallback where missing // Merge fallback values with current subset
foreach( $subset as $key => $value ): $subset = array_merge( $fallback, $subset );
if ( $value != '' ) continue;
$subset[ $key ] = sp_array_value( $fallback, $key, 0 );
endforeach;
endif; endif;
@@ -406,12 +416,20 @@ if ( !function_exists( 'sp_post_adder' ) ) {
} }
} }
if ( !function_exists( 'sp_update_post_meta' ) ) {
function sp_update_post_meta( $post_id, $meta_key, $meta_value, $default = null ) {
if ( !isset( $meta_value ) && isset( $default ) )
$meta_value = $default;
add_post_meta( $post_id, $meta_key, $meta_value, true );
}
}
if ( !function_exists( 'sp_update_post_meta_recursive' ) ) { if ( !function_exists( 'sp_update_post_meta_recursive' ) ) {
function sp_update_post_meta_recursive( $post_id, $name, $array ) { function sp_update_post_meta_recursive( $post_id, $meta_key, $meta_value ) {
delete_post_meta( $post_id, $name ); delete_post_meta( $post_id, $meta_key );
$values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $array ) ); $values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $meta_value ) );
foreach ( $values as $value ): foreach ( $values as $value ):
add_post_meta( $post_id, $name, $value, false ); add_post_meta( $post_id, $meta_key, $value, false );
endforeach; endforeach;
} }
} }

View File

@@ -63,9 +63,7 @@ function sp_team_stats_meta( $post ) {
$placeholders[ $league_id ] = sp_get_stats_row( $args ); $placeholders[ $league_id ] = sp_get_stats_row( $args );
endforeach; endforeach;
?> sp_stats_table( $data, $placeholders, 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), true, 'sp_league' );
<?php sp_stats_table( $data, $placeholders, 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), true, 'sp_league' );
sp_nonce(); sp_nonce();
} }
?> ?>