-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamData_WidgetUtils.php
More file actions
145 lines (133 loc) · 5.38 KB
/
Copy pathTeamData_WidgetUtils.php
File metadata and controls
145 lines (133 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
class TeamData_WidgetUtils extends TeamDataBase {
/**
* Helper function to get the logo link for our team.
*
* @return string The link for our team's logo.
*/
public static function get_our_logo_link() {
global $wpdb;
$utils_instance = new TeamData_WidgetUtils();
$logo_link = '';
$our_team = $utils_instance->get_option('our_team');
if ( !empty($our_team) ) {
$sql = $wpdb->prepare('SELECT logo_link FROM ' . $utils_instance->tables->team . ' WHERE id = %d', $our_team);
$logo_link = $wpdb->get_var($sql);
}
return $logo_link;
}
/**
* Helper function to get the data for the last match played for the level specified in $level_id.
*
* @param integer $level_id ID of level we want to get the match for.
* @param bool $get_logos Flag to control whether logos should be retrieved.
* @return array match data with the following data when a match is found:
* <code>
* $match_data = array(
* '_date' => 'datestring',
* '_time' => 'timestring',
* 'result' => 'string',
* 'our_score' => integer,
* 'opposition_score' => integer,
* 'tourney_name' => 'string',
* 'level' => 'string',
* 'team' => 'string',
* 'team_logo' => 'url_string',
* 'is_home' => integer,
* 'venue' => 'string',
* 'info_link' => 'url_string',
* 'directions_link' => 'url_string',
* );
* </code>
*/
public static function get_last_match($level_id = -1, $get_logos = true) {
global $wpdb;
$match = null;
$utils_instance = new TeamData_WidgetUtils();
$select = array(
"DATE_FORMAT(match.date,'%M %D %Y') AS `_date`",
"DATE_FORMAT(match.time,'%l:%i %p') AS `_time`",
'match.result',
'match.our_score',
'match.opposition_score',
'match.tourney_name',
"IF(level.abbreviation = '', level.name, level.abbreviation) AS level",
"match.team_name AS team",
"match.team_logo",
'(venue.is_home = 1) AS is_home',
"IF(venue.abbreviation = '', venue.name, venue.abbreviation) AS venue",
'venue.info_link',
'venue.directions_link'
);
$from = array(
"( SELECT m.date, m.time, m.tourney_name, m.venue_id, m.level_id, m.result, m.our_score, m.opposition_score, IF(m.opposition_id IS NULL, '', IF(t.abbreviation = '', t.name, t.abbreviation)) AS team_name, " . ($get_logos ? "IF(m.opposition_id IS NULL, '', t.logo_link)" : "''") . " AS team_logo FROM " . $utils_instance->tables->match . " m LEFT OUTER JOIN " . $utils_instance->tables->team . " t ON m.opposition_id = t.id ) `match`",
$utils_instance->tables->level . ' AS level',
$utils_instance->tables->venue . ' AS venue',
);
$where = array(
'match.venue_id = venue.id',
'match.level_id = level.id',
"( match.result <> '' OR ( match.our_score IS NOT NULL AND match.opposition_score IS NOT NULL ) )",
'match.date <= CURDATE()'
);
if ($level_id > 0) $where[] = 'level.id = ' . intval($level_id);
$sql = 'SELECT ' . implode(', ', $select) . ' FROM ' . implode(', ', $from) . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY match.date DESC, match.time DESC LIMIT 1';
$matches = $wpdb->get_results($sql, ARRAY_A);
if (isset($matches[0])) $match = $matches[0];
return $match;
}
/**
* Helper function to get the data for the next match scheduled for the level specified in $level_id.
*
* @param integer $level_id ID of level we want to get the match for.
* @return array match_data, which has the following fields when a match is found:
* <code>
* match_data = array(
* '_date' => 'datestring',
* '_time' => 'timestring',
* 'level' => 'string',
* 'team' => 'string',
* 'team_logo' => 'url_string',
* 'tourney_name' => 'string',
* 'is_home' => integer,
* 'venue' => 'string',
* 'info_link' => 'url_string',
* 'directions_link' => 'url_string',
* );
* </code>
*/
public static function get_next_match($level_id = -1, $get_logos = true) {
global $wpdb;
$match = null;
$utils_instance = new TeamData_WidgetUtils();
$select = array(
"DATE_FORMAT(match.date,'%M %D %Y') AS `_date`",
"DATE_FORMAT(match.time,'%l:%i %p') AS `_time`",
"IF(level.abbreviation = '', level.name, level.abbreviation) AS level",
"match.team_name AS team",
"match.team_logo",
'match.tourney_name',
'(venue.is_home = 1) AS is_home',
"IF(venue.abbreviation = '', venue.name, venue.abbreviation) AS venue",
'venue.info_link',
'venue.directions_link'
);
$from = array(
"( SELECT m.date, m.time, m.tourney_name, m.venue_id, m.level_id, m.result, m.our_score, m.opposition_score, IF(m.opposition_id IS NULL, '', IF(t.abbreviation = '', t.name, t.abbreviation)) AS team_name, " . ( $get_logos ? "IF(m.opposition_id IS NULL, '', t.logo_link)" : "''" ) . " AS team_logo FROM " . $utils_instance->tables->match . " m LEFT OUTER JOIN " . $utils_instance->tables->team . " t ON m.opposition_id = t.id ) `match`",
$utils_instance->tables->level . ' AS level',
$utils_instance->tables->venue . ' AS venue',
);
$where = array(
'match.venue_id = venue.id',
'match.level_id = level.id',
"( match.result = '' AND ( match.our_score IS NULL AND match.opposition_score IS NULL ) )",
'match.date >= CURDATE()'
);
if ($level_id > 0) $where[] = 'level.id = ' . intval($level_id);
$sql = 'SELECT ' . implode(', ', $select) . ' FROM ' . implode(', ', $from) . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY match.date ASC, match.time ASC LIMIT 1';
$matches = $wpdb->get_results($sql, ARRAY_A);
if (isset($matches[0])) $match = $matches[0];
return $match;
}
}
?>