<?php

/**
 * @file
 */

function theme_stormevent_list($header, $events) {
  $rows = array();

  foreach ($events as $event) {
    $dini_unix = date_convert($event->dini, DATE_ISO, DATE_UNIX);
    $dini_date = format_date($dini_unix, 'small', NULL, 0);
    $dini_day = substr($dini_date, 0, strrpos($dini_date, ' ') - 2);
    $dini_hour = substr($dini_date, strrpos($dini_date, ' ') + 1);
    $eventdate = $dini_day . ($event->drule ? '<sup>r</sup>' : '');
    if ($event->dini != $event->dend) {
      $dend_unix = date_convert($event->dend, DATE_ISO, DATE_UNIX);
      $dend_date = format_date($dend_unix, 'small', NULL, 0);
      $dend_day = substr($dend_date, 0, strrpos($dend_date, ' ') - 2);
      $dend_hour = substr($dend_date, strrpos($dend_date, ' ') + 1);
      if ($dini_day == $dend_day) {
        $eventdate .= " $dini_hour&nbsp;-&nbsp;$dend_hour";
      } elseif ($dini_hour == '00:00' && $dend_hour == '00:00') {
        $eventdate .= "&nbsp;- $dend_day";
      } else {
        $eventdate = "$dini_date $dend_date";
      }
    }
    $rows[] = array(
      storm_icon('eventtype_'. $event->eventtype, storm_attribute_value('Event type', $event->eventtype)),
      '<span class="event-title status_' . str_replace(' ', '_', strtolower($event->field_stormevent_status_value)) . '">' . l($event->title, 'node/'. $event->nid) . theme('mark', node_mark($event->nid, $event->changed)) . '</span><br />' .
        l($event->organization_title, 'node/'. $event->organization_nid) .
        (!empty($event->project_title) ? ' » ' . l($event->project_title, 'node/'. $event->project_nid) : '' ) .
        (!empty($event->task_title) ? ' » ' . l($event->task_title, 'node/'. $event->task_nid) : '' ),
      $event->clip ? storm_icon('attached', t('Has attached files')) : '&nbsp;',
      $eventdate,
      format_date($event->changed, 'small'),
      $event->version,
      array(
        'data' => storm_icon_edit_node($event, $_GET) . storm_icon_delete_node($event, $_GET),
        'class' => 'storm_list_operations',
      ),
    );
  }
  $o = theme('table', $header, $rows, array('id' => 'stormevents'));
  return $o;
}

function theme_stormevent_view($node, $teaser = FALSE, $page = FALSE) {
  drupal_add_css(drupal_get_path('module', 'storm') . '/storm-node.css', 'module');

  $node = node_prepare($node, $teaser);
  $l_pos = 1; // Used to increase the link position number (see issue 814820)

  $node->content['links'] = array(
    '#prefix' => '<div class="stormlinks"><dl>',
    '#suffix' => '</dl></div>',
    '#weight' => $l_pos++,
  );

  $node->content['group1'] = array(
    '#prefix' => '<div class="stormfields">',
    '#suffix' => '</div>',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group1') : -20,
  );
  $node->content['group1']['eventtype'] = array(
    '#prefix' => '<div class="eventtype">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Event type'), check_plain(storm_attribute_value('Event type', $node->eventtype))),
    '#weight' => 1,
  );
  if (!empty($node->organization_nid)) {
    $node->content['group1']['organization'] = array(
      '#prefix' => '<div class="organization">',
      '#suffix' => '</div>',
      '#value' => theme('storm_view_item', t('Organization'), l($node->organization_title, 'node/'. $node->organization_nid)),
      '#weight' => 2,
    );
  }
  if (!empty($node->project_nid)) {
    $node->content['group1']['project'] = array(
      '#prefix' => '<div class="project">',
      '#suffix' => '</div>',
      '#value' => theme('storm_view_item', t('Project'), l($node->project_title, 'node/'. $node->project_nid)),
      '#weight' => 3,
    );
  }
  if (!empty($node->task_nid)) {
    $node->content['group1']['task'] = array(
      '#prefix' => '<div class="task">',
      '#suffix' => '</div>',
      '#value' => theme('storm_view_item', t('Task'), l($node->task_title, 'node/'. $node->task_nid)),
      '#weight' => 4,
    );
  }
  $node->content['group1']['author'] = array(
    '#prefix' => '<div class="author">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Submitted by'), theme('username', $node)),
    '#weight' => 11,
  );
  $node->content['group1']['created'] = array(
    '#prefix' => '<div class="created">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Created'), format_date($node->created, 'small')),
    '#weight' => 12,
  );
  if ($node->changed != $node->created) {
    $node->content['group1']['modified'] = array(
      '#prefix' => '<div class="modified">',
      '#suffix' => '</div>',
      '#value' => theme('storm_view_item', t('Modified'), format_date($node->changed, 'small')),
      '#weight' => 13,
    );
  }
  $node->content['group1']['version'] = array(
    '#prefix' => '<div class="version">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Revision'), $node->version),
    '#weight' => 14,
  );

  $node->content['body_field'] = array(
    '#prefix' => '<div class="stormbody">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', NULL, $node->content['body']['#value']),
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'body_field') : -18,
  );
  unset($node->content['body']);

  return $node;
}
