From f41a117a7b5bd0cd5fd7340482979c697c1171e1 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Tue, 9 Dec 2014 21:18:38 +1100 Subject: [PATCH] Auto calculate event outcomes based on primary results close #57 --- .../class-sp-meta-box-event-results.php | 76 +++++++++++++++++++ .../class-sp-meta-box-outcome-details.php | 24 ++++++ includes/admin/views/html-admin-config.php | 2 + includes/sp-core-functions.php | 22 +++++- 4 files changed, 123 insertions(+), 1 deletion(-) diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php index 1a996a86..c8fb3dfb 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php @@ -29,6 +29,81 @@ class SP_Meta_Box_Event_Results { */ public static function save( $post_id, $post ) { $results = (array)sp_array_value( $_POST, 'sp_results', array() ); + $main_result = get_option( 'sportspress_primary_result', null ); + + // Auto outcome + $primary_results = array(); + foreach ( $results as $team => $team_results ) { + if ( $main_result ) { + $primary_results[ $team ] = sportspress_array_value( $team_results, $main_result, null ); + } else { + if ( is_array( $team_results ) ) { + end( $team_results ); + $primary_results[ $team ] = prev( $team_results ); + } else { + $primary_results[ $team ] = null; + } + } + } + + arsort( $primary_results ); + + if ( count( $primary_results ) && ! in_array( null, $primary_results ) && 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 ( $results as $team => $team_results ) { + if ( array_key_exists( 'outcome', $team_results ) ) continue; + if ( $outcomes ) { + foreach ( $outcomes as $outcome ) { + $results[ $team ][ 'outcome' ] = $outcome->post_name; + } + } + } + } else { + reset( $primary_results ); + $max = key( $primary_results ); + if ( ! array_key_exists( 'outcome', $results[ $max ] ) ) { + $args = array( + 'post_type' => 'sp_outcome', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_key' => 'sp_condition', + 'meta_value' => '>', + ); + $outcomes = get_posts( $args ); + if ( $outcomes ) { + foreach ( $outcomes as $outcome ) { + $results[ $max ][ 'outcome' ] = $outcome->post_name; + } + } + } + + end( $primary_results ); + $min = key( $primary_results ); + if ( ! array_key_exists( 'outcome', $results[ $min ] ) ) { + $args = array( + 'post_type' => 'sp_outcome', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_key' => 'sp_condition', + 'meta_value' => '<', + ); + $outcomes = get_posts( $args ); + if ( $outcomes ) { + foreach ( $outcomes as $outcome ) { + $results[ $min ][ 'outcome' ] = $outcome->post_name; + } + } + } + } + + // Update meta update_post_meta( $post_id, 'sp_results', $results ); update_post_meta( $post_id, 'sp_result_columns', sp_array_value( $_POST, 'sp_result_columns', array() ) ); } @@ -90,6 +165,7 @@ class SP_Meta_Box_Event_Results { 'class' => 'sp-outcome', 'property' => 'multiple', 'chosen' => true, + 'placeholder' => __( '(Auto)', 'sportspress' ), ); sp_dropdown_pages( $args ); ?> diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-outcome-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-outcome-details.php index 669f082b..fb76f358 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-outcome-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-outcome-details.php @@ -24,6 +24,10 @@ class SP_Meta_Box_Outcome_Details extends SP_Meta_Box_Config { public static function output( $post ) { wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' ); $abbreviation = get_post_meta( $post->ID, 'sp_abbreviation', true ); + $condition = get_post_meta( $post->ID, 'sp_condition', true ); + $main_result = get_option( 'sportspress_primary_result', null ); + $result = get_page_by_path( $main_result, ARRAY_A, 'sp_result' ); + $label = sp_array_value( $result, 'post_title', __( 'Primary', 'sportspress' ) ); ?>

@@ -34,6 +38,25 @@ class SP_Meta_Box_Outcome_Details extends SP_Meta_Box_Config {

+

+

+ +

+ @@ -35,6 +36,7 @@ post_title; ?> post_name; ?> ID ); ?> + ID ); ?>

post_excerpt; ?>

diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 9fe1bf8c..fa2f14ef 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -352,6 +352,26 @@ if ( !function_exists( 'sp_get_post_abbreviation' ) ) { } } +if ( !function_exists( 'sp_get_post_condition' ) ) { + function sp_get_post_condition( $post_id ) { + $condition = get_post_meta ( $post_id, 'sp_condition', true ); + $main_result = get_option( 'sportspress_primary_result', null ); + $result = get_page_by_path( $main_result, ARRAY_A, 'sp_result' ); + $label = sp_array_value( $result, 'post_title', __( 'Primary', 'sportspress' ) ); + if ( $condition ): + $conditions = array( + '0' => '—', + '>' => sprintf( __( 'Most %s', 'sportspress' ), $label ), + '<' => sprintf( __( 'Least %s', 'sportspress' ), $label ), + '=' => sprintf( __( 'Equal %s', 'sportspress' ), $label ), + ); + return sp_array_value( $conditions, $condition, '—' ); + else: + return '—'; + endif; + } +} + if ( !function_exists( 'sp_get_post_precision' ) ) { function sp_get_post_precision( $post_id ) { $precision = get_post_meta ( $post_id, 'sp_precision', true ); @@ -752,7 +772,7 @@ if ( !function_exists( 'sp_post_checklist' ) ) { $meta, 'numberposts' => -1, 'post_per_page' => -1 ); + $query = array( 'post_type' => $meta, 'numberposts' => -1, 'post_per_page' => -1, 'orderby' => 'menu_order' ); if ( $meta == 'sp_player' ): $query['meta_key'] = 'sp_number'; $query['orderby'] = 'meta_value_num';