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

class CallForwardCriteriaTimeRule extends AbstractCallForwardCriteria {
public $available;
	public function __construct($parent){
		$this->setCriteriaName("TimeRule");
		$this->setCriteriaTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_GENERAL"));

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("timerule");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_GENERAL").":");
		$property->setPropertyTypeName("select");
		$dateTimeRules = $this->getTimeProfiles();
        
        if (count($dateTimeRules) == 0) {
            $this->available = false;
            return;
        } 
		$this->available = true;
		foreach ($dateTimeRules as $timeProfile) {
			
			$cap = new CallForwardCriteriaPropertyCapability();
			$cap->setName($timeProfile->id);
			$cap->setTitle($timeProfile->title);
			$property->addPropertyCapability($cap);
			
		}
		$this->addPropertyType($property);
		
		/*$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_0");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_0"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_1");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_1"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_2");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_2"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_3");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_3"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_4");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_4"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_5");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_5"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("day_6");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_DAY_6"));
		$property->setPropertyTypeName("bool");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("from_h");
		$property->setMaxValue(23);
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_FROM"));
		$property->setPropertyTypeName("number");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("from_m");
		$property->setMaxValue(59);
		$property->setPropertyTitle(":");
		$property->setPropertyTypeName("number");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("to_h");
		$property->setMaxValue(23);
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_TIMERULE_TO"));
		$property->setPropertyTypeName("number");
		$this->addPropertyType($property);

		$property = new CallForwardCriteriaProperty();
		$property->setMaxValue(59);
		$property->setPropertyName("to_m");
		$property->setPropertyTitle(":");
		$property->setPropertyTypeName("number");

		$this->addPropertyType($property);
		*/
	}

	public function getCriteriaDescription() {

		$timeProfileId = $this->getProperty("timerule");
		
		$timeProfile = DateTimeProfile::model()->findByPk($timeProfileId);
		
		if ($timeProfile != NULL) {
			return __("PHONESETTINGSCALLFORWARD_TIMERULE_GENERAL").": ".CHtml::encode($timeProfile->title);
		} else {
			return "?";
		}

	}
	
	public function getTimeProfiles() {
		return DateTimeProfile::model()->findAll();
	}

	public function checkIfMatched($variables) {

		$date = getdate();
		$wday = $date['wday'];
		$checkDay = "day_6";
		switch($wday) {
			case 1: $checkDay = "day_0"; break;
			case 2: $checkDay = "day_1"; break;
			case 3: $checkDay = "day_2"; break;
			case 4: $checkDay = "day_3"; break;
			case 5: $checkDay = "day_4"; break;
			case 6: $checkDay = "day_5"; break;
		}

		//is the weekday checked?
		if ($this->getProperty($checkDay,"false") != "true") {
			return false;
		}

		//is the time in between?
		$from_h = (int)$this->getProperty("from_h","0");
		$from_m = (int)$this->getProperty("from_m","0");
		$to_h   = (int)$this->getProperty("to_h","0");
		$to_m   = (int)$this->getProperty("to_m","0");
		$now_h = $date['hours'];
		$now_m = $date['minutes'];

		if ($now_h >= $from_h && $now_h <= $to_h ) {
			if ($now_h == $from_h) { //have to check from minutes
				if ($now_m < $from_m) {
					return false;
				}
			}
			if ($now_h == $to_h) { //have to check from minutes
				if ($now_m > $to_m) {
					return false;
				}
			}
			return true;
		}
			
		return false;
	}


}
