<?php
/*
 * @todo. riscrivere la classe completamente
 */
class User {
	var $uid;
	var $rid;
	var $locale;
	var $email;
	var $password;
	var $firstname;
	var $lastname;
	var $phone;
	var $created;
	var $access;
	var $login;
	var $status;
	var $session_id;
	
	const USER_ROLE_ADMINISTRATOR 	= 1;
	const USER_ROLE_STANDARD 		= 2;
	const USER_ROLE_READONLY 		= 3;
	
	function user($uid = 0) {
		if ($uid != 0) {
			try {
				$this->load ( $uid );
			} catch ( Exception $ex ) {
				// log l'eccezione...
			}
		}
	}
	
	
	function load($uid) {
		$con = new Connection ();
		$sql = "SELECT * FROM users  " . "WHERE uid = " . $uid;
		
		// esegue la query...
		$rs = $con->selectOneRow ( $sql );
		
		// if user exists
		if (is_array ( $rs ) && ! empty ( $rs )) 
		{ 
			$this->uid 			= $rs ['uid'];
			$this->rid 			= $rs ['rid'];
			$this->locale		= $rs ['locale'];
			$this->email 		= $rs ['email'];
			$this->password 	= $rs ['password'];
			$this->firstname 	= $rs ['firstname'];
			$this->lastname 	= $rs ['lastname'];
			$this->phone 		= $rs ['phone'];
			$this->created 		= $rs ['created'];
			$this->access 		= $rs ['access'];
			$this->login 		= $rs ['login'];
			$this->status 		= $rs ['status'];
			$this->session_id	= session_id();
		}
		
		$con->close ();
		return $this;
	}
	
	
	/**
	 * check login given email and password
	 * 
	 * @param string $email        	
	 * @param string $password        	
	 * @return boolean
	 */
	function check($email, $password) {
		if (empty ( $this->email ) === false && $email == $this->email && md5 ( $password ) == $this->password) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
	
	// @TODO: verify this...
	/**
	 * verify if user is logged in 
	 * 
	 * @param string $sid user session id
	 * 
	 * @return boolean
	 */
	static public function isUserLogged($sid = null) {
		
		$bRV = false;
		if(is_null($sid)){
			$sid = session_id();
		}
		
		// there's a uid saved into session variable and is not empty
		if (is_array ( $_SESSION ) && ! empty ( $_SESSION ['uid'] ) && ! empty ( $_SESSION ['session_id'] )) {
			// get the current session id and match with the one saved into SESSION array
			
			if ($_SESSION ['session_id'] === $sid) {
				$bRV = true;
			}
		}
		
		return $bRV;
	}
	
	function save() {
		// d ( $this );
		
		if (empty ( $this->uid )) {
			return $this->insert ();
		} else {
			return $this->update ();
		}
	}
	
	
	function insert() {
		$bRV = false;
		
		$con = new Connection ();
		
		$sql = "INSERT into users 
	    			(
	    			rid,
	    			locale,
	    			email,
	    			password,
	    			firstname,
	    			lastname,
	    			phone,
	    			created,
	    			access,
	    			login,
	    			status
	    			)values(" 
						. "'" . $this->rid . "', " 
						. "'" . $this->locale . "', " 
						. "'" . $this->email . "', " 
						. "'" . $this->password . "', " 
						. "'" . $this->firstname . "', " 
						. "'" . $this->lastname . "', " 
						. "'" . $this->phone . "', " 
						. "UNIX_TIMESTAMP(), " 
						. "0, " 
						. "0, " 
						. "1" 
						. ")";
		
		
		$rs = $con->execute ( $sql );
		if (! empty ( $rs )) {
			$this->uid = $con->insertID ();
			$con->commit ();
			
			$bRV = true;
		}
		$con->close ();
		return $bRV;
	}
	
	
	function update() {
		$bRV = false;
		$con = new Connection ();
		
		$sql = "UPDATE users set " 
						. "rid = " . $this->rid . ", " 
						. "locale = '" . $this->locale . "', " 
						. "email = '" . $this->email . "', " 
						. "password ='" . $this->password . "', " 
						. "firstname = '" . $this->firstname . "', " 
						. "lastname = '" . $this->lastname . "', " 
						. "phone ='" . $this->phone . "', " 
						. "created = " . $this->created . ", " 
						. "access = " . $this->access . ", " 
						. "login = " . $this->login . ", " 
						. "status = " . $this->status . " " 
						. "WHERE uid = " . $this->uid;
		
		// d($sql);
		
		$rs = $con->execute ( $sql );
		
		if (! empty ( $rs )) {
			$con->commit();
			$bRV = true;
		}
		$con->close ();
		return $bRV;
	}
	
	
	
	function delete() {
		$bRV = false;
		$con = new Connection ();
		
		$sql = "DELETE FROM users WHERE uid = " . $this->uid;
		
		// d($sql);
		
		$rs = $con->execute ( $sql );
		if (! empty ( $rs )) {
			$con->commit ();
			$bRV = true;
		}
		$con->close ();
		return $bRV;
	}
	
	
	function changeStatus() {
		$bRV = false;
		$con = new Connection ();
		
		$sSQL = "UPDATE users SET status = ABS(status - 1) WHERE uid='" . $this->uid . "'";
		
		$rs = $con->Execute ( $sSQL );
		if (! empty ( $rs )) {
			$bRV = true;
		} else {
			$bRV = false;
		}
		
		return $bRV;
	}
}
;

	
