Source

myswap / 1 / code / App / Lib / Action / EmptyAction.class.php

<?php

Class EmptyAction extends Action{ 
    public function _empty()
    {
		$this->setSettings();
		$this->assign('swap',$this->getSwapModel(ACTION_NAME));
		$this->display('Public/single_swap');
    }
    

	public function setSettings(){
		$settings=$this->getUserModel();
		$this->assign('settings',$settings);
	}
	public function getUserModel($username=0){ 
			if (is_numeric(MODULE_NAME)){
				$User=D("User");
				$user=$User->getById(MODULE_NAME);
				return $user[0];
			}else{
				$User=D("User");
				if($username){
					$user=$User->getByUsername($username);
				}else{
					$user=$User->getByUsername(MODULE_NAME);
				}
				return $user[0];
			}
	}
	public function getSwapModel($alias){ 
			if (is_numeric($alias)){
					$Swap=D("Swap");
					$swap=$Swap->getById($alias);
					return $swap[0];
			}else{
				$Swap=D("Swap");
				$swap=$Swap->getByAlias($this->getUserModel(),$alias);
				return $swap[0];
			}
	}
	public function getPublicState(){
		$user=$this->getUserModel();
		return $user["public_state"];
	}
	public function getUserID(){ 
			if (is_numeric(MODULE_NAME)){
				return MODULE_NAME;
			}else{
				$user=$this->getUserModel();
				return $user["user_id"];
			}
	}


    public function index()
    {
		$this->setSettings();
		$this->outputSwap($this->getUserID());
    }
    public function getFriends()
    {
		$settings=$this->getUserModel();
		$friends=preg_split("/\n/", $settings["friends"]);
		return $friends;
    }
    // public function getAliases()
    // {
		// $settings=$this->getUserModel();
		// $aliases=preg_split("/\n/", $settings["aliases"]);
		// return $aliases;
    // }
    public function goHome()
    {
		// header("Location: http://".$_SERVER["HTTP_HOST"]."/".MODULE_NAME."/");
		$this->redirect('index', array(),0,'');
    }
    public function outputSwap($user_id)
    {
    	$Swap=D("Swap");
    	$SWAPS=$Swap->getSwaps($user_id);

		$this->assign('friends',$this->getFriends());
		// $this->assign('aliases',$this->getAliases());
		$this->assign('SWAPS',$SWAPS);
		$this->display('Public/swap');
    }
    public function setting()
	{    	
		if($this->authWithPassword($_POST["password"])){
		//成功修改资料
			$User=M("User");
			$User->find($this->getUserID()); // 查找主键为1的数据
//			if($_POST["public_state"]=="on"){
//				$User->public_state=1;
//			}else{
//				$User->public_state=0;
//			}
			// if($_POST["aliases"]){
				// $User->aliases=trim($_POST["aliases"]);
				// $User->save();
			// }
			if($_POST["friends"]){
				$User->friends=trim($_POST["friends"]);
			}

			if($_POST["username"]){
				$User->username=$_POST["username"];
				$User->save();
				// echo $User->getLastSql();
				$settings=$this->getUserModel($_POST["username"]);
			}else{
				$User->save();
				$settings=$this->getUserModel();
			}


			$this->assign('msg_success',"saved");
			$this->assign('settings',$settings);
			$this->display('Public/setting');
		}else if($_POST["password"]){
			$this->assign('msg_error',"wrong password");
			$this->setSettings();
			$this->display('Public/setting');
		}else{
			$this->setSettings();
			$this->display('Public/setting');
		}

    }
    public function auth()
	{    	
		$this->setSettings();
		if($this->authWithPassword($_POST["password"])){
			$this->index(1);
		}else if($_POST["password"]){
			$this->assign('msg_error',"wrong password");
			$this->display('Public/auth');
		}else{
			$this->display('Public/auth');
		}
    }
    public function authWithPassword($password)
	{    	
		$settings=$this->getUserModel();
		if($password==$settings["password"]){
			return (1);
		}else{
			return (0);
		}
    }
	// public function swap()
    // {
		// $Swap=M("Swap");
		// $Swap->find($_GET["id"]);
		// echo '<textarea cols="160"   rows="40" >'.htmlspecialchars($Swap->content).'</textarea>';
    // }
    public function addSwap()
	{
		if($this->authWithPassword($_POST["password"])){
			$content=$_POST["content"];
			if($content){
				$Swap=D("Swap");
				$Swap->user_id=$this->getUserID();
				$Swap->content=$content;
				$Swap->alias=$_POST["alias"];
				$Swap->add();
				$this->goHome();
			}
		}else if($_POST["password"]){
			$this->assign("msg_error","wrong password");
			$this->setSettings();
			$this->display('Public/swap');
		}
	}
}

?>