<?php

/**
 * @file
 */

function theme_stormticket_list($header, $tickets) {

  $rows = array();
  foreach ($tickets as $ticket) {
    $rows[] = array(
      storm_icon('category_'. $ticket->ticketcategory, storm_attribute_value('Ticket category', $ticket->ticketcategory)),
      storm_icon('status_'. $ticket->ticketstatus, storm_attribute_value('Ticket status', $ticket->ticketstatus)),
      '<span class="ticket-title status_' . str_replace(' ', '_', $ticket->ticketstatus) . '">' . l($ticket->title, 'node/'. $ticket->nid) . theme('mark', node_mark($ticket->nid, $ticket->changed)) . '</span><br />' .
        l($ticket->organization_title, 'node/'. $ticket->organization_nid) .
        (!empty($ticket->project_title) ? ' » ' . l($ticket->project_title, 'node/'. $ticket->project_nid) : '' ),
      $ticket->clip ? storm_icon('attached', t('Has attached files')) : '&nbsp;',
      _format_small_date($ticket->changed),
      storm_icon('priority_'. $ticket->ticketpriority, storm_attribute_value('Ticket priority', $ticket->ticketpriority)),
      array(
        'data' => $ticket->comment_count,
        'class' => 'storm_list_comments',
      ),
      array(
        'data' => storm_icon_comment_node($ticket, $_GET) . storm_icon_edit_node($ticket, $_GET) . storm_icon_delete_node($ticket, $_GET),
        'class' => 'storm_list_operations',
      ),
    );
  }
  $o = theme('table', $header, $rows, array('id' => 'stormtickets'));
  return $o;
}

function theme_stormticket_view($node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node, $teaser);

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

  $node->content['links']['expenses'] = theme('storm_link', 'stormticket', 'stormexpense', $node->nid, 1);
  $node->content['links']['timetrackings'] = theme('storm_link', 'stormticket', 'stormtimetracking', $node->nid, 2);

  // Code to create invoice auto_add link
  if (module_exists('storminvoice')) {

    $node->content['links']['auto_invoice'] = array(
      '#prefix' => variable_get('storm_icons_display', TRUE) ? '<dt id="storminvoice-item" class="stormcomponent">' : '<dt class="stormcomponent">',
      '#suffix' => '</dt>',
      '#value' => theme('storminvoice_autoadd_links', $node->nid, $node->billable, $node->billed),
      '#weight' => 3,
    );
  }

  $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']['organization'] = array(
    '#prefix' => '<div class="organization">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Organization'), l($node->organization_title, 'node/'. $node->organization_nid)),
    '#weight' => 1,
  );

  $node->content['group1']['project_nid'] = array(
    '#prefix' => '<div class="project">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Project'), l($node->project_title, 'node/'. $node->project_nid)),
    '#weight' => 2,
  );

  $node->content['group1']['task_nid'] = array(
    '#prefix' => '<div class="task">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Task'), l($node->task_title, 'node/'. $node->task_nid)),
    '#weight' => 3,
  );

  $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_small_date($node->created)),
    '#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_small_date($node->changed)),
      '#weight' => 13,
    );
  }

  $node->content['group2'] = array(
    '#prefix' => '<div class="stormfields">',
    '#suffix' => '</div>',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group2') : -19,
  );

  $node->content['group2']['category'] = array(
    '#prefix' => '<div class="category">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Category'), check_plain(storm_attribute_value('Ticket category', $node->ticketcategory))),
    '#weight' => 1,
  );

  $node->content['group2']['status'] = array(
    '#prefix' => '<div class="storm_status">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Status'), check_plain(storm_attribute_value('Ticket status', $node->ticketstatus))),
    '#weight' => 2,
  );

  $node->content['group2']['priority'] = array(
    '#prefix' => '<div class="priority">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Priority'), check_plain(storm_attribute_value('Ticket priority', $node->ticketpriority))),
    '#weight' => 3,
  );

  $node->content['group5'] = array(
    '#prefix' => '<div class="stormfields">',
    '#suffix' => '</div>',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group2') : -16,
  );

  $node->content['group5']['assigned'] = array(
    '#prefix' => '<div class="assigned">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Assigned to'), l($node->assigned_title, 'node/'. $node->assigned_nid)),
  );

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

  return $node;
}
