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' ) : '—';
break;
case 'sp_team':
$score = get_post_meta( $post_id, 'sp_score', false );
echo get_post_meta ( $post_id, 'sp_team' ) ? sp_the_posts( $post_id, 'sp_team', '', '<br />', $score, ( empty( $score ) ? ' — ' : ' ' ) ) : '—';
$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 />', $result, ( empty( $result ) ? ' — ' : ' ' ) ) : '—';
break;
case '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;
switch ( $_POST['post_type'] ):
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;
case ( 'sp_event' ):
update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] );
sp_update_post_meta_recursive( $post_id, 'sp_player', $_POST['sp_player'] );
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
delete_post_meta( $post_id, 'sp_result');
$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;
case ( 'sp_player' ):
update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] );
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
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', $_POST['sp_team'] );
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', $_POST['sp_stats'] );
wp_set_post_terms( $post_id, $_POST['sp_league'], 'sp_league' );
sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] );
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_league', 0 ), 'sp_league' );
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
break;
endswitch;

View File

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

View File

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

View File

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