From f1c5657a833fc259423d5fcd3ed6b30c45bfea9b Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Tue, 20 Dec 2016 23:46:07 +1100 Subject: [PATCH] Convert player profile statistics to time format as needed --- assets/js/admin/sportspress-admin.js | 4 ++ .../class-sp-meta-box-player-statistics.php | 45 ++++++++++++++++--- includes/class-sp-player.php | 41 ++++++++++++++++- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js index bfdd2234..8a95f285 100644 --- a/assets/js/admin/sportspress-admin.js +++ b/assets/js/admin/sportspress-admin.js @@ -832,6 +832,10 @@ jQuery(document).ready(function($){ $('.sp-convert-time-input').change(function() { var s = 0; var val = $(this).val(); + if (val === '') { + $(this).siblings('.sp-convert-time-output').val(''); + return; + } var a = val.split(':').reverse(); $.each(a, function( index, value ) { s += parseInt(value) * Math.pow(60, index); diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php index 732e96b0..093b08ff 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php @@ -32,8 +32,8 @@ class SP_Meta_Box_Player_Statistics { ?>

name; ?>

data( $league->term_id, true ); - self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, 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, $formats ); $i ++; endforeach; ?> @@ -83,7 +83,7 @@ class SP_Meta_Box_Player_Statistics { /** * 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; $teams = array_filter( get_post_meta( $id, 'sp_team', false ) ); ?> @@ -188,10 +188,41 @@ class SP_Meta_Box_Player_Statistics { '; + + // Convert value and placeholder to time format + 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 ''; + echo ''; + } else { + echo ''; + } + } ?> diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index f832d638..12e09246 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -160,6 +160,7 @@ class SP_Player extends SP_Custom_Post { } $performance_labels = array(); + $formats = array(); foreach ( $posts as $post ): if ( -1 === $section ) { @@ -175,6 +176,12 @@ class SP_Player extends SP_Custom_Post { $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; // Get statistic labels @@ -590,6 +597,8 @@ class SP_Player extends SP_Custom_Post { $columns = array_merge( $performance_labels, $stats ); + $formats = array(); + $args = array( 'post_type' => array( 'sp_performance', 'sp_statistic' ), 'numberposts' => 100, @@ -610,6 +619,12 @@ class SP_Player extends SP_Custom_Post { if ( in_array( $post->post_name, $usecolumns ) ) { $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 ); $usecolumns = array_merge( $usecolumn_order, $usecolumns ); @@ -624,7 +639,7 @@ class SP_Player extends SP_Custom_Post { $labels[ $key ] = $columns[ $key ]; endif; endforeach; endif; - return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes ); + return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats ); else: if ( is_array( $usecolumns ) ): foreach ( $columns as $key => $label ): @@ -670,6 +685,30 @@ class SP_Player extends SP_Custom_Post { $merged[-1] = $total; $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 );