Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.13 KB | None | 0 0
  1. <?php
  2. /**
  3. * League Table Class
  4. *
  5. * The SportsPress league table class handles individual league table data.
  6. *
  7. * @class SP_League_Table
  8. * @version 2.3
  9. * @package SportsPress/Classes
  10. * @category Class
  11. * @author ThemeBoy
  12. */
  13. class SP_League_Table extends SP_Custom_Post{
  14.  
  15. /** @var array The sort priorities array. */
  16. public $priorities;
  17.  
  18. /** @var array Positions of teams in the table. */
  19. public $pos;
  20.  
  21. /** @var array Inremental value for team position. */
  22. public $counter;
  23.  
  24. /** @var array Teams to check for tiebreakers. */
  25. public $tiebreakers = array();
  26.  
  27. /**
  28. * Returns formatted data
  29. *
  30. * @access public
  31. * @param bool $admin
  32. * @return array
  33. */
  34. public function data( $admin = false, $team_ids = null ) {
  35. $league_ids = sp_get_the_term_ids( $this->ID, 'sp_league' );
  36. $season_ids = sp_get_the_term_ids( $this->ID, 'sp_season' );
  37. $table_stats = (array)get_post_meta( $this->ID, 'sp_teams', true );
  38. $usecolumns = get_post_meta( $this->ID, 'sp_columns', true );
  39. $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true );
  40. $select = get_post_meta( $this->ID, 'sp_select', true );
  41. $abbreviate_teams = get_option( 'sportspress_abbreviate_teams', 'yes' ) === 'yes' ? true : false;
  42. $link_events = get_option( 'sportspress_link_events', 'yes' ) === 'yes' ? true : false;
  43. $form_limit = (int) get_option( 'sportspress_form_limit', 5 );
  44.  
  45. // Apply defaults
  46. if ( empty( $select ) ) $select = 'auto';
  47.  
  48. // Get labels from result variables
  49. $result_labels = (array)sp_get_var_labels( 'sp_result' );
  50.  
  51. // Get labels from outcome variables
  52. $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
  53.  
  54. // Get post type
  55. $post_type = sp_get_post_mode_type( $this->ID );
  56.  
  57. // Determine if main loop
  58. if ( $team_ids ) {
  59.  
  60. $is_main_loop = false;
  61.  
  62. } else {
  63.  
  64. // Get teams automatically if set to auto
  65. if ( 'auto' == $select ) {
  66. $team_ids = array();
  67.  
  68. $args = array(
  69. 'post_type' => $post_type,
  70. 'numberposts' => -1,
  71. 'posts_per_page' => -1,
  72. 'order' => 'ASC',
  73. 'tax_query' => array(
  74. 'relation' => 'AND',
  75. ),
  76. 'fields' => 'ids',
  77. );
  78.  
  79. if ( $league_ids ):
  80. $args['tax_query'][] = array(
  81. 'taxonomy' => 'sp_league',
  82. 'field' => 'term_id',
  83. 'terms' => $league_ids
  84. );
  85. endif;
  86.  
  87. if ( $season_ids ):
  88. $args['tax_query'][] = array(
  89. 'taxonomy' => 'sp_season',
  90. 'field' => 'term_id',
  91. 'terms' => $season_ids
  92. );
  93. endif;
  94.  
  95. $team_ids = get_posts( $args );
  96. } else {
  97. $team_ids = (array)get_post_meta( $this->ID, 'sp_team', false );
  98. }
  99.  
  100. $is_main_loop = true;
  101. }
  102.  
  103. // Get all leagues populated with stats where available
  104. $tempdata = sp_array_combine( $team_ids, $table_stats );
  105.  
  106. // Create entry for each team in totals
  107. $totals = array();
  108. $placeholders = array();
  109.  
  110. // Initialize incremental counter
  111. $this->pos = 0;
  112. $this->counter = 0;
  113.  
  114. // Initialize team compare
  115. $this->compare = null;
  116.  
  117. // Initialize streaks counter
  118. $streaks = array();
  119.  
  120. // Initialize form counter
  121. $forms = array();
  122.  
  123. // Initialize last counters
  124. $last5s = array();
  125. $last10s = array();
  126.  
  127. // Initialize record counters
  128. $homerecords = array();
  129. $awayrecords = array();
  130.  
  131. foreach ( $team_ids as $team_id ):
  132. if ( ! $team_id )
  133. continue;
  134.  
  135. // Initialize team streaks counter
  136. $streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 );
  137.  
  138. // Initialize team form counter
  139. $forms[ $team_id ] = array();
  140.  
  141. // Initialize team last counters
  142. $last5s[ $team_id ] = array();
  143. $last10s[ $team_id ] = array();
  144.  
  145. // Initialize team record counters
  146. $homerecords[ $team_id ] = array();
  147. $awayrecords[ $team_id ] = array();
  148.  
  149. // Add outcome types to team last and record counters
  150. foreach( $outcome_labels as $key => $value ):
  151. $last5s[ $team_id ][ $key ] = 0;
  152. $last10s[ $team_id ][ $key ] = 0;
  153. $homerecords[ $team_id ][ $key ] = 0;
  154. $awayrecords[ $team_id ][ $key ] = 0;
  155. endforeach;
  156.  
  157. // Initialize team totals
  158. $totals[ $team_id ] = array(
  159. 'eventsplayed' => 0,
  160. 'eventsplayed_home' => 0,
  161. 'eventsplayed_away' => 0,
  162. 'eventsplayed_venue' => 0,
  163. 'eventminutes' => 0,
  164. 'eventminutes_home' => 0,
  165. 'eventminutes_away' => 0,
  166. 'eventminutes_venue' => 0,
  167. 'streak' => 0,
  168. 'streak_home' => 0,
  169. 'streak_away' => 0,
  170. 'streak_venue' => 0,
  171. );
  172.  
  173. foreach ( $result_labels as $key => $value ):
  174. $totals[ $team_id ][ $key . 'for' ] = 0;
  175. $totals[ $team_id ][ $key . 'for_home' ] = 0;
  176. $totals[ $team_id ][ $key . 'for_away' ] = 0;
  177. $totals[ $team_id ][ $key . 'for_venue' ] = 0;
  178. $totals[ $team_id ][ $key . 'against' ] = 0;
  179. $totals[ $team_id ][ $key . 'against_home' ] = 0;
  180. $totals[ $team_id ][ $key . 'against_away' ] = 0;
  181. $totals[ $team_id ][ $key . 'against_venue' ] = 0;
  182. endforeach;
  183.  
  184. foreach ( $outcome_labels as $key => $value ):
  185. $totals[ $team_id ][ $key ] = 0;
  186. $totals[ $team_id ][ $key . '_home' ] = 0;
  187. $totals[ $team_id ][ $key . '_away' ] = 0;
  188. $totals[ $team_id ][ $key . '_venue' ] = 0;
  189. endforeach;
  190.  
  191. // Get static stats
  192. $static = get_post_meta( $team_id, 'sp_columns', true );
  193.  
  194. if ( 'yes' == get_option( 'sportspress_team_column_editing', 'no' ) && $league_ids && $season_ids ):
  195. // Add static stats to placeholders
  196. foreach ( $league_ids as $league_id ):
  197. foreach ( $season_ids as $season_id ):
  198. $placeholders[ $team_id ] = (array) sp_array_value( sp_array_value( $static, $league_id, array() ), $season_id, array() );
  199. endforeach;
  200. endforeach;
  201. endif;
  202.  
  203. endforeach;
  204.  
  205. $args = array(
  206. 'post_type' => 'sp_event',
  207. 'post_status' => array( 'publish', 'future' ),
  208. 'numberposts' => -1,
  209. 'posts_per_page' => -1,
  210. 'orderby' => 'post_date',
  211. 'order' => 'DESC',
  212. 'meta_query' => array(
  213. array(
  214. 'key' => 'sp_format',
  215. 'value' => apply_filters( 'sportspress_competitive_event_formats', array( 'league' ) ),
  216. 'compare' => 'IN',
  217. ),
  218. ),
  219. 'tax_query' => array(
  220. 'relation' => 'AND',
  221. ),
  222. );
  223.  
  224. if ( $league_ids ):
  225. $args['tax_query'][] = array(
  226. 'taxonomy' => 'sp_league',
  227. 'field' => 'term_id',
  228. 'terms' => $league_ids
  229. );
  230. endif;
  231.  
  232. if ( $season_ids ):
  233. $args['tax_query'][] = array(
  234. 'taxonomy' => 'sp_season',
  235. 'field' => 'term_id',
  236. 'terms' => $season_ids
  237. );
  238. endif;
  239.  
  240. $args = apply_filters( 'sportspress_table_data_event_args', $args );
  241.  
  242. if ( ! $is_main_loop ):
  243. if ( sizeof( $team_ids ) ):
  244. $args['meta_query'][] = array(
  245. 'key' => 'sp_team',
  246. 'value' => $team_ids,
  247. 'compare' => 'IN',
  248. );
  249. endif;
  250. endif;
  251.  
  252. $events = get_posts( $args );
  253.  
  254. $e = 0;
  255.  
  256. // Event loop
  257. foreach ( $events as $event ):
  258.  
  259. $teams = (array)get_post_meta( $event->ID, 'sp_team', false );
  260. $teams = array_filter( $teams );
  261. if ( ! $is_main_loop && sizeof( array_diff( $teams, $team_ids ) ) ) continue;
  262.  
  263. $results = (array)get_post_meta( $event->ID, 'sp_results', true );
  264. $minutes = get_post_meta( $event->ID, 'sp_minutes', true );
  265. if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 );
  266.  
  267. $i = 0;
  268.  
  269. foreach ( $results as $team_id => $team_result ):
  270.  
  271. if ( ! in_array( $team_id, $teams ) ) continue;
  272.  
  273. if ( ! in_array( $team_id, $team_ids ) ) {
  274. $i++;
  275. continue;
  276. }
  277.  
  278. if ( $team_result ): foreach ( $team_result as $key => $value ):
  279.  
  280. if ( $key == 'outcome' ):
  281.  
  282. if ( ! is_array( $value ) ):
  283. $value = array( $value );
  284. endif;
  285.  
  286. foreach ( $value as $outcome ):
  287.  
  288. // Increment events played and outcome count
  289. if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $outcome, $totals[ $team_id ] ) ):
  290. $totals[ $team_id ]['eventsplayed'] ++;
  291. $totals[ $team_id ]['eventminutes'] += $minutes;
  292. $totals[ $team_id ][ $outcome ] ++;
  293.  
  294. // Add to home or away stats
  295. if ( 0 === $i ):
  296. $totals[ $team_id ]['eventsplayed_home'] ++;
  297. $totals[ $team_id ]['eventminutes_home'] += $minutes;
  298. $totals[ $team_id ][ $outcome . '_home' ] ++;
  299. else:
  300. $totals[ $team_id ]['eventsplayed_away'] ++;
  301. $totals[ $team_id ]['eventminutes_away'] += $minutes;
  302. $totals[ $team_id ][ $outcome . '_away' ] ++;
  303. endif;
  304.  
  305. // Add to venue stats
  306. if ( sp_is_home_venue( $team_id, $event->ID ) ):
  307. $totals[ $team_id ]['eventsplayed_venue'] ++;
  308. $totals[ $team_id ]['eventminutes_venue'] += $minutes;
  309. $totals[ $team_id ][ $outcome . '_venue' ] ++;
  310. endif;
  311. endif;
  312.  
  313. if ( $outcome && $outcome != '-1' ):
  314.  
  315. // Add to streak counter
  316. if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $outcome ) ):
  317. $streaks[ $team_id ]['name'] = $outcome;
  318. $streaks[ $team_id ]['count'] ++;
  319. else:
  320. $streaks[ $team_id ]['fire'] = 0;
  321. endif;
  322.  
  323. // Add to form counter
  324. $forms[ $team_id ][] = array(
  325. 'id' => $event->ID,
  326. 'outcome' => $outcome,
  327. );
  328.  
  329. // Add to last 5 counter if sum is less than 5
  330. if ( array_key_exists( $team_id, $last5s ) && array_key_exists( $outcome, $last5s[ $team_id ] ) && array_sum( $last5s[ $team_id ] ) < 5 ):
  331. $last5s[ $team_id ][ $outcome ] ++;
  332. endif;
  333.  
  334. // Add to last 10 counter if sum is less than 10
  335. if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $outcome, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ):
  336. $last10s[ $team_id ][ $outcome ] ++;
  337. endif;
  338.  
  339. // Add to home or away record
  340. if ( 0 === $i ) {
  341. if ( array_key_exists( $team_id, $homerecords ) && array_key_exists( $outcome, $homerecords[ $team_id ] ) ) {
  342. $homerecords[ $team_id ][ $outcome ] ++;
  343. }
  344. } else {
  345. if ( array_key_exists( $team_id, $awayrecords ) && array_key_exists( $outcome, $awayrecords[ $team_id ] ) ) {
  346. $awayrecords[ $team_id ][ $outcome ] ++;
  347. }
  348. }
  349.  
  350. endif;
  351.  
  352. endforeach;
  353.  
  354. else:
  355. if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
  356.  
  357. // Get numeric value
  358. $value = floatval( $value );
  359.  
  360. $totals[ $team_id ][ $key . 'for' ] += $value;
  361. $totals[ $team_id ][ $key . 'for' . ( $e + 1 ) ] = $value;
  362.  
  363. // Add to home or away stats
  364. if ( 0 === $i ):
  365. $totals[ $team_id ][ $key . 'for_home' ] += $value;
  366. else:
  367. $totals[ $team_id ][ $key . 'for_away' ] += $value;
  368. endif;
  369.  
  370. // Add to venue stats
  371. if ( sp_is_home_venue( $team_id, $event->ID ) ):
  372. $totals[ $team_id ][ $key . 'for_venue' ] += $value;
  373. endif;
  374.  
  375. foreach( $results as $other_team_id => $other_result ):
  376. if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ):
  377.  
  378. // Get numeric value of other team's result
  379. $value = floatval( sp_array_value( $other_result, $key, 0 ) );
  380.  
  381. $totals[ $team_id ][ $key . 'against' ] += $value;
  382. $totals[ $team_id ][ $key . 'against' . ( $e + 1 ) ] = $value;
  383.  
  384. // Add to home or away stats
  385. if ( 0 === $i ):
  386. $totals[ $team_id ][ $key . 'against_home' ] += $value;
  387. else:
  388. $totals[ $team_id ][ $key . 'against_away' ] += $value;
  389. endif;
  390.  
  391. // Add to venue stats
  392. if ( sp_is_home_venue( $team_id, $event->ID ) ):
  393. $totals[ $team_id ][ $key . 'against_venue' ] += $value;
  394. endif;
  395. endif;
  396. endforeach;
  397. endif;
  398. endif;
  399.  
  400. endforeach; endif;
  401.  
  402. $i++;
  403.  
  404. endforeach;
  405.  
  406. $e++;
  407.  
  408. endforeach;
  409.  
  410. // Get outcomes
  411. $outcomes = array();
  412.  
  413. $args = array(
  414. 'post_type' => 'sp_outcome',
  415. 'post_status' => 'publish',
  416. 'posts_per_page' => -1,
  417. );
  418. $posts = get_posts( $args );
  419.  
  420. if ( $posts ):
  421. foreach ( $posts as $post ):
  422. // Get ID
  423. $id = $post->ID;
  424.  
  425. // Get title
  426. $title = $post->post_title;
  427.  
  428. // Get abbreviation
  429. $abbreviation = get_post_meta( $id, 'sp_abbreviation', true );
  430. if ( ! $abbreviation ):
  431. $abbreviation = substr( $title, 0, 1 );
  432. endif;
  433.  
  434. // Get color
  435. $color = get_post_meta( $id, 'sp_color', true );
  436. if ( '' === $color ) $color = '#888888';
  437.  
  438. $outcomes[ $post->post_name ] = array(
  439. 'id' => $id,
  440. 'title' => $title,
  441. 'abbreviation' => $abbreviation,
  442. 'color' => $color,
  443. );
  444. endforeach;
  445. endif;
  446.  
  447. foreach ( $streaks as $team_id => $streak ):
  448. // Compile streaks counter and add to totals
  449. if ( $streak['name'] ):
  450. $outcome = sp_array_value( $outcomes, $streak['name'], false );
  451. if ( $outcome ):
  452. $color = $outcome['color'];
  453. $totals[ $team_id ]['streak'] = '<span style="color:' . $color . '">' . $outcome['abbreviation'] . $streak['count'] . '</span>';
  454. else:
  455. $totals[ $team_id ]['streak'] = null;
  456. endif;
  457. else:
  458. $totals[ $team_id ]['streak'] = null;
  459. endif;
  460. endforeach;
  461.  
  462. foreach ( $forms as $team_id => $form ):
  463. // Apply form limit
  464. if ( $form_limit && sizeof( $form ) > $form_limit ):
  465. $form = array_slice( $form, 0, $form_limit );
  466. endif;
  467.  
  468. // Initialize team form array
  469. $team_form = array();
  470.  
  471. // Reverse form array to display in chronological order
  472. $form = array_reverse( $form );
  473.  
  474. // Loop through event form
  475. foreach ( $form as $form_event ):
  476. if ( $form_event['id'] ):
  477. $outcome = sp_array_value( $outcomes, $form_event['outcome'], false );
  478. if ( $outcome ):
  479. $abbreviation = $outcome['abbreviation'];
  480. $color = $outcome['color'];
  481. if ( $link_events ):
  482. $abbreviation = '<a class="sp-form-event-link" href="' . get_post_permalink( $form_event['id'], false, true ) . '" style="background-color:' . $color . '">' . $abbreviation . '</a>';
  483. else:
  484. $abbreviation = '<span class="sp-form-event-link" style="background-color:' . $color . '">' . $abbreviation . '</span>';
  485. endif;
  486.  
  487. // Add to team form
  488. $team_form[] = $abbreviation;
  489. endif;
  490. endif;
  491. endforeach;
  492.  
  493. // Append to totals
  494. if ( sizeof( $team_form ) ):
  495. $totals[ $team_id ]['form'] = '<div class="sp-form-events">' . implode( ' ', $team_form ) . '</div>';
  496. else:
  497. $totals[ $team_id ]['form'] = null;
  498. endif;
  499. endforeach;
  500.  
  501. foreach ( $last5s as $team_id => $last5 ):
  502. // Add last 5 to totals
  503. $totals[ $team_id ]['last5'] = $last5;
  504. endforeach;
  505.  
  506. foreach ( $last10s as $team_id => $last10 ):
  507. // Add last 10 to totals
  508. $totals[ $team_id ]['last10'] = $last10;
  509. endforeach;
  510.  
  511. foreach ( $homerecords as $team_id => $homerecord ):
  512. // Add home record to totals
  513. $totals[ $team_id ]['homerecord'] = $homerecord;
  514. endforeach;
  515.  
  516. foreach ( $awayrecords as $team_id => $awayrecord ):
  517. // Add away record to totals
  518. $totals[ $team_id ]['awayrecord'] = $awayrecord;
  519. endforeach;
  520.  
  521. $args = array(
  522. 'post_type' => 'sp_column',
  523. 'numberposts' => -1,
  524. 'posts_per_page' => -1,
  525. 'orderby' => 'menu_order',
  526. 'order' => 'ASC'
  527. );
  528. $stats = get_posts( $args );
  529.  
  530. $columns = array();
  531. $this->priorities = array();
  532. $this->priorities_home = array();
  533.  
  534. foreach ( $stats as $stat ):
  535.  
  536. // Get post meta
  537. $meta = get_post_meta( $stat->ID );
  538.  
  539. // Add equation to object
  540. $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, null );
  541. $stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
  542.  
  543. // Add column name to columns
  544. $columns[ $stat->post_name ] = $stat->post_title;
  545.  
  546. // Add order to priorities if priority is set and does not exist in array already
  547. $priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 );
  548.  
  549. if ( $priority && ! array_key_exists( $priority, $this->priorities ) ):
  550. $this->priorities[ $priority ] = array(
  551. 'column' => $stat->post_name,
  552. 'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
  553. );
  554. endif;
  555.  
  556. endforeach;
  557.  
  558. // Sort priorities in descending order
  559. ksort( $this->priorities );
  560.  
  561. // Initialize games back column variable
  562. $gb_column = null;
  563.  
  564. // Fill in empty placeholder values for each team
  565. foreach ( $team_ids as $team_id ):
  566. if ( ! $team_id )
  567. continue;
  568.  
  569. $placeholders[ $team_id ] = array();
  570.  
  571. foreach ( $stats as $stat ):
  572. if ( sp_array_value( sp_array_value( $placeholders, $team_id, array() ), $stat->post_name, '' ) == '' ):
  573.  
  574. if ( $stat->equation == null ):
  575. $placeholder = sp_array_value( sp_array_value( $adjustments, $team_id, array() ), $stat->post_name, null );
  576. if ( $placeholder == null ):
  577. $placeholder = '-';
  578. endif;
  579. else:
  580.  
  581.  
  582.  
  583. if( 'pts' == $stat->post_name ) {
  584. var_dump( $stat->equation );
  585.  
  586. echo $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
  587.  
  588. $parts = explode(' ', $stat->equation );
  589. var_dump( $parts );
  590.  
  591. $home_equation = '';
  592. $away_equation = '';
  593. $limit = count( $parts );
  594. $count = 0;
  595. foreach ( $parts as $value ) {
  596. $count++;
  597.  
  598. if( false === strpos( $value, '$') ) {
  599. $home_equation .= $value;
  600. $away_equation .= $value;
  601. } else {
  602. $home_equation .= $value . '_home';
  603. $away_equation .= $value . '_away';
  604. }
  605.  
  606. if( $count < $limit ) {
  607. $home_equation .= ' ';
  608. $away_equation .= ' ';
  609. }
  610.  
  611. }
  612.  
  613. var_dump( $home_equation );
  614.  
  615.  
  616. echo 'Home:';
  617.  
  618.  
  619. echo $placeholder = sp_solve( $home_equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
  620.  
  621.  
  622. echo 'Away:';
  623.  
  624. echo $placeholder = sp_solve( $away_equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
  625.  
  626.  
  627. var_dump( sp_array_value( $totals, $team_id, array() ) );
  628. }
  629.  
  630.  
  631.  
  632.  
  633.  
  634. // Solve
  635. $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
  636. $placeholder_home = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
  637.  
  638.  
  639.  
  640.  
  641. if ( '$gamesback' == $stat->equation )
  642. $gb_column = $stat->post_name;
  643.  
  644. if ( ! in_array( $stat->equation, array( '$gamesback', '$streak', '$form', '$last5', '$last10', '$homerecord', '$awayrecord' ) ) ):
  645. // Adjustments
  646. $adjustment = sp_array_value( $adjustments, $team_id, array() );
  647.  
  648. if ( $adjustment != 0 ):
  649. $value = floatval( sp_array_value( $adjustment, $stat->post_name, 0 ) );
  650. $placeholder += $value;
  651. $placeholder = number_format( $placeholder, $stat->precision, '.', '' );
  652. endif;
  653. endif;
  654. endif;
  655.  
  656. $placeholders[ $team_id ][ $stat->post_name ] = $placeholder;
  657. $placeholders_home[ $team_id ][ $stat->post_name ] = $placeholder_home;
  658. endif;
  659. endforeach;
  660. endforeach;
  661.  
  662. // Find win and loss variables for games back
  663. $w = $l = null;
  664. if ( $gb_column ) {
  665. $args = array(
  666. 'post_type' => 'sp_outcome',
  667. 'numberposts' => 1,
  668. 'posts_per_page' => 1,
  669. 'meta_query' => array(
  670. array(
  671. 'key' => 'sp_condition',
  672. 'value' => '>',
  673. ),
  674. ),
  675. );
  676. $outcomes = get_posts( $args );
  677.  
  678. if ( $outcomes ) {
  679. $outcome = reset( $outcomes );
  680. if ( is_array( $stats ) ) {
  681. foreach ( $stats as $stat ) {
  682. if ( '$' . $outcome->post_name == $stat->equation ) {
  683. $w = $stat->post_name;
  684. }
  685. }
  686. }
  687. }
  688.  
  689. // Calculate games back
  690. $args = array(
  691. 'post_type' => 'sp_outcome',
  692. 'numberposts' => 1,
  693. 'posts_per_page' => 1,
  694. 'meta_query' => array(
  695. array(
  696. 'key' => 'sp_condition',
  697. 'value' => '<',
  698. ),
  699. ),
  700. );
  701. $outcomes = get_posts( $args );
  702.  
  703. if ( $outcomes ) {
  704. $outcome = reset( $outcomes );
  705. if ( is_array( $stats ) ) {
  706. foreach ( $stats as $stat ) {
  707. if ( '$' . $outcome->post_name == $stat->equation ) {
  708. $l = $stat->post_name;
  709. }
  710. }
  711. }
  712. }
  713. }
  714.  
  715. // Merge the data and placeholders arrays
  716. $merged = array();
  717. $merged_home = array();
  718. foreach( $placeholders as $team_id => $team_data ):
  719.  
  720. // Add team name to row
  721. $merged[ $team_id ] = array();
  722.  
  723. $team_data['name'] = sp_get_team_name( $team_id, $abbreviate_teams );
  724.  
  725. foreach ( $team_data as $key => $value ):
  726.  
  727. // Use static data if key exists and value is not empty, else use placeholder
  728. if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ):
  729. $value = $tempdata[ $team_id ][ $key ];
  730. endif;
  731.  
  732. $merged[ $team_id ][ $key ] = $value;
  733.  
  734. endforeach;
  735.  
  736. endforeach;
  737.  
  738. foreach( $placeholders_home as $team_id => $team_data ):
  739.  
  740. // Add team name to row
  741. $merged_home[ $team_id ] = array();
  742.  
  743. $team_data['name'] = sp_get_team_name( $team_id, $abbreviate_teams );
  744.  
  745. foreach ( $team_data as $key => $value ):
  746.  
  747. // Use static data if key exists and value is not empty, else use placeholder
  748. if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ):
  749. $value = $tempdata[ $team_id ][ $key ];
  750. endif;
  751.  
  752. $merged_home[ $team_id ][ $key ] = $value;
  753.  
  754. endforeach;
  755.  
  756. endforeach;
  757.  
  758. uasort( $merged, array( $this, 'sort' ) );
  759. uasort( $merged_home, array( $this, 'sort' ) );
  760.  
  761. // Calculate position of teams for ties
  762. foreach ( $merged as $team_id => $team_columns ) {
  763. $merged[ $team_id ]['pos'] = $this->calculate_pos( $team_columns, $team_id );
  764. }
  765. foreach ( $merged_home as $team_id => $team_columns ) {
  766. $merged_home[ $team_id ]['pos'] = $this->calculate_pos( $team_columns, $team_id );
  767. }
  768.  
  769. // Head to head table sorting
  770. if ( $is_main_loop && 'h2h' == get_option( 'sportspress_table_tiebreaker', 'none' ) ) {
  771. $order = array();
  772.  
  773. foreach ( $this->tiebreakers as $pos => $teams ) {
  774. if ( sizeof( $teams ) === 1 ) {
  775. $order[] = reset( $teams );
  776. } else {
  777. $standings = $this->data( false, $teams );
  778. $teams = array_keys( $standings );
  779. foreach( $teams as $team ) {
  780. $order[] = $team;
  781. }
  782. }
  783. }
  784.  
  785. $head_to_head = array();
  786. $head_to_head_home = array();
  787.  
  788. foreach ( $order as $team ) {
  789. $head_to_head[ $team ] = sp_array_value( $merged, $team, array() );
  790. }
  791. $merged = $head_to_head;
  792.  
  793. foreach ( $order as $team ) {
  794. $head_to_head_home[ $team ] = sp_array_value( $merged_home, $team, array() );
  795. }
  796. $merged_home = $head_to_head_home;
  797.  
  798. // Recalculate position of teams after head to head
  799. $this->pos = 0;
  800. $this->counter = 0;
  801.  
  802. foreach ( $merged as $team_id => $team_columns ) {
  803. $merged[ $team_id ]['pos'] = $this->calculate_pos( $team_columns, $team_id, false );
  804. }
  805.  
  806. foreach ( $merged_home as $team_id => $team_columns ) {
  807. $merged_home[ $team_id ]['pos'] = $this->calculate_pos( $team_columns, $team_id, false );
  808. }
  809. }
  810.  
  811.  
  812.  
  813.  
  814. // Rearrange data array to reflect values
  815. $data = array();
  816. foreach( $merged as $key => $value ):
  817. $data[ $key ] = $tempdata[ $key ];
  818. endforeach;
  819.  
  820. if ( ! $is_main_loop ):
  821. return array( 'overall' => $merged, 'home' => $merged_home );
  822. elseif ( $admin ):
  823. $this->add_gb( $placeholders, $w, $l, $gb_column );
  824. return array( $columns, $usecolumns, $data, $placeholders, $merged );
  825. else:
  826. $this->add_gb( $merged, $w, $l, $gb_column );
  827. if ( ! is_array( $usecolumns ) )
  828. $usecolumns = array();
  829. $labels = array_merge( array( 'pos' => __( 'Pos', 'sportspress' ), 'name' => __( 'Team', 'sportspress' ) ), $columns );
  830. $merged[0] = $labels;
  831. return array( 'overall' => $merged, 'home' => $merged_home );
  832. endif;
  833. }
  834.  
  835. /**
  836. * Sort the table by priorities.
  837. *
  838. * @param array $a
  839. * @param array $b
  840. * @return int
  841. */
  842. public function sort( $a, $b ) {
  843.  
  844. // Loop through priorities
  845. foreach( $this->priorities as $priority ):
  846.  
  847. // Proceed if columns are not equal
  848. if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ):
  849.  
  850. // Compare column values
  851. $output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 );
  852.  
  853. // Flip value if descending order
  854. if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
  855.  
  856. return ( $output > 0 );
  857.  
  858. endif;
  859.  
  860. endforeach;
  861.  
  862. // Default sort by alphabetical
  863. return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
  864. }
  865.  
  866. /**
  867. * Find accurate position of teams.
  868. *
  869. * @param array $columns
  870. * @param int $id
  871. * @return int
  872. */
  873. public function calculate_pos( $columns, $id = 0, $add_tiebreakers = true ) {
  874. $this->counter++;
  875.  
  876. $pos = $this->increment( $columns );
  877.  
  878. if ( $add_tiebreakers ) {
  879. // Initialize tiebreaker position
  880. if ( ! array_key_exists( $this->pos, $this->tiebreakers ) ) {
  881. $this->tiebreakers[ $this->pos ] = array();
  882. }
  883.  
  884. // Add to tiebreakers
  885. if ( ! in_array( $id, $this->tiebreakers[ $this->pos ] ) ) {
  886. $this->tiebreakers[ $this->pos ][] = $id;
  887. }
  888. }
  889.  
  890. return $pos;
  891. }
  892.  
  893. /**
  894. * Increment position as needed.
  895. *
  896. * @param array $columns
  897. * @return int
  898. */
  899. public function increment( $columns ) {
  900. // Replace compare data and use last set
  901. $compare = $this->compare;
  902. $this->compare = $columns;
  903.  
  904. // Loop through priorities
  905. foreach( $this->priorities as $priority ):
  906.  
  907. // Proceed if columns are not equal
  908. if ( sp_array_value( $columns, $priority['column'], 0 ) !== sp_array_value( $compare, $priority['column'], 0 ) ):
  909.  
  910. // Increment if not equal
  911. $this->pos = $this->counter;
  912. return $this->counter;
  913.  
  914. endif;
  915.  
  916. endforeach;
  917.  
  918. if ( 'yes' == get_option( 'sportspress_table_increment', 'no' ) ) {
  919. return $this->counter;
  920. }
  921.  
  922. // Repeat position if equal
  923. return $this->pos;
  924. }
  925.  
  926. /**
  927. * Calculate and add games back.
  928. *
  929. * @param array $a
  930. * @param string $w
  931. * @param string $l
  932. * @param string $column
  933. * @return null
  934. */
  935. public function add_gb( &$a, $w = null, $l = null, $column ) {
  936. if ( ! is_array( $a ) ) return;
  937. if ( ! $w && ! $l ) return;
  938.  
  939. foreach ( $a as $team_id => $values ) {
  940. if ( isset( $leader ) ) {
  941. $gb = ( sp_array_value( $leader, $w, 0 ) - sp_array_value( $values, $w, 0 ) + sp_array_value( $values, $l, 0 ) - sp_array_value( $leader, $l, 0 ) ) / 2;
  942. if ( '-' == sp_array_value( $values, $column ) && 0 !== $gb ) {
  943. $a[ $team_id ][ $column ] = $gb;
  944. }
  945. } else {
  946. $leader = $values;
  947. }
  948. }
  949. }
  950. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement