<?php

/**
 * @file
 */

function stormtimetracking_list() {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Home'), '<front>');
  drupal_set_breadcrumb($breadcrumb);

  if (array_key_exists('organization_nid', $_GET)) {
    if ($_SESSION['stormtimetracking_list_filter']['organization_nid'] != $_GET['organization_nid']) {
      _storm_set_filter_param('timetracking', 'organization_nid', $_GET['organization_nid']);
    }
    _storm_set_filter_param('timetracking', 'project_nid');
    _storm_set_filter_param('timetracking', 'task_nid');
    _storm_set_filter_param('timetracking', 'ticket_nid');
  }
  if (array_key_exists('project_nid', $_GET)) {
    if ($_SESSION['stormtimetracking_list_filter']['project_nid'] != $_GET['project_nid']) {
      _storm_set_filter_param('timetracking', 'project_nid', $_GET['project_nid']);
    }
    $p = node_load($_GET['project_nid']);
    _storm_set_filter_param('timetracking', 'organization_nid', $p->organization_nid);
    _storm_set_filter_param('timetracking', 'task_nid');
    _storm_set_filter_param('timetracking', 'ticket_nid');
  }
  if (array_key_exists('task_nid', $_GET)) {
    if ($_SESSION['stormtimetracking_list_filter']['task_nid'] != $_GET['task_nid']) {
      _storm_set_filter_param('timetracking', 'task_nid', $_GET['task_nid']);
    }
    $t = node_load($_GET['task_nid']);
    _storm_set_filter_param('timetracking', 'organization_nid', $t->organization_nid);
    _storm_set_filter_param('timetracking', 'project_nid', $t->project_nid);
    _storm_set_filter_param('timetracking', 'ticket_nid');
  }
  if (array_key_exists('ticket_nid', $_GET)) {
    if ($_SESSION['stormtimetracking_list_filter']['ticket_nid'] != $_GET['ticket_nid']) {
      _storm_set_filter_param('timetracking', 'ticket_nid', $_GET['ticket_nid']);
    }
    $ti = node_load($_GET['ticket_nid']);
    _storm_set_filter_param('timetracking', 'organization_nid', $ti->organization_nid);
    _storm_set_filter_param('timetracking', 'project_nid', $ti->project_nid);
    _storm_set_filter_param('timetracking', 'task_nid', $ti->task_nid);
  }
  if (array_key_exists('view', $_GET) && $_GET['view'] == 'all') {
    _storm_set_filter_param('timetracking', 'datefrom');
    _storm_set_filter_param('timetracking', 'dateto');

    _storm_set_filter_param('timetracking', 'user', '');

    _storm_set_filter_param('timetracking', 'billable', '-');
    _storm_set_filter_param('timetracking', 'billed', '-');
  }

  $i = new stdClass();
  $i->type = 'stormtimetracking';

  $report_attrs = array(
    'onclick' => "storm_popup(this, 'timetracking_list_form_report', '" . t('Timetrackings') . "', 300, 244, 'stormtimetracking_list_form_report', 'l'); return false;",
  );

  $header = array(
    array(
      'data' => t('Title') . ' / ' . t('Organization') . ' » ' . t('Project') . ' » ' . t('Detail'),
      'field' => 'n.title',
    ),
    array(
      'data' => t('Date'),
      'field' => 'stt.trackingdate',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Duration'),
      'style' => 'text-align: right;'
    ),
    array(
      'data' => storm_icon_l('report', '', t('Reports'), '', array(), $report_attrs) .'&nbsp;'. storm_icon_add_node($i, $_GET),
      'class' => 'storm_list_operations',
    ),
  );

  $where = array();
  $args = array();
  $filterfields = array();

  $s = "SELECT n.*,  stt.*, nre.format FROM {node} n
    INNER JOIN {stormtimetracking} stt ON n.vid=stt.vid
    INNER JOIN {node_revisions} AS nre ON n.vid = nre.vid
    WHERE n.status=1 AND n.type='stormtimetracking' ";

  $s_duration = "SELECT SUM(billing_duration) FROM {stormtimetracking} stt INNER JOIN {node} n ON n.vid=stt.vid WHERE n.status=1 AND n.type='stormtimetracking' ";

  if (_storm_isset_filter_param('timetracking', 'organization_nid', 0)) {
    $where[] = 'stt.organization_nid=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['organization_nid'];
    $filterfields[] = t('Organization');
  }
  if (_storm_isset_filter_param('timetracking', 'project_nid', 0)) {
    $where[] = 'stt.project_nid=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['project_nid'];
    $filterfields[] = t('Project');
  }
  if (_storm_isset_filter_param('timetracking', 'task_nid', 0)) {
    $where[] = 'stt.task_nid=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['task_nid'];
    $filterfields[] = t('Task');
  }
  if (_storm_isset_filter_param('timetracking', 'ticket_nid', 0)) {
    $where[] = 'stt.ticket_nid=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['ticket_nid'];
    $filterfields[] = t('Ticket');
  }

  if (_storm_isset_filter_param('timetracking', 'datefrom', NULL, -1)) {
    $datefrom = $_SESSION['stormtimetracking_list_filter']['datefrom'];
    $datefrom['hour'] = 0;
    $datefrom['minute'] = 0;
    $where[] = 'stt.trackingdate>=%d';
    $args[] = _storm_datetime_to_gmtimestamp($datefrom);
    $filterfields[] = t('Date');
  }
  if (_storm_isset_filter_param('timetracking', 'dateto', NULL, -1)) {
    $dateto = $_SESSION['stormtimetracking_list_filter']['dateto'];
    $dateto['hour'] = 23;
    $dateto['minute'] = 59;
    $where[] = 'stt.trackingdate<=%d';
    $args[] = _storm_datetime_to_gmtimestamp($dateto);
    $filterfields[] = t('Date');
  }

  if (_storm_isset_filter_param('timetracking', 'user', '')) {
    $trackinguser = user_load(array('name' => $_SESSION['stormtimetracking_list_filter']['user']));
    if ($trackinguser === FALSE) $trackinguser->uid = -1;
    $where[] = 'n.uid=%d';
    $args[] = $trackinguser->uid;
    $filterfields[] = t('User');
  }

  if (_storm_isset_filter_param('timetracking', 'billable', '-')) {
    $where[] = 'stt.billable=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['billable'];
    $filterfields[] = t('Billable');
  }
  if (_storm_isset_filter_param('timetracking', 'billed', '-')) {
    $where[] = 'stt.billed=%d';
    $args[] = $_SESSION['stormtimetracking_list_filter']['billed'];
    $filterfields[] = t('Billed');
  }

  $itemsperpage = _storm_get_filter_param('timetracking', 'itemsperpage', variable_get('storm_default_items_per_page', 10));

  if (count($filterfields) == 0) {
    $filterdesc = t('Not filtered');
  }
  else {
    $filterdesc = t('Filtered by !fields', array('!fields' => implode(", ", array_unique($filterfields))));
  }
  $filterdesc .= ' | '. t('!items items per page', array('!items' => $itemsperpage));

  $o = drupal_get_form('stormtimetracking_list_filter', $filterdesc);

  $s = stormtimetracking_access_sql($s, $where);
  $s = db_rewrite_sql($s);

  $tablesort = tablesort_sql($header);
  $r = pager_query($s . $tablesort, $itemsperpage, 0, NULL, $args);

  $s_duration = stormtimetracking_access_sql($s_duration, $where);
  $s_duration = db_rewrite_sql($s_duration);
  $duration = db_result(db_query($s_duration, $args));

  $_SESSION['stormtimetracking_list_filter']['sql'] = $s . $tablesort;
  $_SESSION['stormtimetracking_list_filter']['args'] = $args;

  $timetrackings = array();
  while ($item = db_fetch_object($r)) {
    $timetrackings[] = $item;
  }

  $o .= theme('stormtimetracking_list', $header, $timetrackings, $duration);
  $o .= theme('pager', NULL, $itemsperpage, 0);
  $o .= drupal_get_form('stormtimetracking_list_form_report');
  print theme('page', $o);
}

function stormtimetracking_list_filter(&$form_state, $filterdesc = 'Filter') {
  $organization_nid = _storm_get_filter_param('timetracking', 'organization_nid', 0);
  $project_nid = _storm_get_filter_param('timetracking', 'project_nid', 0);
  $task_nid = _storm_get_filter_param('timetracking', 'task_nid', 0);
  $ticket_nid = _storm_get_filter_param('timetracking', 'ticket_nid', 0);

  $datefrom = _storm_get_filter_param('timetracking', 'datefrom');
  $dateto = _storm_get_filter_param('timetracking', 'dateto');

  $trackinguser = _storm_get_filter_param('timetracking', 'user', '');

  $billable = _storm_get_filter_param('timetracking', 'billable', '-');
  $billed = _storm_get_filter_param('timetracking', 'billed', '-');

  $itemsperpage = _storm_get_filter_param('timetracking', 'itemsperpage', variable_get('storm_default_items_per_page', 10));


  $form = array();

  $form['filter'] = array(
        '#type' => 'fieldset',
        '#title' => $filterdesc,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#theme' => 'stormtimetracking_list_filter',
  );

  $form['filter']['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => -20,
  );

  $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormorganization} AS sor ON sor.vid=n.vid
  WHERE n.status=1 AND n.type='stormorganization' AND isactive=1 ORDER BY n.title";
  $s = stormorganization_access_sql($s);
  $s = db_rewrite_sql($s);
  $r = db_query($s);
  $organizations = array();
  while ($organization = db_fetch_object($r)) {
    $organizations[$organization->nid] = $organization->title;
  }
  $organizations = array(0 => t('All')) + $organizations;
  $form['filter']['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => $organization_nid,
    '#options' => $organizations,
    '#attributes' => array('onchange' => "stormticket_organization_project_task_tickets(this, 'edit-project-nid', 'edit-task-nid', 'edit-ticket-nid', true, '" . t('All') . "')"),
  );

  $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, $organization_nid);
  $projects = array();
  while ($project = db_fetch_object($r)) {
    $projects[$project->nid] = $project->title;
  }
  $projects = array(0 => t('All')) + $projects;
  $form['filter']['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => $project_nid,
    '#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, '" . t('All') . "')"),
  );

  $tree = _stormtask_get_tree($project_nid);
  $tasks = _stormtask_plain_tree($tree);
  $tasks = array(0 => t('All')) + $tasks;
  $form['filter']['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => $task_nid,
    '#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, '" . t('All') . "')"),
  );

  $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormticket} AS sti
  ON sti.vid=n.vid 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 title ";
  $s = stormticket_access_sql($s);
  $s = db_rewrite_sql($s);
  $r = db_query($s, $organization_nid, $project_nid, $task_nid);
  $tickets = array();
  while ($ticket = db_fetch_object($r)) {
    $tickets[$ticket->nid] = $ticket->title;
  }
  $tickets = array(0 => t('All')) + $tickets;
  $form['filter']['group1']['ticket_nid'] = array(
    '#type' => 'select',
    '#title' => t('Ticket'),
    '#default_value' => $ticket_nid,
    '#process' => array('storm_dependent_select_process'),
    '#options' => $tickets,
  );

  $form['filter']['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#weight' => -19,
  );

  $form['filter']['group2']['datefrom'] = array(
    '#type' => 'dateext',
    '#title' => t('Date from'),
    '#withnull' => TRUE,
    '#default_value' => $datefrom,
  );

  $form['filter']['group2']['dateto'] = array(
    '#type' => 'dateext',
    '#title' => t('Date to'),
    '#withnull' => TRUE,
    '#default_value' => $dateto,
  );

  $form['filter']['group2']['user'] = array(
    '#type' => 'textfield',
    '#title' => t('User'),
    '#size' => 25,
    '#default_value' => $trackinguser,
    '#autocomplete_path' => 'user/autocomplete',
  );

  $form['filter']['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
  );

  $form['filter']['group3']['billable'] = array(
    '#type' => 'select',
    '#title' => t('Billable'),
    '#options' => array('-' => t('all'), '1' => t('billable'), '0' => t('not billable')),
    '#default_value' => $billable,
  );

  $form['filter']['group3']['billed'] = array(
    '#type' => 'select',
    '#title' => t('Billed'),
    '#options' => array('-' => t('all'), '1' => t('billed'), '0' => t('not billed')),
    '#default_value' => $billed,
  );

  $form['filter']['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#attributes' => array('class' => 'formgroup-submit'),
  );

  $form['filter']['group4']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#submit' => array('stormtimetracking_list_filter_filter'),
  );

  $form['filter']['group4']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#submit' => array('stormtimetracking_list_filter_reset'),
  );

  $form['filter']['group4']['itemsperpage'] = array(
    '#type' => 'textfield',
    '#title' => t('Items'),
    '#size' => 10,
    '#default_value' => $itemsperpage,
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );

  return $form;
}

function stormtimetracking_list_filter_filter($form, &$form_state) {
  $_SESSION['stormtimetracking_list_filter']['organization_nid'] = $form_state['values']['organization_nid'];
  $_SESSION['stormtimetracking_list_filter']['project_nid'] = $form_state['values']['project_nid'];
  $_SESSION['stormtimetracking_list_filter']['task_nid'] = $form_state['values']['task_nid'];
  $_SESSION['stormtimetracking_list_filter']['ticket_nid'] = $form_state['values']['ticket_nid'];
  $_SESSION['stormtimetracking_list_filter']['datefrom'] = $form_state['values']['datefrom'];
  $_SESSION['stormtimetracking_list_filter']['dateto'] = $form_state['values']['dateto'];
  $_SESSION['stormtimetracking_list_filter']['billable'] = $form_state['values']['billable'];
  $_SESSION['stormtimetracking_list_filter']['billed'] = $form_state['values']['billed'];
  $_SESSION['stormtimetracking_list_filter']['itemsperpage'] = $form_state['values']['itemsperpage'];
  $_SESSION['stormtimetracking_list_filter']['user'] = $form_state['values']['user'];
}

function stormtimetracking_list_filter_reset($form, &$form_state) {
  unset($_SESSION['stormtimetracking_list_filter']);
}

function stormtimetracking_list_form_report() {
  $form = array();
  $form['#prefix'] = '<div id="stormtimetracking_list_form_report" class="storm_list_form_report">';
  $form['report'] = array(
    '#type' => 'select',
    '#title' => t('Reports'),
    '#options' => theme('stormtimetracking_list_form_report_reports'),
  );

  if (module_exists('locale')) {
    $language_list = language_list();
    $languages = array();
    foreach ($language_list as $key => $lang) {
      $languages[$key] = $lang->name;
    }
    $form['language'] = array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#options' => $languages,
    );
  }
  else {
    $form['language'] = array(
      '#type' => 'hidden',
      '#default_value' => 'en',
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Report'),
  );
  $form['#suffix'] = '</div>';
  return $form;
}

function stormtimetracking_list_form_report_submit($form, &$form_state) {
  $report = $form_state['values']['report'];
  $language = $form_state['values']['language'];
  drupal_goto('timetrackings/report/'. $report .'/'. $language);
}

function stormtimetracking_list_report($report, $language) {
  $timetrackings = array();

  $sql = $_SESSION['stormtimetracking_list_filter']['sql'];
  $args = $_SESSION['stormtimetracking_list_filter']['args'];

  $r = db_query($sql, $args);
  while ($t = db_fetch_object($r)) {
    $timetracking = node_load($t->nid);
    $timetrackings[] = $timetracking;
  }

  print theme('stormtimetracking_list_report', $report, $language, $timetrackings);
}
