<?php
// $Id$

/**
 *		@file
 *		sabreQMS Module for plugin to Drupal 7 Framework
 *
 *		Search SIMULATOR DOWNtime
*/

//require_once('sabreQMS.utils.inc');
require_once('sabreQMS.simulatordownlist.inc');

/*
 *	search_simulator_downtime()
 *
 */

function search_simulator_downtime() {
	drupal_add_library('system','ui.dialog');
	drupal_add_library('system','ui.datepicker');
  
  // this doesn't seem to load consistently unless specified here
  drupal_add_js('misc/tableheader.js');   
	//drupal_add_js('misc/tableselect.js');  
		
	// see http://digitalbush.com/projects/masked-input-plugin/
	drupal_add_js(drupal_get_path('module', 'sabreQMS') . '/js/vendor/jquery.maskedinput-1.3.1.min.js');
	drupal_add_js(drupal_get_path('module', 'sabreTools') . '/js/sabreTools.lib.js');
	drupal_add_js(drupal_get_path('module', 'sabreQMS') . '/js/sabreQMS.searchsimulatordown.js');
	
	if ( user_access('search view reports') == False ) {
		drupal_set_message(t('Unauthorized:  Permission is required'));
    return;
  }

	$form_cache_name = QMS_SIMULATORDOWNTIME_SEARCH_FORM;

	$sd_search_form = '';
	$sd_search_results = '';
	
	global $user;
	$user_is_customer_id = _user_is_customer($user->uid);

	if( $user_is_customer_id ) {
    $form_elems = drupal_get_form('simulator_downtime_search_form');
		$sd_search_form = render($form_elems);
	}
	else {
	
		$form_cache = cache_get($form_cache_name);
		if ( isset($form_cache->data) && ($form_cache->data <> '') ) {
			$sd_search_form = $form_cache->data;
		}
		else {

			// search results not in cache, create empty div for ajax to populate after search
			// search results will get populated to cache after ajax call
      $form_elems = drupal_get_form('simulator_downtime_search_form');
			$sd_search_form = render($form_elems);
		
			//maintains cache entry for the form for 20 min before refresh (time in seconds)
			cache_set($form_cache_name, $sd_search_form, 'cache', time() + QMS_CACHE_TIMEOUT);			
		}	
	}
	
	// get the cache name
	$results_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_RESULTS);

	$results_cache = cache_get($results_cache_name);
	if ( isset($results_cache->data) && ($results_cache->data <> '') ) {
		$sd_search_results = $results_cache->data;
	}
	else {
		// search results not in cache, create empty div for ajax to populate after search
		// search results will get populated to cache after ajax call
		$sd_search_results = '<div id="qms-search-results-div"></div>';
		
		cache_set($results_cache_name, $sd_search_results, 'cache', time() + QMS_CACHE_TIMEOUT);			
	}
	
	$content = $sd_search_form . $sd_search_results;
	
	return $content;
		
}

/*
 * 	simulator_downtime_search_form()
 * 	display form to get user input of search parameters
 *
 */

function simulator_downtime_search_form($form, $form_state) {
	
	//------------ BUILD SEARCH FORM ------------------
	global $tech_list;
	global $simulator_list;
	
	$form['search'] = array(
		'#type' => 'fieldset',
		'#title' => t('Search'),
		'#collapsible' => True,
		'#collapsed' => False,
		'#attributes' => array('id' => array('qms-search-div')),
	);
	
	$form['search']['sd_no'] = array(
		'#type' => 'textfield',
		'#title' => t('SD No.'),
		'#default_value' => 'SD',
		'#size' => 15,
		'#attributes' => array('id' => 'qms-search-sd-no'),
		'#prefix' => '<table class="qms-plain-table" style="width:100%"><tr><td class="qms-search-sd-col1" rowspan="2">',
	);
	
	$form['search']['dr_no'] = array(
		'#type' => 'textfield',
		'#title' => t('DR No.'),
		'#default_value' => 'DR',
		'#size' => 15,
		'#attributes' => array('id' => 'qms-search-dr-no'),
		
	);
	 
	$form['search']['tech'] = array(
		'#type' => 'select',
		'#title' => t('Technician/Instructor'),
		'#options' => $tech_list->getAll(TechnicianList::INCL_ALL),
		'#default_value' => 0,
		'#attributes' => array('class' => array('qms-select'),
													'id' => 'qms-search-tech'),
	);
	
	$form['search']['simulator'] = array(
		'#type' => 'select',
		'#title' => t('Simulator'),
		'#options' => $simulator_list->getAll(SimulatorList::INCL_ALL),
		'#default_value' => 0,
		'#attributes' => array('class' => array('qms-select'),
														'id' => 'qms-search-simulator'),
		'#suffix' => '</td>',
	);
	
	
		
	//-----------------------------------------
	
	$form['search']['search_date_label'] = array(
		'#type' => 'markup',
		'#markup' => '<label style="padding-bottom:8px">Search Date Range for out-of-service or in-service entries</label>',
		'#prefix' => '<td colspan="3" style="vertical-align:top;">',
		'#suffix' => '</td></tr>',
	);
	
	
	$form['search']['out_of_service'] = array(
		'#type' => 'checkbox',
		'#title' => t('Out of Service'),
		'#default_value' => 0,
		'#attributes' => array('id' => 'qms-search-out-of-service'),
		'#prefix' => '<tr><td class="qms-search-sd-col2">',
	);
	
	$form['search']['in_service'] = array(
		'#type' => 'checkbox',
		'#title' => t('In Service'),
		'#default_value' => 0,
		'#attributes' => array('id' => 'qms-search-in-service'),
		'#suffix' => '</td>',
	);
	
		
	$form['search']['from_date'] = array(
		'#type' => 'date_popup',
		'#title' => t('From Date'),
		'#size' => 10,
		'#date_format' => 'm-d-Y',					 	
		'#attributes' => array('style' => array('float:left', 'width:50px'),
														'id' => 'qms-search-from-date',
														'class' => array('qms-date-picker')),
		'#prefix' => '<td class="qms-search-sd-col3">',
		'#suffix' => '</td>',
		
	);
	
	$form['search']['to_date'] = array(
		'#type' => 'date_popup',
		'#title' => t('To Date'),
		'#size' => 10,
		'#date_format' => 'm-d-Y',
		'#attributes' => array('style' => array('float:left', 'clear:right', 'width:50px'),
														'id' => 'qms-search-to-date',
															'class' => array('qms-date-picker')),
		'#prefix' => '<td class="qms-search-sd-col4">',												
		'#suffix' => '</td></tr></table>',
	);
	
	
	//--------------------------------------------
	// Adds a simple submit button that refreshes the form and clears its contents 
	// -- this is the default behavior for forms.
	$form['search']['actions'] = array('#type' => 'actions');
	$form['search']['actions']['submit'] = array(
		'#type' => 'button',
		'#value' => 'Search',
		'#attributes' => array('id' => 'qms-search-btn-submit',
                           'style' => 'font-weight:bolder;',
														'qms-url' => url('search/simulator_downtime/search') ),
/*	DO NOT do Drupal AJAX here, being done through JQuery by the javascript file
		as we need the .live("click") function due to form caching, which won't work here with Drupal
		'#ajax' => array(
			'callback' => 'discrepancy_search_form_callback',
			'wrapper' => 'qms-dr-search-results-div',
			'method' => 'replace',
			'prevent' => 'click',
			'keypress' => TRUE,
		),
*/
	);
  
  global $base_url;
  $form['search']['actions']['clear'] = array(
		'#type' => 'button',
		'#value' => t('Clear'),
		'#attributes' => array('id' => 'qms-search-btn-clear',
                           'class' => array('qms-btn-extra'),
													 'qms-url' => url('search/simulator_downtime/clear') ),
    '#suffix' => '<span class="qms-waiting"><img class="qms-waiting-img" src="' . 
                  $base_url . QMS_IMAGES_DIR . 'waiting.gif" /></span>',
	);
	
	// storage area for dynamic dialog element
	$form['popup_dialog'] = array(
		'#markup' => '<div id="qms-message-box"></div>',
	);
	
	
	return $form;
}

/**
 * simulator_downtime_search_form_callback()
 * displays the search results, first page only
 * Has to be a separate handler from pager & sort link callback
 * This is the callback that gets called when the Search button is clicked
 *
 */
function simulator_downtime_search_form_callback() {
	
	$search = (object) NULL;
	
	$search->sd_no = trim($_POST['sd_no']);
	$search->dr_no = trim($_POST['dr_no']);
	$search->tech_id = (int)$_POST['tech'];
	$search->simulator_id = (int)$_POST['simulator'];
	$search->out_of_service = (int)$_POST['out_of_service'];
	$search->in_service = (int)$_POST['in_service'];
	$search->from_date = trim($_POST['from_date']);
	$search->to_date = trim($_POST['to_date']);
	
	
	// cache the latest search key
	// determine cache name, always use it specific to this user + session
	// set key cache to expire in 30 minutes
	$key_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_KEY);
	
	cache_set($key_cache_name, $search, 'cache', time() + QMS_CACHE_TIMEOUT);		

	return _get_simulator_downtime_search_list($search);
}

/**
 * simulator_downtime_pager_callback()
 * displays the search results, first page only
 * Has to be a separate handler from pager & sort link callback
 * This is the callback that gets called when the Search button is clicked
 *
 */

function simulator_downtime_pager_callback() {
	
	$search = (object) NULL;
	
	// determine the key cache
	$key_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_KEY);
	
	$key_cache = cache_get($key_cache_name);
	if ( isset($key_cache->data) && ($key_cache->data <> '') ) {
		$search = $key_cache->data;
	}
	else {
		// search key not in cache, get it from POST
		// though, this should never happen... should always be in cache
		$search->sd_no = trim($_POST['sd_no']);
		$search->dr_no = trim($_POST['dr_no']);
		$search->tech_id = (int)$_POST['tech'];
		$search->simulator_id = (int)$_POST['simulator'];
		$search->out_of_service = (int)$_POST['out_of_service'];
		$search->in_service = (int)$_POST['in_service'];
		$search->from_date = trim($_POST['from_date']);
		$search->to_date = trim($_POST['to_date']);
		
		// cache the latest search key
		cache_set($key_cache_name, $search, 'cache', time() + QMS_CACHE_TIMEOUT);		
	}
		
	return _get_simulator_downtime_search_list($search);
}

/*
 * simulator_downtime_clear_results_callback()
 * 
 * Clears the search results cache
 *
 */


function simulator_downtime_clear_results_callback() {
	
	// Clear the cache 
	$sd_search_results = '<div id="qms-search-results-div"></div>';
	
	// determine cache name
	$results_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_RESULTS);
	$key_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_KEY);
	
	cache_set($results_cache_name, $sd_search_results, 'cache', time() + QMS_CACHE_TIMEOUT);
	cache_clear_all($key_cache_name, 'cache', TRUE); 
	// this is an ajax callback.  Need to return + die or it renders a full page
	// we only want a piece of a page
	
	die($sd_search_results);  
}











