<?php

/**
 * @file
 */

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

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

  return $o;
}

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

function stormtask_init() {
  $settings = array(
    'storm' => array(
      'project_tasks_url' => url('storm/project_tasks_js/'),
      'project_assignments_url' => url('storm/project_assignments_js/'),
    ),
  );
  drupal_add_js($settings, 'setting');
  drupal_add_js(drupal_get_path('module', 'stormtask') .'/stormtask.js', 'module', 'header', FALSE);
}

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

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

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

  if ($op == 'create') {
    if (user_access('Storm task: add')) {
      return TRUE;
    }
    elseif (user_access('Storm task: add if project is editable') && (empty($node->project_nid) || stormproject_access('update', $node->project_nid))) {
      return TRUE;
    }
  }

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

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

  if ($op == 'view') {
    if (user_access('Storm task: view all')) {
      return TRUE;
    }
    elseif (user_access('Storm task: view own') && ($account->uid == $node->uid)) {
      return TRUE;
    }
    elseif (user_access('Storm task: view of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm task: view if assigned to task') && ($account->stormperson_nid == $node->assigned_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm task: view if assigned to task') && module_exists('stormteam') && stormteam_user_belongs_to_team($node->assigned_nid, $account->stormperson_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm task: view if assigned to task') && module_exists('stormteam') && stormteam_user_belongs_to_team($node->assigned_nid, $account->stormorganization_nid)) {
      return TRUE;
    }
    elseif (user_access('Storm task: 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 stormtask_access_sql($sql, $where = array()) {
  if (!user_access('Storm task: view all')) {
    global $user;

    $cond = '';
    $join = array();
    if (user_access('Storm task: view own')) {
      $cond .= 'n.uid = ' . $user->uid;
    }
    if (user_access('Storm task: view of user organization')) {
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'sta.organization_nid = ' . $user->stormorganization_nid;
    }
    if (user_access('Storm task: view if assigned to task')) {
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'sta.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 sta.assigned_nid = ' . $belonged_team;
        }
      }
    }
    if (user_access('Storm task: view if assigned to project')) {
      $join[] = 'LEFT JOIN {node} npr ON npr.nid = sta.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 stormtask_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 task: view all', $account)) {
      return '';
    }
    $from = '{stormtask} sta1';
    if (user_access('Storm task: view own', $account)) {
      $cond .= " ${primary_table}.uid = " . $account->uid;
    }
    if (user_access('Storm task: 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 .= ' sta1.organization_nid = ' . $account->stormorganization_nid;
    }
    if (user_access('Storm task: view if assigned to task')) {
      $cond .= !empty($cond) ? ' OR ' : '';
      $cond .= 'sta1.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 sta1.assigned_nid = ' . $belonged_team;
        }
      }
    }
    if (user_access('Storm task: view if assigned to project')) {
      $from .= ' LEFT JOIN {node} npr1 ON npr1.nid = sta1.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 'stormtask' THEN (SELECT IF($cond, 1, 0) FROM ${from} WHERE sta1.vid = ${primary_table}.vid) ";
    }
    else {
      $cond = " WHEN 'stormtask' THEN 0 ";
    }
  }
  $conds[$primary_table][$account->uid] = $cond;
  return $cond;
}

function stormtask_menu() {
  $items['node/%node/tasks'] = array(
    'title' => 'Tasks',
    'page callback' => 'stormtask_tasks',
    'page arguments' => array(1),
    'access callback' => '_stormtask_tasks_access',
    'access arguments' => array(1),
    'file' => 'stormtask.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 5
  );
  $items['storm/project_tasks_js/%'] = array(
    'title' => 'Tasks',
    'page callback' => '_stormtask_project_tasks_js',
    'page arguments' => array(2),
    'access callback' => TRUE,
    'file' => 'stormtask.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['storm/project_assignments_js/%'] = array(
    'title' => 'Assignments',
    'page callback' => '_stormtask_project_assignments_js',
    'page arguments' => array(2, 3),
    'access callback' => TRUE,
    'file' => 'stormtask.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['tasks'] = array(
    'title' => 'Tasks',
    'description' => 'SuiteDesk tasks',
    'page callback' => 'stormtask_list',
    'access arguments' => array('Storm task: access'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'stormtask.admin.inc',
    'weight' => 5,
  );

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

  return $items;
}

function stormtask_admin_settings() {
  $form = array();

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

  $form['stormtask_enable_ganttchart'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Gantt Charts (experimental feature)'),
    '#default_value' => variable_get('stormtask_enable_ganttchart', False),
  );
/*
  $form['stormtask_jsgantt_options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Chart display options'),
    '#default_value' => variable_get('stormtask_jsgantt_options', array('ShowStartDate', 'ShowEndDate', 'ShowDur')),
    '#options' => array(
      'ShowStartDate' => t('Show start date'),
      'ShowEndDate' => t('Show end date'),
      'ShowDur' => t('Show duration'),
    ),
    '#description' => t('Choose display options for gantt chart.'),
  );
*/
  $form['stormtask_jsgantt_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Gantt chart default color'),
    '#default_value' => variable_get('stormtask_jsgantt_color', '666666'),
    '#description' => t('Enter default color for gantt chart bars.'),
    '#size' => 6,
  );
  $form['stormtask_jsgantt_color_completed'] = array(
    '#type' => 'textfield',
    '#title' => t('Gantt chart "completed" color'),
    '#default_value' => variable_get('stormtask_jsgantt_color_completed', '0dff24'),
    '#description' => t('Enter "completed" color for gantt chart bars.'),
    '#size' => 6,
  );
  $form['stormtask_jsgantt_color_in_progress'] = array(
    '#type' => 'textfield',
    '#title' => t('Gantt chart "in progress" color'),
    '#default_value' => variable_get('stormtask_jsgantt_color_in_progress', '00ccff'),
    '#description' => t('Enter "in progress" color for gantt chart bars.'),
    '#size' => 6,
  );
  $form['stormtask_jsgantt_color_on_hold'] = array(
    '#type' => 'textfield',
    '#title' => t('Gantt chart "on hold" color'),
    '#default_value' => variable_get('stormtask_jsgantt_color_on_hold', 'fff60d'),
    '#description' => t('Enter "on hold" color for gantt chart bars.'),
    '#size' => 6,
  );
  $form['stormtask_jsgantt_color_overdue'] = array(
    '#type' => 'textfield',
    '#title' => t('Gantt chart "overdue" color'),
    '#default_value' => variable_get('stormtask_jsgantt_color_overdue', 'ff0d0d'),
    '#description' => t('Enter "overdue" color for gantt chart bars.'),
    '#size' => 6,
  );

  return system_settings_form($form);
}

function jsgantt_check_directory($form_element) {
  $file = $form_element['#value'] ."/jsgantt.js";
  if (!file_exists($file)) {
    form_set_error($form_element['#name'], t('The file %file does not exist.', array('%file' => $file)));
  }
  return $form_element;
}

function stormtask_theme() {
  return array(
    'stormtask_list' => array(
      'file'      => 'stormtask.theme.inc',
      'arguments' => array('header', 'tasks'),
    ),
    'stormtask_tasks' => array(
      'file'      => 'stormtask.theme.inc',
      'arguments' => array('form'),
    ),
    'stormtask_view' => array(
      'file'      => 'stormtask.theme.inc',
      'arguments' => array('node', 'teaser', 'page'),
    ),
  );
}

function stormtask_node_info() {
  return array(
    'stormtask' => array(
      'name' => t('Task'),
      'module' => 'stormtask',
      'description' => t("A task for SuiteDesk."),
      'title_label' => t("Title"),
      'body_label' => t("Description"),
    )
  );
}

function stormtask_content_extra_fields($type_name) {
  if ($type_name == 'stormtask') {
    return array(
      'group1' => array('label' => 'Organization/Project/Task/Weight/Step Group', 'weight' => -20),
      'group2' => array('label' => 'Category/Status/Priority Group', 'weight' => -19),
      'group3' => array('label' => 'Date/Duration Group', 'weight' => -18),
      'group4' => array('label' => 'Price Group', 'weight' => -17),
      'group5' => array('label' => 'Assigned to', 'weight' => -16),
      'group6' => array('label' => 'Billable / Billed Group', 'weight' => -15),
    );
  }
}

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

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

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

function stormtask_form(&$node) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Home'), '<front>');

  if (array_key_exists('project_nid', $_GET)) {
    $project = node_load($_GET['project_nid']);
    $breadcrumb[] = l(t('Projects'), 'projects') . ' (' . l($project->title, 'node/'. $project->nid) . ')';
    $breadcrumb[] = l(t('Project tasks'), 'node/'. $project->nid .'/tasks');
  }
  else {
    $breadcrumb[] = l(t('Tasks'), 'tasks');
  }

  drupal_set_breadcrumb($breadcrumb);

  if (arg(1)=='add') {
    $node->datebegin = NULL; // time();
    $node->dateend   = NULL; // time();

    if (array_key_exists('organization_nid', $_GET) && empty($node->organization_nid)) {
      $node->organization_nid = $_GET['organization_nid'];
    }
    if (array_key_exists('project_nid', $_GET) && empty($node->project_nid)) {
      $node->project_nid = $_GET['project_nid'];
      $p = node_load($node->project_nid);
      // Ensure that the correct organization is loaded
      $node->organization_nid = $p->organization_nid;
      if (!stormorganization_access('view', $node->organization_nid)) {
        drupal_set_message(t("You cannot add a task 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) && empty($node->parent_nid)) {
      // Parent task can be autoloaded by use of ?task_nid=string
      $node->parent_nid = $_GET['task_nid'];
      $t = node_load($node->parent_nid);
      // Ensure that the correct project is loaded
      $node->project_nid = $t->project_nid;
      $node->organization_nid = $t->organization_nid;
      // If no parent project then no need to check access rights, there will always be a parent org.
      $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
      if (!$project_access || !stormorganization_access('view', $node->organization_nid)) {
        drupal_set_message(t("You cannot add a task with this parent, as you do not have access to view both the organization and project profiles."));
        drupal_goto('node/'. $node->parent_nid);
      }
    }

    if (!empty($_SESSION['stormtask_list_filter']['organization_nid']) && !$node->organization_nid) {
      $node->organization_nid = $_SESSION['stormtask_list_filter']['organization_nid'];
    }
    if (!empty($_SESSION['stormtask_list_filter']['project_nid']) && !$node->project_nid) {
      $node->project_nid = $_SESSION['stormtask_list_filter']['project_nid'];
    }

    $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 AND n.type='stormorganization' ORDER BY n.title";

    $node->billable = variable_get('stormtask_billable_default', FALSE);
  }
  else {
    $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
      ON so.nid=n.nid WHERE n.status=1 AND n.type='stormorganization' ORDER BY n.title";
  }

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

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

  $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);
  $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' => "stormtask_organization_project_tasks(this, 'edit-project-nid', 'edit-parent-nid', 'edit-assigned-nid', true, '-')"),
  );

  $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormproject} AS spr ON spr.vid=n.vid
  WHERE n.status=1 AND spr.organization_nid=%d 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)) {
   if (arg(1) != 'add' || user_access('Storm task: add') || (user_access('Storm task: add if project is editable') && stormproject_access('update', $project->nid)) || $project->nid == $node->project_nid) {
      $projects[$project->nid] = $project->title;
      if (!isset($node->project_nid)) $node->project_nid = $project->nid;
    }
  }
  if (!isset($node->project_nid)) $node->project_nid = NULL;
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => $node->project_nid,
    '#options' => $projects,
    '#process' => array('storm_dependent_select_process'),
    '#required' => TRUE,
    '#attributes' => array('onchange' => "stormtask_project_tasks('edit-organization-nid', this, 'edit-parent-nid', 'edit-assigned-nid', true, '-')"),
  );

  $form['group1']['project_nid_old'] = array(
    '#type' => 'hidden',
    '#default_value' => $node->project_nid,
  );

  $tree = _stormtask_get_tree($node->project_nid);
  $parent_tasks = _stormtask_plain_tree($tree);
  $form['group1']['parent_nid'] = array(
    '#type' => 'select',
    '#title' => t('Parent task'),
    '#default_value' => isset($node->parent_nid) ? $node->parent_nid : 0,
    '#options' => array(0 => '-') + $parent_tasks,
    '#process' => array('storm_dependent_select_process'),
  );

  $form['group1']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => isset($node->weight) ? $node->weight : 0,
  );

  $form['group1']['stepno'] = array(
    '#type' => 'textfield',
    '#title' => t('Step no.'),
    '#size' => 15,
    '#required' => FALSE,
    '#default_value' => isset($node->stepno) ? $node->stepno : NULL,
  );

  $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') : -18,
  );

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

  $taskcategory_list = storm_attributes_bydomain('Task category');
  $form['group2']['taskcategory'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => $node->taskcategory,
    '#options' => $taskcategory_list['values'],
  );

  $taskstatus_list = storm_attributes_bydomain('Task status');
  $form['group2']['taskstatus'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#default_value' => $node->taskstatus,
    '#options' => $taskstatus_list['values'],
  );

  $taskpriority_list = storm_attributes_bydomain('Task priority');
  $form['group2']['taskpriority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#default_value' => $node->taskpriority,
    '#options' => $taskpriority_list['values'],
  );

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

  $form['group3']['datebegin'] = array(
    '#type' => 'dateext',
    '#title' => t('Date begin'),
    '#withnull' => TRUE,
    '#default_value' => $node->datebegin,
  );

  $form['group3']['dateend'] = array(
    '#type' => 'dateext',
    '#title' => t('Date end'),
    '#withnull' => TRUE,
    '#default_value' => $node->dateend,
  );

  $durationunit_list = storm_attributes_bydomain('Duration unit');
  $form['group3']['durationunit'] = array(
    '#type' => 'select',
    '#title' => t('Duration unit'),
    '#default_value' => $node->durationunit,
    '#options' => $durationunit_list['values'],
  );

  $form['group3']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration'),
    '#size' => 20,
    '#default_value' => isset($node->duration) ? $node->duration : NULL,
  );

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

  $pricemode_list = storm_attributes_bydomain('Price mode');
  $form['group4']['pricemode'] = array(
    '#type' => 'select',
    '#title' => t('Price mode'),
    '#default_value' => $node->pricemode,
    '#options' => $pricemode_list['values'],
  );

  $form['group4']['price'] = array(
    '#title' => t('Price'),
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => isset($node->price) ? $node->price : NULL,
  );

  $currency_list = storm_attributes_bydomain('Currency');
  $form['group4']['currency'] = array(
    '#type' => 'select',
    '#title' => t('Price currency'),
    '#default_value' => $node->currency,
    '#options' => $currency_list['values'],
  );

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

  $options = storm_get_assignment_options($node->organization_nid, $node->project_nid);

  $form['group5']['assigned_nid'] = array(
    '#type' => 'select',
    '#title' => t('Assigned to'),
    '#options' => $options,
    '#default_value' => isset($node->assigned_nid) ? $node->assigned_nid : 0,
  );

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

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

  $form['group6']['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);
  }

  $form['stepno_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->stepno_old) ? $node->stepno_old : NULL,
  );

  $form['title_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->title_old) ? $node->title_old : NULL,
  );

  return $form;
}

// INSERT / UPDATE FUNCTIONS
function stormtask_insert($node) {
  _stormtask_beforesave($node);
  db_query("INSERT INTO {stormtask}
    (vid, nid, stepno, taskcategory, taskstatus, taskpriority,
    organization_nid, organization_title,
    project_nid, project_title, parent_nid, weight,
    datebegin, dateend, durationunit, duration, pricemode, price, currency,
    assigned_nid, assigned_title, billable, billed)
    VALUES
    (%d, %d, '%s', '%s', '%s', '%s',
    %d, '%s',
    %d, '%s', %d, %d, %d, %d, '%s', %f, '%s', %f, '%s', %d, '%s', %d, %d)",
    $node->vid, $node->nid, $node->stepno, $node->taskcategory, $node->taskstatus, $node->taskpriority,
    $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title,
    $node->parent_nid, $node->weight, $node->datebegin, $node->dateend, $node->durationunit, $node->duration,
    $node->pricemode, $node->price, $node->currency,
    $node->assigned_nid, $node->assigned_title, $node->billable, $node->billed
  );
}

function stormtask_update($node) {
  $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 = $p->title;

  if ($node->project_nid != $node->project_nid_old) {
    module_invoke_all('stormtask_change_hierarchy', $node->nid, $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title);
  }
  // if this is a new node or we're adding a new revision,
  if ($node->revision) {
    stormtask_insert($node);
  }
  else {
    _stormtask_beforesave($node);
    db_query("UPDATE {stormtask} SET
      stepno='%s', taskcategory='%s', taskstatus='%s', taskpriority='%s',
      organization_nid=%d, organization_title='%s',
      project_nid=%d, project_title='%s',
      parent_nid=%d, weight=%d, datebegin=%d, dateend=%d, durationunit='%s', duration=%f,
      pricemode='%s', price=%f, currency='%s', assigned_nid=%d, assigned_title='%s',
      billable=%d, billed=%d
      WHERE vid = %d",
      $node->stepno, $node->taskcategory, $node->taskstatus, $node->taskpriority,
      $node->organization_nid, $node->organization_title,
      $node->project_nid, $node->project_title,
      $node->parent_nid,
      $node->weight, $node->datebegin, $node->dateend, $node->durationunit, $node->duration,
      $node->pricemode, $node->price, $node->currency,
      $node->assigned_nid, $node->assigned_title,
      $node->billable, $node->billed, $node->vid
    );
    if (($node->title != $node->title_old) || ($node->stepno != $node->stepno_old)) {
      module_invoke_all('stormtask_change', $node->nid, $node->title, $node->stepno);
    }
  }
}

function _stormtask_beforesave(&$node) {
  $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 = $p->title;

  $assigned = node_load($node->assigned_nid);
  $node->assigned_title = isset($assigned->title) ? $assigned->title : NULL;

  // Allow use of comma when inputting numerical values - str_replace with period decimal
  $node->duration = str_replace(',', '.', $node->duration);
  $node->price = str_replace(',', '.', $node->price);

  if (is_array($node->datebegin)) $node->datebegin = _storm_date_to_gmtimestamp($node->datebegin);
  if (is_array($node->dateend)) $node->dateend = _storm_date_to_gmtimestamp($node->dateend);
}

function stormtask_nodeapi(&$node, $op, $teaser, $page) {
  global $user;

  switch ($op) {
    case 'prepare':
      if ($node->type == 'stormtask' && !isset($node->nid)) {
        $node->iscustomer = 1;
        $node->isactive = 1;

        $taskcategory_list = storm_attributes_bydomain('Task category');
        $node->taskcategory = $taskcategory_list['default'];

        $taskstatus_list = storm_attributes_bydomain('Task status');
        $node->taskstatus = $taskstatus_list['default'];

        $taskpriority_list = storm_attributes_bydomain('Task priority');
        $node->taskpriority = $taskpriority_list['default'];

        $durationunit_list = storm_attributes_bydomain('Duration unit');
        $node->durationunit = $durationunit_list['default'];

        $pricemode_list = storm_attributes_bydomain('Price mode');
        $node->pricemode = $pricemode_list['default'];

        $currency_list = storm_attributes_bydomain('Currency');
        $node->currency = $currency_list['default'];
      }
      break;

    case 'presave':
      // Only for tasks comments:
      if ($node->type == 'stormtaskc') {
        $task_nid = $node->comment_target_nid;
        $node_task = node_load($task_nid);

        // It runs only the first time:
        if (is_null($node->field_stormtaskc_number[0]['value'])) {
          // Runs before computed field field_stormtaskc_number assignment:
          $count = db_result(db_query("SELECT MAX(c.field_stormtaskc_number_value) FROM {content_type_stormtaskc} c JOIN {node_comments} n ON (n.cid = c.nid) WHERE n.nid = ". $task_nid));
          $node->title = '#' . ((empty($count)) ? 1 : $count + 1);

          // Checking the task data:
          $changes = array();

          // Title:
          if ($node->field_stormtaskc_title[0]['value'] != $node_task->title) {
            $changes['title_old'] = $node_task->title;
            $node_task->title = $node->field_stormtaskc_title[0]['value'];
          }
          // Category:
          if ($node->field_stormtaskc_category[0]['value'] != $node_task->taskcategory) {
            $changes['category_old'] = $node_task->taskcategory;
            $node_task->taskcategory = $node->field_stormtaskc_category[0]['value'];
          }
          // Status:
          if ($node->field_stormtaskc_status[0]['value'] != $node_task->taskstatus) {
            $changes['status_old'] = $node_task->taskstatus;
            $node_task->taskstatus = $node->field_stormtaskc_status[0]['value'];
          }
          // Priority:
          if ($node->field_stormtaskc_priority[0]['value'] != $node_task->taskpriority) {
            $changes['priority_old'] = $node_task->taskpriority;
            $node_task->taskpriority = $node->field_stormtaskc_priority[0]['value'];
          }

          $node->field_stormtaskc_changes[0]['value'] = serialize($changes);
        }

        // Update, at least, the task modification date:
        node_save($node_task);

        // Father node:
#       $node->field_stormtaskc_father[0]['nid'] = $task_nid;
      }
      break;

    case 'validate':
      if ($node->type == 'stormtask' && !($node->datebegin['year'] == -1 && $node->dateend['year'] == -1)) {
        if ($node->datebegin['year'] == -1) {
          form_set_error('datebegin', t('If date end field is not empty then date begin field must have a value.'));
        } elseif ($node->dateend['year'] == -1) {
          form_set_error('dateend', t('If date begin field is not empty then date end field must have a value.'));
        } elseif (_storm_date_to_gmtimestamp($node->datebegin) > _storm_date_to_gmtimestamp($node->dateend)) {
          form_set_error('dateend', t('End date must be greater than or equal to start date.'));
        }
      }
      break;

    case 'delete':
      // Only for tasks comments:
      if ($node->type == 'stormtaskc') {
        $task_nid = $node->comment_target_nid;
        $node_task = node_load($task_nid);

        // Update, at least, the task modification date:
        if (!empty($node_task)) {
          node_save($node_task);
        }
      }
      break;

    case 'delete revision':
      if ($node->type == 'stormtask') {
        // Notice that we're matching a single revision based on the node's vid.
        db_query('DELETE FROM {stormtask} WHERE vid = %d', $node->vid);
      }
      break;
  }
}

/**
 * Before delete a task.
 */
function _stormtask_validate_predelete($node) {
  $nid = $node->nid;
  $items = array();

  if (_storm_validate_predelete('stormticket', "s.task_nid = $nid")) {
    $items[] = '<a href="/tickets">' . t('tickets') . '</a>';
  }
  if (_storm_validate_predelete('stormtimetracking', "s.task_nid = $nid")) {
    $items[] = '<a href="/timetrackings">' . t('timetrackings') . '</a>';
  }
  if (_storm_validate_predelete('stormexpense', "s.task_nid = $nid")) {
    $items[] = '<a href="/expenses">' . t('expenses') . '</a>';
  }
  if (_storm_validate_predelete('stormdok', "s.task_nid = $nid")) {
    $items[] = '<a href="/doks">' . t('documents') . '</a>';
  }
  if (_storm_validate_predelete('stormnote', "s.task_nid = $nid")) {
    $items[] = '<a href="/notes">' . t('notes') . '</a>';
  }
  if (_storm_validate_predelete('stormevent', "s.task_nid = $nid")) {
    $items[] = '<a href="/events">' . t('events') . '</a>';
  }

  $nitems = count($items);
  if ($nitems > 0) {
    $elements = $items[0];
    if ($nitems > 2) {
      for ($i = 1; $i < $nitems - 1; $i++) {
        $elements .= ', ' . $items[$i];
      }
    }
    if ($nitems > 1) {
      $elements .= ' ' . t('and') . ' ' . $items[$nitems - 1];
    }
    form_set_error('title', t('Impossible to remove due to existing !elements associated to this task!', array('!elements' => $elements)));
    return FALSE;
  }
  return TRUE;
}

function stormtask_delete($node) {
  // Notice that we're matching all revision, by using the node's nid.
  db_query('DELETE FROM {stormtask} WHERE nid = %d', $node->nid);
}

function stormtask_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {stormtask} WHERE vid = %d', $node->vid));
  $additions->stepno_old = isset($node->stepno) ? $node->stepno : NULL;
  $additions->title_old = $node->title;
  return $additions;
}

function stormtask_validate(&$node) {
  if (($node->nid == $node->parent_nid) && $node->parent_nid) {
    form_set_error('parent_nid', t('Impossible to assign itself as parent.'));
  }
  if (!empty($node->project_nid) && user_access('Storm task: add if project is editable') && !stormproject_access('update', $node->project_nid)) {
    form_set_error('project_nid', t("You cannot add a task for this project, as you do not have access to edit the project's profile."));
  }
}

function stormtask_view($node, $teaser = FALSE, $page = FALSE) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Home'), '<front>');
  $breadcrumb[] = l(t('Projects'), 'projects') . ' (' . l($node->project_title, 'node/'. $node->project_nid) . ')';
  $breadcrumb[] = l(t('Tasks'), 'tasks') . ' (' . l(t('Project tasks'), 'node/'. $node->project_nid .'/tasks') . ')';

  drupal_set_breadcrumb($breadcrumb);

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

function _stormtask_tasks_access($node=NULL) {
  // Determines whether the task tab of a project is visible
  if ($node == NULL) return FALSE;
  if ($node->type == 'stormproject' && user_access('Storm task: access')) return TRUE;
  return FALSE;
}

function _stormtask_get_tree($project_nid, $parent_nid = 0, $depth = -1, $max_depth = NULL, $where = NULL, $args = array()) {
  static $children, $parents, $tasks;

  $depth++;
  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$project_nid])) {
    $children[$project_nid] = array();

    $s = "SELECT n.title, n.uid, sta.* FROM {node} AS n INNER JOIN {stormtask} AS sta
    ON n.vid=sta.vid WHERE n.status=1 AND n.type='stormtask' AND sta.project_nid=%d
    ORDER BY sta.weight";
    $s = stormtask_access_sql($s, $where);
    $s = db_rewrite_sql($s);
    $args = array_merge($args, array($project_nid));
    $r = db_query($s, $args);
    while ($task = db_fetch_object($r)) {
      $children[$project_nid][$task->parent_nid][] = $task->nid;
      $parents[$project_nid][$task->nid][] = $task->parent_nid;
      $tasks[$project_nid][$task->nid] = $task;
    }
  }

  $max_depth = (is_null($max_depth)) ? count($children[$project_nid]) : $max_depth;
  if (isset($children[$project_nid][$parent_nid]) && $children[$project_nid][$parent_nid]) {
    foreach ($children[$project_nid][$parent_nid] as $child_nid) {
      if ($max_depth > $depth) {
        $task = drupal_clone($tasks[$project_nid][$child_nid]);
        $task->depth = $depth;
        $task->parents = $parents[$project_nid][$child_nid];
        $tree[] = $task;
        if (isset($children[$project_nid][$child_nid])) {
          $tree = array_merge($tree, _stormtask_get_tree($project_nid, $child_nid, $depth, $max_depth, $where, $args));
        }
      }
    }
  }
  return isset($tree) ? $tree : array();
}

function _stormtask_plain_tree($tree, $forjs = FALSE) {
  $rows = array();
  foreach ($tree as $item) {
    // Using the space char to preserve the order in Javascript loaded trees:
    $nid = $forjs ? " $item->nid" : $item->nid;
    $title = htmlspecialchars_decode(check_plain($item->title));
    if ($item->stepno) $title = check_plain($item->stepno) .' '. $title;
    if ($nid) $rows[$nid] = str_repeat('--', $item->depth) .' '. $title;
  }
  return $rows;
}

function stormtask_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'stormtask'),
  );
}

// INVOICE AUTO ADD HANDLER
function stormtask_storminvoice_auto_add($node, $invoice_nid = NULL) {
  if (!module_exists('storminvoice')) {
    drupal_set_message(t('This function should only be called from within SuiteDesk Invoice'));
    return;
  }
  elseif ($node->billed) {
    drupal_set_message(t('This task has already been billed!'));
    return;
  }
  elseif (!$node->billable) {
    drupal_set_message(t('This task 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 task as billed.
  db_query("UPDATE {stormtask} SET billed=%d WHERE vid=%d", 1, $node->vid);

  return $invoice_nid;
}

/**
 * Implementation of hook_token_list().
 */
function stormtask_token_list($type='all') {
  $tokens = array();
  if (($type == 'node') || ($type == 'all')) {
    $tokens['node']['stormtask-step-no'] = t('SuiteDesk Task: Step No.');
    $tokens['node']['stormtask-organization-nid'] = t('SuiteDesk Task: Organization Node ID.');
    $tokens['node']['stormtask-organization-title'] = t('SuiteDesk Task: Organization Title.');
    $tokens['node']['stormtask-project-nid'] = t('SuiteDesk Task: Project Node ID.');
    $tokens['node']['stormtask-project-title'] = t('SuiteDesk Task: Project Title.');
    $tokens['node']['stormtask-parent-nid'] = t('SuiteDesk Task: Parent NID.');
    $tokens['node']['stormtask-taskstatus'] = t('SuiteDesk Task: Project Status.');
    $tokens['node']['stormtask-taskcategory'] = t('SuiteDesk Task: Task Category.');
    $tokens['node']['stormtask-taskpriority'] = t('SuiteDesk Task: Task Priority.');
    $tokens['node']['stormtask-durationunit'] = t('SuiteDesk Task: Task Duration Unit.');
    $tokens['node']['stormtask-duration'] = t('SuiteDesk Task: Task Duration.');
    $tokens['node']['stormtask-datebegin'] = t('SuiteDesk Task: Task Begin Date.');
    $tokens['node']['stormtask-dateend'] = t('SuiteDesk Task: Task End Date.');
    $tokens['node']['stormtask-billable'] = t('SuiteDesk Task: Task Billable.');
    $tokens['node']['stormtask-billed'] = t('SuiteDesk Task: Task Billed.');
    $tokens['node']['stormtask-price'] = t('SuiteDesk Task: Task Price.');
    $tokens['node']['stormtask-pricemode'] = t('SuiteDesk Task: Task Price Mode.');
    if (module_exists('stormperson')) {
      // Assignee(s)
      $tokens['node']['stormtask-assigned-to-email'] = t('SuiteDesk Task: Task Assignee(s) Email Address(s).');
      $tokens['node']['stormtask-assigned-title'] = t('SuiteDesk Task: Task Assignee(s) Title(s).');
    }
  }
  return $tokens;
}

/**
 * Implementation of hook_token_values().
 */
function stormtask_token_values($type, $object = NULL) {
  $values = array();
  $node = $object;
  if ((($type == 'node') || ($type == 'all')) && ($node->type === 'stormtask')) {
    $values['stormtask-stepno'] = $node->stepno;
    $values['stormtask-organization-nid'] = $node->organization_nid;
    $values['stormtask-organization-title'] = $node->organization_title;
    $values['stormtask-project-nid'] = $node->project_nid;
    $values['stormtask-project-title'] = $node->project_title;
    $values['stormtask-parent-nid'] = $node->parent_nid;
    $values['stormtask-taskstatus'] = $node->taskstatus;
    $values['stormtask-taskpriority'] = $node->taskpriority;
    $values['stormtask-taskcategory'] = $node->taskcategory;
    $values['stormtask-durationunit'] = $node->durationunit;
    $values['stormtask-duration'] = $node->duration;
    $values['stormtask-datebegin'] = format_date($node->datebegin, 'medium', '', variable_get('date_default_timezone', NULL));
    $values['stormtask-dateend'] = format_date($node->dateend, 'medium', '', variable_get('date_default_timezone', NULL));
    $values['stormtask-price'] = $node->price;
    $values['stormtask-pricemode'] = $node->pricemode;
    $values['stormtask-billable'] = $node->billable;
    $values['stormtask-billed'] = $node->billed;
    if (module_exists('stormperson')) {
      // Project Assignee(s) e-mail
      if ($node->assigned_nid && $node->assigned_nid != 'none') {
        $values['stormtask-assigned-title'] = $node->assigned_title;
        $assignees_node = node_load($node->assigned_nid);
        // Assigned to one person
        if ($assignees_node->type === 'stormperson') {
          $values['stormtask-assigned-to-email'] = stormperson_primary_email($assignees_node);
        }
        // Assigned to a team
        else {
          $assignees_array = $assignees_node->members_array;
          $assignees = array();
          foreach ($assignees_array as $nid => $name) {
            $assignee = node_load($nid);
            $assignees[] = stormperson_primary_email($assignee);
          }
          $assigned = implode(", ", $assignees);
          // Return comma separated list of emails
          $values['stormtask-assigned-to-email'] = $assigned;
        }
      }
      else {
        $values['stormtask-assigned-to-email'] = NULL;
      }
    }
  }
  return $values;
}

function stormtask_storm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'storm_dashboard_link',
      'title' => t('Tasks'),
      'icon' => 'stormtask-item',
      'path' => 'tasks',
      'params' => array(),
      'access_arguments' => 'Storm task: access',
      'node_type' => 'stormtask',
      'add_type' => 'stormtask',
      'map' => array(),
      'weight' => 50,
    );
    $links[] = array(
      'theme' => 'storm_dashboard_link',
      'title' => t('Tasks Kanban'),
      'icon' => 'stormkanban-item',
      'path' => 'tasks/kanban',
      'access_arguments' => 'Storm task: access',
      'weight' => 55,
    );
  }
  return $links;
}
