Fix empty player lists when no columns

This commit is contained in:
Brian Miyaji
2017-01-19 16:31:08 +11:00
parent eaf6f050ad
commit c319bf8c8f

View File

@@ -477,12 +477,11 @@ class SP_Player_List extends SP_Custom_Post {
$formats = array(); $formats = array();
$data = array(); $data = array();
$merged = array(); $merged = array();
$column_order = array();
$ordered_columns = array(); $ordered_columns = array();
if ( $stats ): if ( $stats ):
$column_order = array();
foreach ( $stats as $stat ): foreach ( $stats as $stat ):
// Get post meta // Get post meta
@@ -512,105 +511,106 @@ class SP_Player_List extends SP_Custom_Post {
endforeach; endforeach;
foreach ( $column_order as $slug ): endif;
if ( ! in_array( $slug, $this->columns ) ) continue; foreach ( $column_order as $slug ):
$ordered_columns[] = $slug; if ( ! in_array( $slug, $this->columns ) ) continue;
endforeach; $ordered_columns[] = $slug;
$diff = array_diff( $this->columns, $ordered_columns ); endforeach;
$this->columns = array_merge( $diff, $ordered_columns );
// Fill in empty placeholder values for each player $diff = array_diff( $this->columns, $ordered_columns );
foreach ( $player_ids as $player_id ): $this->columns = array_merge( $diff, $ordered_columns );
if ( ! $player_id )
continue;
$placeholders[ $player_id ] = array_merge( sp_array_value( $totals, $player_id, array() ), array_filter( sp_array_value( $placeholders, $player_id, array() ) ) ); // Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
// Player adjustments $placeholders[ $player_id ] = array_merge( sp_array_value( $totals, $player_id, array() ), array_filter( sp_array_value( $placeholders, $player_id, array() ) ) );
$player_adjustments = sp_array_value( $adjustments, $player_id, array() );
foreach ( $stats as $stat ): // Player adjustments
if ( $stat->equation === null ): $player_adjustments = sp_array_value( $adjustments, $player_id, array() );
$placeholder = sp_array_value( $player_adjustments, $stat->post_name, null );
if ( $placeholder == null ):
$placeholder = '-';
endif;
else:
// Solve
$placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision );
// Adjustment foreach ( $stats as $stat ):
$adjustment = sp_array_value( $player_adjustments, $stat->post_name, 0 ); if ( $stat->equation === null ):
$placeholder = sp_array_value( $player_adjustments, $stat->post_name, null );
// Apply adjustment if ( $placeholder == null ):
if ( $adjustment != 0 ): $placeholder = '-';
$placeholder += $adjustment;
$placeholder = number_format( $placeholder, $stat->precision, '.', '' );
endif;
endif; endif;
else:
// Solve
$placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision );
if ( $placeholder !== '' && is_numeric( $placeholder ) ): // Adjustment
$placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, 0 ) + $placeholder; $adjustment = sp_array_value( $player_adjustments, $stat->post_name, 0 );
else:
$placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, '-' ); // Apply adjustment
if ( $adjustment != 0 ):
$placeholder += $adjustment;
$placeholder = number_format( $placeholder, $stat->precision, '.', '' );
endif; endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
foreach( $placeholders as $player_id => $player_data ):
$player_data = array_merge( $column_order, $player_data );
$placeholders[ $player_id ] = $player_data;
// Add player number and name to row
$merged[ $player_id ] = array();
if ( in_array( 'number', $this->columns ) ):
$player_data['number'] = get_post_meta( $player_id, 'sp_number', true );
endif;
$player_data['name'] = get_the_title( $player_id );
if ( in_array( 'team', $this->columns ) ):
$player_data['team'] = get_post_meta( $player_id, 'sp_team', true );
endif;
if ( in_array( 'position', $this->columns ) ):
$player_data['position'] = null;
endif; endif;
foreach( $player_data as $key => $value ): if ( $placeholder !== '' && is_numeric( $placeholder ) ):
$placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, 0 ) + $placeholder;
// Use static data if key exists and value is not empty, else use placeholder else:
if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ): $placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, '-' );
$value = $tempdata[ $player_id ][ $key ]; endif;
endif;
$merged[ $player_id ][ $key ] = $value;
endforeach;
endforeach; endforeach;
if ( $orderby != 'number' || $order != 'ASC' ): endforeach;
$this->priorities = array(
array( // Merge the data and placeholders arrays
'key' => $orderby, foreach( $placeholders as $player_id => $player_data ):
'order' => $order,
), $player_data = array_merge( $column_order, $player_data );
); $placeholders[ $player_id ] = $player_data;
uasort( $merged, array( $this, 'sort' ) );
// Add player number and name to row
$merged[ $player_id ] = array();
if ( in_array( 'number', $this->columns ) ):
$player_data['number'] = get_post_meta( $player_id, 'sp_number', true );
endif;
$player_data['name'] = get_the_title( $player_id );
if ( in_array( 'team', $this->columns ) ):
$player_data['team'] = get_post_meta( $player_id, 'sp_team', true );
endif;
if ( in_array( 'position', $this->columns ) ):
$player_data['position'] = null;
endif; endif;
// Rearrange data array to reflect values foreach( $player_data as $key => $value ):
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ]; // Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ):
$value = $tempdata[ $player_id ][ $key ];
endif;
$merged[ $player_id ][ $key ] = $value;
endforeach; endforeach;
endforeach;
if ( $orderby != 'number' || $order != 'ASC' ):
$this->priorities = array(
array(
'key' => $orderby,
'order' => $order,
),
);
uasort( $merged, array( $this, 'sort' ) );
endif; endif;
// Rearrange data array to reflect values
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $admin ): if ( $admin ):