From 639f4b86daf22b0f1356af1b9a91250fb7dcca8f Mon Sep 17 00:00:00 2001 From: ThemeBoy Date: Tue, 26 Nov 2013 03:15:06 +1100 Subject: [PATCH] Add equation selector --- admin/post-types/stat.php | 62 +++---------------- assets/js/admin.js | 12 ++-- sportspress-actions.php | 10 +++ sportspress-helpers.php | 124 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 59 deletions(-) diff --git a/admin/post-types/stat.php b/admin/post-types/stat.php index fadfec98..d3d93a6e 100644 --- a/admin/post-types/stat.php +++ b/admin/post-types/stat.php @@ -35,66 +35,18 @@ function sp_stat_meta_init() { } function sp_stat_equation_meta( $post ) { - $args = array( - 'post_type' => 'sp_stat', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'exclude' => $post->ID - ); - $sports = get_the_terms( $post->ID, 'sp_sport' ); - if ( ! empty( $sports ) ): - $terms = array(); - foreach ( $sports as $sport ): - $terms[] = $sport->slug; - endforeach; - $args['tax_query'] = array( - array( - 'taxonomy' => 'sp_sport', - 'field' => 'slug', - 'terms' => $terms - ) - ); - endif; - $stats = get_posts( $args ); + $equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) ); ?>

- +

\ No newline at end of file diff --git a/assets/js/admin.js b/assets/js/admin.js index 33704992..9d8b9a21 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -64,12 +64,16 @@ jQuery(document).ready(function($){ } // Equation selector - $('.sp-equation-selector select').change(function() { + $('.sp-equation-selector select:last').change(function() { + $(this).siblings().change(function() { + if($(this).val() == '') $(this).remove(); + }).find('option:first').text($(this).attr('data-remove-text')); if($(this).val() != '') { - $(this).before($(this).clone().val($(this).val())).val('').siblings().change(function() { - if($(this).val() == '') $(this).remove(); - }).find('option:first').text($(this).attr('data-remove-text')); + $(this).before($(this).clone().val($(this).val())).val(''); } }); + // Trigger equation selector + $('.sp-equation-selector select:last').change(); + }); \ No newline at end of file diff --git a/sportspress-actions.php b/sportspress-actions.php index 5c7d8f54..2738c869 100644 --- a/sportspress-actions.php +++ b/sportspress-actions.php @@ -34,6 +34,9 @@ function sp_manage_posts_custom_column( $column, $post_id ) { case 'sp_sport': echo get_the_terms ( $post_id, 'sp_sport' ) ? preg_replace('#.*?#i', '', sp_the_plain_terms( $post_id, 'sp_sport' ) ) : '—'; break; + case 'sp_equation': + echo get_post_meta ( $post_id, 'sp_equation', true ); + break; case 'sp_player': echo sp_the_posts( $post_id, 'sp_player' ); break; @@ -143,6 +146,13 @@ function sp_save_post( $post_id ) { break; + case ( 'sp_stat' ): + + // Update equation as string + update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); + + break; + case ( 'sp_player' ): update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); diff --git a/sportspress-helpers.php b/sportspress-helpers.php index 55e03c3a..f85d1da2 100644 --- a/sportspress-helpers.php +++ b/sportspress-helpers.php @@ -257,6 +257,130 @@ if ( !function_exists( 'sp_post_checklist' ) ) { } } + +if ( !function_exists( 'sp_get_equation_selector' ) ) { + function sp_get_equation_selector( $type = null, $selected = null ) { + + // Initialize options array + $options = array(); + + // Create array of variables + + if ( $type == 'stat' ): + + // Create array of events ## TODO: should be a custom post under events called outcomes + $events = array( 'wins' => __( 'Wins', 'sportspress' ), 'draws' => __( 'Draws', 'sportspress' ), 'ties' => __( 'Ties', 'sportspres' ), 'losses' => __( 'Losses', 'sportspress' ) ); + + // Add events to options + $options[ __( 'Events', 'sportspress' ) ] = (array) $events; + + $vars = array(); + + // Get stats within the sports that the current stat is in ### TODO: should be for sport selected + $args = array( + 'post_type' => 'sp_stat', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'exclude' => $post->ID + ); + $sports = get_the_terms( $post->ID, 'sp_sport' ); + if ( ! empty( $sports ) ): + $terms = array(); + foreach ( $sports as $sport ): + $terms[] = $sport->slug; + endforeach; + $args['tax_query'] = array( + array( + 'taxonomy' => 'sp_sport', + 'field' => 'slug', + 'terms' => $terms + ) + ); + endif; + $stats = get_posts( $args ); + + // Add vars to the array + foreach ( $stats as $stat ): + $vars[ $stat->post_name ] = $stat->post_title; + endforeach; + + // Add stats to options + $options[ __( 'Statistics', 'sportspress' ) ] = (array) $vars; + + elseif ( $type == 'metric' ): + + $vars = array(); + + // Get metrics within the sports that the current metric is in ### TODO: should be for sport selected + $args = array( + 'post_type' => 'sp_metric', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'exclude' => $post->ID + ); + $sports = get_the_terms( $post->ID, 'sp_sport' ); + if ( ! empty( $sports ) ): + $terms = array(); + foreach ( $sports as $sport ): + $terms[] = $sport->slug; + endforeach; + $args['tax_query'] = array( + array( + 'taxonomy' => 'sp_sport', + 'field' => 'slug', + 'terms' => $terms + ) + ); + endif; + $metrics = get_posts( $args ); + + // Add vars to the array + foreach ( $metrics as $metric ): + $vars[ $metric->post_name ] = $metric->post_title; + endforeach; + + // Add metrics to options + $options[ __( 'Metrics', 'sportspress' ) ] = (array) $vars; + + endif; + + // Create array of operators + $operators = array( '+' => '+', '-' => '−', '*' => '×', '/' => '÷', '(' => '(', ')' => ')' ); + + // Add operators to options + $options[ __( 'Operators', 'sportspress' ) ] = (array) $operators; + + // Create array of constants + $max = 10; + $constants = array(); + for ( $i = 1; $i < $max; $i ++ ): + $constants[$i] = $i; + endfor; + + // Add constants to options + $options[ __( 'Constants', 'sportspress' ) ] = (array) $constants; + + ?> + +