<?php

require_once 'class.callforward.php';
require_once 'class.usercallforward.php';
//require_once 'class.queuecallforward.php';
require_once 'class.abstractcallforwardcriteria.php';
require_once 'class.callforwardcriteria_phonestatus.php';
require_once 'class.callforwardcriteria_queuestatus.php';
require_once 'class.callforwardcriteria_jabberstatus.php';
require_once 'class.callforwardcriteria_timerule.php';
require_once 'class.callforwardcriteria_addressbook.php';
require_once 'class.callforwardcriteria_calendar.php';
require_once 'class.callforwardcriteria_callsource.php';
require_once 'class.callforwardcriteria_nightcircut.php';
require_once 'class.abstractcallforwardtarget.php';
require_once 'class.callforwardtarget_number.php';
require_once 'class.callforwardtarget_voicemail.php';
require_once 'class.callforwardtarget_announcement.php';
require_once 'class.callforwardtarget_queuevoicemail.php';

//require_once 'class.user.php';
//require_once 'class.queue.php';

//require_once 'class.querybuilder.php';

//require_once 'inithinttogglecall.php';


/**
 * Overview over the Callforwarding Framework
 *
 * This Framework has been desinged to be as flexible as possible. It's modular stucture make future enhancements more easy.
 *
 * Technology overview:
 *    There are such things like Callforwards, CallforwardCriterias and CallforwardTargets
 *    Callforwards describes one Definition of an situation and an Target, the Situation are described by Criterias.
 *    Criterias describes the Situation in which this callforward is in active State. A Criteria for example is a DateTime, or an
 *    JabberStatus of an User. Criterias are represented by the Abstract Class AbstractCallforwardCriteria and they are especially implemented in some
 *    classes inherits from this Abstract interface. For example the CallForwardCriteria_JabberStatus.
 *    An Criteria can have Multiple Properties - repesented by the CallForwardCriteriaProperty Class - this Properties describes some States which are
 *    used in the Criteria in Detail. The Property itself helds the CallforwardCriteriaPropertyCapability Instances which holds itself the certain values.
 *
 *
 * The Basic overview over classes:
 * Class CallForwardingFactory:
 *   This is a standard Factory with static methods to get defined callforwards or all types of callforwards etc
 *
 *
 *
 *
 */


function dbg($var) {
	//	$myFile = "/var/log/phonalisa/phonalisa.log";
	//	$fh = fopen($myFile, 'a');
	//	$toast = print_r($var, true);
	//	fwrite($fh, $toast."\n");
	//	fclose($fh);
}



//callforwarding interface class, get or set callforwards for users, huntgroups or queues
class CallForwardingFactory {

	private static $DB = null;
	private static function getDb() {
		if(CallForwardingFactory::$DB != null) {
			return CallForwardingFactory::$DB;
		} else {
			CallForwardingFactory::$DB = gs_db_master_connect();
		}
		return CallForwardingFactory::$DB;
	}



	//get back all Possible Criterias:  FIXME: do this on a user level!
	public static function getAllPossibleCallForwardCriterias($parent) {

		$allCriterias = array();

		if (get_class($parent) == "QueueCallForward") {

			//$criteria = new CallForwardCriteriaPhoneStatus($parent);
			//$allCriterias[] = $criteria;

			$criteria = new CallForwardCriteriaQueueStatus($parent);
			$allCriterias[] = $criteria;


 			$criteria = new CallForwardCriteriaTimeRule($parent);
 			$allCriterias[] = $criteria;

			$criteria = new CallForwardCriteriaAddressBook($parent);
			$allCriterias[] = $criteria;

			$criteria = new CallForwardCriteriaCallSource($parent);
			$allCriterias[] = $criteria;

		} else {
			//phonestatus
                        if (PhonalisaLicenseManager::callForwardFilterPhoneStatusEnabled()) {
                            $criteria = new CallForwardCriteriaPhoneStatus($parent);
                            $allCriterias[] = $criteria;
                        }
                        if (PhonalisaLicenseManager::callForwardFilterJabberStatusEnabled()) {
                            $criteria = new CallForwardCriteriaJabberStatus($parent);
                            $allCriterias[] = $criteria;
                        }
			
                        if (PhonalisaLicenseManager::systemDateTimeProfileEnabled() && PhonalisaLicenseManager::callForwardFilterTimeRuleEnabled()) {
                            $criteria = new CallForwardCriteriaTimeRule($parent);
                            if ($criteria->available) {
                                $allCriterias[] = $criteria;
                            }
                        }
                        
                        if ( PhonalisaLicenseManager::callForwardFilterContactTypeEnabled()) {
                            $criteria = new CallForwardCriteriaAddressBook($parent);
                            $allCriterias[] = $criteria;
                        }

                        if (PhonalisaLicenseManager::callForwardFilterNightCircutEnabled()) {
                            $criteria = new CallForwardCriteriaCallNightCircut($parent);
                            $allCriterias[] = $criteria;
                        }
                        
                        if (PhonalisaLicenseManager::callForwardFilterCallSourceEnabled()) {
                            $criteria = new CallForwardCriteriaCallSource($parent);
                            $allCriterias[] = $criteria;
                        }
			//$criteria = new CallForwardCriteriaCalendar($parent);
			//$allCriterias[] = $criteria;
			

		}

		return $allCriterias;
	}

	//helper function to generate an Criteria by identifier
	public static function getCallNewCallForwardCriteriaByName($name, $parent) {


		if ($name == "PhoneStatus") {
			return new CallForwardCriteriaPhoneStatus($parent);
		} else if( $name == "QueueStatus") {
			return new CallForwardCriteriaQueueStatus($parent);
		} else if( $name == "JabberStatus") {
			return new CallForwardCriteriaJabberStatus($parent);
		} else if( $name == "TimeRule") {
			return new CallForwardCriteriaTimeRule($parent);
		} else if( $name == "AddressBook") {
			return new CallForwardCriteriaAddressBook($parent);
		} else if( $name == "Calendar") {
			return new CallForwardCriteriaCalendar($parent);
		} else if( $name == "CallSource") {
			return new CallForwardCriteriaCallSource($parent);
		} else if( $name == "NightCircutState") {
			return new CallForwardCriteriaCallNightCircut($parent);
		}
		return null;
	}


	//get back all Possible Targets: this is on a user level!
	public static function getAllPossibleCallForwardTargets(UserCallForward $user) {

		$allTargets = array();

		$criteria = new CallForwardTarget_Number($user);
		$allTargets[] = $criteria;

		$criteria = new CallForwardTarget_Voicemail($user);
		$allTargets[] = $criteria;

		$criteria = new CallForwardTarget_Announcement($user);
		$allTargets[] = $criteria;

		return $allTargets;
	}

	//get back all Possible Targets: this is on a queue level!
	public static function getAllPossibleCallForwardTargetsForQueue(QueueCallForward $user) {

		$allTargets = array();

		$criteria = new CallForwardTarget_Number($user);
		$allTargets[] = $criteria;

		$criteria = new CallForwardTarget_QueueVoicemail($user);
		$allTargets[] = $criteria;

		return $allTargets;
	}




	//turns back an JSON-Representation for the UserCallForward
	public static function getJSONRepresentationOfUserCallForward(UserCallForward $forward) {

		$callforwards = $forward->getDefinedAdvancedCallforwards();
		//print_r($callforwards);
		$json = array();
		$json["active"] = $forward->getAcitve(); //$_REQUEST['active']; //FIXME: get this back from the UserCallForward
		$json["advanced"] = array();

			
		krsort($callforwards);
		foreach ($callforwards as $callforward) {
			$json["advanced"][$callforward->getId()] = json_decode($callforward->toJSON());
		}

		$json["simple"] = array();

		$simpleForward = $forward->getDefinedSimpleCallforward();
		if ($simpleForward != null) {
			$json["simple"][] = json_decode($simpleForward->toJSON());
		}

		$json["possibleCriterias"] = array();
		$allCriterias = CallForwardingFactory::getAllPossibleCallForwardCriterias($forward);
		foreach ($allCriterias as $criteria) {
			$json["possibleCriterias"][$criteria->getCriteriaName()] = json_decode($criteria->descriptionToJSON());
		}

		$json["possibleTargets"] = array();
		$allCriterias = CallForwardingFactory::getAllPossibleCallForwardTargets($forward);

		foreach ($allCriterias as $criteria) {
			$json["possibleTargets"][] = json_decode($criteria->descriptionToJSON());
		}

		return json_encode($json);

	}

	//	turns back an JSON-Representation for the QueueCallForward
	public static function getJSONRepresentationOfQueueCallForward(QueueCallForward $forward) {

		$callforwards = $forward->getDefinedAdvancedCallforwards();
		dbg($forward);
		//print_r($callforwards);
		$json = array();
		$json["active"] = $forward->getAcitve(); //$_REQUEST['active']; //FIXME: get this back from the UserCallForward
		$json["advanced"] = array();

			
		krsort($callforwards);
		foreach ($callforwards as $callforward) {
			$json["advanced"][$callforward->getId()] = json_decode($callforward->toJSON());
		}

		$json["simple"] = array();

		$simpleForward = $forward->getDefinedSimpleCallforward();
		if ($simpleForward != null) {
			$json["simple"][] = json_decode($simpleForward->toJSON());
		}

		$json["possibleCriterias"] = array();
		$allCriterias = CallForwardingFactory::getAllPossibleCallForwardCriterias($forward);
		foreach ($allCriterias as $criteria) {
			$json["possibleCriterias"][$criteria->getCriteriaName()] = json_decode($criteria->descriptionToJSON());
		}

		$json["possibleTargets"] = array();
		$allCriterias = CallForwardingFactory::getAllPossibleCallForwardTargetsForQueue($forward);

		foreach ($allCriterias as $criteria) {
			$json["possibleTargets"][] = json_decode($criteria->descriptionToJSON());
		}

		return json_encode($json);

	}


	//Transfers an JSONRepresentation of an UserCallForward object back into the PHP Object
	public static function getUserCallForwardFromJSONRepresentation($jsonObject, $user) {

		$forward = new UserCallForward($user);

		//dbg($jsonObject);

		if ($jsonObject == NULL) {
			//create fresh object
			$jsonData = CallForwardingFactory::getJSONRepresentationOfUserCallForward($forward);
			$jsonObject = json_decode($jsonData);
		}

		//get active state
		$active = $jsonObject->active;
		if ($active != NULL) {
			$forward->setActive($active);
		} else {
			$forward->setActive("none");
		}
                
		$callforwards = (array)$jsonObject->advanced;
		$definedForwards = array();
		if (is_array($callforwards)) {
			foreach ($callforwards as $callforward) {

				if(!is_object($callforward)) {
					continue;
				}
					
				dbg($callforward);
				$type = $callforward->type;
				$id   = $callforward->id;
				$position = $callforward->position;
					
				$fw = new CallForward($forward); //set userforward as parent for this rule
				$fw->setId($id);
				$fw->setType($type);
				$fw->setPosition($position);

				//get target
				$targetDefinition = $callforward->target;
				$target = CallForwardingFactory::getCallNewCallForwardTargetByName($targetDefinition->type, $fw); //set rule as parent of target
				$targetProperties = (array) $targetDefinition->properties;
				if (is_array($targetProperties)) {
					foreach ($targetProperties as $propertyName => $propertyValue) {
						$target->setProperty($propertyName, $propertyValue);
					}
				}
				$fw->setCallForwardTarget($target);

				//get criterias
				$criteriaDefinitions = $callforward->criterias;
				foreach ($criteriaDefinitions as $criteriaDefinition) {

					$name = $criteriaDefinition->name;
					$criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName($name, $fw);
					$criteriaProperties = $criteriaDefinition->properties;
					foreach ($criteriaProperties as $propertyName => $propertyValue) {
						$criteria->setProperty($propertyName, $propertyValue);
					}
					$fw->addCriteria($criteria);
				}

				$definedForwards[] = $fw;
			}
		}
		$forward->setDefinedAdvancedCallForwards($definedForwards);
                
		$callforwards = $jsonObject->simple;
		$definedForwards = array();
		if (is_array($callforwards)) {
			foreach ($callforwards as $callforward) {
				//print_r($callforward);
				$type = $callforward->type;
				$id   = $callforward->id;

				$fw = new CallForward($forward);
				$fw->setId($id);
				$fw->setType($type);

				//get target
				$targetDefinition = $callforward->target;
				$target = CallForwardingFactory::getCallNewCallForwardTargetByName($targetDefinition->type, $fw);
				$targetProperties = $targetDefinition->properties;

				if (is_object($targetProperties)) {
					foreach ($targetProperties as $propertyName => $propertyValue) {
						$target->setProperty($propertyName, $propertyValue);
					}
					$fw->setCallForwardTarget($target);
				}

				//get criterias
				$criteriaDefinitions = $callforward->criterias;
				if (is_object($criteriaDefinitions)) {
					foreach ($criteriaDefinitions as $criteriaDefinition) {

						$name = $criteriaDefinition->name;
						$criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName($name);
						$criteriaProperties = $criteriaDefinition->properties;
						foreach ($criteriaProperties as $propertyName => $propertyValue) {
							$criteria->setProperty($propertyName, $propertyValue);
						}
						$fw->addCriteria($criteria);
					}
				}

				$definedForwards[] = $fw;
			}
		}
		if (@$definedForwards[0] != NULL) {
			$forward->setDefinedSimpleCallForward($definedForwards[0]);
		}

		return $forward;
	}

	//Transfers an JSONRepresentation of an UserCallForward object back into the PHP Object
	public static function getQueueCallForwardFromJSONRepresentation($jsonObject, $queue) {

		$forward = new QueueCallForward($queue);
		if ($jsonObject == NULL) {
			//create fresh object
			$jsonObject = CallForwardingFactory::getJSONRepresentationOfQueueCallForward($forward);
		}

		//get active state
		$active = $jsonObject->active;
		if ($active != NULL) {
			$forward->setActive($active);
		} else {
			$forward->setActive("none");
		}

		$callforwards = (array)$jsonObject->advanced;
		$definedForwards = array();
		if (is_array($callforwards)) {
			foreach ($callforwards as $callforward) {

				if(!is_object($callforward)) {
					continue;
				}
					
				dbg($callforward);
				$type = $callforward->type;
				$id   = $callforward->id;
				$position = $callforward->position;
					
				$fw = new CallForward($forward); //set userforward as parent for this rule
				$fw->setId($id);
				$fw->setType($type);
				$fw->setPosition($position);

				//get target
				$targetDefinition = $callforward->target;
				$target = CallForwardingFactory::getCallNewCallForwardTargetByName($targetDefinition->type, $fw); //set rule as parent of target
				$targetProperties = (array) $targetDefinition->properties;
				if (is_array($targetProperties)) {
					foreach ($targetProperties as $propertyName => $propertyValue) {
						$target->setProperty($propertyName, $propertyValue);
					}
				}
				$fw->setCallForwardTarget($target);

				//get criterias
				$criteriaDefinitions = $callforward->criterias;
				foreach ($criteriaDefinitions as $criteriaDefinition) {

					$name = $criteriaDefinition->name;
					$criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName($name, $fw);
					$criteriaProperties = $criteriaDefinition->properties;
					foreach ($criteriaProperties as $propertyName => $propertyValue) {
						$criteria->setProperty($propertyName, $propertyValue);
					}
					$fw->addCriteria($criteria);
				}

				$definedForwards[] = $fw;
			}
		}
		$forward->setDefinedAdvancedCallForwards($definedForwards);
		$callforwards = $jsonObject->simple;

		$definedForwards = array();
		if (is_array($callforwards)) {
			foreach ($callforwards as $callforward) {
				//print_r($callforward);
				$type = $callforward->type;
				$id   = $callforward->id;

				$fw = new CallForward($forward);
				$fw->setId($id);
				$fw->setType($type);

				//get target
				$targetDefinition = $callforward->target;
				$target = CallForwardingFactory::getCallNewCallForwardTargetByName($targetDefinition->type, $fw);
				$targetProperties = $targetDefinition->properties;

				if (is_object($targetProperties)) {
					foreach ($targetProperties as $propertyName => $propertyValue) {
						$target->setProperty($propertyName, $propertyValue);
					}
					$fw->setCallForwardTarget($target);
				}

				//get criterias
				$criteriaDefinitions = $callforward->criterias;
				if (is_object($criteriaDefinitions)) {
					foreach ($criteriaDefinitions as $criteriaDefinition) {

						$name = $criteriaDefinition->name;
						$criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName($name);
						$criteriaProperties = $criteriaDefinition->properties;
						foreach ($criteriaProperties as $propertyName => $propertyValue) {
							$criteria->setProperty($propertyName, $propertyValue);
						}
						$fw->addCriteria($criteria);
					}
				}

				$definedForwards[] = $fw;
			}
		}
		if (@$definedForwards[0] != NULL) {
			$forward->setDefinedSimpleCallForward($definedForwards[0]);
		}
		return $forward;
	}


	//saves UserCallForward Object for the given user, writes the Database
	public static function setUserForwardForUser(UserCallForward $forward,User $user) {
		// save the Callforward as JSON Representation into the database!
		$contents = CallForwardingFactory::getJSONRepresentationOfUserCallForward($forward);

		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("users_callforwarding");
		$queryBuilder->addField('_user_id', '');
		$queryBuilder->setType(QueryType::TypeSelect);
		$queryBuilder->overwriteWhere("`_user_id` = ".$user->getId());
		$id = CallForwardingFactory::getDb()->executeGetOne($queryBuilder->getQueryString());
		$QueryType = QueryType::TypeInsert;
		if ($id != '') {
			$QueryType = QueryType::TypeUpdate;
		}

		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("users_callforwarding");
		$queryBuilder->setType($QueryType);
		$queryBuilder->overwriteWhere("`_user_id` = ".$user->getId());
		$queryBuilder->addField('json', $contents);
		$queryBuilder->addField('_user_id', $user->getId());
		$res = CallForwardingFactory::getDb()->execute($queryBuilder->getQueryString());

		@InitHinttoggleCall($user);
		
		//$rs = CallForwardingFactory::getDb()->execute("INSERT `INTO` `callforwarding` (".$user->getId().", '". CallForwardingFactory::getDb()->escape($contents)."'");

		//file_put_contents("/tmp/forward_".$user->getId(), $contents);

	}

	//saves UserCallForward Object for the given user, writes the Database
	public static function setQueueForwardForQueue(QueueCallForward $forward,Queue $user) {
		// save the Callforward as JSON Representation into the database!
		$contents = CallForwardingFactory::getJSONRepresentationOfQueueCallForward($forward);

		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("queues_callforwarding");
		$queryBuilder->addField('_queue_id', '');
		$queryBuilder->setType(QueryType::TypeSelect);
		$queryBuilder->overwriteWhere("`_queue_id` = ".$user->getId());
		$id = CallForwardingFactory::getDb()->executeGetOne($queryBuilder->getQueryString());
		$QueryType = QueryType::TypeInsert;
		if ($id != '') {
			$QueryType = QueryType::TypeUpdate;
		}

		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("queues_callforwarding");
		$queryBuilder->setType($QueryType);
		$queryBuilder->overwriteWhere("`_queue_id` = ".$user->getId());
		$queryBuilder->addField('json', $contents);
		$queryBuilder->addField('_queue_id', $user->getId());
		$res = CallForwardingFactory::getDb()->execute($queryBuilder->getQueryString());
			
		//$rs = CallForwardingFactory::getDb()->execute("INSERT `INTO` `callforwarding` (".$user->getId().", '". CallForwardingFactory::getDb()->escape($contents)."'");

		//file_put_contents("/tmp/forward_".$user->getId(), $contents);

	}


/*
	//returns the UserCallForward Object for the given user, reads the database
	public static function getUserForwardForUser(User $user) {
		//load (JSON-Representation) from database and make an object
		$forward = new UserCallForward($user);

		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("users_callforwarding");
		$queryBuilder->addField('json', '');
		$queryBuilder->setType(QueryType::TypeSelect);
		$queryBuilder->overwriteWhere("`_user_id` = ".$user->getId());
		$json = CallForwardingFactory::getDb()->executeGetOne($queryBuilder->getQueryString());

		//$json = file_get_contents("/tmp/forward_".$user->getId());
		$jsonObject = json_decode($json);
		$callforward = CallForwardingFactory::getUserCallForwardFromJSONRepresentation($jsonObject, $user);
			
		//		$forward->setDefinedCallForwards(CallForwardingFactory::getDefinedForwardsForUser($user));
		//$forward->setActive("simple"); //FIXME: Read the Database!

		return $callforward;

	}
*/
	public static function getQueueForwardForQueue(Queue $queue) {
		//load (JSON-Representation) from database and make an object
		$queryBuilder = new QueryBuilder();
		$queryBuilder->setTable("queues_callforwarding");
		$queryBuilder->addField('json', '');
		$queryBuilder->setType(QueryType::TypeSelect);
		$queryBuilder->overwriteWhere("`_queue_id` = ".$queue->getId());
		$json = CallForwardingFactory::getDb()->executeGetOne($queryBuilder->getQueryString());

		//$json = file_get_contents("/tmp/forward_".$user->getId());
		$jsonObject = json_decode($json);

		$callforward = CallForwardingFactory::getQueueCallForwardFromJSONRepresentation($jsonObject, $queue);

		//		$forward->setDefinedCallForwards(CallForwardingFactory::getDefinedForwardsForUser($user));
		//$forward->setActive("simple"); //FIXME: Read the Database!

		return $callforward;

	}

	//helper function to generate an Target by identifier
	public static function getCallNewCallForwardTargetByName($name, $parent) {

		if ($name == "number") {
			return new CallForwardTarget_Number($parent);
		} else if( $name == "voicemail") {
			return new CallForwardTarget_Voicemail($parent);
		} else if( $name == "queuevoicemail") {
			return new CallForwardTarget_QueueVoicemail($parent);
		}else if( $name == "announcement") {
			return new CallForwardTarget_Announcement($parent);
		}

		return null;
	}


        public static function getDefaultCriteria($parent) 
        {
            if (PhonalisaLicenseManager::callForwardFilterPhoneStatusEnabled()) {
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("PhoneStatus", $parent);
                $criteria->setProperty("phonestatus","offline"); 
                return $criteria;
            } elseif (PhonalisaLicenseManager::callForwardFilterJabberStatusEnabled()) {
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("JabberStatus", $parent);
                $criteria->setProperty("jabberstatus","offline"); 
                return $criteria;
            }elseif (PhonalisaLicenseManager::callForwardFilterTimeRuleEnabled() && PhonalisaLicenseManager::systemDateTimeProfileEnabled()) {
                
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("TimeRule", $parent);
                $timeRules = $criteria->getTimeProfiles();
                $timeRule = $timeRules[0];
                $criteria->setProperty("timerule",$timeRule->id);
                return $criteria;
            } elseif (PhonalisaLicenseManager::callForwardFilterContactTypeEnabled()) {
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("AddressBook", $parent);
                $criteria->setProperty("is_or_is_not",true);
                $addressbooks = $criteria->getAvialableAddressBooks();
                foreach ($addressbooks as $book) {
                    $criteria->setProperty("addressbookId",$book->id);
                    break;
                }
                $criteria->setProperty("addressbook",  DefaultContactType::none);
                return $criteria;
            } elseif (PhonalisaLicenseManager::callForwardFilterNightCircutEnabled()) {
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("NightCircutState", $parent);
                $groups = $criteria->getAvialableGroups();
                $group = $groups[0];
                $criteria->setProperty("groupId", $group->id);
                return $criteria;
            } elseif (PhonalisaLicenseManager::callForwardFilterCallSourceEnabled()) {
                $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("CallSource", $parent);
                $criteria->setProperty("callsource", CallForwardCriteriaCallSourceType::Internal);
                return $criteria;
            }
            //FIXME need this for simple Callforwardings !? 
            $criteria = CallForwardingFactory::getCallNewCallForwardCriteriaByName("PhoneStatus", $parent);
            $criteria->setProperty("phonestatus","offline"); 
            return $criteria;
        }

}