<?php

/*
 *	  CLASS:  SimulatorList
 *    
*/

class SimulatorList {
	private $active = array();
	private $all = array();
  
  private $all_opt = array();
	private $select_opt = array();
	
	const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
  
	function __construct($vars = array()) {
    $customer_id = 0;
    $omit_if_qtg = False;
    
    if ( isset($vars['customer_id'])) {
      $customer_id = $vars['customer_id'];
    }
    if ( isset($vars['omit_if_qtg']) ) {
      $omit_if_qtg = True;
    }
		$this->init($customer_id, $omit_if_qtg);
	}
	
	private function init($customer_id = 0, $omit_if_qtg = False) {
		try {
				$results = (object) null;
        $this->all_opt = array(0 => '- ' . t('All') . ' -');
				$this->select_opt = array(0 => '- ' . t('Select') . ' -');
        
				$sql = "SELECT s.simulator_id, s.sim_name, 
                s.device_id_internal, s.faa_id, 
                s.active, c.customer 
								FROM {qms_simulators} s 
								LEFT JOIN {qms_customers} c ON 
                          s.customer_id = c.customer_id ";
				
				if ( $customer_id > 0 ) { 
					$sql .= "WHERE s.customer_id = :cid AND s.deleted = 0 
                   ORDER BY c.customer, s.sim_name";
					$results = db_query($sql, array(':cid' => $customer_id));
				}
        else if ( $omit_if_qtg ) {
          $sql .= "WHERE s.deleted = 0 
                   AND s.simulator_id NOT IN 
                   (SELECT simulator_id FROM {qms_qtg_test_plans} 
                    WHERE active = 1) 
                   AND s.active = 1 
                   ORDER BY c.customer, s.sim_name";
					$results = db_query($sql, array(':cid' => $customer_id));
        }
				else {
					$sql .= "WHERE s.deleted = 0 ORDER BY c.customer, s.sim_name";	
					$results = db_query($sql);
				}
			
		
        $prefix_sims = variable_get(QMS_VAR_SIMS_USE_CUSTOMER_PREFIX, 1);
        $faa_label = ' ' . t('FAA ID') . ' ';

				foreach($results as $row) {
          $dev_id = (strlen($row->device_id_internal) ? '-' . $row->device_id_internal : '');
          $active = ($row->active ? '' : '*');
          $prefix = ($prefix_sims ? $row->customer . ' - ' : '');
          $faa_id = (strlen($row->faa_id) ? $faa_label . $row->faa_id : '');
          
          $sim_name = $prefix . $row->sim_name . $dev_id . $faa_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;
					}
				}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'SimulatorList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
  public function get_active($flag = 0) {
    return $this->get(True, $flag);
  }
  
  public function getActive($flag = 0) {
		return $this->get(True, $flag);
	}
  
  public function get_all($flag = 0) {
    return $this->get(False, $flag);
  }
	
	
  public function getAll($flag = 0) {
		return $this->get(False, $flag);
	}
	
	public function get($active_only = False, $flag = 0) {
    if ($flag == self::NO_SELECT_OPT) {
			return ($active_only ? $this->active : $this->all);
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + ($active_only ? $this->active : $this->all);
		}
		return $this->select_opt + ($active_only ? $this->active : $this->all);
	}
	
	public function get_active_count() {
		return count($this->active); 
	}

	public function get_count() {
		return count($this->all);			
	}
	
	public function get_first_active() {
		reset($this->active);
		//next($this->active);  // bump one past '-Select-'
		return key($this->active);
	}
	
	public function get_first() {
		reset($this->all);
		//next($this->all);			// bump one past '-Select-'
		return key($this->all);
	}
	
	public function getName($id) {
		if ( $id && isset($this->all[$id])  ) {
			return $this->all[$id];
		}
		return '';
	}
	
	public function reload($customer_id = 0) {
		//_clear_qms_cache(QMS_SIMULATOR_LIST); 
		//_clear_qms_cache(QMS_DISCREPANCY_SEARCH_FORM); 
		//_clear_qms_cache(QMS_ENGINEERING_SEARCH_FORM); 
		//_clear_qms_cache(QMS_SIMULATORDOWNTIME_SEARCH_FORM);
		//_clear_qms_cache(QMS_SHIFTLOG_SEARCH_FORM);
    //_clear_qms_cache(QMS_JOBS_SEARCH_FORM);
		
		unset($this->active);
		unset($this->all);
		$this->init($customer_id);
	}
	
	
} // end - class SimulatorList
