Location: PHPKode > projects > Todo Manager > todo/controllers/profile.class.php
<?php
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/controller.class.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/database.class.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/activerecord.class.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/authentication.class.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/model/user.class.php";

class Profile extends Controller {
	var $db;
	var $auth;
	var $current_user;
	var $user_id;

	function init() {
		// database connection
		$this->db = new Database(DATABASE_NAME, USER_NAME, PASSWORD);
		
		// authentication
		$this->auth = new Authentication($this->db);
		if (!$this->auth->isLogged())
			$this->redirect("login.php");
			
		// current user
		$this->user_id = $this->auth->getUserId();
		$this->current_user = new User($this->db, $this->auth->getUserId());
		
		// messages
		$this->loadMessages("messages/generic", $this->current_user->getLanguage());
		$this->loadMessages("messages/profile", $this->current_user->getLanguage());
	}
	
	function onGet() {
		$t = new ActiveRecord($this->db, "user", $this->user_id);
		$this->setParam("username", $t->get("username"));
		$this->setParam("email", $t->get("email"));
		$this->setParam("full_name", $t->get("full_name"));
		$this->setParam("receive_notifications", $t->get("receive_notifications"));
		$this->setParam("description", $t->get("description"));
		$this->setParam("language", $this->current_user->getLanguage());
	}
	
	function onPost() {
		// parameters
		$email = $this->getParam("email");
		$full_name = $this->getParam("full_name");
		$password = $this->getParam("password");
		$re_password = $this->getParam("re_password");
		$receive_notifications = $this->getParam("receive_notifications");
		$description = $this->getParam("description");
		$language = $this->getParam("language");
		
		// required fields
		if (util_empty($full_name) || util_empty($email) || util_empty($description) || util_empty($language)) {
			$this->setError("error", $this->getRawMessage("required_fields"));
			return;
		}
		
		// verify the password
		if (!util_empty($password) && ($password != $re_password)) {
			$this->setError("error", $this->getRawMessage("reenter_password"));
			return;
		}
		
		// edit the curren user
		$this->current_user->set("email", $email);
		$this->current_user->set("full_name", $full_name);
		$this->current_user->set("password", util_empty($password)? $this->current_user->get("password"): md5($password));
		$this->current_user->set("receive_notifications", util_empty($receive_notifications)? "no": $receive_notifications);
		$this->current_user->set("description", $description);
		$this->current_user->setLanguage($language);
		$this->current_user->post();
		
		// messages
		$this->loadMessages("messages/generic", $this->current_user->getLanguage());
		$this->loadMessages("messages/profile", $this->current_user->getLanguage());
		
		// message
		$this->setMessage("ok", $this->getMessage("the_changes_has_been_saved"));
	}
	
	function getCurrentUser() {
		return $this->current_user;
	}
}
?>
Return current item: Todo Manager