This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoanext-crew-schedule-viewer.php
300 lines (277 loc) · 10.7 KB
/
oanext-crew-schedule-viewer.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* Plugin Name: OA NEXT Crew Schedule Viewer
* Plugin URI: https://github.com/oabsa/oanext-crew-schedule-viewer
* Description: WordPress plugin for displaying crew schedules for NEXT conference.
* Version: 1.0
* Author: Eric Silva (ericjsilva)
* Author URI: http://oa-bsa.org
* Author Email: [email protected]
* License: GPLv3
*/
/*
* WordPress plugin for displaying crew schedules for NEXT conference.
* Copyright (C) 2016 Order of the Arrow
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!class_exists('OANextCrewScheduleViewer')) {
//instantiate...
add_action('plugins_loaded', array('OANextCrewScheduleViewer', 'init'));
/**
* Main plugin class.
*/
class OANextCrewScheduleViewer
{
protected static $instance;
private $page_slug = 'crew';
private $sat_gsheet_url = 'https://spreadsheets.google.com/feeds/list/1YqBU4TaM8ub4cPCJYePDZGEsyxlprBqB2UCGh8luGdU/od6/public/values?alt=json';
private $sun_gsheet_url = 'https://spreadsheets.google.com/feeds/list/1YqBU4TaM8ub4cPCJYePDZGEsyxlprBqB2UCGh8luGdU/2/public/values?alt=json';
private $mon_gsheet_url = 'https://spreadsheets.google.com/feeds/list/1YqBU4TaM8ub4cPCJYePDZGEsyxlprBqB2UCGh8luGdU/3/public/values?alt=json';
private $tue_gsheet_url = 'https://spreadsheets.google.com/feeds/list/1YqBU4TaM8ub4cPCJYePDZGEsyxlprBqB2UCGh8luGdU/4/public/values?alt=json';
function __construct()
{
add_action('wp_enqueue_scripts', array(&$this, 'enqueueJavascript'));
add_action('wp_enqueue_scripts', array(&$this, 'enqueueCss'));
add_filter('query_vars', array(&$this, 'queryVariables'), 10);
add_action('init', array(&$this, 'initRewrites'), 10, 0);
add_action('parse_request', array(&$this, 'handleUrl'));
}
/**
* hooked into plugins_loaded action : creates the plugin instance
*/
public static function init()
{
is_null(self::$instance) && self::$instance = new self;
return self::$instance;
}
function enqueueJavascript()
{
wp_register_style('oanextcrews-script', plugins_url('oanext.js', __FILE__));
wp_enqueue_script('oanextcrews-script');
}
function enqueueCss()
{
wp_register_style('oanextcrews-style', plugins_url('style.css', __FILE__));
wp_enqueue_style('oanextcrews-style');
}
function initRewrites()
{
add_rewrite_rule('^crew/([0-9]+)/?$', 'crew/?crew_id=' . $matches[1], 'top');
flush_rewrite_rules(false);
}
function queryVariables($query_vars)
{
$query_vars[] = 'crew_id';
return $query_vars;
}
function handleUrl(&$wp)
{
if ($wp->request == $this->page_slug) {
# http://stackoverflow.com/questions/17960649/wordpress-plugin-generating-virtual-pages-and-using-theme-template
# Note that we don't need to do a template redirect as suggesting in
# the example because all we do is load the template anyway. We can let
# the real template code work like it's supposed to and only override
# the content.
add_filter('the_posts', array(&$this, 'dummyPost'));
remove_filter('the_content', 'wpautop');
}
}
function dummyPost($posts)
{
// have to create a dummy post as otherwise many templates
// don't call the_content filter
global $wp, $wp_query;
//create a fake post instance
$p = new stdClass;
// fill $p with everything a page in the database would have
$p->ID = -1;
$p->post_author = 1;
$p->post_date = current_time('mysql');
$p->post_date_gmt = current_time('mysql', $gmt = 1);
$p->post_content = $this->renderPage($wp);
$p->post_title = 'NEXT Crew Schedule';
$p->post_excerpt = '';
$p->post_status = 'publish';
$p->ping_status = 'closed';
$p->post_password = '';
$p->post_name = $this->page_slug;
$p->to_ping = '';
$p->pinged = '';
$p->modified = $p->post_date;
$p->modified_gmt = $p->post_date_gmt;
$p->post_content_filtered = '';
$p->post_parent = 0;
$p->guid = get_home_url('/' . $p->post_name); // use url instead?
$p->menu_order = 0;
$p->post_type = 'page';
$p->post_mime_type = '';
$p->comment_status = 'closed';
$p->comment_count = 0;
$p->filter = 'raw';
$p->ancestors = array(); // 3.6
// reset wp_query properties to simulate a found page
$wp_query->is_page = TRUE;
$wp_query->is_singular = TRUE;
$wp_query->is_home = FALSE;
$wp_query->is_archive = FALSE;
$wp_query->is_category = FALSE;
unset($wp_query->query['error']);
$wp->query = array();
$wp_query->query_vars['error'] = '';
$wp_query->is_404 = FALSE;
$wp_query->current_post = $p->ID;
$wp_query->found_posts = 1;
$wp_query->post_count = 1;
$wp_query->comment_count = 0;
// -1 for current_comment displays comment if not logged in!
$wp_query->current_comment = null;
$wp_query->is_singular = 1;
$wp_query->post = $p;
$wp_query->posts = array($p);
$wp_query->queried_object = $p;
$wp_query->queried_object_id = $p->ID;
$wp_query->current_post = $p->ID;
$wp_query->post_count = 1;
return array($p);
}
function fetchGsheetJson($url)
{
// Get the body of the response
$response = wp_remote_get($url);
try {
// Note that we decode the body's response since it's the actual JSON feed
$json = json_decode($response['body']);
} catch (Exception $ex) {
$json = null;
}
return $json;
}
function renderPage(&$wp)
{
ob_start();
$current_schedule_url = $this->mon_gsheet_url;
$crew_id = get_query_var('crew_id');
if (!empty($crew_id)) {
$crew_id = trim(get_query_var('crew_id'));
} elseif (!empty($_POST['crew_id'])) {
$crew_id = trim($_POST['crew_id']);
}
$results_shown = false;
if (strlen($crew_id) > 0) {
// Remove hyphen from string, if exists.
$crew_id = trim($crew_id, '-');
// Force single numbers to zero-padded numbers
if (preg_match('/^[0-9]{1}+$/', $crew_id)) {
$crew_id = sprintf("%02d", $crew_id);
}
if (preg_match('/^[0-9]{2}+$/', $crew_id)) {
$results = $this->fetchGsheetJson($current_schedule_url);
if (empty($results)) {
?>
<div class="oanext_crew_num_bad">
<p>Error loading schedule.</p>
</div>
<?php
} else {
?>
<?php
$day = $results->feed->title->{'$t'};
$entries = $results->feed->entry;
$crew_found = false;
foreach ((array)$entries as $entry) {
$crew_number = $entry->{'gsx$crew'}->{'$t'};
if ($crew_number === $crew_id) {
$crew_found = true;
?>
<h4>Crew <?php echo htmlspecialchars($crew_id) ?> Schedule (<?php echo $day ?>)</h4>
<div class="table-responsive">
<table style="width:100%; " class="easy-table easy-table-default schedule" border="1">
<thead>
<tr>
<th style="width:25%;text-align:center">Time</th>
<th style="width:25%;text-align:center">Activity</th>
<th style="width:25%;text-align:center">Location</th>
</tr>
</thead>
<tbody>
<?php
for ($i = 1; $i <= 23; $i++) {
$time = $entry->{'gsx$duration' . $i}->{'$t'};
$activity = $entry->{'gsx$activity' . $i}->{'$t'};
$location = $entry->{'gsx$location' . $i}->{'$t'};
echo "<tr>";
echo "<td style=\"text-align:center\">" . $time . "</td>";
echo "<td style=\"text-align:center\">" . $activity . "</td>";
echo "<td style=\"text-align:center\">" . $location . "</td>";
echo "</tr>";
}
$results_shown = true;
}
}
if (!$crew_found) {
?>
<div class="oanext_crew_num_bad">
<p>Could not find a schedule for Crew number <?php echo htmlspecialchars($crew_id) ?>.</p>
</div>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
?>
<div class="oanext_crew_num_entry">
<p class="oanext_entry_inst">Check another Crew schedule:</p>
<form method="POST" action="">
<div class="oanext_input_group">
<label for="crew_id">Crew #:</label> <input id="crew_id" name="crew_id" type="number" size="9"><input
type="submit" value="Go">
</div>
</form>
<p class="oanext_help_inst">You can find your Crew number on the receipt you received at check-in. All crew
numbers are two digits (e.g. 01, 02, 10, 23, ...)</p>
</div>
<?php
} else {
?>
<div class="oanext_crew_num_bad">
<p>Could not find a schedule for Crew number <?php echo htmlspecialchars($crew_id) ?>.<br>Remember all crew
numbers are two digits (e.g. 01, 02, 10, 23, ...)</p>
</div>
<?php
}
}
$crew_id = get_query_var('crew_id');
if ((!isset($crew_id) || !isset($_POST['crew_id'])) && !$results_shown) {
?>
<div class="oanext_crew_num_entry">
<p class="oanext_entry_inst">Enter your Crew number to check your schedule.</p>
<form method="POST" action="">
<div class="oanext_input_group">
<label for="crew_id">Crew #:</label> <input id="crew_id" name="crew_id" type="number" size="9"><input
type="submit" value="Go">
</div>
</form>
<p class="oanext_help_inst">You can find your Crew number on the receipt you received at check-in. All crew
numbers are two digits (e.g. 01, 02, 10, 23, ...)</p>
</div>
<?php
}
return ob_get_clean();
}
}
}