-
Notifications
You must be signed in to change notification settings - Fork 2
/
movie-quotes.php
220 lines (176 loc) · 6.12 KB
/
movie-quotes.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
global $wpdb;
define("QUOTES_CSV_FILE", IFLPM_PLUGIN_PATH."quotes.csv");
define("MOVIE_QUOTES_TABLE_NAME", $wpdb->prefix . IFLPM_TABLE_PREFIX . "movie_quotes");
define("MOVIE_QUOTES_DB_VERSION", "1.0");
define("USER_PAIRINGS_TABLE_NAME", $wpdb->prefix . IFLPM_TABLE_PREFIX . "user_pairings");
define("USER_PAIRINGS_DB_VERSION", "1.0");
Class MovieQuotes {
public static function get_random_movie_quote() {
global $wpdb;
if (!self::does_movie_quotes_table_exist_in_database()) {
return false;
}
$sql = "SELECT * FROM ".MOVIE_QUOTES_TABLE_NAME." ORDER BY RAND() LIMIT 1";
$quote = $wpdb->get_results($sql);
return $quote[0];
}
public static function does_movie_quotes_table_exist_in_database() {
return IFLPMDBManager::does_table_exist_in_database(MOVIE_QUOTES_TABLE_NAME);
}
public static function does_user_pairings_table_exist_in_database() {
return IFLPMDBManager::does_table_exist_in_database(USER_PAIRINGS_TABLE_NAME);
}
public static function does_table_exist_in_database($table_name) {
global $wpdb;
$mytables = $wpdb->get_results("SHOW TABLES");
foreach ($mytables as $mytable) {
foreach ($mytable as $t) {
//echo "table name: " . $t . "<br>";
if ($t == $table_name) {
return true;
}
}
}
return false;
}
public static function is_movie_quotes_table_empty() {
return self::is_table_empty(MOVIE_QUOTES_TABLE_NAME);
}
public static function is_user_pairings_table_empty() {
return self::is_table_empty(USER_PAIRINGS_TABLE_NAME);
}
public static function is_table_empty($table_name) {
global $wpdb;
$rows = $wpdb->get_results("SELECT COUNT(*) as num_rows FROM " . $table_name);
return $rows[0]->num_rows == 0;
}
public static function create_movie_quotes_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE " . MOVIE_QUOTES_TABLE_NAME . "(
id mediumint(9) NOT NULL AUTO_INCREMENT,
quote tinytext NOT NULL,
movie_name tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('movie_quotes_db_version', MOVIE_QUOTES_DB_VERSION);
}
public static function create_user_pairings_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE " . USER_PAIRINGS_TABLE_NAME . "(
id mediumint(9) NOT NULL AUTO_INCREMENT,
pairing tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('user_pairings_db_version', USER_PAIRINGS_DB_VERSION);
}
public static function delete_all_quotes_from_movie_quotes_table() {
self::delete_all_rows_from_table(MOVIE_QUOTES_TABLE_NAME);
}
public static function delete_all_pairings_from_user_pairings_table() {
self::delete_all_rows_from_table(USER_PAIRINGS_TABLE_NAME);
}
public static function delete_all_rows_from_table($table_name) {
global $wpdb;
$result = $wpdb->query("TRUNCATE TABLE " . $table_name);
}
public static function drop_movie_quotes_table() {
self::drop_table(MOVIE_QUOTES_TABLE_NAME);
}
public static function drop_user_pairings_table() {
self::drop_table(USER_PAIRINGS_TABLE_NAME);
}
public static function drop_table($table_name) {
global $wpdb;
$result = $wpdb->query("DROP TABLE IF EXISTS " . $table_name);
}
public static function does_quotes_csv_file_exist() {
return file_exists(QUOTES_CSV_FILE);
}
public static function import_movie_quotes_to_database() {
global $wpdb;
$file = fopen(QUOTES_CSV_FILE, "r");
$quotes_array = [];
// read all quotes into an array
while (!feof($file)) {
$entry = fgetcsv($file);
if ($entry[0] != "") {
array_push($quotes_array, $entry);
}
}
fclose($file);
// I commented shuffle out because
// (1) My Python script had shuffled them in the first place, so they're already not grouped by movie
// (2) If you have to read the quotes file in again, you don't want to change which quote is assigned to which pairing
// shuffle($quotes_array); @tané
// write them to the quotes database
foreach ($quotes_array as $entry) {
$quote = $entry[0];
$movie_name = $entry[1];
//echo $quote . " - " . $movie_name . "<br>";
$wpdb->insert(
MOVIE_QUOTES_TABLE_NAME,
array(
'quote' => $quote,
'movie_name' => $movie_name,
)
);
}
}
public static function get_movie_quote_by_id($id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(MOVIE_QUOTES_TABLE_NAME) || self::is_movie_quotes_table_empty()) {
return null;
} else {
$result = $wpdb->get_results("SELECT * FROM " . MOVIE_QUOTES_TABLE_NAME . " WHERE id = " . $id);
if ($wpdb->num_rows == 0) {
return null;
}
$quote = $result[0]->quote;
$movie_name = $result[0]->movie_name;
return $quote . " - " . $movie_name;
}
}
public static function get_movie_quote_by_pairing($user_id_1, $user_id_2) {
// special case by request of John for a display loop between users
if (($user_id_1 == 0 && $user_id_2 == 0) || ($user_id_1 == "00000000" && $user_id_2 == "00000000")) {
return " Find a Friend and Get Your Movie Fortune ";
}
$users = [$user_id_1, $user_id_2];
sort($users);
$pairing_string = "{$users[0]}-{$users[1]}";
if (!IFLPMDBManager::does_table_exist_in_database(USER_PAIRINGS_TABLE_NAME)) {
return null;
}
$id = self::get_id_by_pairing($pairing_string);
return self::get_movie_quote_by_id($id);
}
public static function get_id_by_pairing($pairing_string) {
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM " . USER_PAIRINGS_TABLE_NAME . " WHERE pairing = '" . $pairing_string . "'");
if ($wpdb->num_rows == 0) {
$id = self::add_pairing_to_user_pairings_table($pairing_string);
} else {
$id = $result[0]->id;
}
return $id;
}
public static function add_pairing_to_user_pairings_table($pairing_string) {
global $wpdb;
$wpdb->insert(
USER_PAIRINGS_TABLE_NAME,
array(
'pairing' => $pairing_string,
)
);
$result = $wpdb->get_results("SELECT * FROM " . USER_PAIRINGS_TABLE_NAME . " WHERE pairing = '" . $pairing_string . "'");
return $result[0]->id;
}
}
?>