diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js index e7dcfdb3..10c3f502 100644 --- a/assets/js/admin/sportspress-admin.js +++ b/assets/js/admin/sportspress-admin.js @@ -889,4 +889,13 @@ jQuery(document).ready(function($){ // Trigger box score time converter $('.sp-convert-time-input').change(); -}); + + // Trigger show/hide of team table ordering + $('#sp_orderby').change(function(){ + if ($('#sp_orderby').val() == 'default') { + $("#sp_order").hide(); + }else{ + $('#sp_order').show(); + } + }); +}); \ No newline at end of file diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-details.php index 865d5b6d..8257a250 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-details.php @@ -29,6 +29,8 @@ class SP_Meta_Box_Table_Details { $date_to = get_post_meta( $post->ID, 'sp_date_to', true ); $date_past = get_post_meta( $post->ID, 'sp_date_past', true ); $date_relative = get_post_meta( $post->ID, 'sp_date_relative', true ); + $orderby = get_post_meta( $post->ID, 'sp_orderby', true ); + $order = get_post_meta( $post->ID, 'sp_order', true ); $event_status = get_post_meta( $post->ID, 'sp_event_status', true ); if ( empty( $event_status ) ) { $event_status = array( 'publish', 'future' ); @@ -99,6 +101,31 @@ class SP_Meta_Box_Table_Details { > Scheduled/Future

+

+

+ array( + 'default' => __( 'Default', 'sportspress' ), + 'name' => __( 'Name', 'sportspress' ), + ), + 'post_type' => array( 'sp_column' ), + 'name' => 'sp_orderby', + 'selected' => $orderby, + 'values' => 'slug', + ); + sp_dropdown_pages( $args ); + ?> +

+
> +

+

+ +

+
ID, 'sp_columns', true ); $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true ); $select = get_post_meta( $this->ID, 'sp_select', true ); + $this->orderby = get_post_meta( $this->ID, 'sp_orderby', true ); + $this->orderbyorder = get_post_meta( $this->ID, 'sp_order', true ); $link_events = get_option( 'sportspress_link_events', 'yes' ) === 'yes' ? true : false; $form_limit = (int) get_option( 'sportspress_form_limit', 5 ); @@ -53,7 +59,10 @@ class SP_League_Table extends SP_Secondary_Post { $this->date = 0; // Apply defaults + if ( empty( $this->orderby ) ) $this->orderby = 'default'; + if ( empty( $this->orderbyorder ) ) $this->orderbyorder = 'ASC'; if ( empty( $select ) ) $select = 'auto'; + if ( 'range' == $this->date ) { @@ -789,6 +798,17 @@ class SP_League_Table extends SP_Secondary_Post { } } + // Rearrange the table if Default ordering is not selected + if ( $this->orderby != 'default' ) { + uasort( $merged, array( $this, 'simple_order' ) ); + // Recalculate position of teams + $this->pos = 0; + $this->counter = 0; + foreach ( $merged as $team_id => $team_columns ) { + $merged[ $team_id ]['pos'] = $this->calculate_pos( $team_columns, $team_id, false ); + } + } + // Rearrange data array to reflect values $data = array(); foreach( $merged as $key => $value ): @@ -840,6 +860,30 @@ class SP_League_Table extends SP_Secondary_Post { // Default sort by alphabetical return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); } + + /** + * Sort the table by ordering. + * + * @param array $a + * @param array $b + * @return int + */ + public function simple_order( $a, $b ) { + + if ( $this->orderbyorder == 'DESC' ) { + if ( $this->orderby == 'name' ){ + return strcmp( sp_array_value( $b, 'name', '' ), sp_array_value( $a, 'name', '' ) ); + }else{ + return (float) $b[ $this->orderby ] - (float) $a[ $this->orderby ]; + } + }else{ + if ( $this->orderby == 'name' ){ + return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); + }else{ + return (float) $a[ $this->orderby ] - (float) $b[ $this->orderby ]; + } + } + } /** * Find accurate position of teams.