Allow statistic and column editing for admin only

This commit is contained in:
Brian Miyaji
2014-01-20 14:49:20 +11:00
parent 12f3d9cf5a
commit f2641222ad
6 changed files with 71 additions and 48 deletions

View File

@@ -628,7 +628,7 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) {
}
if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) {
function sportspress_edit_team_columns_table( $league_id, $columns = array(), $data = array(), $seasons = array() ) {
function sportspress_edit_team_columns_table( $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $seasons = array(), $readonly = true ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table">
@@ -658,7 +658,11 @@ if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) {
<?php foreach( $columns as $column => $label ):
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 );
?>
<td><?php echo $value; ?></td>
<td><?php
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sportspress_array_value( sportspress_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_columns[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
<?php endforeach; ?>
</tr>
<?php
@@ -673,7 +677,7 @@ if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) {
}
if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
function sportspress_edit_player_statistics_table( $league_id, $columns = array(), $data = array(), $seasons_teams = array() ) {
function sportspress_edit_player_statistics_table( $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $seasons_teams = array(), $readonly = true ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table">
@@ -690,7 +694,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
<?php
$i = 0;
foreach ( $data as $div_id => $div_stats ):
if ( !$div_id ) continue;
if ( !$div_id || $div_id == 'statistics' ) continue;
$div = get_term( $div_id, 'sp_season' );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
@@ -702,7 +706,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
$value = sportspress_array_value( $seasons_teams, $div_id, '-1' );
$args = array(
'post_type' => 'sp_team',
'name' => 'sp_seasons_teams[' . $league_id . '][' . $div_id . ']',
'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']',
'show_option_none' => __( '-- Select --', 'sportspress' ),
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
@@ -726,9 +730,12 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
?>
</td>
<?php foreach( $columns as $column => $label ):
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 );
?>
<td><?php echo $value; ?></td>
<td><?php
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 );
$placeholder = sportspress_array_value( sportspress_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
<?php endforeach; ?>
</tr>
<?php
@@ -888,6 +895,7 @@ if ( !function_exists( 'sportspress_edit_event_players_table' ) ) {
endforeach;
?>
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>&nbsp;</td>
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php foreach( $columns as $column => $label ):
$player_id = 0;
@@ -1139,8 +1147,10 @@ if ( !function_exists( 'sportspress_get_team_columns_data' ) ) {
endif;
endforeach;
// Get all leagues populated with columns where available
$data = $div_ids;
$data = array();
// Get all seasons populated with data where available
$data = sportspress_array_combine( $div_ids, sportspress_array_value( $columns, $league_id, array() ) );
// Get equations from column variables
$equations = sportspress_get_var_equations( 'sp_column' );
@@ -1307,7 +1317,7 @@ if ( !function_exists( 'sportspress_get_team_columns_data' ) ) {
endforeach;
if ( $admin ):
return array( $columns, $placeholders, $leagues_seasons );
return array( $columns, $data, $placeholders, $merged, $leagues_seasons );
else:
$labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ) ), $columns );
$merged[0] = $labels;
@@ -1440,6 +1450,11 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
else:
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
$totals[ $team_id ][ $key . 'for' ] += $value;
foreach( $results as $other_team_id => $other_result ):
if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ):
$totals[ $team_id ][ $key . 'against' ] += sportspress_array_value( $other_result, $key, 0 );
endif;
endforeach;
endif;
endif;
@@ -1794,7 +1809,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
endforeach;
if ( $admin ):
return array( $columns, $placeholders );
return array( $columns, $tempdata, $placeholders, $merged );
else:
$labels = array_merge( array( 'name' => __( 'Player', 'sportspress' ) ), $columns );
$merged[0] = $labels;
@@ -1829,7 +1844,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
$seasons = (array)get_the_terms( $post_id, 'sp_season' );
$stats = (array)get_post_meta( $post_id, 'sp_statistics', true );
$seasons_teams = sportspress_array_value( (array)get_post_meta( $post_id, 'sp_seasons_teams', true ), $league_id, array() );
$seasons_teams = sportspress_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() );
// Get labels from statistic variables
$statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' );
@@ -1971,7 +1986,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
endforeach;
if ( $admin ):
return array( $columns, $placeholders, $seasons_teams );
return array( $columns, $tempdata, $placeholders, $merged, $seasons_teams );
else:
$labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ) ), $columns );
$merged[0] = $labels;

View File

@@ -10,6 +10,10 @@ function sportspress_save_post( $post_id ) {
// Update leagues seasons to show
update_post_meta( $post_id, 'sp_leagues_seasons', sportspress_array_value( $_POST, 'sp_leagues_seasons', array() ) );
// Update player statistics array
if ( current_user_can( 'edit_sp_tables' ) )
update_post_meta( $post_id, 'sp_columns', sportspress_array_value( $_POST, 'sp_columns', array() ) );
break;
case ( 'sp_event' ):
@@ -88,7 +92,7 @@ function sportspress_save_post( $post_id ) {
case ( 'sp_player' ):
// Update seasons teams to show
update_post_meta( $post_id, 'sp_seasons_teams', sportspress_array_value( $_POST, 'sp_seasons_teams', array() ) );
update_post_meta( $post_id, 'sp_leagues', sportspress_array_value( $_POST, 'sp_leagues', array() ) );
// Update team array
sportspress_update_post_meta_recursive( $post_id, 'sp_team', sportspress_array_value( $_POST, 'sp_team', array() ) );
@@ -102,6 +106,10 @@ function sportspress_save_post( $post_id ) {
// Update player metrics array
update_post_meta( $post_id, 'sp_metrics', sportspress_array_value( $_POST, 'sp_metrics', array() ) );
// Update player statistics array
if ( current_user_can( 'edit_sp_teams' ) )
update_post_meta( $post_id, 'sp_statistics', sportspress_array_value( $_POST, 'sp_statistics', array() ) );
break;
case ( 'sp_staff' ):

View File

@@ -37,7 +37,7 @@ function sportspress_the_content( $content ) {
global $post;
sportspress_league_table( $post->ID );
$table = sportspress_league_table( $post->ID );
$content = $table . $content;

View File

@@ -109,9 +109,9 @@ function sportspress_player_stats_meta( $post ) {
<?php
endif;
list( $columns, $data, $seasons_teams ) = sportspress_get_player_statistics_data( $post->ID, $league->term_id, true );
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = sportspress_get_player_statistics_data( $post->ID, $league->term_id, true );
sportspress_edit_player_statistics_table( $league->term_id, $columns, $data, $seasons_teams );
sportspress_edit_player_statistics_table( $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) );
endforeach;
}

View File

@@ -20,15 +20,15 @@ function sportspress_team_post_init() {
add_action( 'init', 'sportspress_team_post_init' );
function sportspress_team_meta_init( $post ) {
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
$seasons = (array)get_the_terms( $post->ID, 'sp_season' );
$leagues = get_the_terms( $post->ID, 'sp_league' );
$seasons = get_the_terms( $post->ID, 'sp_season' );
remove_meta_box( 'submitdiv', 'sp_team', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sp_team', 'side', 'high' );
remove_meta_box( 'postimagediv', 'sp_team', 'side' );
add_meta_box( 'postimagediv', __( 'Logo', 'sportspress' ), 'post_thumbnail_meta_box', 'sp_team', 'side', 'high' );
if ( $leagues && $leagues != array(0) && $seasons && $seasons != array(0) ):
if ( $leagues && $seasons ):
add_meta_box( 'sp_columnssdiv', __( 'Table Columns', 'sportspress' ), 'sportspress_team_columns_meta', 'sp_team', 'normal', 'high' );
endif;
}
@@ -61,9 +61,9 @@ function sportspress_team_columns_meta( $post ) {
<?php
endif;
list( $columns, $data, $leagues_seasons ) = sportspress_get_team_columns_data( $post->ID, $league_id, true );
list( $columns, $data, $placeholders, $merged, $leagues_seasons ) = sportspress_get_team_columns_data( $post->ID, $league_id, true );
sportspress_edit_team_columns_table( $league_id, $columns, $data, $leagues_seasons );
sportspress_edit_team_columns_table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) );
endforeach;

View File

@@ -71,7 +71,7 @@ if ( !function_exists( 'sportspress_event_results' ) ) {
else:
$value = '—';
endif;
$table_rows .= '<td class="column-' . $key . '">' . $value . '</td>';
$table_rows .= '<td class="data-' . $key . '">' . $value . '</td>';
endforeach;
$table_rows .= '</tr>';
@@ -86,9 +86,9 @@ if ( !function_exists( 'sportspress_event_results' ) ) {
$output .= '<h3>' . __( 'Results', 'sportspress' ) . '</h3>';
$output .= '<table class="sp-event-results sp-data-table"><thead>';
$output .= '<th class="column-name">' . __( 'Team', 'sportspress' ) . '</th>';
$output .= '<th class="data-name">' . __( 'Team', 'sportspress' ) . '</th>';
foreach( $result_labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$output .= $table_rows;
@@ -122,11 +122,11 @@ if ( !function_exists( 'sportspress_event_players' ) ) {
$output .= '<table class="sp-event-statistics sp-data-table">' . '<thead>' . '<tr>';
$output .= '<th class="column-number">#</th>';
$output .= '<th class="column-number">' . __( 'Player', 'sportspress' ) . '</th>';
$output .= '<th class="data-number">#</th>';
$output .= '<th class="data-number">' . __( 'Player', 'sportspress' ) . '</th>';
foreach( $statistic_labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
@@ -143,12 +143,12 @@ if ( !function_exists( 'sportspress_event_players' ) ) {
$number = get_post_meta( $player_id, 'sp_number', true );
// Player number
$output .= '<td class="column-number">' . $number . '</td>';
$output .= '<td class="data-number">' . $number . '</td>';
// Name as link
$permalink = get_post_permalink( $player_id );
$name = get_the_title( $player_id );
$output .= '<td class="column-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
$output .= '<td class="data-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
foreach( $statistic_labels as $key => $label ):
if ( $key == 'name' )
@@ -158,7 +158,7 @@ if ( !function_exists( 'sportspress_event_players' ) ) {
else:
$value = 0;
endif;
$output .= '<td class="column-' . $key . '">' . $value . '</td>';
$output .= '<td class="data-' . $key . '">' . $value . '</td>';
endforeach;
$output .= '</tr>';
@@ -176,8 +176,8 @@ if ( !function_exists( 'sportspress_event_players' ) ) {
$number = get_post_meta( $player_id, 'sp_number', true );
// Player number
$output .= '<td class="column-number">&nbsp;</td>';
$output .= '<td class="column-name">' . __( 'Total', 'sportspress' ) . '</td>';
$output .= '<td class="data-number">&nbsp;</td>';
$output .= '<td class="data-name">' . __( 'Total', 'sportspress' ) . '</td>';
$row = $data[0];
@@ -185,7 +185,7 @@ if ( !function_exists( 'sportspress_event_players' ) ) {
if ( $key == 'name' ):
continue;
endif;
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr></tfoot>';
@@ -257,9 +257,9 @@ if ( !function_exists( 'sportspress_league_table' ) ) {
// Remove the first row to leave us with the actual data
unset( $data[0] );
$output .= '<th class="column-number">#</th>';
$output .= '<th class="data-number">#</th>';
foreach( $labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
@@ -271,18 +271,18 @@ if ( !function_exists( 'sportspress_league_table' ) ) {
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
// Position as number
$output .= '<td class="column-number">' . $i . '</td>';
$output .= '<td class="data-number">' . $i . '</td>';
// Thumbnail and name as link
$permalink = get_post_permalink( $team_id );
$thumbnail = get_the_post_thumbnail( $team_id, 'thumbnail', array( 'class' => 'logo' ) );
$name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', '&nbsp;' ) );
$output .= '<td class="column-name">' . ( $thumbnail ? $thumbnail . ' ' : '' ) . '<a href="' . $permalink . '">' . $name . '</a></td>';
$output .= '<td class="data-name">' . ( $thumbnail ? $thumbnail . ' ' : '' ) . '<a href="' . $permalink . '">' . $name . '</a></td>';
foreach( $labels as $key => $value ):
if ( $key == 'name' )
continue;
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr>';
@@ -326,7 +326,7 @@ if ( !function_exists( 'sportspress_team_columns' ) ) {
$output .= '<table class="sp-team-columns sp-data-table">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
@@ -338,7 +338,7 @@ if ( !function_exists( 'sportspress_team_columns' ) ) {
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr>';
@@ -370,9 +370,9 @@ if ( !function_exists( 'sportspress_player_list' ) ) {
// Remove the first row to leave us with the actual data
unset( $data[0] );
$output .= '<th class="column-number">#</th>';
$output .= '<th class="data-number">#</th>';
foreach( $labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
@@ -385,17 +385,17 @@ if ( !function_exists( 'sportspress_player_list' ) ) {
// Player number
$number = get_post_meta( $player_id, 'sp_number', true );
$output .= '<td class="column-number">' . ( $number ? $number : '&nbsp;' ) . '</td>';
$output .= '<td class="data-number">' . ( $number ? $number : '&nbsp;' ) . '</td>';
// Name as link
$permalink = get_post_permalink( $player_id );
$name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', '&nbsp;' ) );
$output .= '<td class="column-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
$output .= '<td class="data-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
foreach( $labels as $key => $value ):
if ( $key == 'name' )
continue;
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr>';
@@ -473,7 +473,7 @@ if ( !function_exists( 'sportspress_player_statistics' ) ) {
$output .= '<table class="sp-player-statistics sp-data-table">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
@@ -485,7 +485,7 @@ if ( !function_exists( 'sportspress_player_statistics' ) ) {
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr>';