Location: PHPKode > projects > Todo Manager > todo/controllers/project_edit.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."/classes/util.class.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/model/user.class.php";

class ProjectEdit extends Controller {
	var $db;
	var $auth;
	var $current_user;

	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->current_user = new User($this->db, $this->auth->getUserId());
		
		// messages
		$this->loadMessages("messages/generic", $this->current_user->getLanguage());
		$this->loadMessages("messages/project_edit", $this->current_user->getLanguage());
		
		// default parameters
		$this->loadParams("project_id");
		$this->project_id = $this->getParam("project_id");
	}
	
	function onGet() {
		if (util_empty($this->project_id)) {
			// new task
			$this->setParam("status", "open");
			$this->setParam("from_user_id", $this->getAvailableUsers());
		} else {
			// edit task
			$t = new ActiveRecord($this->db, "project", $this->project_id);
			$this->setParam("title", $t->get("title"));
			$this->setParam("status", $t->get("status"));
			$this->setParam("description", $t->get("description"));
			$this->setParam("from_user_id", $this->getAvailableUsers());
			$this->setParam("to_user_id", $this->getNotAvailableUsers());
		}
	}
	
	function onPost() {
		// parameters
		$title = $this->getParam("title");
		$status = $this->getParam("status");
		$description = $this->getParam("description");
		$to_user_ids = $this->getParam("to_user_id:array");
		
		// required fields
		if (util_empty($title) || util_empty($status) || util_empty($description)) {
			$this->setError("error", $this->getRawMessage("required_fields"));
			return;
		}
		
		// insert or edit the task
		$t = new ActiveRecord($this->db, "project", $this->project_id);
		$t->set("title", $title);
		$t->set("status", $status);
		$t->set("description", $description);
		$t->post();
		
		// delete
		$sql = "
		delete
		from project_vs_user
		where project_id = ".$this->db->quote($t->get("id"));
		$this->db->execute($sql);
		
		// insert
		foreach($to_user_ids as $user_id) {
			$u = new ActiveRecord($this->db, "project_vs_user");
			$u->set("project_id", $t->get("id"));
			$u->set("user_id", $user_id);
			$u->post();
		}
		
		// go back to task.php
		$this->redirect("project.php", array("order" => "updated", "sense" => "desc"));
	}
	
	function getLoginName() {
		$user_id = $this->auth->getUserId();
		$t = new ActiveRecord($this->db, "user", $user_id);
		return $t->get("username");
	}
	
	function getCurrentUser() {
		return $this->current_user;
	}
	
	function getAvailableUsers() {
		$ret = array();
		$sql = NULL;
		if (util_empty($this->project_id)) {
			$sql = "
			select
				id,
				username
			from user
			order by username";
		} else {
			$sql = "
			select
				u.id,
				u.username
			from user u
			left join project_vs_user pu
				on pu.project_id = ".$this->db->quote($this->project_id)."
				and pu.user_id = u.id
			where pu.id is null
			order by u.username";
		}
		$result = $this->db->getResult($sql);
		while ($row = $this->db->getRow($result))
			array_push($ret, $row["id"]);
		
		return $ret;
	}
	
	function getNotAvailableUsers() {
		$ret = array();
		$sql = "
		select
			u.id
		from user u
		inner join project_vs_user pu
			on pu.project_id = ".$this->db->quote($this->project_id)."
			and pu.user_id = u.id
		order by u.username";
		$result = $this->db->getResult($sql);
		while ($row = $this->db->getRow($result))
			array_push($ret, $row["id"]);
		
		return $ret;
	}
	
	function getUsers($user_ids) {
		$ret = array();
		if (!is_array($user_ids))
			return $ret;
		
		foreach($user_ids as $user_id) {
			$t = new ActiveRecord($this->db, "user", $user_id);
			array_push($ret, array("id" => $t->get("id"), "username" => $t->get("username")));
		}
		return $ret;
	}
	
	function getTitle() {
		if (util_empty($this->project_id))
			return $this->getMessage("insert_new_project");
		return $this->getMessage("edit_project");
	}
}
?>
Return current item: Todo Manager