public function update_main_results($results)
 {
     $main_result = sp_get_main_result_option();
     if (!$this->ID || !is_array($results) || null === $main_result) {
         return false;
     }
     // Get current results meta
     $meta = get_post_meta($this->ID, 'sp_results', true);
     $primary_results = array();
     foreach ($results as $id => $result) {
         $primary_results[$id] = $result;
         if (!$id) {
             continue;
         }
         $meta[$id][$main_result] = $result;
     }
     arsort($primary_results);
     if (count($primary_results) && !in_array(null, $primary_results)) {
         if (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 ($meta as $team => $team_results) {
                 if ($outcomes) {
                     $meta[$team]['outcome'] = array();
                     foreach ($outcomes as $outcome) {
                         $meta[$team]['outcome'][] = $outcome->post_name;
                     }
                 }
             }
         } else {
             reset($primary_results);
             $max = key($primary_results);
             $args = array('post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '>');
             $outcomes = get_posts($args);
             if ($outcomes) {
                 $meta[$max]['outcome'] = array();
                 foreach ($outcomes as $outcome) {
                     $meta[$max]['outcome'][] = $outcome->post_name;
                 }
             }
             end($primary_results);
             $min = key($primary_results);
             $args = array('post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '<');
             $outcomes = get_posts($args);
             if ($outcomes) {
                 $meta[$min]['outcome'] = array();
                 foreach ($outcomes as $outcome) {
                     $meta[$min]['outcome'][] = $outcome->post_name;
                 }
             }
         }
     }
     // Update results
     update_post_meta($this->ID, 'sp_results', $meta);
 }
Example #2
0
 public function update_main_results($results)
 {
     $main_result = sp_get_main_result_option();
     if (!$this->ID || !is_array($results) || null === $main_result) {
         return false;
     }
     // Get current results meta
     $meta = get_post_meta($this->ID, 'sp_results', true);
     $primary_results = array();
     foreach ($results as $id => $result) {
         $primary_results[$id] = $result;
         if (!$id) {
             continue;
         }
         $meta[$id][$main_result] = $result;
     }
     arsort($primary_results);
     if (count($primary_results) && !in_array(null, $primary_results)) {
         if (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 ($meta as $team => $team_results) {
                 if ($outcomes) {
                     $meta[$team]['outcome'] = array();
                     foreach ($outcomes as $outcome) {
                         $meta[$team]['outcome'][] = $outcome->post_name;
                     }
                 }
             }
         } else {
             // Get default outcomes
             $args = array('post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => 'else');
             $default_outcomes = get_posts($args);
             // Get greater than outcomes
             $args = array('post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '>');
             $gt_outcomes = get_posts($args);
             if (empty($gt_outcomes)) {
                 $gt_outcomes = $default_outcomes;
             }
             // Get less than outcomes
             $args = array('post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '<');
             $lt_outcomes = get_posts($args);
             if (empty($lt_outcomes)) {
                 $lt_outcomes = $default_outcomes;
             }
             // Get min and max values
             $min = min($primary_results);
             $max = max($primary_results);
             foreach ($primary_results as $key => $value) {
                 if ($min == $value) {
                     $outcomes = $lt_outcomes;
                 } elseif ($max == $value) {
                     $outcomes = $gt_outcomes;
                 } else {
                     $outcomes = $default_outcomes;
                 }
                 $meta[$key]['outcome'] = array();
                 foreach ($outcomes as $outcome) {
                     $meta[$key]['outcome'][] = $outcome->post_name;
                 }
             }
         }
     }
     // Update results
     update_post_meta($this->ID, 'sp_results', $meta);
 }
        /**
         * Output event score status.
         */
        public function output_event_score_status($id = 0)
        {
            if ('yes' !== get_option('sportspress_event_show_score_status', 'yes')) {
                return;
            }
            if (!isset($id)) {
                $id = get_the_ID();
            }
            // Get the winner of the event
            $winner = sp_get_winner($id);
            // Return if no winner is set
            if (empty($winner)) {
                return;
            }
            // Get the name of the winning team
            $winner_name = get_the_title($winner);
            // Get results of the winning team
            $results = sp_get_results($id);
            $winner_results = sp_array_value($results, $winner, array());
            // Check that results for the winning team are available
            if (!is_array($winner_results) || 0 == sizeof($winner_results)) {
                return;
            }
            // Get the main result option
            $main = sp_get_main_result_option();
            // Check that main results exist for the winning team
            if (!array_key_exists($main, $winner_results)) {
                return;
            }
            // Get index of winning team
            $winner_index = array_search($winner, array_keys($results));
            if (!in_array($winner_index, array(0, 1))) {
                return;
            }
            // Find the main result of the winning team
            $val = reset($winner_results);
            while (key($winner_results) !== $main) {
                $val = next($winner_results);
            }
            // Get the next result if winning team was batting last
            if (0 == $winner_index) {
                $loser = sp_array_value(array_keys($results), 1, false);
                // Return if no entry for losing team
                if (false === $loser) {
                    return;
                }
                // Get results of the losing team
                $loser_results = sp_array_value($results, $loser, array());
                // Check that results for the losing team are available
                if (!is_array($loser_results) || 0 == sizeof($loser_results)) {
                    return;
                }
                // Check that main results exist for the losing team
                if (!array_key_exists($main, $loser_results)) {
                    return;
                }
                // Find the main result of the losing team
                $loser_val = reset($loser_results);
                while (key($loser_results) !== $main) {
                    $loser_val = next($loser_results);
                }
                // Subtract runs from losing team
                if (is_numeric($loser_val)) {
                    $val -= $loser_val;
                }
            } else {
                $val = next($winner_results);
                // Return if no result to report
                if (false === $val) {
                    return;
                }
                // Subtract wickets from 10
                $val = 10 - $val;
            }
            ?>
		<p class="sp-event-score-status sp-align-center">
			<?php 
            if (0 == $winner_index) {
                printf(__('%1$s won by %2$s runs', 'sportspress-for-cricket'), $winner_name, $val);
            } else {
                printf(__('%1$s won by %2$s wickets', 'sportspress-for-cricket'), $winner_name, $val);
            }
            ?>
		</p>
		<?php 
        }
 /**
  * Save meta boxes data
  */
 public static function save($post_id)
 {
     // Format
     update_post_meta($post_id, 'sp_format', sp_array_value($_POST, 'sp_format', 'bracket'));
     // Rounds
     $limit = intval(get_option('sportspress_tournament_rounds', '6'));
     $rounds = sp_array_value($_POST, 'sp_rounds', 1);
     if ($rounds < 1) {
         $rounds = 1;
     } elseif ($rounds > $limit) {
         $rounds = $limit;
     }
     update_post_meta($post_id, 'sp_rounds', $rounds);
     // Winner
     update_post_meta($post_id, 'sp_winner', sp_array_value($_POST, 'sp_winner'));
     // Labels
     update_post_meta($post_id, 'sp_labels', sp_array_value($_POST, 'sp_labels', array()));
     // Get main result option
     $main_result = sp_get_main_result_option();
     // Get terms
     $tax_input = sp_array_value($_POST, 'tax_input', array());
     $leagues = array_map('intval', sp_array_value($tax_input, 'sp_league', array()));
     $seasons = array_map('intval', sp_array_value($tax_input, 'sp_season', array()));
     // Events
     $events = sp_array_value($_POST, 'sp_event', array());
     ksort($events);
     $event_ids = array();
     foreach ($events as $event) {
         // Get details
         $id = sp_array_value($event, 'id', 0);
         $teams = sp_array_value($event, 'teams', array());
         $results = sp_array_value($event, 'results', array());
         $date = sp_array_value($event, 'date', '');
         $h = sp_array_value($event, 'hh', '');
         $m = sp_array_value($event, 'mm', '00');
         // Update or add new event
         if (strlen($date)) {
             // Add time to date if given
             if (strlen($h)) {
                 $h = substr(str_pad($h, 2, '0', STR_PAD_LEFT), 0, 2);
                 $m = substr(str_pad($m, 2, '0', STR_PAD_LEFT), 0, 2);
                 $time = $h . ':' . $m;
                 $date .= ' ' . trim($time);
             }
             // Generate title
             $team_names = array();
             foreach ($teams as $team) {
                 if (!$team) {
                     continue;
                 }
                 $team_names[] = get_the_title($team);
             }
             $new_title = implode(' ' . get_option('sportspress_event_teams_delimiter', 'vs') . ' ', $team_names);
             // Update or add new event
             if ($id) {
                 $post = array('ID' => $id, 'post_date' => $date);
                 // Update title if not set
                 $title = get_the_title($id);
                 if (!strlen($title)) {
                     $post['post_title'] = $new_title;
                     wp_update_post($post);
                 }
             } else {
                 $args = array('post_type' => 'sp_event', 'post_title' => $new_title, 'post_status' => 'publish', 'post_date' => $date);
                 $id = wp_insert_post($args);
                 // Update league
                 if (array_filter($leagues)) {
                     wp_set_object_terms($id, $leagues, 'sp_league', false);
                 }
                 // Update season
                 if (array_filter($seasons)) {
                     wp_set_object_terms($id, $seasons, 'sp_season', false);
                 }
             }
             // Update teams
             delete_post_meta($id, 'sp_team');
             foreach ($teams as $team) {
                 add_post_meta($id, 'sp_team', $team);
             }
             // Update results
             if (sizeof($results) && sizeof($teams)) {
                 $results = array_combine($teams, $results);
                 if ($results) {
                     sp_update_main_results($id, $results);
                 }
             }
             // Update event format
             update_post_meta($id, 'sp_format', 'tournament');
         }
         // Add to event IDs
         $event_ids[] = $id;
     }
     update_post_meta($post_id, 'sp_events', $events);
     sp_update_post_meta_recursive($post_id, 'sp_event', $event_ids);
 }