<?php

/**
 * @file
 */

function theme_stormtimetracking_list($header, $timetrackings, $billing_duration) {
  $rows = array();
  foreach ($timetrackings as $timetracking) {
    $rows[] = array(
      '<span class="timetracking-title">' . l($timetracking->title, 'node/'. $timetracking->nid) . theme('mark', node_mark($timetracking->nid, $timetracking->changed)) . '</span><br />' .
        l($timetracking->organization_title, 'node/'. $timetracking->organization_nid) .
        (!empty($timetracking->project_title) ? ' » ' . l($timetracking->project_title, 'node/'. $timetracking->project_nid) : '' ) .
        (!empty($timetracking->task_title) ? '<br />' . l($timetracking->task_title, 'node/'. $timetracking->task_nid) . ' (' . t('task') . ')' : '' ) .
        (!empty($timetracking->ticket_title) ? '<br />' . l($timetracking->ticket_title, 'node/'. $timetracking->ticket_nid) . ' (' . t('ticket') . ')' : '' ),
      _format_small_date($timetracking->trackingdate),
      array('data' => stormtimetracking_duration($timetracking->billing_duration), 'align' => 'right'),
      array(
        'data' => storm_icon_edit_node($timetracking, $_GET) .'&nbsp;'. storm_icon_delete_node($timetracking, $_GET),
        'class' => 'storm_list_operations',
      ),
    );
  }
  $o = theme('table', $header, $rows, array('id' => 'stormtimetrackings'));
  $o .= '<span style="font-weight: bold;">'. t('Total duration') .' : '. stormtimetracking_duration($billing_duration) .'</span>';
  return $o;
}

function theme_stormtimetracking_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' => -25,
  );

  // 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' => $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']['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'] = 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'] = 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']['ticket'] = array(
    '#prefix' => '<div class="ticket">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Ticket'), l($node->ticket_title, 'node/'. $node->ticket_nid)),
    '#weight' => 4,
  );

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

  $node->content['group2']['trackingdate'] = array(
    '#prefix' => '<div class="trackingdate">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Date'), empty($node->trackingdate) ? '' : _format_short_date($node->trackingdate)),
    '#weight' => 2,
  );

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

  $node->content['group3']['duration'] = array(
    '#prefix' => '<div class="duration">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Duration'), stormtimetracking_duration($node->billing_duration)),
    '#weight' => 2,
  );

  $node->content['group_item'] = array(
    '#prefix' => '<div class="stormfields">',
    '#suffix' => '</div>',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group_item') : -20,
  );
  $node->content['group_item']['author'] = array(
    '#prefix' => '<div class="author">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Submitted by'), theme('username', $node)),
    '#weight' => 1,
  );
  $node->content['group_item']['created'] = array(
    '#prefix' => '<div class="created">',
    '#suffix' => '</div>',
    '#value' => theme('storm_view_item', t('Created'), _format_small_date($node->created)),
    '#weight' => 2,
  );
  if ($node->changed != $node->created) {
    $node->content['group_item']['modified'] = array(
      '#prefix' => '<div class="modified">',
      '#suffix' => '</div>',
      '#value' => theme('storm_view_item', t('Modified'), _format_small_date($node->changed)),
      '#weight' => 3,
    );
  }

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

  unset($node->content['body']);

  return $node;
}

function theme_stormtimetracking_list_form_report_reports() {
  $reports = array(
      'std' => t('Standard'),
      'for_organization' => t('For an organization'),
      'for_organization_w_task' => t('For an org. with tasks'),
      'for_project' => t('For a project'),
  );

  return $reports;
}

function theme_stormtimetracking_list_report($report, $language, $timetrackings) {
  switch ($report) {
    case 'std':
      $header = array(
        t('Organization', array(), $language),
        t('Project', array(), $language),
        t('Title', array(), $language),
        t('Date', array(), $language),
        t('Billing duration (hours)', array(), $language));
      $total_billing_duration = 0;
      $rows = array();
      foreach ($timetrackings as $timetracking) {
        $rows[] = array(
          check_plain($timetracking->organization_title),
          check_plain($timetracking->project_title),
          check_plain($timetracking->title),
          _format_small_date($timetracking->trackingdate),
          array('data' => stormtimetracking_duration($timetracking->billing_duration), 'align' => 'right'),
        );
        $total_billing_duration += $timetracking->billing_duration;
      }
      $title  = '<h2>'. t('Timetrackings report', array(), $language) .'</h2><br />';
    break;
    case 'for_organization':
      $organization = node_load($_SESSION['stormtimetracking_list_filter']['organization_nid']);
      $header = array(
        t('Project', array(), $language),
        t('Title', array(), $language),
        t('Date', array(), $language),
        t('Billing duration (hours)', array(), $language));
      $total_billing_duration = 0;
      foreach ($timetrackings as $timetracking) {
        $rows[] = array(
          check_plain($timetracking->project_title),
          check_plain($timetracking->title),
          _format_small_date($timetracking->trackingdate),
          array('data' => stormtimetracking_duration($timetracking->billing_duration), 'align' => 'right'),
        );
        $total_billing_duration += $timetracking->billing_duration;
      }
      $title  = '<h2>'. t('Timetrackings report', array(), $language) .'</h2><br />';
      $title .= t('Organization : @organization', array('@organization' => $organization->fullname), $language) .'<br />';
    break;
    case 'for_organization_w_task':
      $organization = node_load($_SESSION['stormtimetracking_list_filter']['organization_nid']);
      $header = array(
        t('Project', array(), $language),
        t('Task', array(), $language),
        t('Title', array(), $language),
        t('Date', array(), $language),
        t('Billing duration (hours)', array(), $language));
      $total_billing_duration = 0;
      foreach ($timetrackings as $timetracking) {
        $rows[] = array(
          check_plain($timetracking->project_title),
          check_plain($timetracking->task_title),
          check_plain($timetracking->title),
          _format_small_date($timetracking->trackingdate),
          array('data' => stormtimetracking_duration($timetracking->billing_duration), 'align' => 'right'),
        );
        $total_billing_duration += $timetracking->billing_duration;
      }
      $title  = '<h2>'. t('Timetrackings report', array(), $language) .'</h2><br />';
      $title .= t('Organization : @organization', array('@organization' => $organization->fullname), $language) .'<br />';
    break;
    case 'for_project':
      $organization = node_load($_SESSION['stormtimetracking_list_filter']['organization_nid']);
      $project = node_load($_SESSION['stormtimetracking_list_filter']['project_nid']);

      $header = array(
        t('Task', array(), $language),
        t('Title', array(), $language),
        t('Date', array(), $language),
        t('Duration (hours)', array(), $language));
      $total_billing_duration = 0;
      foreach ($timetrackings as $timetracking) {
        $rows[] = array(
          check_plain($timetracking->task_title),
          check_plain($timetracking->title),
          _format_small_date($timetracking->trackingdate),
          array('data' => stormtimetracking_duration($timetracking->billing_duration), 'align' => 'right'),
        );
        $total_billing_duration += $timetracking->billing_duration;
      }
      $title  = '<h2>'. t('Timetrackings report', array(), $language) .'</h2><br />';
      $title .= t('Organization : @organization', array('@organization' => $organization->fullname), $language) .'<br />';
      $title .= t('Project : @project', array('@project' => $project->title), $language) .'<br />';
    break;
  }
  $footer = '<h3>'. t('Total billing duration : %total_billing_duration', array('%total_billing_duration' => stormtimetracking_duration($total_billing_duration)), $language) .'</h3>';
  return theme('storm_list_report', $header, $rows, $title, $footer);
}
