Add equation selector

This commit is contained in:
ThemeBoy
2013-11-26 03:15:06 +11:00
parent 7aeec083b8
commit 639f4b86da
4 changed files with 149 additions and 59 deletions

View File

@@ -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 ) );
?>
<div>
<p class="sp-equation-selector">
<select data-remove-text="<?php _e( 'Remove', 'sportspress' ); ?>">
<option value=""><?php _e( 'Select', 'sportspress' ); ?></option>
<optgroup label="<?php _e( 'Events', 'sportspress' ); ?>">
<option value="wins"><?php _e( 'Wins', 'sportspress' ); ?></option>
<option value="draws"><?php _e( 'Draws', 'sportspress' ); ?></option>
<option value="ties"><?php _e( 'Ties', 'sportspress' ); ?></option>
<option value="losses"><?php _e( 'Losses', 'sportspress' ); ?></option>
</optgroup>
<optgroup label="<?php _e( 'Statistics', 'sportspress' ); ?>">
<?php foreach ( $stats as $stat ): ?>
<option value="<?php echo $stat->post_name; ?>"><?php echo $stat->post_title; ?></option>
<?php endforeach; ?>
</optgroup>
<optgroup label="<?php _e( 'Operators', 'sportspress' ); ?>">
<option value="+">&plus;</option>
<option value="-">&minus;</option>
<option value="X">&times;</option>
<option value="/">&divide;</option>
<option value="(">(</option>
<option value=")">)</option>
</optgroup>
<optgroup label="<?php _e( 'Constants', 'sportspress' ); ?>">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</optgroup>
</select>
<?php
foreach ( $equation as $piece ):
sp_get_equation_selector( 'stat', $piece );
endforeach;
?>
</p>
</div>
<?php
sp_nonce();
}
?>

View File

@@ -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();
});

View File

@@ -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('#<a.*?>.*?</a>#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() ) );

View File

@@ -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( '+' => '&plus;', '-' => '&minus;', '*' => '&times;', '/' => '&divide;', '(' => '(', ')' => ')' );
// 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;
?>
<select name="sp_equation[]" data-remove-text="<?php _e( 'Remove', 'sportspress' ); ?>">
<option value=""><?php _e( 'Select', 'sportspress' ); ?></option>
<?php
foreach ( $options as $label => $option ):
printf( '<optgroup label="%s">', $label );
foreach ( $option as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( true, $key == $selected, false ), $value );
endforeach;
echo '</optgroup>';
endforeach;
?>
</select>
<?php
}
}
if ( !function_exists( 'sp_get_eos_rows' ) ) {
function sp_get_eos_rows( $raw ) {
$raw = str_replace( array( "\r\n", ' ' ), array( "\n", '' ), $raw );