<?php
require_once 'class.abstractcallforwardcriteria.php';

final class CallForwardCriteriaCallSourceType {
	const Internal         = 0x00000000;
	const External         = 0x00000001;
	
	private function __construct(){
	}
	
	public static function toMySQLEnum($type) {
		if($type == CallForwardCriteriaCallSourceType::Internal) {
			return "internal";
		} else {
			return "external";
		}
	}
	
	public static function fromMySQLEnum($type) {
		if ($type == "internal" ) {
			return CallForwardCriteriaCallSourceType::Internal;
		} else {
			return CallForwardCriteriaCallSourceType::External;
		}
	}
	
}

class CallForwardCriteriaCallSource extends AbstractCallForwardCriteria {
	
	private $mCallForwardCriteriaCallSourceType = CallForwardCriteriaCallSourceType::Internal;
	
	public function __construct($parent){
		$this->setCriteriaName("CallSource");
		$this->setCriteriaTitle(__("PHONESETTINGSCALLFORWARD_IN_CALLSOURCE_GENERAL"));
		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("callsource");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_CALLSOURCE_IS"));
		$property->setPropertyTypeName("select");
		
		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName(CallForwardCriteriaCallSourceType::toMySQLEnum(CallForwardCriteriaCallSourceType::Internal));
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_CALLSOURCE_INTERNAL"));
		$property->addPropertyCapability($cap);
		
		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName(CallForwardCriteriaCallSourceType::toMySQLEnum(CallForwardCriteriaCallSourceType::External));
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_CALLSOURCE_EXTERNAL"));
		$property->addPropertyCapability($cap);
						
		$this->addPropertyType($property);
		
	}
	
	public function setMatchedPhoneStatus($type) {
		$this->setProperty("callsource", CallForwardCriteriaCallSourceType::toMySQLEnum($type));
	}
		
	public function getMatchedPhoneStatus() {
		return CallForwardCriteriaCallSourceType::fromMySQLEnum($this->getProperty("callsource"));
	}
	
	public function getCriteriaDescription() {
		
		$type = $this->getMatchedPhoneStatus();
		switch ($type) {
			case CallForwardCriteriaCallSourceType::Internal :
				return __('PHONESETTINGSCALLFORWARD_IN_CALLSOURCE_GENERAL') . ": " . __("PHONESETTINGSCALLFORWARD_CALLSOURCE_INTERNAL"); 
				break;
			case CallForwardCriteriaCallSourceType::External :
				return __('PHONESETTINGSCALLFORWARD_IN_CALLSOURCE_GENERAL') . ": " . __("PHONESETTINGSCALLFORWARD_CALLSOURCE_EXTERNAL"); 
				break;
		}

	}

	public function checkIfMatched($variables) {
		
		//is the phonestatus set?
		$currentState = @$variables['callsource'];
		if ($currentState == "") {		
			return false;
		}
		//transform into enum
		$currentState = CallForwardCriteriaCallSourceType::fromMySQLEnum($currentState);
		
		if ($this->getMatchedPhoneStatus() == $currentState) {
			//current state matches.
			return true;
		}
		return false;
	}

	
}