<?php

/**
 * @file
 */

function theme_stormdok_list($header, $doks) {
  $rows = array();

  foreach ($doks as $dok) {
    $rows[] = array(
      storm_icon('doktype_'. str_replace(' ', '_', strtolower($dok->field_stormdok_content_value)), t($dok->field_stormdok_content_value)),
      storm_icon('dokstatus_'. str_replace(' ', '_', strtolower($dok->field_stormdok_status_value)), t($dok->field_stormdok_status_value)),
      '<span class="dok-title status_' . str_replace(' ', '_', strtolower($dok->field_stormdok_status_value)) . '">' . l($dok->title, 'node/'. $dok->nid) . theme('mark', node_mark($dok->nid, $dok->changed)) . '</span><br />' .
        l($dok->organization_title, 'node/'. $dok->organization_nid) .
        (!empty($dok->project_title) ? ' » ' . l($dok->project_title, 'node/'. $dok->project_nid) : '' ) .
        (!empty($dok->task_title) ? ' » ' . l($dok->task_title, 'node/'. $dok->task_nid) : '' ),
      $dok->clip ? storm_icon('attached', t('Has attached files')) : '&nbsp;',
      _format_small_date($dok->created),
      _format_small_date($dok->changed),
      $dok->version,
      array(
        'data' => storm_icon_edit_node($dok, $_GET) . storm_icon_delete_node($dok, $_GET),
        'class' => 'storm_list_operations',
      ),
    );
  }
  $o = theme('table', $header, $rows, array('id' => 'stormdoks'));
  return $o;
}

function theme_stormdok_view($node, $teaser = FALSE, $page = FALSE) {
  $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,
  );
  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' => 4,
    );
  }
  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' => 5,
    );
  }
  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' => 6,
    );
  }
  $node->content['group1']['author'] = array(
    '#prefix' => '<div class="author">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Author'), 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['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', t('Description'), $node->content['body']['#value']),
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'body_field') : -18,
  );
  unset($node->content['body']);

  return $node;
}
