-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstation.module
491 lines (447 loc) · 13.9 KB
/
station.module
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'station') . '/dayhour.inc';
/**
* Implements hook_help().
*/
function station_help($path, $arg) {
switch ($path) {
case 'admin/config/station':
return t('Configure the core station settings. These settings are used by all of the station modules.');
break;
case 'admin/help#station';
return t('A module to manage radiostations');
break;
}
}
/**
* Implements hook_menu().
*/
function station_menu() {
$items = array();
$items['admin/config/station'] = array(
'title' => 'Station',
'description' => 'Change settings for the Station module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('station_admin_settings'),
'file' => 'station.admin.inc',
'access arguments' => array('administer site configuration'),
);
$items['admin/config/station/main'] = array(
'title' => 'Core',
'page callback' => 'drupal_get_form',
'page arguments' => array('station_admin_settings'),
'file' => 'station.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => '-10',
);
$items['station'] = array(
'title' => 'Station',
'page callback' => 'station_page',
'access arguments' => array('view published content'),
);
return $items;
}
/**
* Implements hook_theme().
*/
function station_theme() {
return array(
'station_block_current_program' => array(
'variables' => array('schedule' => NULL, 'program' => NULL),
),
'station_hour' => array(
'variables' => array('time' => NULL),
'file' => 'dayhour.inc',
),
'station_hour_duration' => array(
'variables' => array('start' => NULL, 'finish' => NULL),
'file' => 'dayhour.inc',
),
'station_day' => array(
'variables' => array('time' => NULL),
'file' => 'dayhour.inc',
),
'station_dayhour' => array(
'variables' => array('time' => NULL),
'file' => 'dayhour.inc',
),
'station_dayhour_range' => array(
'variables' => array('start' => NULL, 'finish' => NULL),
'file' => 'dayhour.inc',
),
'station_hour_range' => array(
'variables' => array('start' => NULL, 'finish' => NULL),
'file' => 'dayhour.inc',
),
'station_streams' => array(
'variables' => array('streams' => NULL),
),
);
}
/**
* Implements hook_block_info().
*/
function station_block_info() {
$blocks['current_program'] = array(
'info' => t('Station: Current Program'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function station_block_view($delta = '') {
if (user_access('view_station_schedule_content')) {
switch ($delta) {
case 'current_program':
return array(
'subject' => t('On Air'),
'content' => station_block_current_program(),
);
}
}
return;
}
/**
* Callback of /station menu item.
* menu_get_item() without an arguments returns the current's page menu_router item (an associative array).
* system_admin_menu_block() creates the content for an admin page by quering the database for pages that have the given page as parent.
* theme_admin_block_content() themes content that has been created by the previous function.
*/
function station_page() {
$output = '';
if ($content = system_admin_menu_block(menu_get_item())) {
$output = theme('admin_block_content', array('content' => $content));
}
return $output;
}
/**
* AJAX form handler.
* TODO: rewrite, it is used by the playlist module and gets called on /station/ahah/playlist page.
*/
function station_ajax_form_handler() {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
print('asdf');
// $form_build_id is randomly generated when a form is initializing
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We need to process the form, prepare for that by setting a few internals.
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form_state['#redirect'] = FALSE;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// If validation fails, force form submission.
if (form_get_errors()) {
form_execute_handlers('submit', $form, $form_state);
}
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
return $form;
}
/**
* Determine if we have a station schedule module running locally or access to a
* remote one.
*
* @return boolean
*/
function station_has_schedule() {
return (module_exists('station_schedule') || variable_get('station_remote_schedule_url', FALSE));
}
/**
* Return a list of schedules.
*
* The list of schedules is cached between calls.
*
* @return Array keyed to nid of schedules or FALSE on error.
*/
function station_get_schedules() {
static $schedules;
if (!isset($schedules)) {
if (module_exists('station_schedule')) {
$schedules = station_schedule_get_list();
}
else {
// If they haven't provided a URL we can't retreive any data.
$url = variable_get('station_remote_schedule_url', '');
if (empty($url)) {
return FALSE;
}
// Try to load the schedules from the cache.
$cacheid = 'station_remote:schedules_' . $url;
if ($cache = cache_get($cacheid, 'cache')) {
$schedules = $cache->data;
}
else {
// TO_CHECK
//$schedules = xmlrpc(check_url($url . '/xmlrpc.php'), 'station.schedule.get.list');
$schedules = xmlrpc(check_url($url . '/xmlrpc.php'), array('station.schedule.get.list' => array()));
if (xmlrpc_errno()) {
watchdog('station', 'Failed to load schedule info remotely. Error %code : %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg()), WATCHDOG_ERROR);
return FALSE;
}
// Save the value for the next call.
cache_set($cacheid, $schedules, 'cache', CACHE_TEMPORARY);
}
}
}
return $schedules;
}
/**
* Return the default schedule.
*
* @return Array with schedule info, or NULL if no schedules are available.
*/
function station_default_schedule() {
if (module_exists('station_schedule')) {
$id = variable_get('station_schedule_default', 0);
}
else {
$id = variable_get('station_remote_schedule_nid', 0);
}
$schedules = station_get_schedules();
// Return the default schedule if one has been selected, if not just use the
// first one.
if (isset($schedules[$id])) {
return $schedules[$id];
}
if (is_array($schedules)) {
return reset($schedules);
}
return NULL;
}
/**
* Return an object for the current program from the local machine or RPC if
* that's not available.
*
* @param $time
* A GMT timestamp.
* @param $schedule_nid
* Schedule Id, 0 will load the default schedule.
* @return
* FALSE if there was an error loading the data, NULL if nothing could be
* found, or, a program object if everything worked out.
*/
function station_get_program_at($timestamp, $schedule_nid) {
// If no schedule was provided, use the default.
if (empty($schedule_nid)) {
$schedule = station_default_schedule();
$schedule_nid = $schedule['nid'];
}
// Force the params to integers, the xmlrpc() call gets pissy if an int isn't
// typed as an int.
$schedule_nid = (int) $schedule_nid;
$timestamp = (int) $timestamp;
// Use the local schedule if one is available.
if (module_exists('station_schedule')) {
if ($program = station_schedule_program_get_at($timestamp, $schedule_nid)) {
if (!empty($program->nid)) {
return $program;
}
}
}
else {
// Try to connect to a remote schedule via XMLRPC for program information.
// The info will be cached to cut down on repeated RPC calls.
// If they haven't provided a url we can't retreive any data
$url = variable_get('station_remote_schedule_url', '');
if (empty($url)) {
return FALSE;
}
// add in our magic offset
$timestamp += 60 * variable_get('station_remote_schedule_offset', 0);
// round the time to the nearest 15 minute increment so we can do some
// caching
$parts = getdate($timestamp);
$minutes = $parts['minutes'];
if ($minutes < 15) {
$minutes = 0;
}
elseif ($minutes < 30) {
$minutes = 15;
}
elseif ($minutes < 45) {
$minutes = 30;
}
else {
$minutes = 45;
}
$timestamp = mktime($parts['hours'], $minutes, 0, $parts['mon'], $parts['mday'], $parts['year']);
// try to grab it from the cache
$cacheid = 'station_remote:program_at_' . $timestamp;
if ($cache = cache_get($cacheid, 'cache')) {
$program = $cache->data;
}
else {
// if it isn't cached get the program info from the server
// TO_CHECK
//$program = xmlrpc(check_url($url . '/xmlrpc.php'), 'station.program.get.at', $timestamp, $schedule_nid);
//$program = xmlrpc(check_url($url . '/xmlrpc.php'), array('station.program.get.at' => array()), array('timestamp' => $timestamp, 'schedule_nid' => $schedule_nid));
$program = xmlrpc(check_url($url . '/xmlrpc.php'), array('station.program.get.at' => array($timestamp, $schedule_nid)));
if (xmlrpc_errno()) {
watchdog('station', 'Failed to load program info remotely. Error %code : %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg()), WATCHDOG_ERROR);
return FALSE;
}
// save it to the cache
cache_set($cacheid, $program, 'cache', CACHE_TEMPORARY);
}
// program returned by XMLRPC is an array
if ($program['nid']) {
return (object) $program;
}
}
return NULL;
}
/**
* Return HTML body of the block listing the current program.
*
* @return string
*/
function station_block_current_program() {
$schedule = station_default_schedule();
$program = station_get_program_at(REQUEST_TIME, $schedule['nid']);
return theme('station_block_current_program', array('schedule' => $schedule, 'program' => $program));
}
/**
* Theme the current program block.
*
* @param $schedule
* Schedule array returned by station_default_schedule().
* @param $program
* Program node object.
* @return string
*/
function theme_station_block_current_program($variables) {
$schedule = $variables['schedule'];
$program = $variables['program'];
// Program or unscheduled...
if ($program) {
$output = l($program->title, $program->node_url) . '<br />';
}
else {
$output = check_plain($schedule['unscheduled_message']) . '<br />';
}
// Streams
$output .= theme('station_streams', array('streams' => $schedule['streams']));
return $output;
}
/**
* Theme a schedule's web streams.
*
* @param $nid
* A station schedule node id (might be on a remote site).
* @param $streams
* Schedule's streams.
*/
function theme_station_streams($variables) {
$streams = $variables['streams'];
$items = array();
foreach ((array) $streams as $key => $stream) {
$items[] = l($stream['name'], $stream['m3u_url'], array('title' => $stream['description']));
}
if (count($items)) {
return t('Tune in:') . ' ' . station_ored_list($items);
}
}
/**
* Convert an array to a comma separated list with an 'add' between the last
* terms.
*
* @param $array array of items
*/
function station_anded_list($array) {
switch (count($array)) {
case 0:
return '';
case 1:
return array_pop($array);
// Why no break?
default:
$last = array_pop($array);
// Shouldn't it be @list-items so as to pass text through check_plain()?
return t('!list-items and !last-item', array('!list-items' => implode(', ', $array), '!last-item' => $last));
}
}
/**
* Convert an array to a comma separates list with an 'or' between the last
* terms.
*
* @param $array array of items
*/
function station_ored_list($array) {
switch (count($array)) {
case 0:
return '';
case 1:
return array_pop($array);
default:
$last = array_pop($array);
return t('!list-items or !last-item', array('!list-items' => implode(', ', $array), '!last-item' => $last));
}
}
/**
* Return a timezone corrected timestamp.
*/
function station_local_ts($ts = FALSE) {
$ts = ($ts === FALSE) ? REQUEST_TIME : $ts;
return ($ts - date('Z', $ts)) + variable_get('date_default_timezone', 0);
}
/**
* Return the timezone corrected day of the week (1-7).
*/
function station_today() {
return date('w', station_local_ts());
}
/**
* Function to send notices of station changes via hook_station_notice.
*/
function _station_send_notice($type, $op, $data) {
module_invoke_all('station_notice', $type, $op, $data);
}
/**
* Implements hook_station_notices to display simple notices about
* station changes.
*
* @param $type 'dj' or 'schedule'
* @param $op 'add', 'remove', 'change' - Change only applies to schedule
* items.
* @param $data associative array with details like: nid, sid, uid.
*/
# Renamed this since it's basically just for debugging.
function _station_station_notice($type, $op, $data) {
if ($type == 'dj') {
$pid = $data['program_nid'];
$uid = $data['uid'];
switch ($op) {
case 'add':
drupal_set_message(check_plain("$sid adding $uid to $nid"));
break;
case 'remove':
drupal_set_message(check_plain("$sid removing $uid from $nid"));
break;
}
}
elseif ($type == 'schedule') {
$iid = $data['iid'];
$sid = $data['schedule_nid'];
$pid = $data['program_nid'];
switch ($op) {
case 'add':
drupal_set_message(check_plain("$sid adding $iid to $pid"));
break;
case 'change':
drupal_set_message(check_plain("$sid changing $iid on $pid"));
break;
case 'remove':
drupal_set_message(check_plain("$sid removing $iid from $pid"));
break;
}
}
}