<?php

/*
 *	  CLASS:  CorrectiveActionList
 *    
*/

class CorrectiveActionList {
	private $dr_list = array();
	private $tc_list = array();
  
  private $all_opt = array();
	private $select_opt = array();
	
	const INCL_ALL = -1;
	const NO_SELECT_OPT = -2;
	
	
	const DR = 'DR';
	const TC = 'TC';
	
	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_CORRECTIVE_ACTION_LIST);
			
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->dr_list = $list_cache->data->getDRlist(self::NO_SELECT_OPT);
				$this->tc_list = $list_cache->data->getTClist(self::NO_SELECT_OPT);
			}
			else {
			
				$sql = "SELECT action_id, action, report_code 
								FROM {qms_corrective_actions} 
								ORDER BY action";	
				$results = db_query($sql);
		
				$select_opt = '- ' . t('Select') . ' -';  // add default option
				
				$this->dr_list[0] = $select_opt;
				$this->tc_list[0] = $select_opt;

				foreach($results as $row) {
					switch ($row->report_code) {
						case self::DR:
							$this->dr_list[$row->action_id] = $row->action;
							break;
						case self::TC:
							$this->tc_list[$row->action_id] = $row->action;
							break;
						default: 
							// no specified code which indicates used for BOTH report types
							$this->dr_list[$row->action_id] = $row->action;
							$this->tc_list[$row->action_id] = $row->action;
							break;
					}
					
				}
			
				// cache the list values for the next time
				cache_set(QMS_CORRECTIVE_ACTION_LIST, $this, 'cache', time() + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'CorrectiveActionList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
//	public function getMembers(&$dr, &$tc) {
//		$dr = $this->dr_list;
//		$tc = $this->tc_list;
//	}
	
	public function getTClist($flag = 0) {
    
    if ($flag == self::NO_SELECT_OPT) {
			return $this->tc_list;
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + $this->tc_list;
		}
		return $this->select_opt + $this->tc_list;
	}
  
	public function getDRlist($flag = 0) {

    if ($flag == self::NO_SELECT_OPT) {
			return $this->dr_list;
		}
		if ($flag == self::INCL_ALL) {
			return $this->all_opt + $this->dr_list;
		}
		return $this->select_opt + $this->dr_list;
	}
	
	
	public function getList($type = self::DR, $flag = 0) {
		return ( (self::DR == $type) ? $this->getDRlist($flag) : $this->getTClist($flag) );
	}
	
	public function getName($type, $id) {
		if ( $type == self::DR) {
			if ( $id && isset($this->dr_list[$id])  ) {
				return $this->dr_list[$id];
			}
		}
		else if ( $type == self::TC) {
			if ( $id && isset($this->tc_list[$id])  ) {
				return $this->tc_list[$id];
			}			
		}
		return '';
	}
	
	public function reload() {
		_clear_qms_cache(QMS_CORRECTIVE_ACTION_LIST); 
		_clear_qms_cache(QMS_DISCREPANCY_SEARCH_FORM); 
		
		unset($this->dr_list);
		unset($this->tc_list);
		$this->init();
	}
} // end - class CorrectiveActionList
