| 1 |
<?php
|
| 2 |
// $Id: station.module,v 1.56 2009/09/12 17:13:39 drewish Exp $
|
| 3 |
|
| 4 |
require_once(drupal_get_path('module', 'station') .'/dayhour.inc');
|
| 5 |
|
| 6 |
function station_help($path, $arg) {
|
| 7 |
switch ($path) {
|
| 8 |
case 'admin/settings/station':
|
| 9 |
return t('Configure the core station settings. These settings are used by all of the station modules.');
|
| 10 |
}
|
| 11 |
}
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_menu().
|
| 15 |
*/
|
| 16 |
function station_menu() {
|
| 17 |
$items = array();
|
| 18 |
|
| 19 |
$items['admin/settings/station'] = array(
|
| 20 |
'title' => 'Station',
|
| 21 |
'description' => 'Change settings for the Station module.',
|
| 22 |
'page callback' => 'drupal_get_form',
|
| 23 |
'page arguments' => array('station_admin_settings'),
|
| 24 |
'file' => 'station.admin.inc',
|
| 25 |
'access arguments' => array('administer site configuration'),
|
| 26 |
);
|
| 27 |
$items['admin/settings/station/main'] = array(
|
| 28 |
'title' => 'Core',
|
| 29 |
'page callback' => 'drupal_get_form',
|
| 30 |
'page arguments' => array('station_admin_settings'),
|
| 31 |
'file' => 'station.admin.inc',
|
| 32 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 33 |
'weight' => '-10',
|
| 34 |
);
|
| 35 |
$items['station'] = array(
|
| 36 |
'title' => 'Station',
|
| 37 |
'page callback' => 'station_page',
|
| 38 |
'access arguments' => array('access content'),
|
| 39 |
);
|
| 40 |
|
| 41 |
return $items;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implementation of hook_theme().
|
| 46 |
*/
|
| 47 |
function station_theme() {
|
| 48 |
return array(
|
| 49 |
'station_block_current_program' => array(
|
| 50 |
'arguments' => array('schedule' => NULL, 'program' => NULL),
|
| 51 |
),
|
| 52 |
'station_hour' => array(
|
| 53 |
'arguments' => array('time' => NULL),
|
| 54 |
'file' => 'dayhour.inc',
|
| 55 |
),
|
| 56 |
'station_hour_duration' => array(
|
| 57 |
'arguments' => array('start' => NULL, 'finish' => NULL),
|
| 58 |
'file' => 'dayhour.inc',
|
| 59 |
),
|
| 60 |
'station_dayhour' => array(
|
| 61 |
'arguments' => array('time' => NULL),
|
| 62 |
'file' => 'dayhour.inc',
|
| 63 |
),
|
| 64 |
'station_dayhour_range' => array(
|
| 65 |
'arguments' => array('start' => NULL, 'finish' => NULL),
|
| 66 |
'file' => 'dayhour.inc',
|
| 67 |
),
|
| 68 |
'station_hour_range' => array(
|
| 69 |
'arguments' => array('start' => NULL, 'finish' => NULL),
|
| 70 |
'file' => 'dayhour.inc',
|
| 71 |
),
|
| 72 |
'station_streams' => array(
|
| 73 |
'arguments' => array('streams' => NULL),
|
| 74 |
),
|
| 75 |
);
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_block().
|
| 80 |
*/
|
| 81 |
function station_block($op = 'list', $delta = 0, $edit = array()) {
|
| 82 |
switch ($op) {
|
| 83 |
case 'view':
|
| 84 |
if (user_access('view station schedule content')) {
|
| 85 |
switch ($delta) {
|
| 86 |
case 0:
|
| 87 |
return array(
|
| 88 |
'subject' => t('On Air'),
|
| 89 |
'content' => station_block_current_program(),
|
| 90 |
);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
return;
|
| 94 |
|
| 95 |
case 'list':
|
| 96 |
$blocks[0] = array(
|
| 97 |
'info' => t('Station: Current Program'),
|
| 98 |
'status' => TRUE,
|
| 99 |
'region' => 'left',
|
| 100 |
'cache' => BLOCK_NO_CACHE,
|
| 101 |
);
|
| 102 |
return $blocks;
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
function station_page() {
|
| 107 |
$output = '';
|
| 108 |
if ($content = system_admin_menu_block(menu_get_item())) {
|
| 109 |
$output = theme('admin_block_content', $content);
|
| 110 |
}
|
| 111 |
return $output;
|
| 112 |
}
|
| 113 |
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Determine if we have a station archive module running locally or access to a
|
| 117 |
* remote one.
|
| 118 |
*
|
| 119 |
* @return boolean
|
| 120 |
*/
|
| 121 |
function station_has_archive() {
|
| 122 |
return (module_exists('station_archive') || variable_get('station_remote_archive_url', FALSE));
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Determine if we have a station schedule module running locally or access to a
|
| 127 |
* remote one.
|
| 128 |
*
|
| 129 |
* @return boolean
|
| 130 |
*/
|
| 131 |
function station_has_schedule() {
|
| 132 |
return (module_exists('station_schedule') || variable_get('station_remote_schedule_url', FALSE));
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* If there's an archive, return the URL.
|
| 137 |
*
|
| 138 |
* @return FALSE or string URL.
|
| 139 |
*/
|
| 140 |
function station_get_archive_url() {
|
| 141 |
if (module_exists('station_archive')) {
|
| 142 |
return 'station/archives/';
|
| 143 |
}
|
| 144 |
elseif ($url = variable_get('station_remote_archive_url', FALSE)) {
|
| 145 |
return $url .'station/archives/';
|
| 146 |
}
|
| 147 |
return FALSE;
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Return a list of schedules.
|
| 152 |
*
|
| 153 |
* The list of schedules is cached between calls.
|
| 154 |
*
|
| 155 |
* @return Array keyed to nid of schedules or FALSE on error.
|
| 156 |
*/
|
| 157 |
function station_get_schedules() {
|
| 158 |
static $schedules;
|
| 159 |
|
| 160 |
if (!isset($schedules)) {
|
| 161 |
if (module_exists('station_schedule')) {
|
| 162 |
$schedules = station_schedule_get_list();
|
| 163 |
}
|
| 164 |
else {
|
| 165 |
// If they haven't provided a URL we can't retreive any data.
|
| 166 |
$url = variable_get('station_remote_schedule_url', '');
|
| 167 |
if (empty($url)) {
|
| 168 |
return FALSE;
|
| 169 |
}
|
| 170 |
|
| 171 |
// Try to load the schedules from the cache.
|
| 172 |
$cacheid = 'station_remote:schedules_'. $url;
|
| 173 |
if ($cache = cache_get($cacheid, 'cache')) {
|
| 174 |
$schedules = $cache->data;
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
$schedules = xmlrpc(check_url($url .'/xmlrpc.php'), 'station.schedule.get.list');
|
| 178 |
if (xmlrpc_errno()) {
|
| 179 |
watchdog('station', 'Failed to load schedule info remotely. Error %code : %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg()), WATCHDOG_ERROR);
|
| 180 |
return FALSE;
|
| 181 |
}
|
| 182 |
// Save the value for the next call.
|
| 183 |
cache_set($cacheid, $schedules, 'cache', CACHE_TEMPORARY);
|
| 184 |
}
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
return $schedules;
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Return the default schedule.
|
| 193 |
*
|
| 194 |
* @return Array with schedule info, or NULL if no schedules are available.
|
| 195 |
*/
|
| 196 |
function station_default_schedule() {
|
| 197 |
if (module_exists('station_schedule')) {
|
| 198 |
$id = variable_get('station_schedule_default', 0);
|
| 199 |
}
|
| 200 |
else {
|
| 201 |
$id = variable_get('station_remote_schedule_nid', 0);
|
| 202 |
}
|
| 203 |
$schedules = station_get_schedules();
|
| 204 |
// Return the default schedule if one has been selected, if not just use the
|
| 205 |
// first one.
|
| 206 |
if (isset($schedules[$id])) {
|
| 207 |
return $schedules[$id];
|
| 208 |
}
|
| 209 |
if (is_array($schedules)) {
|
| 210 |
return reset($schedules);
|
| 211 |
}
|
| 212 |
return NULL;
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
| 216 |
* Return an object for the current program from the local machine or RPC if
|
| 217 |
* that's not available.
|
| 218 |
*
|
| 219 |
* @param $time
|
| 220 |
* A GMT timestamp.
|
| 221 |
* @param $schedule_nid
|
| 222 |
* Schedule Id, 0 will load the default schedule.
|
| 223 |
* @return
|
| 224 |
* FALSE if there was an error loading the data, NULL if nothing could be
|
| 225 |
* found, or, a program object if everything worked out.
|
| 226 |
*/
|
| 227 |
function station_get_program_at($timestamp, $schedule_nid) {
|
| 228 |
// If no schedule was provided, use the default.
|
| 229 |
if (empty($schedule_nid)) {
|
| 230 |
$schedule = station_default_schedule();
|
| 231 |
$schedule_nid = $schedule['nid'];
|
| 232 |
}
|
| 233 |
|
| 234 |
// Force the params to integers, the xmlrpc() call gets pissy if an int isn't
|
| 235 |
// typed as an int.
|
| 236 |
$schedule_nid = (int) $schedule_nid;
|
| 237 |
$timestamp = (int) $timestamp;
|
| 238 |
|
| 239 |
// Use the local schedule if one is available.
|
| 240 |
if (module_exists('station_schedule')) {
|
| 241 |
if ($program = station_schedule_program_get_at($timestamp, $schedule_nid)) {
|
| 242 |
if (!empty($program->nid)) {
|
| 243 |
return $program;
|
| 244 |
}
|
| 245 |
}
|
| 246 |
}
|
| 247 |
else {
|
| 248 |
// Try to connect to a remote schedule via XMLRPC for program information.
|
| 249 |
// The info will be cached to cut down on repeated RPC calls.
|
| 250 |
|
| 251 |
// If they haven't provided a url we can't retreive any data
|
| 252 |
$url = variable_get('station_remote_schedule_url', '');
|
| 253 |
if (empty($url)) {
|
| 254 |
return FALSE;
|
| 255 |
}
|
| 256 |
|
| 257 |
// add in our magic offset
|
| 258 |
$timestamp += 60 * variable_get('station_remote_schedule_offset', 0);
|
| 259 |
|
| 260 |
// round the time to the nearest 15 minute increment so we can do some
|
| 261 |
// caching
|
| 262 |
$parts = getdate($timestamp);
|
| 263 |
$minutes = $parts['minutes'];
|
| 264 |
if ($minutes < 15)
|
| 265 |
$minutes = 0;
|
| 266 |
elseif ($minutes < 30)
|
| 267 |
$minutes = 15;
|
| 268 |
elseif ($minutes < 45)
|
| 269 |
$minutes = 30;
|
| 270 |
else
|
| 271 |
$minutes = 45;
|
| 272 |
$timestamp = mktime($parts['hours'], $minutes, 0, $parts['mon'], $parts['mday'], $parts['year']);
|
| 273 |
|
| 274 |
// try to grab it from the cache
|
| 275 |
$cacheid = 'station_remote:program_at_'. $timestamp;
|
| 276 |
if ($cache = cache_get($cacheid, 'cache')) {
|
| 277 |
$program = $cache->data;
|
| 278 |
}
|
| 279 |
else {
|
| 280 |
// if it isn't cached get the program info from the server
|
| 281 |
$program = xmlrpc(check_url($url .'/xmlrpc.php'), 'station.program.get.at', $timestamp, $schedule_nid);
|
| 282 |
if (xmlrpc_errno()) {
|
| 283 |
watchdog('station', 'Failed to load program info remotely. Error %code : %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg()), WATCHDOG_ERROR);
|
| 284 |
return FALSE;
|
| 285 |
}
|
| 286 |
// save it to the cache
|
| 287 |
cache_set($cacheid, $program, 'cache', CACHE_TEMPORARY);
|
| 288 |
}
|
| 289 |
|
| 290 |
// program returned by XMLRPC is an array
|
| 291 |
if ($program['nid']) {
|
| 292 |
return (object) $program;
|
| 293 |
}
|
| 294 |
}
|
| 295 |
|
| 296 |
return NULL;
|
| 297 |
}
|
| 298 |
|
| 299 |
/**
|
| 300 |
* Return HTML body of the block listing the current program.
|
| 301 |
*
|
| 302 |
* @return string
|
| 303 |
*/
|
| 304 |
function station_block_current_program() {
|
| 305 |
$schedule = station_default_schedule();
|
| 306 |
$program = station_get_program_at(time(), $schedule['nid']);
|
| 307 |
return theme('station_block_current_program', $schedule, $program);
|
| 308 |
}
|
| 309 |
|
| 310 |
/**
|
| 311 |
* Theme the current program block.
|
| 312 |
*
|
| 313 |
* @param $schedule
|
| 314 |
* Schedule array returned by station_default_schedule().
|
| 315 |
* @param $program
|
| 316 |
* Program node object.
|
| 317 |
* @return string
|
| 318 |
*/
|
| 319 |
function theme_station_block_current_program($schedule, $program) {
|
| 320 |
// Program or unscheduled...
|
| 321 |
if ($program) {
|
| 322 |
$output = l($program->title, $program->node_url) .'<br />';
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
$output = check_plain($schedule['unscheduled_message']) .'<br />';
|
| 326 |
}
|
| 327 |
|
| 328 |
// Streams
|
| 329 |
$output .= theme('station_streams', $schedule['streams']);
|
| 330 |
|
| 331 |
return $output;
|
| 332 |
}
|
| 333 |
|
| 334 |
/**
|
| 335 |
* Theme a schedule's web streams.
|
| 336 |
*
|
| 337 |
* @param $nid
|
| 338 |
* A station schedule node id (might be on a remote site).
|
| 339 |
* @param $streams
|
| 340 |
* Schedule's streams.
|
| 341 |
*/
|
| 342 |
function theme_station_streams($streams) {
|
| 343 |
$items = array();
|
| 344 |
foreach ((array) $streams as $key => $stream) {
|
| 345 |
$items[] = l($stream['name'], $stream['m3u_url'], array('title' => $stream['description']));
|
| 346 |
}
|
| 347 |
if (count($items)) {
|
| 348 |
return t('Tune in: ') . station_ored_list($items);
|
| 349 |
}
|
| 350 |
}
|
| 351 |
|
| 352 |
|
| 353 |
/**
|
| 354 |
* Convert an array to a comma separates list with an 'add' between the last
|
| 355 |
* terms.
|
| 356 |
*
|
| 357 |
* @param $array array of items
|
| 358 |
*/
|
| 359 |
function station_anded_list($array) {
|
| 360 |
switch (count($array)) {
|
| 361 |
case 0:
|
| 362 |
return '';
|
| 363 |
case 1:
|
| 364 |
return array_pop($array);
|
| 365 |
default:
|
| 366 |
$last = array_pop($array);
|
| 367 |
return implode(', ', $array) . t(' and ') . $last;
|
| 368 |
}
|
| 369 |
}
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Convert an array to a comma separates list with an 'or' between the last
|
| 373 |
* terms.
|
| 374 |
*
|
| 375 |
* @param $array array of items
|
| 376 |
*/
|
| 377 |
function station_ored_list($array) {
|
| 378 |
switch (count($array)) {
|
| 379 |
case 0:
|
| 380 |
return '';
|
| 381 |
case 1:
|
| 382 |
return array_pop($array);
|
| 383 |
default:
|
| 384 |
$last = array_pop($array);
|
| 385 |
return implode(', ', $array) . t(' or ') . $last;
|
| 386 |
}
|
| 387 |
}
|
| 388 |
|
| 389 |
/**
|
| 390 |
* Return a timezone corrected timestamp.
|
| 391 |
*/
|
| 392 |
function station_local_ts($ts = FALSE) {
|
| 393 |
$ts = ($ts === FALSE) ? time() : $ts;
|
| 394 |
return ($ts - date('Z', $ts)) + variable_get('date_default_timezone', 0);
|
| 395 |
}
|
| 396 |
|
| 397 |
/**
|
| 398 |
* Return the timezone corrected day of the week (1-7).
|
| 399 |
*/
|
| 400 |
function station_today() {
|
| 401 |
return date('w', station_local_ts());
|
| 402 |
}
|
| 403 |
|
| 404 |
/**
|
| 405 |
* Function to send notices of station changes via hook_station_notices.
|
| 406 |
*/
|
| 407 |
function _station_send_notice($type, $op, $data) {
|
| 408 |
module_invoke_all('station_notice', $type, $op, $data);
|
| 409 |
}
|
| 410 |
|
| 411 |
/**
|
| 412 |
* Implementation of hook_station_notices to display simple notices about
|
| 413 |
* station changes.
|
| 414 |
*
|
| 415 |
* @param $type 'dj' or 'schedule'
|
| 416 |
* @param $op 'add', 'remove', 'change' - Change only applies to schedule
|
| 417 |
* items.
|
| 418 |
* @param $data associative array with details like: nid, sid, uid.
|
| 419 |
*/
|
| 420 |
# Renamed this since it's basically just for debugging.
|
| 421 |
function _station_station_notice($type, $op, $data) {
|
| 422 |
if ($type == 'dj') {
|
| 423 |
$pid = $data['program_nid'];
|
| 424 |
$uid = $data['uid'];
|
| 425 |
switch ($op) {
|
| 426 |
case 'add':
|
| 427 |
drupal_set_message("$sid adding $uid to $nid");
|
| 428 |
break;
|
| 429 |
case 'remove':
|
| 430 |
drupal_set_message("$sid removing $uid from $nid");
|
| 431 |
break;
|
| 432 |
}
|
| 433 |
}
|
| 434 |
elseif ($type == 'schedule') {
|
| 435 |
$iid = $data['iid'];
|
| 436 |
$sid = $data['schedule_nid'];
|
| 437 |
$pid = $data['program_nid'];
|
| 438 |
switch ($op) {
|
| 439 |
case 'add':
|
| 440 |
drupal_set_message("$sid adding $iid to $pid");
|
| 441 |
break;
|
| 442 |
case 'change':
|
| 443 |
drupal_set_message("$sid changing $iid on $pid");
|
| 444 |
break;
|
| 445 |
case 'remove':
|
| 446 |
drupal_set_message("$sid removing $iid from $pid");
|
| 447 |
break;
|
| 448 |
}
|
| 449 |
}
|
| 450 |
}
|