Convert player profile statistics to time format as needed

This commit is contained in:
Brian Miyaji
2016-12-20 23:46:07 +11:00
parent 3cd240937d
commit f1c5657a83
3 changed files with 82 additions and 8 deletions

View File

@@ -832,6 +832,10 @@ jQuery(document).ready(function($){
$('.sp-convert-time-input').change(function() { $('.sp-convert-time-input').change(function() {
var s = 0; var s = 0;
var val = $(this).val(); var val = $(this).val();
if (val === '') {
$(this).siblings('.sp-convert-time-output').val('');
return;
}
var a = val.split(':').reverse(); var a = val.split(':').reverse();
$.each(a, function( index, value ) { $.each(a, function( index, value ) {
s += parseInt(value) * Math.pow(60, index); s += parseInt(value) * Math.pow(60, index);

View File

@@ -32,8 +32,8 @@ class SP_Meta_Box_Player_Statistics {
?> ?>
<p><strong><?php echo $league->name; ?></strong></p> <p><strong><?php echo $league->name; ?></strong></p>
<?php <?php
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes ) = $player->data( $league->term_id, true ); list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats ) = $player->data( $league->term_id, true );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true ); self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true, $formats );
$i ++; $i ++;
endforeach; endforeach;
?> ?>
@@ -83,7 +83,7 @@ class SP_Meta_Box_Player_Statistics {
/** /**
* Admin edit table * Admin edit table
*/ */
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false ) { public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false, $formats = array() ) {
$readonly = false; $readonly = false;
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) ); $teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
?> ?>
@@ -188,10 +188,41 @@ class SP_Meta_Box_Player_Statistics {
<td><?php <td><?php
$value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, null ); $value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 ); $placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
if ( $readonly )
echo $value ? $value : $placeholder; // Convert value and placeholder to time format
else if ( 'time' === sp_array_value( $formats, $column, 'number' ) ) {
// Convert value
$intval = intval( $value );
$timeval = gmdate( 'i:s', $intval );
$hours = gmdate( 'H', $intval );
if ( '00' != $hours )
$timeval = $hours . ':' . $timeval;
$timeval = ereg_replace( '^0', '', $timeval );
// Convert placeholder
$intval = intval( $placeholder );
$placeholder = gmdate( 'i:s', $intval );
$hours = gmdate( 'H', $intval );
if ( '00' != $hours )
$placeholder = $hours . ':' . $placeholder;
$placeholder = ereg_replace( '^0', '', $placeholder );
}
if ( $readonly ) {
echo $timeval ? $timeval : $placeholder;
} else {
if ( 'time' === sp_array_value( $formats, $column, 'number' ) ) {
echo '<input class="sp-convert-time-input" type="text" name="sp_times[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . ( '' === $value ? '' : esc_attr( $timeval ) ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
echo '<input class="sp-convert-time-output" type="hidden" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . esc_attr( $value ) . '" />';
} else {
echo '<input type="text" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />'; echo '<input type="text" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
}
}
?></td> ?></td>
<?php endforeach; ?> <?php endforeach; ?>
</tr> </tr>

View File

@@ -160,6 +160,7 @@ class SP_Player extends SP_Custom_Post {
} }
$performance_labels = array(); $performance_labels = array();
$formats = array();
foreach ( $posts as $post ): foreach ( $posts as $post ):
if ( -1 === $section ) { if ( -1 === $section ) {
@@ -175,6 +176,12 @@ class SP_Player extends SP_Custom_Post {
$performance_labels[ $post->post_name ] = $post->post_title; $performance_labels[ $post->post_name ] = $post->post_title;
} }
} }
$format = get_post_meta( $post->ID, 'sp_format', true );
if ( '' === $format ) {
$format = 'number';
}
$formats[ $post->post_name ] = $format;
endforeach; endforeach;
// Get statistic labels // Get statistic labels
@@ -590,6 +597,8 @@ class SP_Player extends SP_Custom_Post {
$columns = array_merge( $performance_labels, $stats ); $columns = array_merge( $performance_labels, $stats );
$formats = array();
$args = array( $args = array(
'post_type' => array( 'sp_performance', 'sp_statistic' ), 'post_type' => array( 'sp_performance', 'sp_statistic' ),
'numberposts' => 100, 'numberposts' => 100,
@@ -610,6 +619,12 @@ class SP_Player extends SP_Custom_Post {
if ( in_array( $post->post_name, $usecolumns ) ) { if ( in_array( $post->post_name, $usecolumns ) ) {
$usecolumn_order[] = $post->post_name; $usecolumn_order[] = $post->post_name;
} }
$format = get_post_meta( $post->ID, 'sp_format', true );
if ( '' === $format ) {
$format = 'number';
}
$formats[ $post->post_name ] = $format;
} }
$columns = array_merge( $column_order, $columns ); $columns = array_merge( $column_order, $columns );
$usecolumns = array_merge( $usecolumn_order, $usecolumns ); $usecolumns = array_merge( $usecolumn_order, $usecolumns );
@@ -624,7 +639,7 @@ class SP_Player extends SP_Custom_Post {
$labels[ $key ] = $columns[ $key ]; $labels[ $key ] = $columns[ $key ];
endif; endif;
endforeach; endif; endforeach; endif;
return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes ); return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats );
else: else:
if ( is_array( $usecolumns ) ): if ( is_array( $usecolumns ) ):
foreach ( $columns as $key => $label ): foreach ( $columns as $key => $label ):
@@ -671,6 +686,30 @@ class SP_Player extends SP_Custom_Post {
$merged[-1]['name'] = __( 'Total', 'sportspress' ); $merged[-1]['name'] = __( 'Total', 'sportspress' );
} }
// Convert to time notation
if ( in_array( 'time', $formats ) ):
foreach ( $merged as $season => $season_performance ):
if ( $season <= 0 ) continue;
foreach ( $season_performance as $performance_key => $performance_value ):
// Continue if not time format
if ( 'time' !== sp_array_value( $formats, $performance_key ) ) continue;
$intval = intval( $performance_value );
$timeval = gmdate( 'i:s', $intval );
$hours = gmdate( 'H', $intval );
if ( '00' != $hours )
$timeval = $hours . ':' . $timeval;
$timeval = ereg_replace( '^0', '', $timeval );
$merged[ $season ][ $performance_key ] = $timeval;
endforeach;
endforeach;
endif;
$merged[0] = array_merge( $labels, $columns ); $merged[0] = array_merge( $labels, $columns );
return $merged; return $merged;