Add placeholder feature to auto-populate data table

This commit is contained in:
Takumi
2013-07-31 18:06:57 +10:00
parent b355a7bb4c
commit 5809170073
6 changed files with 49 additions and 22 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_data_table( $data, $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, false ); ?>
</div>
<?php
endforeach;

View File

@@ -34,7 +34,7 @@ if ( !function_exists( 'sp_array_between' ) ) {
if ( !function_exists( 'sp_array_value' ) ) {
function sp_array_value( $arr = array(), $key = 0, $default = null ) {
if ( array_key_exists( $key, $arr ) )
if ( is_array( $arr ) && array_key_exists( $key, $arr ) )
$subset = $arr[ $key ];
else
$subset = $default;
@@ -211,11 +211,20 @@ if ( !function_exists( 'sp_post_checklist' ) ) {
}
}
if ( !function_exists( 'sp_data_table' ) ) {
function sp_data_table( $data = array(), $index = 0, $columns = array( 'Name' ), $total = true, $auto = true, $rowtype = 'post' ) {
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 ) )
return sp_array_value( sp_array_value( (array)get_post_meta( $post_id, $slug, true ), $set_id, array() ), $subset_id, array() );
else
return array();
}
}
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' ) {
global $pagenow;
if ( !is_array( $data ) )
$data = array();
if ( !is_array( $stats ) )
$stats = array();
?>
<table class="widefat sp-data-table">
<thead>
@@ -231,7 +240,7 @@ if ( !function_exists( 'sp_data_table' ) ) {
<tbody>
<?php
$i = 0;
foreach ( $data as $key => $values ):
foreach ( $stats as $key => $values ):
if ( !$key ) continue;
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
?>
@@ -253,22 +262,20 @@ if ( !function_exists( 'sp_data_table' ) ) {
?>
</td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
if ( array_key_exists( $j, $values ) )
$value = (int)$values[ $j ];
else
$value = 0;
$value = (int)sp_array_value( $values, $j, 0 );
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $key, 0), $j, 0 );
?>
<td><input type="text" name="sportspress[sp_stats][<?php echo $index; ?>][<?php echo $key; ?>][]" value="<?php echo $value; ?>" /></td>
<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>
<?php endfor; ?>
<?php if ( $auto ): ?>
<td><input type="checkbox" name="sportspress[sp_stats][<?php echo $index; ?>][<?php echo $key; ?>][auto]" value="1"<?php if ( $is_auto ) echo ' checked="checked"'; ?> /></td>
<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, $data ) ? $data[0] : array();
$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
@@ -285,10 +292,10 @@ if ( !function_exists( 'sp_data_table' ) ) {
else
$value = 0;
?>
<td><input type="text" name="sportspress[sp_stats][<?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; ?>" /></td>
<?php endfor; ?>
<?php if ( $auto ): ?>
<td><input type="checkbox" name="sportspress[sp_stats][<?php echo $index; ?>][0][auto]" value="1"<?php if ( $is_auto ) echo ' checked="checked"'; ?> /></td>
<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; ?>

View File

@@ -41,7 +41,7 @@ function sp_player_stats_meta( $post ) {
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
?>
<p><strong><?php _e( 'Overall', 'sportspress' ); ?></strong></p>
<?php sp_data_table( $data, 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
<?php sp_stats_table( $data, array(), 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
<?php
// Leagues
@@ -50,7 +50,7 @@ function sp_player_stats_meta( $post ) {
$data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
?>
<p><strong><?php echo $league->name; ?></strong></p>
<?php sp_data_table( $data, $league->term_id, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
<?php sp_stats_table( $data, array(), $league->term_id, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
<?php
endforeach;

View File

@@ -22,7 +22,13 @@ jQuery(document).ready(function($){
// Data table checkboxes
$('.sp-data-table tr input[type=checkbox]').change(function() {
$(this).closest('tr').find('input[type=text], input[type=number]').prop('readonly', $(this).prop('checked'));
$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

View File

@@ -43,7 +43,21 @@ function sp_table_team_meta( $post ) {
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 );
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
sp_data_table( $data, 0, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
$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 );
endforeach;
}
?>

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_data_table( $data, 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, true, 'sp_league' );
<?php sp_stats_table( $data, array(), 0, array( 'League', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), true, true, 'sp_league' );
sp_nonce();
}