<?php

/**
 * @file
 */

function stormtimetracking_help($path, $arg) {
  $o = '';

  switch ($path) {
    case "admin/help#stormtimetracking":
      $o = '<p>'. t("Provides timetracking support for Storm") .'</p>';
      break;
  }

  return $o;
}

function stormtimetracking_perm() {
  return array(
    'Storm timetracking: access',
    'Storm timetracking: add',
    'Storm timetracking: delete all',
    'Storm timetracking: delete own',
    'Storm timetracking: delete of user organization',
    'Storm timetracking: edit all',
    'Storm timetracking: edit own',
    'Storm timetracking: edit of user organization',
    'Storm timetracking: view all',
    'Storm timetracking: view own',
    'Storm timetracking: view of user organization',
    'Storm timetracking: view if assigned to project',
  );
}

function stormtimetracking_access($op, $node, $account = NULL) {
  if (empty($account)) {
    global $user;
    $account = $user;
  }

  if ($op == 'create') {
    return user_access('Storm timetracking: add');
  }

  if (is_numeric($node)) {
    $node = node_load($node);
  }

  if (!isset($account->stormorganization_nid) && module_exists('stormperson')) {
    _stormperson_user_load($account);
  }

  if ($op == 'delete') {
    if (user_access('Storm timetracking: delete all')) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: delete own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: delete of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
  }

  if ($op == 'update') {
    if (user_access('Storm timetracking: edit all')) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: edit own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: edit of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
  }

  if ($op == 'view') {
    if (user_access('Storm timetracking: view all')) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: view own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: view of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm timetracking: view if assigned to project') && module_exists('stormteam')) {
      $project = node_load($node->project_nid);
      if (stormteam_user_belongs_to_team($project->assigned_nid, $account->stormperson_nid)) {
        return TRUE;
      }
      elseif (stormteam_user_belongs_to_team($project->assigned_nid, $account->stormorganization_nid)) {
        return TRUE;
      }
    }
  }

  return FALSE;
}

function stormtimetracking_access_sql($sql, $where = array()) {
  if (!user_access('Storm timetracking: view all')) {
    global $user;

    $cond = '';
    $join = array();
    if (user_access('Storm timetracking: view own')) {
      $cond .= 'n.uid = ' . $user->uid;
    }
    if (user_access('Storm timetracking: view of user organization')) {
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'stt.organization_nid = ' . $user->stormorganization_nid;
    }
    if (user_access('Storm timetracking: view if assigned to project')) {
      $join[] = 'LEFT JOIN {node} npr ON npr.nid = stt.project_nid';
      $join[] = 'LEFT JOIN {stormproject} spr ON spr.vid = npr.vid';

      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'spr.assigned_nid = ' . $user->stormperson_nid;

      if (module_exists('stormteam')) {
        // Load teams that the account belongs to:
        $belonged_teams = stormteam_user_return_teams();
        // Allow access if any of those teams is the one in question:
        foreach ($belonged_teams as $belonged_team) {
          $cond .= ' OR spr.assigned_nid = ' . $belonged_team;
        }
      }
    }
    $where[] = empty($cond) ? '0 = 1' : $cond;
  }

  $where[] = "'storm_access' = 'storm_access'";
  return storm_rewrite_sql($sql, $where, $join);
}

function stormtimetracking_storm_rewrite_where_sql($query, $primary_table, $account) {
  static $conds = array();

  if (isset($conds[$primary_table][$account->uid])) {
    return $conds[$primary_table][$account->uid];
  }

  $cond = '';
  if (!preg_match("/'storm_access' = 'storm_access'/", $query)) {
    if (user_access('Storm timetracking: view all', $account)) {
      return '';
    }
    $from = '{stormtimetracking} stt1';
    if (user_access('Storm timetracking: view own', $account)) {
      $cond .= "${primary_table}.uid = " . $account->uid;
    }
    if (user_access('Storm timetracking: view of user organization', $account)) {
      $cond .= !empty($cond) ? ' OR ' : '';
      # If function is called without viewing an organization, this variable
      # may not be set. These lines check for that and set the organization
      # node id to zero if not otherwise set.
      if (!isset($account->stormorganization_nid)) {
        $account->stormorganization_nid = 0;
      }
      $cond .= ' stt1.organization_nid = ' . $account->stormorganization_nid;
    }
    if (user_access('Storm timetracking: view if assigned to project')) {
      $from .= ' LEFT JOIN {node} npr1 ON npr1.nid = stt1.project_nid LEFT JOIN {stormproject} spr1 ON spr1.vid = npr1.vid';
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'spr1.assigned_nid = ' . $account->stormperson_nid;

      if (module_exists('stormteam')) {
        // Load teams that the account belongs to:
        $belonged_teams = stormteam_user_return_teams($account);
        // Allow access if any of those teams is the one in question:
        foreach ($belonged_teams as $belonged_team) {
          $cond .= ' OR spr1.assigned_nid = ' . $belonged_team;
        }
      }
    }
    if ($cond) {
      $cond = " WHEN 'stormtimetracking' THEN (SELECT IF($cond, 1, 0) FROM ${from} WHERE stt1.vid = ${primary_table}.vid) ";
    }
    else {
      $cond = " WHEN 'stormtimetracking' THEN 0 ";
    }
  }
  $conds[$primary_table][$account->uid] = $cond;
  return $cond;
}

function stormtimetracking_menu() {
  $items = array();

  $items['timetrackings'] = array(
    'title' => 'Timetrackings',
    'description' => 'SuiteDesk timetrackings',
    'page callback' => 'stormtimetracking_list',
    'access arguments' => array('Storm timetracking: access'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'stormtimetracking.admin.inc',
    'weight' => 6,
  );

  $items['timetrackings/report/%/%'] = array(
    'title' => 'Timetrackings',
    'description' => 'SuiteDesk timetrackings',
    'page arguments' => array(3, 4),
    'page callback' => 'stormtimetracking_list_report',
    'access arguments' => array('Storm timetracking: access'),
    'type' => MENU_CALLBACK,
    'file' => 'stormtimetracking.admin.inc',
  );

  $items['admin/settings/suitedesk/timetracking'] = array(
    'title' => 'SuiteDesk timetracking',
    'description' => 'SuiteDesk timetracking administration page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('stormtimetracking_admin_settings'),
    'access arguments' => array('Storm: access administration pages'),
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

function stormtimetracking_theme() {
  return array(
    'stormtimetracking_list' => array(
      'file'      => 'stormtimetracking.theme.inc',
      'arguments' => array('header', 'tasks', 'duration'),
    ),
    'stormtimetracking_view' => array(
      'file'      => 'stormtimetracking.theme.inc',
      'arguments' => array('node', 'teaser', 'page'),
    ),
    'stormtimetracking_list_form_report_reports' => array(
      'file'      => 'stormtimetracking.theme.inc',
    ),
    'stormtimetracking_list_report' => array(
      'file'      => 'stormtimetracking.theme.inc',
      'arguments' => array('report', 'language', 'timetrackings'),
    ),
  );
}

function stormtimetracking_node_info() {
  return array(
    'stormtimetracking' => array(
      'name' => t('Timetracking'),
      'module' => 'stormtimetracking',
      'description' => t("A timetracking for Storm."),
      'title_label' => t("Title"),
      'body_label' => t("Description"),
    )
  );
}

function stormtimetracking_content_extra_fields($type_name) {
  if ($type_name == 'stormtimetracking') {
    return array(
      'group1' => array('label' => 'Organization/Project/Task/Ticket Group', 'weight' => -20),
      'group2' => array('label' => 'Date', 'weight' => -19),
      'group3' => array('label' => 'Time / Duration Group', 'weight' => -18),
      'group4' => array('label' => 'Billable / Billed Group', 'weight' => -17),
    );
  }
}

function stormtimetracking_stormorganization_change($organization_nid, $organization_title) {
  $s = "UPDATE {stormtimetracking} SET organization_title='%s' WHERE organization_nid=%d AND organization_title <> '%s'";
  db_query($s, $organization_title, $organization_nid, $organization_title);
}

function stormtimetracking_stormproject_change($project_nid, $project_title) {
  $s = "UPDATE {stormtimetracking} SET project_title='%s' WHERE project_nid=%d AND project_title <> '%s'";
  db_query($s, $project_title, $project_nid, $project_title);
}

function stormtimetracking_stormtask_change($task_nid, $task_title, $task_stepno) {
  $s = "UPDATE {stormtimetracking} SET task_title='%s', task_stepno='%s' WHERE task_nid=%d AND
      (task_title<>'%s' OR task_stepno<>'%s')";
  db_query($s, $task_title, $task_stepno, $task_nid, $task_title, $task_stepno);
}

function stormtimetracking_stormticket_change($ticket_nid, $ticket_title) {
  $s = "UPDATE {stormtimetracking} SET ticket_title='%s' WHERE ticket_nid=%d AND ticket_title <> '%s'";
  db_query($s, $ticket_title, $ticket_nid, $ticket_title);
}

function stormtimetracking_stormproject_change_hierarchy($project_nid, $organization_nid, $organization_title) {
  $s = "UPDATE {stormtimetracking} SET organization_nid=%d, organization_title='%s' WHERE project_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid);
}

function stormtimetracking_stormtask_change_hierarchy($task_nid, $organization_nid, $organization_title, $project_nid, $project_title) {
  $s = "UPDATE {stormtimetracking} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s' WHERE task_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid);
}

function stormtimetracking_stormticket_change_hierarchy($ticket_nid, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title) {
  $s = "UPDATE {stormtimetracking} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s', task_nid=%d, task_title='%s' WHERE ticket_nid=%d";
  db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title, $ticket_nid);
}

function stormtimetracking_form(&$node) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Storm'), 'storm');
  $breadcrumb[] = l(t('Timetrackings'), 'timetrackings');
  drupal_set_breadcrumb($breadcrumb);

  if (arg(1)=='add') {
    if (array_key_exists('organization_nid', $_GET) && !$node->organization_nid) {
      $node->organization_nid = $_GET['organization_nid'];
    }
    if (array_key_exists('project_nid', $_GET) && !$node->project_nid) {
      $node->project_nid = $_GET['project_nid'];
      $p = node_load($node->project_nid);
      $node->organization_nid = $p->organization_nid;
      if (!stormorganization_access('view', $node->organization_nid)) {
        drupal_set_message(t("You cannot add a timetracking for this project, as you do not have access to view the organization's profile"));
        drupal_goto('node/'. $node->project_nid);
      }
    }
    if (array_key_exists('task_nid', $_GET) && !$node->task_nid) {
      $node->task_nid = $_GET['task_nid'];
      $t = node_load($node->task_nid);
      $node->organization_nid = $t->organization_nid;
      $node->project_nid = $t->project_nid;
      // $project_access deals with the case whereby the project could be blank, hence access rules not required
      $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
      if (!stormorganization_access('view', $node->organization_nid) || !$project_access) {
        drupal_set_message(t("You cannot add a timetracking for this task, as you do not have access to view the both the organization and project's profile"));
        drupal_goto('node/'. $node->task_nid);
      }
    }
    if (array_key_exists('ticket_nid', $_GET) && !$node->ticket_nid) {
      $node->ticket_nid = $_GET['ticket_nid'];
      $t = node_load($node->ticket_nid);
      $node->organization_nid = $t->organization_nid;
      $node->project_nid = $t->project_nid;
      $node->task_nid = $t->task_nid;
      // $project_access deals with the case whereby the project could be blank, hence access rules not required
      $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
      $task_access = $node->task_nid ? stormtask_access('view', $node->task_nid) : TRUE;
      if (!stormorganization_access('view', $node->organization_nid) || !project_access || !task_access) {
        drupal_set_message(t("You cannot add a timetracking for this ticket, as you do not have access to view all of the organization, project and task's profile"));
        drupal_goto('node/'. $node->ticket_nid);
      }
    }

    if (!empty($_SESSION['stormtimetracking_list_filter']['organization_nid']) && !$node->organization_nid) {
      $node->organization_nid = $_SESSION['stormtimetracking_list_filter']['organization_nid'];
    }
    if (!empty($_SESSION['stormtimetracking_list_filter']['project_nid']) && !$node->project_nid) {
      $node->project_nid = $_SESSION['stormtimetracking_list_filter']['project_nid'];
    }
    if (!empty($_SESSION['stormtimetracking_list_filter']['task_nid']) && !$node->task_nid) {
      $node->task_nid = $_SESSION['stormtimetracking_list_filter']['task_nid'];
    }
    if (!empty($_SESSION['stormtimetracking_list_filter']['ticket_nid']) && !$node->ticket_nid) {
      $node->ticket_nid = $_SESSION['stormtimetracking_list_filter']['ticket_nid'];
    }
    if (array_key_exists('organization_nid', $_GET)) $node->organization_nid = $_GET['organization_nid'];
    if (array_key_exists('project_nid', $_GET)) $node->project_nid = $_GET['project_nid'];
    if (array_key_exists('task_nid', $_GET)) $node->task_nid = $_GET['task_nid'];
    if (array_key_exists('ticket_nid', $_GET)) $node->ticket_nid = $_GET['ticket_nid'];

    $node->trackingdate = time();

    $today = _storm_gmtimestamp_without_time(time());
    $s = 'SELECT MAX(stt.timeend) FROM {node} n LEFT JOIN {stormtimetracking} stt ON n.nid=stt.nid
    WHERE stt.trackingdate>=%d AND stt.trackingdate<=%d AND n.uid=%d';
    $timebegin = db_result(db_query($s, $today, $today, $node->uid));
    $node->timebegin = $timebegin;
    $node->timeend = format_date(time(), 'custom', 'H:i');

    $timebegin = _storm_strtotime($node->timebegin);
    $timeend = _storm_strtotime($node->timeend);
    if ($timeend['hour'] && $timebegin['hour']) {
      $node->duration = ($timeend['hour'] - $timebegin['hour'] + ($timeend['minute'] - $timebegin['minute']) / 60);
    }
    $node->billable = variable_get('stormtimetracking_billable_default', FALSE);
  }
  $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
    ON so.nid=n.nid WHERE n.status=1 AND (so.isactive=1 OR n.nid=%d) AND n.type='stormorganization' ORDER BY n.title";

  $type = node_get_types('type', $node);

  $form['#attributes']['class'] = 'stormcomponent_node_form';

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -21,
);

  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group1') : -20,
  );

  $s_org = stormorganization_access_sql($s_org);
  $s_org = db_rewrite_sql($s_org);
  $r = db_query($s_org, isset($node->organization_nid) ? $node->organization_nid : -1);
  $organizations = array();
  while ($organization = db_fetch_object($r)) {
    $organizations[$organization->nid] = $organization->title;
    if (!isset($node->organization_nid)) $node->organization_nid = $organization->nid;
  }
  $form['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => $node->organization_nid,
    '#options' => $organizations,
    '#required' => TRUE,
    '#attributes' => array('onchange' => "stormticket_organization_project_task_tickets(this, 'edit-project-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
    '#weight' => 1,
  );

  $s = "SELECT n.nid, n.title FROM {stormproject} spr INNER JOIN {node} n ON spr.nid=n.nid WHERE spr.organization_nid=%d AND n.status=1 AND n.type='stormproject' ORDER BY n.title";
  $s = stormproject_access_sql($s);
  $s = db_rewrite_sql($s);
  $r = db_query($s, $node->organization_nid);
  $projects = array();
  while ($project = db_fetch_object($r)) {
    $projects[$project->nid] = $project->title;
  }
  $projects = array(0 => '-') + $projects;
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => isset($node->project_nid) ? $node->project_nid : 0,
    '#options' => $projects,
    '#process' => array('storm_dependent_select_process'),
    '#attributes' => array('onchange' => "stormticket_project_task_tickets(this, 'edit-organization-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
    '#weight' => 2,
  );

  $tree = _stormtask_get_tree(isset($node->project_nid) ? $node->project_nid : 0);
  $tasks = _stormtask_plain_tree($tree);
  $tasks = array(0 => '-') + $tasks;
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : 0,
    '#options' => $tasks,
    '#process' => array('storm_dependent_select_process'),
    '#attributes' => array('onchange' => "stormticket_task_tickets(this, 'edit-organization-nid', 'edit-project-nid', 'edit-ticket-nid', true, '-')"),
    '#weight' => 3,
  );

  $tickets = array();
  $s = "SELECT n.nid, n.title FROM {stormticket} sti INNER JOIN {node} n ON sti.nid=n.nid WHERE n.status=1 AND n.type='stormticket' AND sti.organization_nid=%d AND sti.project_nid=%d AND sti.task_nid=%d ORDER BY n.title";
  $s = stormticket_access_sql($s);
  $s = db_rewrite_sql($s);
  $r = db_query($s, $node->organization_nid, isset($node->project_nid) ? $node->project_nid : -1, isset($node->task_nid) ? $node->task_nid : -1);
  while ($ticket = db_fetch_object($r)) {
    $tickets[$ticket->nid] = $ticket->title;
  }
  $form['group1']['ticket_nid'] = array(
    '#type' => 'select',
    '#title' => t('Ticket'),
    '#default_value' => isset($node->ticket_nid) ? $node->ticket_nid : 0,
    '#options' => array(0 => '-') + $tickets,
    '#process' => array('storm_dependent_select_process'),
    '#weight' => 4,
  );

  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group2') : -19,
  );

  $form['group2']['trackingdate'] = array(
    '#type' => 'dateext',
    '#title' => t('Date'),
    '#default_value' => $node->trackingdate,
  );

  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group3') : -18,
  );

  $form['group3']['timebegin'] = array(
    '#type' => 'textfield',
    '#title' => t('Time begin'),
    '#size' => 5,
    '#maxlength' => 5,
    '#default_value' => $node->timebegin,
    '#weight' => 1,
  );

  $form['group3']['timeend'] = array(
    '#type' => 'textfield',
    '#title' => t('Time end'),
    '#size' => 5,
    '#maxlength' => 5,
    '#default_value' => $node->timeend,
    '#weight' => 2,
  );

  $form['group3']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Total duration'),
    '#default_value' => stormtimetracking_duration($node->duration),
    '#size' => 10,
    '#maxlength' => 5,
    '#weight' => 3,
    '#disabled' => TRUE,
  );

  $form['group3']['billing_duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Billing duration'),
    '#default_value' => sprintf("%01.2f", isset($node->billing_duration) ? $node->billing_duration : 0),
    '#size' => 10,
    '#maxlength' => 5,
    '#weight' => 4,
  );

  $form['group3']['billing_duration_time'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration'),
    '#default_value' => stormtimetracking_duration($node->billing_duration),
    '#size' => 10,
    '#maxlength' => 5,
    '#weight' => 5,
    '#disabled' => TRUE,
  );

  $form['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group3') : -17,
  );

  $form['group4']['billable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Billable'),
    '#default_value' => isset($node->billable) ? $node->billable : TRUE,
    '#weight' => 1,
  );

  $form['group4']['billed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Billed'),
    '#default_value' => isset($node->billed) ? $node->billed : FALSE,
    '#weight' => 2,
  );

  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }

  return $form;
}

function stormtimetracking_insert($node) {
  _stormtimetracking_beforesave($node);

  db_query("INSERT INTO {stormtimetracking}
    (vid, nid,
    organization_nid, organization_title, project_nid, project_title,
    task_nid, task_title, task_stepno, ticket_nid, ticket_title,
    trackingdate, timebegin, timeend, duration, billing_duration, billable, billed) VALUES
    (%d, %d,
    %d, '%s', %d, '%s',
    %d, '%s', '%s', %d, '%s',
    %d, '%s', '%s', %f, %f, %d, %d)",
    $node->vid, $node->nid,
    $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title,
    $node->task_nid, $node->task_title, $node->task_stepno, $node->ticket_nid, $node->ticket_title,
    $node->trackingdate, $node->timebegin, $node->timeend, $node->duration, $node->billing_duration, $node->billable, $node->billed);
}

function stormtimetracking_update($node) {
  _stormtimetracking_beforesave($node);

  // If this is a new revision, then it is actually an insert operation
  if ($node->revision) {
    stormtimetracking_insert($node);
  }
  else {
    db_query("UPDATE {stormtimetracking} SET
      organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s',
      task_nid=%d, task_title='%s', task_stepno='%s', ticket_nid=%d, ticket_title='%s',
      trackingdate=%d, timebegin='%s', timeend='%s', duration=%f, billing_duration=%f,
      billable=%d, billed=%d WHERE vid = %d",
      $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title,
      $node->task_nid, $node->task_title, $node->task_stepno, $node->ticket_nid, $node->ticket_title,
      $node->trackingdate, $node->timebegin, $node->timeend, $node->duration, $node->billing_duration,
      $node->billable, $node->billed, $node->vid);
  }
}

function _stormtimetracking_beforesave(&$node) {
  $timebegin = _storm_strtotime($node->timebegin);
  $timeend = _storm_strtotime($node->timeend);

  if (is_array($node->trackingdate)) {
    $node->trackingdate = _storm_datetime_to_gmtimestamp($node->trackingdate + $timebegin);
  }

  $node->timebegin = _timetostr($timebegin);
  $node->timeend = _timetostr($timeend);

  $node->duration = ($timeend['hour'] - $timebegin['hour'] + ($timeend['minute'] - $timebegin['minute']) / 60);
  if ($node->timeend < $node->timebegin) {
    $node->duration += 24;
  }

  if (variable_get('stormtimetracking_auto_duration', TRUE)) {
    $node->billing_duration = $node->duration;
  }

  $s = "SELECT n.title FROM {node} n INNER JOIN {stormorganization} o ON n.nid=o.nid WHERE type='stormorganization' AND n.nid=%d";
  $r = db_query($s, $node->organization_nid);
  $o = db_fetch_object($r);
  $node->organization_title = $o->title;

  $s = "SELECT n.title, p.organization_title FROM {node} n INNER JOIN {stormproject} p ON n.nid=p.nid WHERE type='stormproject' AND n.nid=%d";
  $r = db_query($s, $node->project_nid);
  $p = db_fetch_object($r);
  $node->project_title = isset($p->title) ? $p->title : '';

  $s = "SELECT title, stepno FROM {node} n INNER JOIN {stormtask} t ON n.nid=t.nid WHERE n.type='stormtask' AND n.nid=%d";
  $r = db_query($s, $node->task_nid);
  $ta = db_fetch_object($r);
  $node->task_title = isset($ta->title) ? $ta->title : '';
  $node->task_stepno = isset($ta->stepno) ? $ta->stepno : '';

  $s = "SELECT title FROM {node} n INNER JOIN {stormticket} t ON n.nid=t.nid WHERE n.type='stormticket' AND n.nid=%d";
  $r = db_query($s, $node->ticket_nid);
  $ti = db_fetch_object($r);
  $node->ticket_title = isset($ti->title) ? $ti->title : '';
}

function stormtimetracking_nodeapi(&$node, $op, $teaser, $page) {
  if ($node->type != 'stormtimetracking') {
    return;
  }
  switch ($op) {
    case 'delete revision':
      // Notice that we're matching a single revision based on the node's vid.
      db_query('DELETE FROM {stormtimetracking} WHERE vid = %d', $node->vid);
      break;
  }
}

function stormtimetracking_delete($node) {
  db_query('DELETE FROM {stormtimetracking} WHERE nid = %d', $node->nid);
}

function stormtimetracking_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {stormtimetracking} WHERE vid = %d', $node->vid));
  return $additions;
}

function stormtimetracking_view($node, $teaser = FALSE, $page = FALSE) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Storm'), 'storm');
  $breadcrumb[] = l(t('Timetrackings'), 'timetrackings');
  drupal_set_breadcrumb($breadcrumb);

  return theme('stormtimetracking_view', $node, $teaser, $page);
}

// CONVERT DECIMAL DURATION TO TIME DURATION
function stormtimetracking_duration($duration) {
  if (empty($duration)) {
    return '';
  }
  $h = intval($duration);
  $m = round(($duration - $h) * 60);
  if ($h == 0 && $m == 0) {
    return '0h';
  }
  return ($h > 0 ? $h . 'h' : '') . ($m > 0 ? ($h > 0 ? ' ' : '') . $m . 'm' : '');
}

// ADMIN SETTINGS
function stormtimetracking_admin_settings() {
  $form = array();

  $form['stormtimetracking_auto_duration'] = array(
    '#type' => 'checkbox',
    '#title' => t('Always set billing duration to duration value.'),
    '#default_value' => variable_get('stormtimetracking_auto_duration', TRUE),
    '#description' => t('When checked, Storm will set the billing duration equal to the duration each time the node is saved. The duration is always calculated from the start and end times.'),
    '#size' => 5,
  );

  $form['stormtimetracking_billable_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Default Value for billable field.'),
    '#default_value' => variable_get('stormtimetracking_billable_default', FALSE),
    '#description' => t('When checked, Storm will set the timetracking to billable by default'),
    '#size' => 5,
  );

  return system_settings_form($form);
}

// INVOICE AUTO ADD HANDLER
function stormtimetracking_storminvoice_auto_add($node, $invoice_nid = NULL) {
  if (!module_exists('storminvoice')) {
    drupal_set_message(t('This function should only be called from within Storm Invoice'));
    return;
  }
  elseif ($node->billed) {
    drupal_set_message(t('This timetracking has already been billed!'));
    return;
  }
  elseif (!$node->billable) {
    drupal_set_message(t('This timetracking is marked unbillable!'));
    return;
  }
  else {
    global $user;

    if (!$invoice_nid) {

      $new_invoice = new StdClass;

      // Code copied with edits from node form
      $new_invoice->requestdate = time();
      $new_invoice->duedate = $new_invoice->requestdate + (variable_get('storminvoice_payment_days', 30) * 86400);
      $new_invoice->number = storminvoice_get_invoice_number($new_invoice->requestdate);

      $new_invoice->title = $node->title;
      $new_invoice->uid = $user->uid;
      $new_invoice->type = 'storminvoice';
      // $new_invoice->reference
      $new_invoice->organization_nid = $node->organization_nid;
      $new_invoice->organization_title = $node->organization_title;
      $new_invoice->project_nid = $node->project_nid;
      $new_invoice->project_title = $node->project_title;
      // $new_invoice->amount
      // $new_invoice->tax
      // $new_invoice->total
      // $new_invoice->totalcustomercurr
      // $new_invoice->taxexempt
      $new_invoice->src_nid = $node->nid;
      $new_invoice->src_vid = $node->vid;

      node_save($new_invoice);
      $invoice_nid = $new_invoice->nid;
    }
    else {
      $new_invoice = node_load($invoice_nid);
    }

    $rate_array = array('pricemode_used' => '', 'rate_to_use' => 0);

    $rate_array = storminvoice_get_rate($node);

    $count = count($new_invoice->items);

    $new_invoice->items[$count]->description = storminvoice_get_item_desc($rate_array, $node);

    $new_invoice->items[$count]->amount = storminvoice_get_item_amount($rate_array, $node);

    // Tax percent simply uses default at the moment
    $new_invoice->items[$count]->tax1percent = variable_get('storm_tax1_percent', 20);
    // $new_invoice_item->items[$count]->tax1
    // $new_invoice_item->items[$count]->total
    $new_invoice->items[$count]->src_nid = $node->nid;
    $new_invoice->items[$count]->src_vid = $node->vid;

    storm_taxation($new_invoice->items[$count]);
    storminvoice_update($new_invoice);
  }

  // Mark timetracking as billed.
  db_query("UPDATE {stormtimetracking} SET billed=%d WHERE vid=%d", 1, $node->vid);

  return $invoice_nid;
}

// VIEWS HANDLER
function stormtimetracking_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'stormtimetracking'),
  );
}

function stormtimetracking_storm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'storm_dashboard_link',
      'title' => t('Timetrackings'),
      'icon' => 'stormtimetracking-item',
      'path' => 'timetrackings',
      'params' => array(),
      'access_arguments' => 'Storm timetracking: access',
      'node_type' => 'stormtimetracking',
      'add_type' => 'stormtimetracking',
      'map' => array(),
      'weight' => 8,
    );
  }
  return $links;
}
