<?php
require_once 'class.abstractcallforwardcriteria.php';
/*
 Translation Tags in this Class :
PHONESETTINGSCALLFORWARD_IN_ADDRESSBOOK_GENERAL
PHONESETTINGSCALLFORWARD_NOT_IN_ADDRESSBOOK_GENERAL
PHONESETTINGSCALLFORWARD_IN_ADDRESSBOOK
PHONESETTINGSCALLFORWARD_CRITERIA_CALLER
PHONESETTINGSCALLFORWARD_ADDRESSBOOK_CONTACT_TYPE_IS
PHONESETTINGSCALLFORWARD_ADDRESSBOOK_CONTACT_TYPE_IS_NOT
PHONESETTINGSCALLFORWARD_CRITERIA_CONTACT_TYPE
PHONESETTINGSCALLFORWARD_CRITERIA_ADDRESSBOOK_ID
*/
class CallForwardCriteriaAddressBook extends AbstractCallForwardCriteria {

	public function __construct($parent){

		$this->mParent = $parent;

		$this->setCriteriaName("AddressBook");
		$this->setCriteriaTitle(__("PHONESETTINGSCALLFORWARD_IN_ADDRESSBOOK_GENERAL"));

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("is_or_is_not");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_CRITERIA_CALLER"));
		$property->setPropertyTypeName("select");

		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName("is");
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_ADDRESSBOOK_CONTACT_TYPE_IS"));
		$property->addPropertyCapability($cap);

		$cap = new CallForwardCriteriaPropertyCapability();
		$cap->setName("is_not");
		$cap->setTitle(__("PHONESETTINGSCALLFORWARD_ADDRESSBOOK_CONTACT_TYPE_IS_NOT"));
		$property->addPropertyCapability($cap);
		$this->addPropertyType($property);

		//$addrbooks = $this->getAvialableAddressBooks();

		$property = new CallForwardCriteriaProperty();
		$property->setPropertyName("addressbook");
		$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_CRITERIA_CONTACT_TYPE"));
		$property->setPropertyTypeName("select");
		$bookList = $this->getContactTypes();
                
		if (!empty($bookList)) {
			foreach ( $bookList as $type) {
				$cap = new CallForwardCriteriaPropertyCapability();
				$cap->setName($type->id);
				$cap->setTitle($type->name);
				$property->addPropertyCapability($cap);
			}
			$this->addPropertyType($property);
		}

		$bookList =$this->getAvialableAddressBooks();
		if (count($bookList)) {
			$property = new CallForwardCriteriaProperty();
			$property->setPropertyName("addressbookId");
			$property->setPropertyTitle(__("PHONESETTINGSCALLFORWARD_CRITERIA_ADDRESSBOOK_ID"));
			$property->setPropertyTypeName("select");
			foreach ($this->getAvialableAddressBooks() as $book) {
				$cap = new CallForwardCriteriaPropertyCapability();
				$cap->setName($book->id);
				$cap->setTitle($book->title);
				$property->addPropertyCapability($cap);
			}
			$this->addPropertyType($property);
		}

	}

	private function getContactTypes() {
		return ContactType::getDefinedContactTypes();
		/*$types = ContactType::model()->findAll();
		 $ret = array();
		foreach ($types as $type) {
		$ret[] = $type->name;
		}

		return $ret;
		*/
	}

	public function getAvialableAddressBooks() {

		return $this->mParent->getAvialableAddressBooks();
	}

	public function getCriteriaDescription() {

		$type_choosen = $this->getProperty("addressbook","");
		$book_choosen = $this->getProperty("addressbookId","");
		$book = null;
		foreach ($this->getAvialableAddressBooks() as $book) {
			if ($book_choosen == $book->id) {
				break;
			} else {
				$book = NULL;
			}
		}



		$type = ContactType::model()->findByPk($type_choosen);
		if (!$type) {
			foreach($this->getContactTypes() as $type) {
				if ($type_choosen == $type->id) {
					break;
				}
			}
		}


		$ret = $this->getProperty("is_or_is_not","is");
		if ($ret == "is") {
			$ret = __("PHONESETTINGSCALLFORWARD_IN_ADDRESSBOOK_GENERAL")." ";
		} else {
			$ret = __("PHONESETTINGSCALLFORWARD_NOT_IN_ADDRESSBOOK_GENERAL")." ";
		}


		if ($type != null ) {

			$bookName = $book != null ? $book->title : '---';
			return $ret.'"'.CHtml::encode($type->name).'" '.__("PHONESETTINGSCALLFORWARD_IN_ADDRESSBOOK").' "'.$bookName.'".';
		} else {
			return "ERROR, Contacttype not found.";
		}


	}

	public function checkIfMatched($variables) {
		if (@$variables["callerid"] == "") {
			return false;
		}

		//get addressbook by name
		$addrbook_choosen = $this->getProperty("addressbook","");
		if ($addrbook_choosen == "") {
			return false;
		}

		$isTrueIfIsIn = $this->getProperty("is_or_is_not","is");
		if ($isTrueIfIsIn == "is") {
			$isTrueIfIsIn = true;
		} else {
			$isTrueIfIsIn = false;
		}

		$addrbooks = $this->getAvialableAddressBooks();
		$addrbook = null;
		foreach($addrbooks as $addrbook) {
			if ($addrbook->getName() == $addrbook_choosen) {
				break;
			}
		}

		// FIXME: this will not work for number from extern without the 0 prefix
		$callerid = @$variables["callerid"];
		if ($callerid[0] == "0") {
			$callerid = substr($callerid, 1);
		}

		$is_in_addressbook = false;
		if ($addrbook != null) {
			$name = $addrbook->reverseSearchName($callerid);
			if ($name != "") { //entry is in addressbook
				$is_in_addressbook = true;
			} else { //entry is not in addressbook
				$is_in_addressbook = false;
			}
		}

		if ($is_in_addressbook) {
			if ($isTrueIfIsIn) {
				return true;
			} else {
				return false;
			}
		} else {
			if ($isTrueIfIsIn) {
				return false;
			} else {
				return true;
			}
		}

		return false;
	}

}
