You are here

function rave_alerts_remote_data in RAVE Alerts 7

Same name and namespace in other branches
  1. 1.0.x rave_alerts.module \rave_alerts_remote_data()

Gets the remote RSS data and returns sanitized data.

2 calls to rave_alerts_remote_data()
rave_alerts_block_view in ./rave_alerts.module
Implements hook_block_view().
rave_alerts_exit in ./rave_alerts.module
Implements hook_exit(). Used so this hook is called even if page is cached by Drupal

File

./rave_alerts.module, line 99
CU Alerts module

Code

function rave_alerts_remote_data() {

  //@TODO: We only need to set $data to NULL because there is no else handling

  // if we don't get a valid 200 response
  $data = NULL;
  if (variable_get('rave_alerts_check_enable', 0)) {
    $url = variable_get('rave_alerts_rss_url', 'https://www.getrave.com/rss/DemoUniversityAlert/channel8');
    if ($cache = cache_get('rave_alert', 'cache_rave_alerts')) {
      $data = $cache->data;
    }
    else {
      $response = drupal_http_request($url, array(
        'timeout' => 15,
      ));
      if ($response->code == '200') {

        // Check to see if the response is XML
        $is_xml = simplexml_load_string($response->data);
        if ($is_xml === FALSE) {
          $watchdog_message = 'Alerts xml is not valid: ' . $url;
          watchdog('rave_alerts', $watchdog_message, array(), WATCHDOG_NOTICE, NULL);
        }
        else {

          // Create new XMLElement
          $data = new SimpleXMLElement($response->data);

          // Convert into a nice array for us to work with.
          $data = json_encode($data);
          $data = json_decode($data, true);

          //@TODO: Make the cache timeout configurable
          cache_set('rave_alert', $data, 'cache_rave_alerts', time() + 60);

          // There is a new RSS item.  Evaluate the description for triggers.
          variable_set('rave_alerts_pubdate', $data['channel']['item']['pubDate']);
          $isCLEAR = strpos($data['channel']['item']['title'], variable_get('rave_alerts_clear_text', '[CLEAR]'));
          $isACTIVE = strpos($data['channel']['item']['title'], variable_get('rave_alerts_active_text', '[ACTIVE]'));
          if ($isCLEAR !== FALSE) {
            variable_set('rave_alerts_active_event', 0);
            variable_set('rave_alerts_display', 0);
            watchdog('rave_alerts', 'CLEAR token found. Clearing rave alert.');
          }
          if ($isACTIVE !== FALSE) {
            variable_set('rave_alerts_active_event', 1);
            watchdog('rave_alerts', 'ACTIVE token found');
          }
          if ($isCLEAR === FALSE) {
            $logMessageData = [
              'title' => $data['channel']['item']['title'],
              'description' => $data['channel']['item']['description'],
            ];
            variable_set('rave_alerts_display', 1);
            watchdog('rave_alerts', 'New Rave RSS item -> @title: @description', $logMessageData);
          }
        }

        // @TODO: Add option to leave all clear message up for X hours
      }
      else {
        variable_set('rave_alerts_active_event', 0);
        variable_set('rave_alerts_display', 0);
        $logMessage = "Bad Request: {$response->code} - {$response->error}. Request URL: {$response->url}";
        watchdog('rave_alerts', $logMessage);
      }
    }
  }
  return $data;
}