<?php
// $Id$

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

/*
 *	  CLASS:  TechnicianList
 *    
*/
class CustomerList {
	private $customers_all = array();
	private $customers_active = array();
	private $groups = array();
	
	private $all_opt = array();
	private $select_opt = array();
	
	
	const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		try {
			$list_cache = cache_get(SCH_CUSTOMER_LIST);
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				//$this = $list_cache->data;
				$this->customers_active = $list_cache->data->getActive(self::NO_SELECT_OPT);
				$this->customers_all = $list_cache->data->getAll(self::NO_SELECT_OPT);
				$this->groups = $list_cache->data->getAllCustomerGroups();
				$this->select_opt = array(0 => '- ' . t('Select') . ' -');
				$this->all_opt = array(0 => '- ' . t('All') . ' -');
			}
			else {
				
		 		$this->all_opt = array(0 => '- ' . t('All') . ' -');
				$this->select_opt = array(0 => '- ' . t('Select') . ' -');
				
				
				// get the list of customers & groups (active and inactive)
				$sql = "SELECT c.customer_id, c.customer, c.customer_code, c.active, 
											g.group_id, g.group_name, g.group_code 
								FROM {sch_customers} c 
								LEFT JOIN {sch_groups} g ON c.customer_id = g.customer_id 
								ORDER BY c.customer, g.group_name";		
								
				$results = db_query($sql);
				
				$select_text = '- ' . t('Select') . ' -';

				//$this->customers_all[0] = $select_text;  // add default option
				//$this->customers_active[0] = $select_text; 
				$this->groups[0] = array();
				$this->groups[0][0] = $select_text; 

				foreach($results as $row) {
					$c = $row->customer_code . ': ' . $row->customer;
					$g = '';
					
					if ( !isset($this->customers_all[$row->customer_id]) )  {
						$this->customers_all[$row->customer_id] = $c  . ($row->active ? '' : '*');
						$this->groups[$row->customer_id] = array();
						$this->groups[$row->customer_id][0] = $select_text;
						if ( $row->group_id ) {
							$g = $row->group_code . ': ' . $row->group_name;
							$this->groups[$row->customer_id][$row->group_id] = $g;
						}
						if ( $row->active ) { 
							// add only if customer is active
							$this->customers_active[$row->customer_id] = $c;
						}
					}
					else {  // customer record exists, add the group record(s)
						$this->groups[$row->customer_id][$row->group_id] = $g;
					}
				}
				// cache the tech list object for the next time
				cache_set(SCH_CUSTOMER_LIST, $this, 'cache', REQUEST_TIME + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'CustomerList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 	
	}
	
	public function getActive($flag = 0) {
		if ($flag == self::NO_SELECT_OPT) {
			return $this->customers_active;
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + $this->customers_active;
		}
		
		return $this->select_opt + $this->customers_active;
	}
	
	public function getAll($flag = 0) {
		if ($flag == self::NO_SELECT_OPT) {
			return $this->customers_all;
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + $this->customers_all;
		}
		return $this->select_opt + $this->customers_all;
	}
	
	public function get($active_only = False, $flag = 0) {
		if ($flag == self::NO_SELECT_OPT) {
			return ($active_only ? $this->customers_active : $this->customers_all);
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + ($active_only ? $this->customers_active : $this->customers_all);
		}
		
		return $this->select_opt + ($active_only ? $this->customers_active : $this->customers_all);
	}

	public function getCustomerName($customer_id) {
		if ( $customer_id && isset($this->customers_all[$customer_id]) ) {
			return $this->customers_all[$customer_id];
		}
		return '';
	}
	
	public function getCustomerGroups($customer_id) {
		if ( $customer_id && isset($this->groups[$customer_id]) ) {
			return $this->groups[$customer_id];
		}
		return array();
	}
	
	public function getAllCustomerGroups() {
		if ( isset($this->groups) ) {
			return $this->groups;
		}
		return array();
	}
	
	
	public function getCustomerGroupName($customer_id, $group_id) {
		if ( $customer_id && $group_id && isset($this->groups[$customer_id][$group_id]) ) {
			return $this->groups[$customer_id][$group_id];
		}
		return '';
	}
	
	public function reload() {
		_clear_scheduler_cache(SCH_CUSTOMER_LIST); 
    _clear_scheduler_cache(SCH_ELOGS_SEARCH_FORM); 
		unset($this->customers_all);
		unset($this->customers_active);
		unset($this->groups);
		$this->init();
	}
	
	public function clear() {
		_clear_scheduler_cache(SCH_CUSTOMER_LIST); 
    _clear_scheduler_cache(SCH_ELOGS_SEARCH_FORM); 
	}
	
} // end - class CustomerList


/*
 *	  CLASS:  MonthList
 *    
*/

class MonthList {
	private $list = array();
  
  const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			$cache_name = SCH_MONTH_LIST . '_en';  // lang code -- default to English for now
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
					// the view month sent to the server is always in English, display name may change with i18n enabled
				$this->list = array(
					//'0' => '- ' . t('Select') . ' -',
					'January' => t('January'),
					'February' => t('February'),
					'March' => t('March'),
					'April' => t('April'),
					'May' => t('May'),
					'June' => t('June'),
					'July' => t('July'),
					'August' => t('August'),
					'September' => t('September'),
					'October' => t('October'),
					'November' => t('November'),
					'December' => t('December'),
				);
					
				// cache the list values for the next time
				cache_set($cache_name, $this->list, 'cache', 0);
			}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'MonthList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get($flag = 0) {
		if ($flag == self::NO_SELECT_OPT) {
			return $this->list;
		}
		return array('0' => '- ' . t('Select') . ' -') + $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	public function reload() {
		_clear_scheduler_cache(SCH_MONTH_LIST); 
		
		unset($this->list);
		$this->init();
	}
	
} // end - class MonthList


/*
 *	  CLASS:  YearList
 *    
*/
/*
class YearList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			$cache_name = SCH_YEAR_LIST;  // lang code -- default to English for now
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
					// the view month sent to the server is always in English, display name may change with i18n enabled
				$this->list = array(
					'0' => '- ' . t('Select') . ' -',
					2012 => '2012',
					2013 => '2013',
					2014 => '2014',
					2015 => '2015',
					2016 => '2016',
					2017 => '2017',
					2018 => '2018',
					2019 => '2019',
					2020 => '2020',
					2021 => '2021',
					2022 => '2022',
					2023 => '2023',
					2024 => '2024',
					2025 => '2025',
					2026 => '2026',
					2027 => '2027',
					2028 => '2028',
					2029 => '2029',
					2030 => '2030',
				);
					
				// cache the list values for the next time
				cache_set($cache_name, $this->list, 'cache', 0);
			}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'YearList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	public function reload() {
		_clear_scheduler_cache(SCH_YEAR_LIST); 
		
		unset($this->list);
		$this->init();
	}
	
} // end - class YearList
*/

/*
 *	  CLASS:  IntervalList
 *    
*/

class IntervalList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			/*
			$cache_name = SCH_INTERVAL_LIST;  // lang code -- default to English for now
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
			*/
					// the view month sent to the server is always in English, display name may change with i18n enabled
				$this->list = array(
					//'0' => '- ' . t('Select') . ' -',
					'7' => t('7 Days'),  // we want this to be the default
					'30' => t('30 Days'),
					'C' => t('Custom'),
				);
					
				// cache the list values for the next time
				//cache_set($cache_name, $this->list, 'cache', 0);
			//}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'IntervalList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	/*
	public function reload() {
		_clear_scheduler_cache(SCH_YEAR_LIST); 
		unset($this->list);
		$this->init();
	}
	*/
	
} // end - class IntervalList


/*
 *	  CLASS:  SessionTypeList
 *    
*/

class SessionTypeList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			//$cache_name = SCH_SESSION_TYPE_LIST;  // lang code -- default to English for now
			//$list_cache = cache_get($cache_name);  
			//if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
			//	$this->list = $list_cache->data;
			//}
			//else {
				// get the list of customers & groups (active and inactive)
				
				$sql = "SELECT session_type, session_type_desc
								FROM {sch_session_types} t 
								ORDER BY t.session_type";		
								
				$results = db_query($sql);
				
				$this->list[0] = '- ' . t('Select') . ' -';  // add default option

				foreach($results as $row) {
						$this->list[$row->session_type] = $row->session_type_desc;
				}
				
				
				// cache the tech list object for the next time
				//cache_set(SCH_SESSION_TYPE_LIST, $this, 'cache', REQUEST_TIME + QMS_CACHE_TIMEOUT);
				//}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'SessionTypeList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	/*
	public function reload() {
		_clear_scheduler_cache(SCH_SESSION_TYPE_LIST); 
		
		unset($this->list);
		$this->init();
	}
	*/
	
} // end - class SessionTypeList

/*
 *	  CLASS:  SimulatorList
 *    
*/

class SimulatorList {
	// these are for loading into select lists
	private $active = array();
	private $all = array();
	private $scheduled = array();
	
	// these are for report processing with schedule info
	private $sched_sim_list = array();
	
	
	
	private $all_opt = array();
	private $select_opt = array();
	
	const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		try {
				$this->all_opt = array(0 => '- ' . t('ALL') . ' -');
				$this->select_opt = array(0 => '- ' . t('Select') . ' -');
				
				$query = db_select('sch_simulators', 'sim');
				$query->leftJoin('sch_schedules', 'sch', 'sim.simulator_id = sch.simulator_id');
				$query->condition('sim.deleted', 0);	// do not include sims that are going to be deleted
				$query->fields('sch', array('schedule_id', 'schedule_name', 'timezone'))
						  ->fields('sim', array('simulator_id', 'sim_name', 'active', 
                                    'device_id_internal', 'faa_id'));
				$query->orderBy('sim_name', 'ASC')->orderBy('device_id_internal', 'ASC');
				$results = $query->execute();
				
			
				if ( $results->rowCount() ) {
					
					foreach($results as $row) {
            $dev_id = (strlen($row->device_id_internal) ? '-' . $row->device_id_internal : '');
            $active = ($row->active ? '' : '*');
						$sim_name = $row->sim_name . $dev_id . $active;
            
						$this->all[$row->simulator_id] = $sim_name;
						if ( $row->active ) { 
							// add only if record is active
							$this->active[$row->simulator_id] = $sim_name;
						}
						if ( $row->schedule_id ) {
							$this->sched_sim_list[] = $row;
							$this->scheduled[$row->simulator_id] = $sim_name;
						}
					}
					
				}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'SimulatorList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function getScheduledSimList() {
		return $this->sched_sim_list;  // returns the raw array (with schedule info)
	}
	
	public function getScheduledSimSelectList($flag = 0) {
		if ( $flag == self::NO_SELECT_OPT ) {
			return $this->scheduled;
		}
		else if ( $flag == self::INCL_ALL ) {
			return $this->all_opt + $this->scheduled;
		}		
		return $this->select_opt + $this->scheduled;  // returns the assoc array (sims only)
	}
	
	public function getScheduledSim($requested_sim_id) {
		foreach($this->sched_sim_list as $ss) {
			if ( $ss->simulator_id == $requested_sim_id ) {
				return $ss;
			}
		}
		
		return Null;
	}
	
	public function getActive($flag = 0) {
		if ( $flag == self::NO_SELECT_OPT ) {
			return $this->active;
		}
		else if ( $flag == self::INCL_ALL ) {
			return $this->all_opt + $this->active;
		}		
		return $this->select_opt + $this->active;
	}
	
	public function getAll($flag = 0) {
		if ( $flag == self::NO_SELECT_OPT ) {
			return $this->all;
		}
		else if ( $flag == self::INCL_ALL ) {
			return $this->all_opt + $this->all;
		}		
		return $this->select_opt + $this->all;
	}
	
	
	public function get($index) {
		return $this->sched_sim_list[$index];
	}
	
	public function getCount() {
		return (count($this->all) );		
	}
	
	public function getActiveCount() {
		return (count($this->active)); 
	}
	
	/*

	public function get_first_active() {
		reset($this->active);
		return key($this->active);
	}
	
	public function get_first() {
		reset($this->all);
		return key($this->all);
	}
	*/
	
	public function getName($id) {
		if ( $id && isset($this->all[$id])  ) {
			return $this->all[$id];
		}
		return '';
	}
	
	public function reload() {
		//_clear_qms_cache(QMS_SIMULATOR_LIST); 
		_clear_qms_cache(SCH_EXCEPTIONS_SEARCH_FORM);
    _clear_qms_cache(SCH_ELOGS_SEARCH_FORM);
		
		unset($this->active);
		unset($this->all);
		unset($this->sched_sim_list);
		unset($this->scheduled);
		$this->init();
	}
	
	
} // end - class SimulatorList




/*
 *	  CLASS:  Session Name List
 *    
*/

class SessionNameList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			$cache_name = SCH_SESSION_NAME_LIST; 
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
				//get the list of session names
				
				$sql = "SELECT session_name_id, session_name 
								FROM {sch_session_names} 
								ORDER BY session_name_id";		
								
				$results = db_query($sql);
				
				$this->list[0] = '- ' . t('Select') . ' -';  // add default option

				foreach($results as $row) {
						$this->list[$row->session_name_id] = $row->session_name;
				}
				
				
				//cache the list object for the next time
				cache_set($cache_name, $this->list, 'cache', REQUEST_TIME + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'SessionNameList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	
} // end - class SessionNameList


/*
 *	  CLASS:  Flight Roles List
 *    
*/

class FlightRolesList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
			$cache_name = SCH_FLIGHT_ROLES_LIST; 
			$list_cache = cache_get($cache_name);  
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
				//get the list of flight roles
				
				$sql = "SELECT flight_role_id, flight_role_code, flight_role_name 
								FROM {sch_flight_roles} 
								ORDER BY flight_role_id";		
								
				$results = db_query($sql);
				
				$this->list[0] = '- ' . t('Select') . ' -';  // add default option

				foreach($results as $row) {
						$this->list[$row->flight_role_id] = $row->flight_role_code . ' : ' . $row->flight_role_name;
				}
				
				
				//cache the list object for the next time
				cache_set(SCH_FLIGHT_ROLES_LIST, $this->list, 'cache', REQUEST_TIME + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'FlightRolesList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	/*
	public function reload() {
		_clear_scheduler_cache(SCH_SESSION_TYPE_LIST); 
		
		unset($this->list);
		$this->init();
	}
	*/
	
} // end - class FlightRolesList


/*
 *	  CLASS:  Session Name List
 *    
*/

class CustomerColorLookup {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		
		try {
//			$cache_name = SCH_SESSION_NAME_LIST; 
//			$list_cache = cache_get($cache_name);  
//			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
//				$this->list = $list_cache->data;
//			}
//			else {
				
        $sql = "SELECT customer_code, fill_color, font_color 
                FROM sch_customers ORDER BY customer_code ASC";

        $result = db_query($sql);
        if ($result->rowCount()) {
          foreach($result as $row) {
            // default light blue bkgd fill and black text color
            if (empty($row->fill_color)) { $row->fill_color = '3498DB'; }
            if (empty($row->font_color)) { $row->font_color = '000000'; }
            $this->list[$row->customer_code] = $row;
          }
        }
        // add on 2 additional system required lookups
        $this->list['MAINT'] = new stdClass();
        $this->list['MAINT']->fill_color = '000000';
        $this->list['MAINT']->font_color = 'FFFFFF';
        
        $this->list['***'] = new stdClass();
        $this->list['***']->fill_color = '666666';
        $this->list['***']->font_color = 'FFFFFF';
        
				
				//cache the list object for the next time
			//	cache_set($cache_name, $this->list, 'cache', REQUEST_TIME + QMS_CACHE_TIMEOUT);
			//}
		}
		catch (Exception $e) {
			watchdog(SCH_SCHEDULER, 'CustomerColorLookup::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
  
  public function getCount() {
    return count($this->list);
  }
  
  public function isValid($cust_code) {
    return isset($this->list[$cust_code]);
  }
	
	public function get() {
		return $this->list;
	}
	
	public function getFillColor($code) {
		if ( !empty($code) && isset($this->list[$code])  ) {
			return 'FF' . $this->list[$code]->fill_color;
		}
    // always prefix with the alpha channel 'FF' for argb
		return 'FF000000';
	}
  
  public function getFontColor($code) {
		if ( !empty($code) && isset($this->list[$code])  ) {
			return 'FF' . $this->list[$code]->font_color;
		}
    // always prefix with the alpha channel 'FF' for argb
		return 'FFFFFFFF';
	}
	
	
} // end - class CustomerColorLookup






