Auto calculate event outcomes based on primary results close #57

This commit is contained in:
Brian Miyaji
2014-12-09 21:18:38 +11:00
parent f942e4861c
commit f41a117a7b
4 changed files with 123 additions and 1 deletions

View File

@@ -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 );
?>

View File

@@ -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' ) );
?>
<p><strong><?php _e( 'Variable', 'sportspress' ); ?></strong></p>
<p>
@@ -34,6 +38,25 @@ class SP_Meta_Box_Outcome_Details extends SP_Meta_Box_Config {
<p>
<input name="sp_abbreviation" type="text" id="sp_abbreviation" value="<?php echo $abbreviation; ?>" placeholder="<?php echo substr( $post->post_title, 0, 1 ); ?>">
</p>
<p><strong><?php _e( 'Condition', 'sportspress' ); ?></strong></p>
<p>
<select name="sp_condition">
<?php
$options = array(
'0' => __( '&mdash;', 'sportspress' ),
'>' => sprintf( __( 'Most %s', 'sportspress' ), $label ),
'<' => sprintf( __( 'Least %s', 'sportspress' ), $label ),
'=' => sprintf( __( 'Equal %s', 'sportspress' ), $label ),
);
for( $i = 1; $i <= $count->publish; $i++ ):
$options[ $i ] = $i;
endfor;
foreach ( $options as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( true, $key == $condition, false ), $value );
endforeach;
?>
</select>
</p>
<?php
}
@@ -42,5 +65,6 @@ class SP_Meta_Box_Outcome_Details extends SP_Meta_Box_Config {
*/
public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_abbreviation', sp_array_value( $_POST, 'sp_abbreviation', array() ) );
update_post_meta( $post_id, 'sp_condition', sp_array_value( $_POST, 'sp_condition', array() ) );
}
}

View File

@@ -26,6 +26,7 @@
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Variable', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Abbreviation', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Condition', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Description', 'sportspress' ); ?></th>
<th scope="col" class="edit"></th>
</tr>
@@ -35,6 +36,7 @@
<td class="row-title"><?php echo $row->post_title; ?></td>
<td><?php echo $row->post_name; ?></td>
<td><?php echo sp_get_post_abbreviation( $row->ID ); ?></td>
<td><?php echo sp_get_post_condition( $row->ID ); ?></td>
<td><p class="description"><?php echo $row->post_excerpt; ?></p></td>
<td class="edit"><a class="button" href="<?php echo get_edit_post_link( $row->ID ); ?>"><?php _e( 'Edit', 'sportspress' ); ?></s></td>
</tr>

View File

@@ -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' => '&mdash;',
'>' => sprintf( __( 'Most %s', 'sportspress' ), $label ),
'<' => sprintf( __( 'Least %s', 'sportspress' ), $label ),
'=' => sprintf( __( 'Equal %s', 'sportspress' ), $label ),
);
return sp_array_value( $conditions, $condition, '&mdash;' );
else:
return '&mdash;';
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' ) ) {
<?php
$selected = sp_array_between( (array)get_post_meta( $post_id, $meta, false ), 0, $index );
if ( empty( $posts ) ):
$query = array( 'post_type' => $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';