<?php

/*
 *    CLASSES:   JobList && ReportJobList
 */

/*
 *	  CLASS:  Job List
 *    
*/
class JobList {
	private $active = array();
  private $inactive = array();
	private $all = array();
	
	function __construct($customer_id = 0) {
		$this->init($customer_id);
	}
	
	private function init($customer_id = 0) {
		try {
				// get the list of jobs (active and inactive)
				$query = db_select('qms_jobs', 'j')
									->fields('j', array('job_id', 'job_name', 'active'));
									
				if ( $customer_id ) {
					// add filter to limit results to those related to this customer
					$query->leftJoin('qms_simulators', 's', 's.simulator_id = j.simulator_id');
					$query->condition('s.customer_id', $customer_id);
				}	
				$results = $query->orderBy('job_name')->execute();
				
				//$results = db_query("SELECT job_id, job_name, active FROM {qms_jobs} ORDER BY job_name");

				$this->all[0] = '- Select -';  // add default option
				$this->active[0] = '- Select -'; 
        $this->inactive[0] = '- Select -'; 

				foreach($results as $job) {
					$this->all[$job->job_id] = _st_convert_symbols($job->job_name) . 
                                        ($job->active ? '' : '*');
					if ( (int)$job->active ) { 
						// add only if job is active
						$this->active[$job->job_id] = _st_convert_symbols($job->job_name);
					} else {
            $this->inactive[$job->job_id] = _st_convert_symbols($job->job_name);
          }
          
				}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'JobList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 	
	}
	
	public function get_active() {
		return $this->active;
	}
  
  public function get_inactive() {
		return $this->inactive;
	}
	
	public function get_count($active_only = False) {
		// decrement for '- Select -'
		return ($active_only ? count($this->active) -1 : count($this->all) -1);
	}
	
	
	public function get_all($flag = 0) {
		return $this->all;
	}
	
	public function get($active_only = False) {
		return ($active_only ? $this->active : $this->all);
	}

	public function getName($id) {
		if ( $id && isset($this->all[$id]) ) {
			return $this->all[$id];
		}
		return '';
	}
	
	public function reload() {
		//_clear_qms_cache(QMS_JOB_LIST); 
		_clear_qms_cache(QMS_PUNCHCLOCKEVENT_SEARCH_FORM); 
		unset($this->active);
    unset($this->inactive);
		unset($this->all);
		$this->init();
	}
	
} // end - class JobList


/*
 *	  CLASS:  Report Job List
 *    
*/
class ReportJobList {
	private $active = array();

	function __construct() {
	}
	
	public function init($employee_user_id = 0) {
		try {
      
				// only get active jobs tied to the punch_clock table
        $sql = "SELECT job_id, job_name, active FROM qms_jobs j 
                WHERE active = 1 ";
        if ( $employee_user_id ) {
          $sql .= "AND job_id IN 
                   ( SELECT DISTINCT job_id FROM qms_punch_clock
                     WHERE employee_user_id = :id )"; 
        }
        $results = db_query($sql, array(':id' => $employee_user_id));
        
					//$query->condition('p.employee_user_id', $employee_user_id);
      
				//$query = db_select('qms_jobs', 'j');
				
				//$query->leftJoin('qms_punch_clock', 'p', 'j.job_id = p.job_id');
				//$query->fields('j', array('job_id', 'job_name', 'active'));
									
//				$query->condition('j.active', 1);
//									
//				if ( $employee_user_id ) {
//					$query->condition('p.employee_user_id', $employee_user_id);
//				}	
//				$results = $query->orderBy('job_name') ->execute();
			
				$this->active[0] = '- Select -'; 

				foreach($results as $job) {
					$this->active[$job->job_id] = _st_convert_symbols($job->job_name);
				}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'ReportJobList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 	
	}
	
	// redundancy here to keep consistent with other class definitions
	public function get_active() {
		return $this->active;
	}
	
	public function get_count() {
		return count($this->active)-1;  // decrement for '- Select -'
	}
	
	public function get_all() {
		return $this->active;
	}
	
	public function get() {
		return $this->active;
	}

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

