Add placeholder feature to auto-populate data table
This commit is contained in:
@@ -84,7 +84,7 @@ function sp_event_stats_meta( $post ) {
|
|||||||
?>
|
?>
|
||||||
<div>
|
<div>
|
||||||
<p><strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
|
<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>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|||||||
37
helpers.php
37
helpers.php
@@ -34,7 +34,7 @@ if ( !function_exists( 'sp_array_between' ) ) {
|
|||||||
|
|
||||||
if ( !function_exists( 'sp_array_value' ) ) {
|
if ( !function_exists( 'sp_array_value' ) ) {
|
||||||
function sp_array_value( $arr = array(), $key = 0, $default = null ) {
|
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 ];
|
$subset = $arr[ $key ];
|
||||||
else
|
else
|
||||||
$subset = $default;
|
$subset = $default;
|
||||||
@@ -211,11 +211,20 @@ if ( !function_exists( 'sp_post_checklist' ) ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !function_exists( 'sp_data_table' ) ) {
|
if ( !function_exists( 'sp_get_stats' ) ) {
|
||||||
function sp_data_table( $data = array(), $index = 0, $columns = array( 'Name' ), $total = true, $auto = true, $rowtype = 'post' ) {
|
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;
|
global $pagenow;
|
||||||
if ( !is_array( $data ) )
|
if ( !is_array( $stats ) )
|
||||||
$data = array();
|
$stats = array();
|
||||||
?>
|
?>
|
||||||
<table class="widefat sp-data-table">
|
<table class="widefat sp-data-table">
|
||||||
<thead>
|
<thead>
|
||||||
@@ -231,7 +240,7 @@ if ( !function_exists( 'sp_data_table' ) ) {
|
|||||||
<tbody>
|
<tbody>
|
||||||
<?php
|
<?php
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ( $data as $key => $values ):
|
foreach ( $stats as $key => $values ):
|
||||||
if ( !$key ) continue;
|
if ( !$key ) continue;
|
||||||
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
|
$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>
|
</td>
|
||||||
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
|
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
|
||||||
if ( array_key_exists( $j, $values ) )
|
$value = (int)sp_array_value( $values, $j, 0 );
|
||||||
$value = (int)$values[ $j ];
|
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $key, 0), $j, 0 );
|
||||||
else
|
|
||||||
$value = 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 endfor; ?>
|
||||||
<?php if ( $auto ): ?>
|
<?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; ?>
|
<?php endif; ?>
|
||||||
</tr>
|
</tr>
|
||||||
<?php
|
<?php
|
||||||
$i++;
|
$i++;
|
||||||
endforeach;
|
endforeach;
|
||||||
if ( $total ):
|
if ( $total ):
|
||||||
$values = array_key_exists( 0, $data ) ? $data[0] : array();
|
$values = array_key_exists( 0, $stats ) ? $stats[0] : array();
|
||||||
if ( $auto )
|
if ( $auto )
|
||||||
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
|
$is_auto = array_key_exists( 'auto', $values ) ? (int)$values[ 'auto' ] : $pagenow == 'post-new.php';
|
||||||
else
|
else
|
||||||
@@ -285,10 +292,10 @@ if ( !function_exists( 'sp_data_table' ) ) {
|
|||||||
else
|
else
|
||||||
$value = 0;
|
$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 endfor; ?>
|
||||||
<?php if ( $auto ): ?>
|
<?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; ?>
|
<?php endif; ?>
|
||||||
</tr>
|
</tr>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ function sp_player_stats_meta( $post ) {
|
|||||||
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
|
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
|
||||||
?>
|
?>
|
||||||
<p><strong><?php _e( 'Overall', 'sportspress' ); ?></strong></p>
|
<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
|
<?php
|
||||||
|
|
||||||
// Leagues
|
// Leagues
|
||||||
@@ -50,7 +50,7 @@ function sp_player_stats_meta( $post ) {
|
|||||||
$data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
|
$data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
|
||||||
?>
|
?>
|
||||||
<p><strong><?php echo $league->name; ?></strong></p>
|
<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
|
<?php
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,13 @@ jQuery(document).ready(function($){
|
|||||||
|
|
||||||
// Data table checkboxes
|
// Data table checkboxes
|
||||||
$('.sp-data-table tr input[type=checkbox]').change(function() {
|
$('.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
|
// Uncheck auto to edit
|
||||||
|
|||||||
18
table.php
18
table.php
@@ -43,7 +43,21 @@ function sp_table_team_meta( $post ) {
|
|||||||
function sp_table_stats_meta( $post ) {
|
function sp_table_stats_meta( $post ) {
|
||||||
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||||
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
|
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
|
||||||
sp_data_table( $data, 0, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
|
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;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
2
team.php
2
team.php
@@ -48,7 +48,7 @@ function sp_team_stats_meta( $post ) {
|
|||||||
|
|
||||||
$data = sp_array_combine( $keys, sp_array_value( $stats, 0, array() ) );
|
$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();
|
sp_nonce();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user