Auto calculate outcomes from event quick edit

This commit is contained in:
Brian Miyaji
2014-12-10 01:33:48 +11:00
parent 77ef9094ee
commit f2028deddb
4 changed files with 111 additions and 44 deletions

View File

@@ -579,7 +579,8 @@ jQuery(document).ready(function($){
nonce: $("#sp-inline-nonce").val()
}, function(response) {
$column.find(".sp-edit-result").each(function() {
$column.find(".sp-result[data-team='"+$(this).data("team")+"']").html($(this).val());
val = $(this).val();
$column.find(".sp-result[data-team='"+$(this).data("team")+"']").html(val==''?'-':val);
});
$column.find(".sp-edit-result, .sp-inline-edit-save").hide();
$column.find(".sp-result, .sp-row-actions").show();

View File

@@ -46,6 +46,7 @@ class SP_Admin_AJAX {
$post_id = sp_array_value( $_POST, 'post_id' );
$results = sp_array_value( $_POST, 'results' );
$main_result = get_option( 'sportspress_primary_result', null );
if ( ! $post_id || ! is_array( $results ) ) {
// Return error
@@ -55,14 +56,75 @@ class SP_Admin_AJAX {
// Get current results meta
$meta = get_post_meta( $post_id, 'sp_results', true );
$primary_results = array();
foreach ( $results as $result ) {
$id = sp_array_value( $result, 'id' );
$key = sp_array_value( $result, 'key' );
$primary_results[ $id ] = sp_array_value( $result, 'result', null );
if ( ! $id || ! $key ) continue;
$meta[ $id ][ $key ] = sp_array_value( $result, 'result' );
}
arsort( $primary_results );
if ( count( $primary_results ) && ! in_array( null, $primary_results ) ) {
if ( count( array_unique( $primary_results ) ) === 1 ) {
$args = array(
'post_type' => 'sp_outcome',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_key' => 'sp_condition',
'meta_value' => '=',
);
$outcomes = get_posts( $args );
foreach ( $meta as $team => $team_results ) {
if ( $outcomes ) {
$meta[ $team ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$meta[ $team ][ 'outcome' ][] = $outcome->post_name;
}
}
}
} else {
reset( $primary_results );
$max = key( $primary_results );
$args = array(
'post_type' => 'sp_outcome',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_key' => 'sp_condition',
'meta_value' => '>',
);
$outcomes = get_posts( $args );
if ( $outcomes ) {
$meta[ $max ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$meta[ $max ][ 'outcome' ][] = $outcome->post_name;
}
}
end( $primary_results );
$min = key( $primary_results );
$args = array(
'post_type' => 'sp_outcome',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_key' => 'sp_condition',
'meta_value' => '<',
);
$outcomes = get_posts( $args );
if ( $outcomes ) {
$meta[ $min ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$meta[ $min ][ 'outcome' ][] = $outcome->post_name;
}
}
}
}
// Update results
update_post_meta( $post_id, 'sp_results', $meta );

View File

@@ -153,8 +153,7 @@ class SP_Admin_CPT_Event extends SP_Admin_CPT {
$team_results = implode( ' | ', $team_results );
endif;
if ( $team_result == null ) $team_result = '-';
echo '<a class="sp-result tips" tabindex="10" title="' . $team_results . '" data-team="' . $team_id . '" href="#">' . $team_result . '</a>';
echo '<a class="sp-result tips" tabindex="10" title="' . $team_results . '" data-team="' . $team_id . '" href="#">' . ( $team_result == '' ? '-' : $team_result ) . '</a>';
echo '<input type="text" tabindex="10" class="sp-edit-result hidden small-text" data-team="' . $team_id . '" data-key="' . $main_result . '" value="' . $team_result . '"> ';
echo $team->post_title;
echo '<br>';

View File

@@ -35,7 +35,7 @@ class SP_Meta_Box_Event_Results {
$primary_results = array();
foreach ( $results as $team => $team_results ) {
if ( $main_result ) {
$primary_results[ $team ] = sportspress_array_value( $team_results, $main_result, null );
$primary_results[ $team ] = sp_array_value( $team_results, $main_result, null );
} else {
if ( is_array( $team_results ) ) {
end( $team_results );
@@ -48,7 +48,8 @@ class SP_Meta_Box_Event_Results {
arsort( $primary_results );
if ( count( $primary_results ) && ! in_array( null, $primary_results ) && count( array_unique( $primary_results ) ) === 1 ) {
if ( count( $primary_results ) && ! in_array( null, $primary_results ) ) {
if ( count( array_unique( $primary_results ) ) === 1 ) {
$args = array(
'post_type' => 'sp_outcome',
'numberposts' => -1,
@@ -60,8 +61,9 @@ class SP_Meta_Box_Event_Results {
foreach ( $results as $team => $team_results ) {
if ( array_key_exists( 'outcome', $team_results ) ) continue;
if ( $outcomes ) {
$results[ $team ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$results[ $team ][ 'outcome' ] = $outcome->post_name;
$results[ $team ][ 'outcome' ][] = $outcome->post_name;
}
}
}
@@ -78,8 +80,9 @@ class SP_Meta_Box_Event_Results {
);
$outcomes = get_posts( $args );
if ( $outcomes ) {
$results[ $max ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$results[ $max ][ 'outcome' ] = $outcome->post_name;
$results[ $max ][ 'outcome' ][] = $outcome->post_name;
}
}
}
@@ -96,8 +99,10 @@ class SP_Meta_Box_Event_Results {
);
$outcomes = get_posts( $args );
if ( $outcomes ) {
$results[ $min ][ 'outcome' ] = array();
foreach ( $outcomes as $outcome ) {
$results[ $min ][ 'outcome' ] = $outcome->post_name;
$results[ $min ][ 'outcome' ][] = $outcome->post_name;
}
}
}
}