<?php
// $Id$

/**
 *		@file
 *		sabreQMS Module for plugin to Drupal 7 Framework
 *
 */

/*
 *	  CLASS:  TechnicianList
 *    
*/
class TechnicianList {
	private $active = array();
  private $inactive = array();
	private $all = array();
  
  private $all_opt = array();
	private $select_opt = array();
	
	const INCL_UNASSIGNED = -1;
  
	const NO_SELECT_OPT = -2;
  const INCL_ALL = -3;
  

	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		try {
      $this->all_opt = array(0 => '- ' . t('All') . ' -');
			$this->select_opt = array(0 => '- ' . t('Select') . ' -');
        
			$list_cache = cache_get(QMS_TECHNICIAN_LIST);
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->active = $list_cache->data->get_active();
        $this->inactive = $list_cache->data->get_inactive();
				$this->all = $list_cache->data->get_all();
			}
			else {
				// get the list of users who are marked as technicians or contractors, (active and inactive)
				$sql = "SELECT e.user_id, e.full_name AS `name`, u.status FROM {qms_employees} e, {users} u
								WHERE e.user_id = u.uid 
								UNION
								SELECT u.uid AS `user_id`, u.name, u.status FROM {users} u 
								WHERE u.uid IN (SELECT ur.uid FROM {users_roles} ur, {role} r WHERE ur.rid = r.rid AND r.name = 'contractor' )
								ORDER BY `name` ASC";	
				$results = db_query($sql);


				foreach($results as $tech) {
					$this->all[$tech->user_id] = $tech->name. ($tech->status ? '' : '*');
					if ( $tech->status ) { 
						// add only if tech user is active
						$this->active[$tech->user_id] = $tech->name;
					} else {
            $this->inactive[$tech->user_id] = $tech->name;
          }
				}
				// cache the tech list object for the next time
				cache_set(QMS_TECHNICIAN_LIST, $this, 'cache', time() + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'TechnicianList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 	
	}
	
	public function get_active($flag = 0) {
		return $this->getActive($flag);
	}
  
  public function get_inactive($flag = 0) {
		return $this->getInactive($flag);
	}
  
  public function getActive($flag1 = 0, $flag2 = 0) {
    $unassigned = array(self::INCL_UNASSIGNED => '[unassigned]');
    $bUnassigned = False;
    
    if ( ($flag1 == self::INCL_UNASSIGNED) || ($flag2 == self::INCL_UNASSIGNED) ) {
      $bUnassigned = True;
    }
    if ( ($flag1 == self::NO_SELECT_OPT) || ($flag2 == self::NO_SELECT_OPT) ) {
			return $this->active + ($bUnassigned ? $unassigned : array());
		}
		
		return $this->select_opt + $this->active + ($bUnassigned ? $unassigned : array());
	}
	
	public function get_all($flag = 0) {
		return $this->getAll($flag);
	}
  
  public function getAll($flag1 = 0, $flag2 = 0) {
    $unassigned = array(self::INCL_UNASSIGNED => '[unassigned]');
    $bUnassigned = False;
    
    if ( ($flag1 == self::INCL_UNASSIGNED) || ($flag2 == self::INCL_UNASSIGNED) ) {
      $bUnassigned = True;
    }
		if ( ($flag1 == self::NO_SELECT_OPT) || ($flag2 == self::NO_SELECT_OPT) ) {
			return $this->all + ($bUnassigned ? $unassigned : array());
		}
		if ( ($flag1 == self::INCL_ALL) || ($flag2 == self::INCL_ALL) ) {
			return $this->all_opt + $this->all + ($bUnassigned ? $unassigned : array());
		}
    
		return $this->select_opt + $this->all + ($bUnassigned ? $unassigned : array());
	}
  
  public function getInactive($flag1 = 0) {
   
		if ( $flag1 == self::NO_SELECT_OPT) {
			return $this->inactive;
		}
    
		return $this->select_opt + $this->inactive;
	}
	
	public function get($active_only = False) {
    if ( $active_only ) {
      return $this->getActive();
    }
    return $this->getAll();
	}

	public function getName($id) {
		if ( $id && isset($this->all[$id]) ) {
			return $this->all[$id];
		}
		return '';
	}
	
	public function reload() {
		_clear_qms_cache(QMS_TECHNICIAN_LIST); 
		_clear_qms_cache(QMS_DISCREPANCY_SEARCH_FORM); 
		_clear_qms_cache(QMS_ENGINEERING_SEARCH_FORM); 
		_clear_qms_cache(QMS_SIMULATORDOWNTIME_SEARCH_FORM);
		_clear_qms_cache(QMS_SHIFTLOG_SEARCH_FORM);
		_clear_qms_cache(QMS_PUNCHCLOCKEVENT_SEARCH_FORM);
		unset($this->active);
		unset($this->all);
		$this->init();
	}
	
} // end - class TechnicianList


