<?php

/*
 *    CLASS:  LoadUpdatedList
 * 
 */

class LoadUpdatedList {
	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_LOAD_UPDATED_LIST);
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
			
				$sql = "SELECT eng_load_updated_id, eng_load_updated_desc FROM {qms_eng_load_updated} ORDER BY eng_load_updated_desc";	
				$results = db_query($sql);
		
		
				foreach($results as $row) {
					$this->list[$row->eng_load_updated_id] = $row->eng_load_updated_desc;
				}
				// cache the list values for the next time
				cache_set(QMS_LOAD_UPDATED_LIST, $this->list, 'cache', time() + QMS_CACHE_TIMEOUT);
				
			}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'LoadUpdatedList::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_ENGINEERING_SEARCH_FORM); 
		unset($this->list);
		$this->init();
	}
} // end - class LoadUpdatedList


