Make league table show played games automatically from event count

This commit is contained in:
Takumi
2013-08-02 02:32:27 +10:00
parent e563b870ef
commit 3da6d85d41
3 changed files with 77 additions and 6 deletions

View File

@@ -232,6 +232,34 @@ 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(
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 );
$row = array();
$row[] = sizeof( $posts );
return $row;
}
}
if ( !function_exists( 'sp_get_stats' ) ) {
function sp_get_stats( $post_id, $set_id = 0, $subset_id = 0, $slug = 'sp_stats' ) {
if ( isset( $post_id ) && $post_id ):
@@ -262,6 +290,32 @@ if ( !function_exists( 'sp_get_stats' ) ) {
$subset[ $key ] = $value;
endforeach;
else:
// Get current post type
$post_type = get_post_type( $post_id );
// Get fallback values
switch ( $post_type ):
case 'sp_team':
// Get all events attended in that league
$args = array(
'post_type' => 'sp_event',
'meta_key' => 'sp_team',
'meta_value' => $post_id,
'taxonomy' => 'sp_league',
'terms' => $subset_id
);
$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;
endif;
endif;

View File

@@ -40,10 +40,11 @@ jQuery(document).ready(function($){
var sum = 0;
$self.find('.sp-post').each(function() {
$el = $($(this).find('input')[i]);
if($el.val() != '')
if($el.val() != '') {
if($.isNumeric($el.val())) sum += parseInt($el.val(), 10);
else
} else {
sum += parseInt($el.attr('placeholder'), 10);
}
});
$(this).attr('placeholder', sum);
});

View File

@@ -40,15 +40,31 @@ function sp_team_stats_meta( $post ) {
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
$keys = array( 0 );
// Generate array of all league ids
$league_ids = array( 0 );
foreach ( $leagues as $key => $value ):
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
$keys[] = $value->term_id;
$league_ids[] = $value->term_id;
endforeach;
// Get all leagues populated with stats where availabled
$data = sp_array_combine( $league_ids, sp_array_value( $stats, 0, array() ) );
// Generate array of placeholder values for each league
$placeholders = array();
foreach ( $league_ids as $league_id ):
$args = array(
'post_type' => 'sp_event',
'meta_key' => 'sp_team',
'meta_value' => $post->ID,
'taxonomy' => 'sp_league',
'terms' => $league_id
);
$placeholders[ $league_id ] = sp_get_stats_row( $args );
endforeach;
$data = sp_array_combine( $keys, sp_array_value( $stats, 0, array() ) );
?>
<?php sp_stats_table( $data, array(), 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();
}