<?php

/*
 *	  CLASS:  TroubleCauseList
 *    
*/

class TroubleCauseList {
	private $list = array();
  
  private $all_opt = array();
	private $select_opt = array();
	
	const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	function __construct() {
		$this->init();
	}
	
	public function init() {
		try {
      $this->all_opt = array(0 => '- ' . t('All') . ' -');
			$this->select_opt = array(0 => '- ' . t('Select') . ' -');
      
			$list_cache = cache_get(QMS_TROUBLE_CAUSE_LIST);
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
			
				$sql = "SELECT trouble_cause_code, trouble_cause_desc 
								FROM  {qms_trouble_causes} ORDER BY trouble_cause_desc";	
				$results = db_query($sql);
		
				$this->list[0] = '- Select -';  // add default option

				foreach($results as $row) {
					$this->list[$row->trouble_cause_code] = $row->trouble_cause_desc;
				}
				// cache the list values for the next time
				cache_set(QMS_TROUBLE_CAUSE_LIST, $this->list, 'cache', time() + QMS_CACHE_TIMEOUT);
				
			}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'TroubleCauseList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get($flag = 0) {
    
    if ($flag == self::NO_SELECT_OPT) {
			return $this->list;
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + $this->list;
		}
		return $this->select_opt + $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	public function reload() {
    _clear_qms_cache(QMS_TROUBLE_CAUSE_LIST);
		_clear_qms_cache(QMS_TROUBLECALL_SEARCH_FORM); 
		unset($this->list);
		$this->init();
	}
} // end - class TroubleCauseList
