diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php index 9ec15895..17b1350d 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php @@ -19,8 +19,9 @@ class SP_Meta_Box_Table_Data { * Output the metabox */ public static function output( $post ) { - list( $columns, $usecolumns, $data, $placeholders, $merged ) = sp_get_league_table_data( $post->ID, true ); - $adjustments = get_post_meta( $post->ID, 'sp_adjustments', true ); + $table = new SP_Table( $post ); + list( $columns, $usecolumns, $data, $placeholders, $merged ) = $table->data( true ); + $adjustments = $table->adjustments; self::table( $columns, $usecolumns, $data, $placeholders, $adjustments ); } diff --git a/includes/class-sp-table.php b/includes/class-sp-table.php new file mode 100644 index 00000000..5ac6a3e2 --- /dev/null +++ b/includes/class-sp-table.php @@ -0,0 +1,383 @@ +ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @param bool $admin + * @return array + */ + public function data( $admin = false ) { + $league_id = sp_get_the_term_id( $this->ID, 'sp_league', 0 ); + $div_id = sp_get_the_term_id( $this->ID, 'sp_season', 0 ); + $team_ids = (array)get_post_meta( $this->ID, 'sp_team', false ); + $table_stats = (array)get_post_meta( $this->ID, 'sp_teams', true ); + $usecolumns = get_post_meta( $this->ID, 'sp_columns', true ); + $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true ); + + // Get labels from result variables + $result_labels = (array)sp_get_var_labels( 'sp_result' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Get all leagues populated with stats where available + $tempdata = sp_array_combine( $team_ids, $table_stats ); + + // Create entry for each team in totals + $totals = array(); + $placeholders = array(); + + // Initialize streaks counter + $streaks = array(); + + // Initialize last counters + $last5s = array(); + $last10s = array(); + + foreach ( $team_ids as $team_id ): + if ( ! $team_id ) + continue; + + // Initialize team streaks counter + $streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 ); + + // Initialize team last counters + $last5s[ $team_id ] = array(); + $last10s[ $team_id ] = array(); + + // Add outcome types to team last counters + foreach( $outcome_labels as $key => $value ): + $last5s[ $team_id ][ $key ] = 0; + $last10s[ $team_id ][ $key ] = 0; + endforeach; + + // Initialize team totals + $totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 ); + + foreach ( $result_labels as $key => $value ): + $totals[ $team_id ][ $key . 'for' ] = 0; + $totals[ $team_id ][ $key . 'against' ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $team_id ][ $key ] = 0; + endforeach; + + // Get static stats + $static = get_post_meta( $team_id, 'sp_columns', true ); + + // Add static stats to placeholders + $placeholders[ $team_id ] = sp_array_value( $static, $div_id, array() ); + + endforeach; + + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'order' => 'ASC', + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ) + ) + ); + $events = get_posts( $args ); + + // Event loop + foreach ( $events as $event ): + + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + + foreach ( $results as $team_id => $team_result ): + + if ( ! in_array( $team_id, $team_ids ) ) + continue; + + foreach ( $team_result as $key => $value ): + + if ( $key == 'outcome' ): + + if ( ! is_array( $value ) ): + $value = array( $value ); + endif; + + foreach ( $value as $outcome ): + + // Increment events played and outcome count + if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $outcome, $totals[ $team_id ] ) ): + $totals[ $team_id ]['eventsplayed']++; + $totals[ $team_id ][ $outcome ]++; + endif; + + if ( $outcome && $outcome != '-1' ): + + // Add to streak counter + if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $outcome ) ): + $streaks[ $team_id ]['name'] = $outcome; + $streaks[ $team_id ]['count'] ++; + else: + $streaks[ $team_id ]['fire'] = 0; + endif; + + // Add to last 5 counter if sum is less than 5 + if ( array_key_exists( $team_id, $last5s ) && array_key_exists( $outcome, $last5s[ $team_id ] ) && array_sum( $last5s[ $team_id ] ) < 5 ): + $last5s[ $team_id ][ $outcome ] ++; + endif; + + // Add to last 10 counter if sum is less than 10 + if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $outcome, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ): + $last10s[ $team_id ][ $outcome ] ++; + endif; + + endif; + + endforeach; + + else: + if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ): + $totals[ $team_id ][ $key . 'for' ] += $value; + foreach( $results as $other_team_id => $other_result ): + if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ): + $totals[ $team_id ][ $key . 'against' ] += sp_array_value( $other_result, $key, 0 ); + endif; + endforeach; + endif; + endif; + + endforeach; + + endforeach; + + endforeach; + + foreach ( $streaks as $team_id => $streak ): + // Compile streaks counter and add to totals + if ( $streak['name'] ): + $args = array( + 'name' => $streak['name'], + 'post_type' => 'sp_outcome', + 'post_status' => 'publish', + 'posts_per_page' => 1 + ); + $outcomes = get_posts( $args ); + + if ( $outcomes ): + $outcome = reset( $outcomes ); + $totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count']; + else: + $totals[ $team_id ]['streak'] = null; + endif; + else: + $totals[ $team_id ]['streak'] = null; + endif; + endforeach; + + foreach ( $last5s as $team_id => $last5 ): + // Add last 5 to totals + $totals[ $team_id ]['last5'] = $last5; + endforeach; + + foreach ( $last10s as $team_id => $last10 ): + // Add last 10 to totals + $totals[ $team_id ]['last10'] = $last10; + endforeach; + + $args = array( + 'post_type' => 'sp_column', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC' + ); + $stats = get_posts( $args ); + + $columns = array(); + $this->priorities = array(); + + foreach ( $stats as $stat ): + + // Get post meta + $meta = get_post_meta( $stat->ID ); + + // Add equation to object + $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); + $stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 ); + + // Add column name to columns + $columns[ $stat->post_name ] = $stat->post_title; + + // Add order to priorities if priority is set and does not exist in array already + $priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 ); + if ( $priority && ! array_key_exists( $priority, $this->priorities ) ): + $this->priorities[ $priority ] = array( + 'column' => $stat->post_name, + 'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' ) + ); + endif; + + endforeach; + + // Sort priorities in descending order + ksort( $this->priorities ); + + // Fill in empty placeholder values for each team + foreach ( $team_ids as $team_id ): + if ( ! $team_id ) + continue; + + foreach ( $stats as $stat ): + if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ): + + // Solve + $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision ); + + // Adjustments + $placeholder += sp_array_value( sp_array_value( $adjustments, $team_id, array() ), $stat->post_name, 0 ); + + $placeholders[ $team_id ][ $stat->post_name ] = $placeholder; + endif; + endforeach; + endforeach; + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $team_id => $team_data ): + + // Add team name to row + $merged[ $team_id ] = array(); + + $team_data['name'] = get_the_title( $team_id ); + + foreach( $team_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ): + $merged[ $team_id ][ $key ] = $tempdata[ $team_id ][ $key ]; + else: + $merged[ $team_id ][ $key ] = $value; + endif; + + endforeach; + endforeach; + + uasort( $merged, array( $this, 'sort' ) ); + + // Rearrange data array to reflect values + $data = array(); + foreach( $merged as $key => $value ): + $data[ $key ] = $tempdata[ $key ]; + endforeach; + + if ( $admin ): + return array( $columns, $usecolumns, $data, $placeholders, $merged ); + else: + if ( ! is_array( $usecolumns ) ) + $usecolumns = array(); + foreach ( $columns as $key => $label ): + if ( ! in_array( $key, $usecolumns ) ): + unset( $columns[ $key ] ); + endif; + endforeach; + $labels = array_merge( array( 'name' => SP()->text->string('Team') ), $columns ); + $merged[0] = $labels; + return $merged; + endif; + } + + /** + * Sort the table by priorities. + * + * @param array $a + * @param array $b + * @return int + */ + private function sort( $a, $b ) { + + // Loop through priorities + foreach( $this->priorities as $priority ): + + // Proceed if columns are not equal + if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ): + + // Compare column values + $output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 ); + + // Flip value if descending order + if ( $priority['order'] == 'DESC' ) $output = 0 - $output; + + return ( $output > 0 ); + + endif; + + endforeach; + + // Default sort by alphabetical + return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); + } + +} diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index f0390415..6f2dc6fd 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -1590,20 +1590,6 @@ if ( !function_exists( 'sp_get_team_columns_data' ) ) { } } -if ( !function_exists( 'sp_get_player_list_data' ) ) { - function sp_get_player_list_data( $post_id, $admin = false ) { - $formatter = new SP_Data_Formatter; - return $formatter->player_list( $post_id, $admin ); - } -} - -if ( !function_exists( 'sp_get_league_table_data' ) ) { - function sp_get_league_table_data( $post_id, $admin = false ) { - $formatter = new SP_Data_Formatter; - return $formatter->league_table( $post_id, $admin ); - } -} - if ( !function_exists( 'sp_sort_table_teams' ) ) { function sp_sort_table_teams ( $a, $b ) { diff --git a/includes/sp-deprecated-functions.php b/includes/sp-deprecated-functions.php index b1c546cc..2449582e 100644 --- a/includes/sp-deprecated-functions.php +++ b/includes/sp-deprecated-functions.php @@ -167,3 +167,17 @@ function sportspress_delete_duplicate_post( &$post ) { function sportspress_highlight_admin_menu( $p = 'options-general.php', $s = 'sportspress' ) { return sp_highlight_admin_menu( $p, $s ); } + +if ( !function_exists( 'sp_get_player_list_data' ) ) { + function sp_get_player_list_data( $post_id, $admin = false ) { + $formatter = new SP_Data_Formatter; + return $formatter->player_list( $post_id, $admin ); + } +} + +if ( !function_exists( 'sp_get_league_table_data' ) ) { + function sp_get_league_table_data( $post_id, $admin = false ) { + $table = new SP_Table( $post_id ); + return $table->data( $admin ); + } +} diff --git a/templates/league-table.php b/templates/league-table.php index 4e76913a..10b6a59f 100644 --- a/templates/league-table.php +++ b/templates/league-table.php @@ -27,7 +27,9 @@ extract( $defaults, EXTR_SKIP ); $output = '