<?php

//require_once 'PhoneModelFactory.php' ;


abstract class AbstractXMLApp
{
	protected $mController;
	protected $mPhone = null;
	protected $mSipAccount = null;
	protected $mUser = null;
	
	public $mMacAddress;
	
	public function getPhoneModel() {

		$phone = Phones::model()->findByAttributes(array("mac_addr" => $this->mMacAddress));
		return $phone;
	}
	
	
	public function appURL($params = array()) {
 
        $request = app()->request;
        $appUrl = $request->getHostInfo().'/'.$request->getPathInfo();

        $firstParam = true; 
        foreach ($params as $key => $value) {
            if ($firstParam) {
                $appUrl .= "?".$key.'='.$value;
                $firstParam = false;
            } else {
                $appUrl .= "&".$key.'='.$value;
            }
        }
 		return $appUrl;
	}
	
	
	public function run($controller)
	{
		$mController = $controller;

		$this->mPhone = $this->getPhoneModel();

		if ($this->mPhone != null) {
			$this->mSipAccount = $this->mPhone->getSipAccount();
                        if ($this->mSipAccount) {
                            if($this->mSipAccount->getUser()!=null){
                                $this->mUser = $this->mSipAccount->getUser();
				Yii::app()->user->setId($this->mUser->id);
				Yii::app()->user->setState('sudoUser', $this->mUser->id);
                           }
                        }
                }
	}
	
		
}
