<?php

/*
 *	  CLASS:  EngineTypes
 *    
*/
class EngineTypes {
	
	private $all = array();

		
	function __construct($test_id = 0) {
		$this->init($test_id);
	}
	
	private function init($test_id) {
		try {
				// get the list of engines for the test plan or test
				$query = db_select('qms_qtg_engine_types', 'e')
									->fields('e', array('engine_type', 
                                      'engine_code'));
									
				if ( $test_id ) {
          // get engine types for the test_id
					$query->leftJoin('qms_qtg_test_engines', 'te', 
                            'te.engine_type = e.engine_type');
					$query->condition('te.qtg_test_id', $test_id);
          
				}	
				$results = $query->orderBy('engine_code') ->execute();
				
				$this->all[0] = '- Select -';  // add default option

				foreach($results as $row) {
          // if requested by test_id, return just the engine code
          // if requested by test plan, return the code & desc
					$this->all[$row->engine_type] = $row->engine_code;
				}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'EngineTypes::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 	
	}
	
	
	public function get_count() {
		return count($this->all);
	}
	
	
	public function get() {
		return $this->all;
	}

	public function getName($id) {
		if ( $id && isset($this->all[$id]) ) {
			return $this->all[$id];
		}
		return '';
	}
	
	public function reload() {
		
		unset($this->all);
		$this->init();
	}
	
} // end - class EngineTypes
