<?php
// $Id$

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


/*
 * _get_simulator_downtime_search_list()
 * display the search results
 * This function gets called by simulator_downtime_search_form_callback() and simulator_downtime_pager_callback()
 * also gets called after a delete submit to refresh the cache ($bRefresh = True), but does not return anything
 *
 */

function _get_simulator_downtime_search_list($search,	$bRefresh = False) {
	
	if ( user_access('search view reports') == FALSE ) {
		drupal_set_message(t('Unauthorized:  User authentication required'));
    return;
  }

	$bAdmin = user_access('administer sabreQMS');	
  global $simulator_list;
  
	global $user;
	$user_is_customer_id = _user_is_customer($user->uid);
	
	$table_header = array( 
		array( 'data' => 'SD No.', 'field' => 'sd_no', 'sort' => 'desc', 'class' => array('qms-sd-results-sdno') ),
		array( 'data' => 'DR No.', 'field' => 'dr_no', 'sort' => 'desc', 'class' => array('qms-sd-results-drno') ),
		array( 'data' => 'Out Date', 'field' => 'out_date', 'sort' => 'desc', 'class' => array('qms-sd-results-date') ),
		array( 'data' => 'In Date', 'field' => 'in_date', 'sort' => 'desc', 'class' => array('qms-sd-results-date') ),
		array( 'data' => 'Technician', 'field' => 'full_name', 'sort' => 'asc', 'class' => array('qms-sd-results-tech') ),
		array( 'data' => 'Simulator', 'field' => 'sim_name', 'sort' => 'asc', 'class' => array('qms-sd-results-simulator') ),
	);
	if ( $bAdmin ) {
		$table_header[] = array( 'data' => 'Admin', 'class' => array('qms-results-admin'));
	}
	
	
	$search_init = array(
		'table_name' => 'qms_simulator_downtime',
		'table_alias' => 'm',
		'table_header' => $table_header,
		'pager_path' => 'search/simulator_downtime/pager',
		'order' => QMS_SIMULATORDOWN_DEFAULT_SORT_COLUMN,
		'order_default' => 'SD No.',
		'sort' => QMS_SIMULATORDOWN_DEFAULT_SORT,
		'sort_default' => 'desc',
		'variable' => True,
	);
	
	$query = _st_setup_paged_search($search_init);
	
		
	//--------- Date filtering -----------
	
	// Out of Service Date
	if ( isset($search->out_of_service) && ($search->out_of_service == 1) ) {
		if ( (strlen($search->from_date) > 0) && (strlen($search->to_date) > 0)) {
			$query->condition('out_date', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
			$query->condition('out_date', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else if (strlen($search->from_date) > 0) {
			$query->condition('out_date', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
		}
		else if (strlen($search->to_date) > 0) {
			$query->condition('out_date', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else {
			$query->condition('in_date', 0, '=');
		}
	}
	
	// In Service Date
	if ( isset($search->in_service) && ($search->in_service == 1)  ) {
		if ( (strlen($search->from_date) > 0) && (strlen($search->to_date) > 0)) {
			$query->condition('in_date', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
			$query->condition('in_date', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else if (strlen($search->from_date) > 0) {
			$query->condition('in_date', _st_format_timestamp($search->from_date . " 00:00:00"), '>=');
		}
		else if (strlen($search->to_date) > 0) {
			$query->condition('in_date', _st_format_timestamp($search->to_date . " 23:59:59"), '<=');
		}
		else {
			$query->condition('in_date', 0, '>');
		}
	}
	
		// sd_no
	if ( isset($search->sd_no) && ($search->sd_no <> '') && ( $search->sd_no <> 'SD') ) {  // if only SD prefix, don't bother
		$search->sd_no .= '%';  // append wildcard for fuzzy match
		$query->condition('sd_no', $search->sd_no, 'LIKE');
	}
	
	// tech		
	if ( isset($search->tech_id) && ($search->tech_id > 0) ) {
		//$query->innerJoin('qms_employees', 'e', 'e.user_id = m.tech_user_id AND e.user_id = :uid',
		//									array(':uid' => $search->tech_id) );	
		$query->condition('m.tech_user_id', $search->tech_id);
	}
	$query->leftJoin('qms_employees', 'e', 'e.user_id = m.tech_user_id');
	$query->leftJoin('users', 'u', 'u.uid = m.tech_user_id');
	
	//----------------------- discrepancy linked search parms --------------------------------
	// dr_no
	if ( isset($search->dr_no) && ($search->dr_no <> '') && ( $search->dr_no <> 'DR') ) {  // if only DR prefix, don't bother
		$search->dr_no .= '%';  // append wildcard for fuzzy match
		$query->innerJoin('qms_discrepancy_log', 'd', 'd.discrepancy_id = m.discrepancy_id AND d.dr_no LIKE :drno',
											array(':drno' => $search->dr_no) );	
	}
	else {
		$query->innerJoin('qms_discrepancy_log', 'd', 'd.discrepancy_id = m.discrepancy_id');	
	}
	
	// customer
	if ( $user_is_customer_id ) {
		// add filter to limit results to those related to this customer
		$query->condition('s.customer_id', $user_is_customer_id);
	}
	
  
  // simulators  
  $query->innerJoin('qms_simulators', 's', 's.simulator_id = d.simulator_id');
	if ( isset($search->simulator_id) && $search->simulator_id ) {
		$query->condition('d.simulator_id', $search->simulator_id);
	}
  else {
    // ALL SIMS SEARCH - ignore the sims that have been turned off for ALL searches
    $query->condition('s.include_all_searches', 1);
  }
  
	//----------------------- END -- discrepancy linked search parms --------------------------------	
	
	
	$query->fields('m', array('simulator_downtime_id', 'sd_no', 'out_date', 'in_date', 'discrepancy_id') )
				//->fields('s', array('sim_name', 'active'))
				->fields('e', array('full_name'))
				->fields('u', array('name'))
				->fields('d', array('dr_no', 'simulator_id'));

	$max_count = 0;
	$results = _st_execute_paged_search($query, $max_count);
	
	$i = 0;
	$table_rows = array();
	
	foreach ($results as $row) {
		
		$row_data = array(
			l($row->sd_no, 'simulator_downtime/view/' . $row->simulator_downtime_id),
			l($row->dr_no, 'discrepancy/view/' . $row->discrepancy_id,
				array('query' => array('destination' => 'search/simulator_downtime'))),
			_st_format_date($row->out_date, 'short'),
			( $row->in_date > 0 ? _st_format_date($row->in_date, 'short') : ''),
			( (strlen($row->full_name) > 0) ? check_plain($row->full_name) : check_plain($row->name) . "*"),
			'sim_name' => $simulator_list->getName($row->simulator_id),
		);
		
		if ( $bAdmin ) {
			$row_data[] = array(
				'data' => _get_results_table_options('simulator_downtime', $row->simulator_downtime_id, 
																							$bAdmin, 'search/simulator_downtime'), 
				'class' => array('qms-results-admin'),
			);
			
		}
		
		$table_rows[] = array('data' => $row_data);
		$i++;
	}
  
  $pager = theme('pager', array('tags' => array(), 
                                'quantity' => QMS_RECORDS_PER_PAGE));
	
	$content = '<div id="qms-search-results-div">';
  $content .= $pager;
	
	$content .= theme( 'table', array(
		'header' => $table_header,
		'rows' => $table_rows,
		'empty' => t('None'),
    'sticky' => True,
	));
	
	//Append pager:  http://api.drupal.org/api/drupal/includes--pager.inc/function/theme_pager
	$content .= $pager;
	$content .= '</div>';
	
	// Save to cache
	$results_cache_name = _get_qms_cache_name(QMS_SIMULATORDOWNTIME_SEARCH_RESULTS);
	
	$sd_search_results = $content;
	cache_set($results_cache_name, $sd_search_results, 'cache', time() + (30*60));
	
	//refreshing cache after an edit or delete, don't return anything here
	if ( $bRefresh ) {  
		return;
	}
	
	// All calls to this function are standard AJAX calls return $content and die
	die($content);
}










