You are here

function agenda_debug in Agenda 7

Same name and namespace in other branches
  1. 6.2 agenda.admin.php \agenda_debug()
  2. 6 agenda.admin.php \agenda_debug()
  3. 7.2 agenda.admin.php \agenda_debug()

Provide a page to debug a calendar ID that is not working

1 string reference to 'agenda_debug'
agenda_menu in ./agenda.module
Implements hook_menu().

File

./agenda.admin.php, line 325
Administration interface for the agenda module

Code

function agenda_debug($bid) {
  $output = array();

  // Date check (http://drupal.org/node/545174)
  $output[] = t('Checking server time: %date', array(
    '%date' => gmdate('r'),
  ));
  $output[] = t('Checking real UTC time via NTP: %date', array(
    '%date' => gmdate('r', agenda_debug_ntp_time('0.pool.ntp.org')),
  ));
  $output[] = t('Ensure these values are approximately the same, otherwise your system is incorrectly configured and agenda will be unable to calculate dates properly.');

  // Find calendar sources
  $block = agenda_settings($bid);
  $output[] = t('Reading calendar input:');
  $output[] = '<pre>' . htmlspecialchars($block->calendars) . '</pre>';
  $calendars = preg_split('@\\r\\n?|\\n@', $block->calendars);
  $calendars = array_map('trim', $calendars);
  $output[] = t('Found following calendars:');
  $output[] = sprintf('<pre>%s</pre>', print_r($calendars, TRUE));
  $googleid = $calendars[0];
  if (count($calendars) > 1) {
    $output[] = t('Multiple calendars found, debugging with the first calendar: %googleid', array(
      '%googleid' => $googleid,
    ));
  }

  // Load the calendar
  list($address, $token) = _agenda_parse_googleid($googleid);
  $source = _agenda_feed_url($address, $token, $block);
  $output[] = t('Fetching feed from <em>%source</em>', array(
    '%source' => $source,
  ));

  // Load the XML
  $calendar = _agenda_load_xml($address, $token, $block);
  if (!$calendar) {
    $output[] = t('<strong>Warning</strong>: Failed to load XML');
    return theme('item_list', $output, NULL, 'ul', array(
      'id' => 'agenda-debug',
    ));
  }
  $output[] = t('Loaded XML successfully');

  // Find the events
  $number_of_events = count($calendar->entry);
  $output[] = t('Found @count events', array(
    '@count' => $number_of_events,
  ));
  if ($number_of_events === 0) {
    $output[] = t('<strong>Warning</strong>: Nothing to do with empty calendar, stopping!');
    return theme('item_list', $output, NULL, 'ul', array(
      'id' => 'agenda-debug',
    ));
  }

  // Parse the events
  $i = 0;
  $eventdata = array();
  foreach ($calendar->entry as $event) {
    $output[] = t('Loading event @number which is @size bytes', array(
      '@number' => ++$i,
      '@size' => strlen($event
        ->asXml()),
    ));
    $output[] = sprintf("<pre>%s</pre>", htmlspecialchars($event
      ->asXml()));
    $thisevent = _agenda_parse_event($event, $block);
    if (!$thisevent) {
      $output[] = t('<strong>Warning</strong>: Failed to parse event!');
      continue;
    }
    $output[] = t('Successfully parsed event:');
    $output[] = '<pre>' . print_r($thisevent, TRUE) . '</pre>';
    $eventdata[] = $thisevent;
  }
  $output[] = t('Parsed @success/@total events successfully', array(
    '@success' => count($eventdata),
    '@total' => $number_of_events,
  ));
  $output[] = t('Begin filtering based on date: string provided (@old) which has timestamp (@timestamp) which has date (@date).', array(
    '@old' => $what_is_old = agenda_variable_get($bid, 'start', '-1 day'),
    '@timestamp' => $what_is_old_ts = strtotime($what_is_old),
    '@date' => format_date($what_is_old_ts),
  ));
  foreach ($eventdata as $key => $event) {
    if ($event['start timestamp'] < $what_is_old_ts) {
      $output[] = t('&raquo; Discarding %title because %start is older than %old', array(
        '%title' => $event['title'],
        '%start' => format_date($event['start timestamp'], 'small'),
        '%old' => format_date($what_is_old_ts, 'small'),
      ));
      unset($eventdata[$key]);
    }
  }
  $output[] = t('Finished. Started with @total events, will display @display events', array(
    '@total' => $number_of_events,
    '@display' => count($eventdata),
  ));

  // Now take all of our debug data and theme it up, starting with the logs
  $debug_log = theme('item_list', array(
    'items' => $output,
  ));

  // Build a table of all of the events found
  $event_table = '';
  if (count($eventdata)) {
    $first_row = array_slice($eventdata, 0, 1);
    $event_table = theme('table', array(
      'header' => array_keys($first_row[0]),
      'rows' => $eventdata,
      'sticky' => FALSE,
    ));
  }

  // Render
  return array(
    'title' => array(
      '#markup' => '<h2>' . t('Debugging %calendar block', array(
        '%calendar' => $block->title,
      )) . '</h2>',
    ),
    'debug_log' => array(
      '#markup' => $debug_log,
      '#prefix' => '<h3>Log</h3><div id="agenda-debug-log">',
      '#suffix' => '</div>',
    ),
    'event_table' => array(
      '#markup' => $event_table,
      '#prefix' => '<h3>Events</h3><div id="agenda-debug-table">',
      '#suffix' => '</div>',
    ),
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'agenda') . '/agenda.css',
      ),
    ),
  );
}