Skip to content

Instantly share code, notes, and snippets.

@aiiddqd
Created November 21, 2019 08:18
Show Gist options
  • Save aiiddqd/c11ad4ca8cdcd40eda8a15ab80c5cce9 to your computer and use it in GitHub Desktop.
Save aiiddqd/c11ad4ca8cdcd40eda8a15ab80c5cce9 to your computer and use it in GitHub Desktop.
<?php
namespace SB\Models;
/**
* Class Event model for map data
*
* @package SB
*/
class Event {
/**
* Post ID.
*
* @var int
*/
public $ID;
/**
* The post's title.
*
* @var string
*/
public $post_title = '';
/**
* The $post data
*/
public $post;
/**
* Stores data in meta table.
*
* @var array
*/
protected $data_meta = [];
/**
* Stores data in custom table.
*
* @var array
*/
protected $data_custom = [
'event_id' => '',
'date_game' => '',
'first_team_id' => '',
'second_team_id' => '',
];
protected $custom_table_name = '';
/**
* The Event constructor.
*
* @param int $post_id
*/
public function __construct($post_id = 0) {
global $wpdb;
$post_id = (int) $post_id;
if ( ! $post_id) {
return false;
}
$post = get_post($post_id);
if (! ($post instanceof \WP_Post) || 'event' != $post->post_type) {
return false;
}
$this->ID = $post->ID;
$this->post = $post;
$this->post_title = $this->post->post_title;
//
// foreach ( get_object_vars( $this->post ) as $key => $value ) {
// $this->$key = $value;
// }
$this->custom_table_name = $wpdb->prefix . "socbet_events_meta";
if ( ! $row_custom_table = wp_cache_get($post_id, 'event_data_custom')) {
$row_custom_table = $this->get_row_custom_table();
wp_cache_set($post_id, $row_custom_table, 'event_data_custom');
}
if ( ! empty($row_custom_table)) {
foreach (get_object_vars($row_custom_table) as $key => $value) {
$this->data_custom[ $key ] = $value;
}
}
}
private function get_row_custom_table() {
global $wpdb;
$custom_table_name = $wpdb->prefix . "socbet_events_meta";
$row_custom_table = $wpdb->get_row(
$wpdb->prepare("
SELECT *
FROM $custom_table_name
WHERE event_id = %s
", $this->ID)
);
return $row_custom_table;
}
/*
* get row from custom table
*/
public function get_sportradar_id() {
if (empty($this->data_custom['sportradar_id'])) {
return '';
} else {
return $this->data_custom['sportradar_id'];
}
}
/**
* exist_timeline
*/
public function exist_timeline() {
// Получение таймлайна
$timeline = get_event_field($this->ID, 'timeline');
// Валидация таймлайна
if ( ! is_object($timeline) || ! $timeline instanceof Model\EventTimeline) {
return false;
}
// Отфильтровываем элементы без комментариев
$timeline->apply_filter('COMMENTS_OR_GOALS');
// Проверяем осталось ли что-то в итераторе после применения фильтра
if ($timeline->is_empty()) {
return false;
}
return true;
}
/**
* get url
*
* @param string $page
*/
public function get_url($subpage = '') {
if (empty($subpage)) {
return get_permalink($this->ID);
}
$url = untrailingslashit(get_permalink($this->ID));
$url .= '/' . $subpage;
$url = user_trailingslashit($url);
return $url;
}
/**
* get url for actual event
*/
public function get_url_for_actual_event() {
// Получаем объекты \WP_Post c установлеными данными команд
if ( ! $first_team = $this->get_first_team()) {
return false;
}
if ( ! $second_team = $this->get_second_team()) {
return false;
}
if (is_wp_error($first_team) || is_wp_error($second_team)) {
return false;
}
// Команда с меньшим ID будет первой в слаге
if ($first_team->ID < $second_team->ID) {
$slug = "{$first_team->post->post_name}-{$second_team->post->post_name}";
} else {
$slug = "{$second_team->post->post_name}-{$first_team->post->post_name}";
}
global $wp_rewrite;
// Шаблон урлов для матчей
$event_link_template = site_url() . $wp_rewrite->get_extra_permastruct($this->post->post_type);
$permalink = str_replace('%event%', $slug, $event_link_template);
$permalink = user_trailingslashit($permalink);
return $permalink;
}
/**
* get first team
*/
public function get_first_team() {
if (empty($this->data_custom['first_team_id'])) {
return false;
}
if ($team = sb_get_team($this->data_custom['first_team_id'])) {
return $team;
}
return false;
}
/**
* get second team
*/
public function get_second_team() {
if (empty($this->data_custom['second_team_id'])) {
return false;
}
if ($team = sb_get_team($this->data_custom['second_team_id'])) {
return $team;
}
return false;
}
/**
* Get past events
*/
public function get_past_events_ids() {
$first_team_id = $this->data_custom['first_team_id'];
$second_team_id = $this->data_custom['second_team_id'];
global $wpdb;
$sql = $wpdb->prepare("
SELECT event_id
FROM $this->custom_table_name as em
WHERE
em.date_game < NOW()
AND
(
(em.first_team_id = %s AND em.second_team_id = %s)
OR
(em.first_team_id = %s AND em.second_team_id = %s)
)
ORDER BY em.date_game DESC
", $first_team_id, $second_team_id, $second_team_id, $first_team_id
);
$data = $wpdb->get_col($sql);
if (empty($data)) {
return false;
}
return $data;
}
/**
* Get tips count
*
* @return int
*/
public function get_tips_count() {
$query = "
SELECT
COUNT(`wp_sb_bets`.`tip_id`)
FROM
`wp_sb_bets`
WHERE
`wp_sb_bets`.`tip_id` IS NOT NULL
AND
`wp_sb_bets`.`event_id`=" . $this->ID . "
AND
`wp_sb_bets`.`express_id` IS NULL
ORDER BY
`wp_sb_bets`.`datetime` DESC
";
global $wpdb;
$tips_count = intval($wpdb->get_var($query));
return $tips_count;
}
/**
* check is main event
* if have single url
*
* @return bool
*/
public function is_main_event() {
if (is_numeric($this->post_name)) {
return false;
} else {
return true;
}
}
/**
* check is archive event
* if have single url
*
* @return bool
*/
public function is_archive_event() {
if (is_numeric($this->post_name)) {
return true;
} else {
return false;
}
}
/**
* Get date for event
* With format and GMT option for site
*
* @param string $format
*
* @return string | bool
*/
public function get_date($format = 'd.m.Y') {
if ($date = get_post_meta($this->ID, 'event_game_date', true)) {
$date = \DateTime::createFromFormat('U', $date);
$date->setTimezone(new \DateTimeZone(get_option('timezone_string')));
$date = $date->format('Y-m-d H:i:s');
$date = Time::i18n($date);
$date = strtotime($date);
$date = date_i18n($format, $date, false);
return $date;
}
return false;
}
/**
* Get sport_name
*
* @return string | false
*/
public function get_sport_name() {
if ($type = $this->get_type()) {
return $type->name;
}
$terms = wp_get_post_terms($this->ID, 'sport_type', ['fields' => 'ids']);
if ( ! empty($terms[0])) {
$ancestors_ids = get_ancestors($terms[0], 'sport_type');
$parent_id = (int) end($ancestors_ids);
}
if (empty($parent_id)) {
return false;
}
$term_top = get_term_by('id', $parent_id, 'sport_type');
if (empty($term_top->name)) {
return false;
}
return $term_top->name;
}
/**
* get type of event
*
* @return bool|\WP_Term
*/
public function get_type() {
$event_type = wp_get_post_terms($this->ID, 'event_type');
if (empty($event_type[0])) {
return false;
} else {
$term = get_term($event_type[0]);
return $term;
}
}
/**
* set type of event
*
* @param $terms
*
* @return array|false|int|string|\WP_Error
*/
public function set_type($terms) {
$set_event_type = wp_set_post_terms($this->ID, $terms, 'event_type');
if (is_wp_error($set_event_type)) {
return $set_event_type->get_error_code();
}
return $set_event_type;
}
public function __get($key) {
if (property_exists($this, $key)) {
return $this->$key;
}
}
public function __set($key, $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
return $this;
}
/**
* get first team data post
*
* @return array|\WP_Post|bool
*/
public function get_first_team_data() {
if ( ! $team = get_post($this->data_custom['first_team_id'])) {
return false;
}
return $team;
}
public function get_second_team_data() {
$team = get_post($this->data_custom['second_team_id']);
return $team;
}
/**
* Получаем ID лиги
*
* @return int
*/
public function get_league_id() {
$league_id = Helper\Taxonomy::get_event_league($this->ID);
return $league_id->term_id;
}
/**
* Возвращает имя лиги
*
* @return string
*/
public function get_league_name() {
$league = Helper\Taxonomy::get_event_league($this->ID);
return $league->name;
}
/**
* Возвращает счет матча
*
* @return string
*/
public function get_score() {
$event_score = get_event_field($this->ID, 'score');
return $event_score;
}
/**
* Возвращает статус матча
*
* @return string
*/
public function get_status() {
$event_status = get_event_field($this->ID, 'status');
return $event_status;
}
/**
* Возвращает количество ставок на матч
*
* @return string
*/
public function get_bets_count() {
return get_event_field($this->ID, 'bets_count');
}
/**
* Получаем объект места проведения события
*
* @return Venue\EventVenue
*/
public function get_venue(){
return sb_get_event_venue($this);
}
/**
* Получает аудиторию матча
*
* @return int
*/
public function get_attendance(){
return (int)get_post_meta($this->ID, 'attendance', true);
}
/**
* Получает предварительные составы на матч
*
* @return array|false
*/
public function get_teams_composition(){
return get_post_meta($this->ID, 'teams_composition', true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment