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

class TaskEdit 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/task_edit", $this->current_user->getLanguage());
		
		// default parameters
		$this->loadParams("task_id");
		$this->task_id = $this->getParam("task_id");
	}
	
	function onGet() {
		if (util_empty($this->task_id)) {
			// new task
			$this->setParam("assigned_to_user_id", $this->current_user->get("id"));
			$this->setParam("priority", "normal");
			$this->setParam("status", "pending");
		} else {
			// edit task
			$t = new ActiveRecord($this->db, "task", $this->task_id);
			$this->setParam("title", $t->get("title"));
			$this->setParam("project_id", $t->get("project_id"));
			$this->setParam("assigned_to_user_id", $t->get("assigned_to_user_id"));
			$this->setParam("priority", $t->get("priority"));
			$this->setParam("status", $t->get("status"));
			$this->setParam("description", $t->get("description"));
		}
	}
	
	function onPost() {
		if ($this->getAction() == "delete_attached_file") {
			if (!util_empty($this->task_id)) {
				$t = new ActiveRecord($this->db, "task", $this->task_id);
				
				// delete the attached file
				$error = NULL;
				file_unlink($t->get("attached_file"), $error);
				$t->set("attached_file", NULL);
				$t->post();
			}
		} else {
			// parameters
			$title = $this->getParam("title");
			$project_id = $this->getParam("project_id");
			$assigned_to_user_id = $this->getParam("assigned_to_user_id");
			$priority = $this->getParam("priority");
			$status = $this->getParam("status");
			$description = $this->getParam("description");
			
			$filename = $_FILES["file"]["name"];
			$absolute_filename = file_concat(DATA_FOLDER, file_unix_filename($filename));
			
			// required fields
			if (util_empty($title) || util_empty($project_id) || util_empty($assigned_to_user_id) || util_empty($priority) || util_empty($status) || util_empty($description)) {
				$this->setError("error", $this->getRawMessage("required_fields"));
				return;
			}
			
			// insert or edit the task
			$t = new ActiveRecord($this->db, "task", $this->task_id);
			$t->set("title", $title);
			$t->set("project_id", $project_id);
			$t->set("created_by_user_id", $this->current_user->get("id"));
			$t->set("assigned_to_user_id", $assigned_to_user_id);
			$t->set("priority", $priority);
			$t->set("description", $description);
			$t->set("status", $status);
			$t->set("attached_file", util_empty($filename)? NULL: $absolute_filename);
			$t->post();
			
			// upload the attached file and remove the previous one
			if (!util_empty($filename)) {
				// try to unlink the previous attached file
				if (!util_empty($this->task_id) && !util_empty($t->get("attached_file"))) {
					$error = NULL;
					file_unlink($t->get("attached_file"), $error);
				}
				
				// upload the attached file
				if (!move_uploaded_file($_FILES["file"]["tmp_name"], $absolute_filename)) {
					$this->setError("error", $this->getRawMessage("unable_to_upload_file"));
					return;
				}
			}
			
			// go back to task.php
			$this->redirect("task.php", array("order" => "updated", "sense" => "desc", "filter[project_id]" => $project_id));
		}
	}
	
	function getUsers() {
		return Util::getUsers($this->db);
	}
	
	function getProjects() {
		return Util::getProjects($this->db);
	}
	
	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 getTitle() {
		if (util_empty($this->task_id))
			return $this->getMessage("insert_new_task");
		return $this->getMessage("edit_task");
	}
	
	function getAttachedFile() {
		if (util_empty($this->task_id))
			return NULL;
		
		$t = new ActiveRecord($this->db, "task", $this->task_id);
		return $t->get("attached_file");
	}
}
?>
Return current item: Todo Manager