Enable selecting columns to display in player list

This commit is contained in:
Brian Miyaji
2014-06-18 13:29:00 +10:00
parent 033fa2949c
commit 6333025646
3 changed files with 84 additions and 91 deletions

View File

@@ -4,8 +4,8 @@
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta Boxes
* @version 0.8
* @package SportsPress/Admin/Meta_Boxes
* @version 1.1
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@@ -19,28 +19,20 @@ class SP_Meta_Box_List_Columns {
* Output the metabox
*/
public static function output( $post ) {
$column_groups = (array) get_post_meta( $post->ID, 'sp_column_group' );
?>
<ul id="sp-column-group-select">
<li>
<label class="selectit">
<input type="checkbox" name="sp_column_group[]" value="sp_performance" <?php checked( true, in_array( 'sp_performance', $column_groups ) ); ?>>
<?php _e( 'Performance', 'sportspress' ); ?>
</label>
</li>
<li>
<label class="selectit">
<input type="checkbox" name="sp_column_group[]" value="sp_metric" <?php checked( true, in_array( 'sp_metric', $column_groups ) ); ?>>
<?php _e( 'Metrics', 'sportspress' ); ?>
</label>
</li>
<li>
<label class="selectit">
<input type="checkbox" name="sp_column_group[]" value="sp_statistic" <?php checked( true, in_array( 'sp_statistic', $column_groups ) ); ?>>
<?php _e( 'Statistics', 'sportspress' ); ?>
</label>
</li>
</ul>
<div class="sp-instance">
<ul id="sp_column-tabs" class="wp-tab-bar sp-tab-bar">
<li class="wp-tab-active"><a href="#sp_performance-all"><?php _e( 'Performance', 'sportspress' ); ?></a></li>
<li class="wp-tab"><a href="#sp_metric-all"><?php _e( 'Metrics', 'sportspress' ); ?></a></li>
<li class="wp-tab"><a href="#sp_statistic-all"><?php _e( 'Statistics', 'sportspress' ); ?></a></li>
</ul>
<?php
$selected = (array)get_post_meta( $post->ID, 'sp_columns', true );
sp_column_checklist( $post->ID, 'sp_performance', 'block', $selected );
sp_column_checklist( $post->ID, 'sp_metric', 'none', $selected );
sp_column_checklist( $post->ID, 'sp_statistic', 'none', $selected );
?>
</div>
<?php
}
@@ -48,6 +40,6 @@ class SP_Meta_Box_List_Columns {
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_update_post_meta_recursive( $post_id, 'sp_column_group', sp_array_value( $_POST, 'sp_column_group', array() ) );
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
}
}

View File

@@ -4,7 +4,7 @@
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta Boxes
* @package SportsPress/Admin/Meta_Boxes
* @version 1.0.2
*/
@@ -20,16 +20,15 @@ class SP_Meta_Box_List_Data {
*/
public static function output( $post ) {
$list = new SP_Player_List( $post );
list( $columns, $usecolumns, $data, $placeholders, $merged ) = $list->data( true );
list( $columns, $data, $placeholders, $merged ) = $list->data( true );
$adjustments = $list->adjustments;
self::table( $columns, $usecolumns, $data, $placeholders, $adjustments );
self::table( $columns, $data, $placeholders, $adjustments );
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
update_post_meta( $post_id, 'sp_adjustments', sp_array_value( $_POST, 'sp_adjustments', array() ) );
update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) );
}
@@ -37,9 +36,7 @@ class SP_Meta_Box_List_Data {
/**
* Admin edit table
*/
public static function table( $columns = array(), $usecolumns = null, $data = array(), $placeholders = array(), $adjustments = array() ) {
if ( is_array( $usecolumns ) )
$usecolumns = array_filter( $usecolumns );
public static function table( $columns = array(), $data = array(), $placeholders = array(), $adjustments = array() ) {
?>
<ul class="subsubsub sp-table-bar">
<li><a href="#sp-table-values" class="current"><?php _e( 'Values', 'sportspress' ); ?></a></li> |
@@ -52,12 +49,12 @@ class SP_Meta_Box_List_Data {
<th>#</th>
<th><?php _e( 'Player', 'sportspress' ); ?></th>
<th><label for="sp_columns_team">
<input type="checkbox" name="sp_columns[]" value="team" id="sp_columns_team" <?php checked( ! is_array( $usecolumns ) || in_array( 'team', $usecolumns ) ); ?>>
<input type="checkbox" name="sp_columns[]" value="team" id="sp_columns_team" <?php checked( ! is_array( $columns ) || array_key_exists( 'team', $columns ) ); ?>>
<?php _e( 'Team', 'sportspress' ); ?>
</label></th>
<?php foreach ( $columns as $key => $label ): ?>
<?php if ( $key == 'team' ) continue; ?>
<th><label for="sp_columns_<?php echo $key; ?>">
<input type="checkbox" name="sp_columns[]" value="<?php echo $key; ?>" id="sp_columns_<?php echo $key; ?>" <?php checked( ! is_array( $usecolumns ) || in_array( $key, $usecolumns ) ); ?>>
<?php echo $label; ?>
</label></th>
<?php endforeach; ?>
@@ -92,6 +89,7 @@ class SP_Meta_Box_List_Data {
</td>
<td><?php echo get_the_title( $team ); ?></td>
<?php foreach( $columns as $column => $label ):
if ( $column == 'team' ) continue;
$value = sp_array_value( $player_stats, $column, '' );
$placeholder = sp_array_value( sp_array_value( $placeholders, $player_id, array() ), $column, 0 );
?>

View File

@@ -41,7 +41,6 @@ class SP_Player_List extends SP_Custom_Post {
$adjustments = get_post_meta( $this->ID, 'sp_adjustments', true );
$orderby = get_post_meta( $this->ID, 'sp_orderby', true );
$order = get_post_meta( $this->ID, 'sp_order', true );
$column_groups = (array)get_post_meta( $this->ID, 'sp_column_group' );
// Get labels from performance variables
$performance_labels = (array)sp_get_var_labels( 'sp_performance' );
@@ -242,72 +241,68 @@ class SP_Player_List extends SP_Custom_Post {
$totals[ $player_id ]['last10'] = $last10;
endforeach;
if ( ! empty( $column_groups ) ):
$args = array(
'post_type' => array( 'sp_performance', 'sp_metric', 'sp_statistic' ),
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$args = array(
'post_type' => $column_groups,
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
if ( $stat->post_type == 'sp_metric' ):
$stat->equation = null;
else:
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
endif;
// Add precision to object
$stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
endforeach;
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
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() ) ) );
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
if ( $stat->post_type == 'sp_metric' ):
$stat->equation = null;
else:
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
endif;
// Add precision to object
$stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
endforeach;
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
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() ) ) );
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ):
if ( $stat->equation === null ):
$placeholder = sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, null );
if ( $placeholder == null ):
$placeholder = '-';
endif;
else:
// Solve
$placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision );
// Adjustments
$adjustment = sp_array_value( $adjustments, $player_id, array() );
if ( $adjustment != 0 ):
$placeholder += sp_array_value( $adjustment, $stat->post_name, 0 );
$placeholder = number_format( $placeholder, $stat->precision );
endif;
if ( $stat->equation === null ):
$placeholder = sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, null );
if ( $placeholder == null ):
$placeholder = '-';
endif;
else:
// Solve
$placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision );
$placeholders[ $player_id ][ $stat->post_name ] = $placeholder;
// Adjustments
$adjustment = sp_array_value( $adjustments, $player_id, array() );
if ( $adjustment != 0 ):
$placeholder += sp_array_value( $adjustment, $stat->post_name, 0 );
$placeholder = number_format( $placeholder, $stat->precision );
endif;
endif;
endforeach;
$placeholders[ $player_id ][ $stat->post_name ] = $placeholder;
endif;
endforeach;
endif;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
@@ -348,7 +343,15 @@ class SP_Player_List extends SP_Custom_Post {
endforeach;
if ( $admin ):
return array( $columns, $this->columns, $data, $placeholders, $merged );
$labels = array();
foreach( $this->columns as $key ):
if ( $key == 'team' ):
$labels[ $key ] = __( 'Team', 'sportspress' );
elseif ( array_key_exists( $key, $columns ) ):
$labels[ $key ] = $columns[ $key ];
endif;
endforeach;
return array( $labels, $data, $placeholders, $merged );
else:
if ( ! is_array( $this->columns ) )
$this->columns = array();