Improve stats table filters

This commit is contained in:
Takumi
2013-08-01 00:55:21 +10:00
parent 5809170073
commit e09a5bb311
7 changed files with 85 additions and 79 deletions

View File

@@ -84,7 +84,7 @@ function sp_event_stats_meta( $post ) {
?>
<div>
<p><strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
<?php sp_stats_table( $data, array(), $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, false ); ?>
<?php sp_stats_table( $data, array(), $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true ); ?>
</div>
<?php
endforeach;

View File

@@ -36,6 +36,9 @@ $sportspress_texts = array(
'Scheduled for: <b>%1$s</b>' => __( 'Joined', 'sportspress' ) . ': <b>%1$s</b>',
'Published on: <b>%1$s</b>' => __( 'Joined', 'sportspress' ) . ': <b>%1$s</b>',
'Publish <b>immediately</b>' => __( 'Joined', 'sportspress' ) . ': <b>%1$s</b>'
),
'sp_table' => array(
'Enter title here' => ''
)
);

View File

@@ -115,7 +115,7 @@ if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
printf( '<option value="-1">%s</option>', $args['show_option_none'] );
}
foreach ( $terms as $term ) {
printf( '<option value="%s" %s>%s</option>', $term->slug, selected( true, $args['selected'] == $term->slug ), $term->name );
printf( '<option value="%s" %s>%s</option>', $term->term_id, selected( true, $args['selected'] == $term->term_id, false ), $term->name );
}
print( '</select>' );
}
@@ -179,8 +179,14 @@ if ( !function_exists( 'sp_post_checklist' ) ) {
$posts = get_posts( array( 'post_type' => $meta, 'numberposts' => 0 ) );
foreach ( $posts as $post ):
$parents = get_post_ancestors( $post );
if ( $filter )
$filter_values = (array)get_post_meta( $post->ID, $filter, false )
if ( $filter ):
$filter_values = (array)get_post_meta( $post->ID, $filter, false );
$terms = (array)get_the_terms( $post->ID, 'sp_league' );
foreach ( $terms as $term ):
if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
$filter_values[] = $term->term_id;
endforeach;
endif;
?>
<li class="sp-post<?php
if ( $filter ):
@@ -221,20 +227,17 @@ if ( !function_exists( 'sp_get_stats' ) ) {
}
if ( !function_exists( 'sp_stats_table' ) ) {
function sp_stats_table( $stats = array(), $placeholders = array(), $index = 0, $columns = array( 'Name' ), $total = true, $auto = true, $rowtype = 'post', $slug = 'sp_stats' ) {
function sp_stats_table( $stats = array(), $placeholders = array(), $index = 0, $columns = array( 'Name' ), $total = true, $rowtype = 'post', $slug = 'sp_stats' ) {
global $pagenow;
if ( !is_array( $stats ) )
$stats = array();
?>
<table class="widefat sp-data-table">
<table class="widefat sp-stats-table">
<thead>
<tr>
<?php foreach ( $columns as $column ): ?>
<th><?php echo $column; ?></th>
<?php endforeach; ?>
<?php if ( $auto ): ?>
<th><?php _e( 'Auto', 'sportspress' ); ?></th>
<?php endif; ?>
</tr>
</thead>
<tbody>
@@ -242,9 +245,8 @@ if ( !function_exists( 'sp_stats_table' ) ) {
$i = 0;
foreach ( $stats as $key => $values ):
if ( !$key ) continue;
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
?>
<tr class="sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<?php
switch( $rowtype ):
@@ -262,41 +264,26 @@ if ( !function_exists( 'sp_stats_table' ) ) {
?>
</td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
$value = (int)sp_array_value( $values, $j, 0 );
$value = sp_array_value( $values, $j, '' );
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $key, 0), $j, 0 );
?>
<td><input type="text" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][<?php echo $key; ?>][]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /><?php echo $placeholder; ?></td>
<td><input type="text" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][<?php echo $key; ?>][]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
<?php endfor; ?>
<?php if ( $auto ): ?>
<td><input type="checkbox" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][<?php echo $key; ?>][auto]" value="1"<?php if ( $is_auto ) echo ' checked="checked"'; ?> /></td>
<?php endif; ?>
</tr>
<?php
$i++;
endforeach;
if ( $total ):
$values = array_key_exists( 0, $stats ) ? $stats[0] : array();
if ( $auto )
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
else
$is_auto = $i;
?>
<tr<?php
if ( $i % 2 == 0 )
echo ' class="alternate"';
?>>
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
if ( array_key_exists( $j, $values ) )
$value = (int)$values[ $j ];
else
$value = 0;
$value = sp_array_value( $values, $j, '' );
$placeholder = 0;
?>
<td><input type="text" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][0][]" value="<?php echo $value; ?>" /></td>
<td><input type="text" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][0][]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
<?php endfor; ?>
<?php if ( $auto ): ?>
<td><input type="checkbox" name="sportspress[<?php echo $slug; ?>][<?php echo $index; ?>][0][auto]" value="1"<?php if ( $is_auto ) echo ' checked="checked"'; ?> /></td>
<?php endif; ?>
</tr>
<?php endif; ?>
</tbody>

View File

@@ -2,7 +2,7 @@
function sp_league_tax_init() {
$name = __( 'Leagues', 'sportspress' );
$singular_name = __( 'League', 'sportspress' );
$object_type = array( 'sp_team', 'sp_event', 'sp_player', 'sp_staff', 'sp_table', 'sp_list' );
$object_type = array( 'sp_team', 'sp_event', 'sp_player', 'sp_staff', 'sp_list' );
$labels = sp_tax_labels( $name, $singular_name );
$args = array(
'label' => $name,

View File

@@ -1,12 +1,12 @@
jQuery(document).ready(function($){
// Switch tabs
// Tab switcher
$('.sp-tab-panel').siblings('.sp-tab-bar').find('a').click(function() {
$(this).closest('li').removeClass('wp-tab').addClass('wp-tab-active').siblings().removeClass('wp-tab-active').addClass('wp-tab').closest('.wp-tab-bar').siblings($(this).attr('href')).show().siblings('.wp-tab-panel').hide();
return false;
});
// Filter tabs
// Tab filter
$('.sp-tab-panel').siblings('.sp-tab-select').find('select').change(function() {
var val = $(this).val();
$(this).closest('.sp-tab-select').siblings('.sp-tab-panel').find('.sp-post').hide(0, function() {
@@ -17,37 +17,44 @@ jQuery(document).ready(function($){
});
});
// Activate tab filters
// Trigger tab filter
$('.sp-tab-panel').siblings('.sp-tab-select').find('select').change();
// Data table checkboxes
$('.sp-data-table tr input[type=checkbox]').change(function() {
$el = $(this).closest('tr').find('input[type=text], input[type=number]');
$el.prop('readonly', $(this).prop('checked'));
if($(this).prop('checked')) {
$el.each(function() {
$(this).val($(this).attr('placeholder'));
});
}
})
// Uncheck auto to edit
$('.sp-data-table tr input[type=text], .sp-data-table tr input[type=text]').click(function() {
$el = $(this).closest('tr').find('input[type=checkbox]');
if($el.size()) {
$el.prop('checked', false).change();
}
})
// Activate checkboxes
$('.sp-data-table tr input[type=checkbox]').change();
// Change title
$('.sp-title-generator select').change(function() {
// Title changer
$('input[name=post_title]').on('updateTitle', function() {
title = $('.sp-title-generator select[value!=0]').map(function(){
return $(this).find(':selected').html().replace(/&[^;]+;/g, '');
}).get().join(' vs ');
$('input[name=post_title]').val(title);
$(this).val(title);
});
// Activate title changer
$('.sp-title-generator select').change(function() {
$('input[name=post_title]').trigger('updateTitle');
});
// Total stats calculator
$('.sp-stats-table').on('updateTotals', function() {
$self = $(this);
$self.find('.sp-total input').each(function(i) {
var sum = 0;
$self.find('.sp-post').each(function() {
$el = $($(this).find('input')[i]);
if($el.val() != '')
if($.isNumeric($el.val())) sum += parseInt($el.val(), 10);
else
sum += parseInt($el.attr('placeholder'), 10);
});
$(this).attr('placeholder', sum);
});
});
// Activate total stats calculator
$('.sp-stats-table .sp-post input').on('keyup', function() {
$(this).closest('.sp-stats-table').trigger('updateTotals');
});
// Trigger total stats calculator
$('.sp-stats-table').trigger('updateTotals');
});

View File

@@ -35,29 +35,38 @@ function sp_table_meta_init() {
}
function sp_table_team_meta( $post ) {
sp_post_checklist( $post->ID, 'sp_team' );
sp_post_adder( 'sp_team' );
$league = get_post_meta( $post->ID, 'sp_league', true );
?>
<div>
<p class="sp-tab-select sp-title-generator">
<?php
$args = array(
'show_option_all' => sprintf( __( 'All %s', 'sportspress' ), __( 'Leagues', 'sportspress' ) ),
'taxonomy' => 'sp_league',
'name' => 'sportspress[sp_league]',
'selected' => $league
);
sp_dropdown_taxonomies( $args );
?>
</p>
<?php
sp_post_checklist( $post->ID, 'sp_team', 'block', 'sp_league' );
sp_post_adder( 'sp_team' );
?>
</div>
<?php
sp_nonce();
}
function sp_table_stats_meta( $post ) {
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
foreach ( $leagues as $league ):
if ( is_object( $league ) && property_exists( $league, 'term_id' ) )
$index = $league->term_id;
else
$index = 0;
$data = sp_array_combine( $teams, sp_array_value( $stats, $index, array() ) );
$placeholders = array();
foreach ( $teams as $team ):
$placeholders[ $team ] = sp_get_stats( $team, 0, $index );
endforeach;
sp_stats_table( $data, $placeholders, $index, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
$league = (int)get_post_meta( $post->ID, 'sp_league', true );
$data = sp_array_combine( $teams, sp_array_value( $stats, $league, array() ) );
$placeholders = array();
foreach ( $teams as $team ):
$placeholders[ $team ] = sp_get_stats( $team, 0, $league );
endforeach;
sp_stats_table( $data, $placeholders, $league, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
}
?>

View File

@@ -48,7 +48,7 @@ function sp_team_stats_meta( $post ) {
$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, true, 'sp_league' );
<?php sp_stats_table( $data, array(), 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), true, 'sp_league' );
sp_nonce();
}