[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/ -> station.module (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7