Location: PHPKode > projects > Todo Manager > todo/controllers/task.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."/model/user.class.php";

class Task extends Controller {
	var $num_rows_per_page = 30;
	var $db;
	var $auth;
	var $current_user;
	var $page;
	var $order;
	var $sense;
	var $created_by_user_id;
	var $assigned_to_user_id;
	var $project_id;
	var $priority;
	var $status;
	var $filter;
	var $filter_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->current_user = new User($this->db, $this->auth->getUserId());
		
		// messages
		$this->loadMessages("messages/generic", $this->current_user->getLanguage());
		$this->loadMessages("messages/task", $this->current_user->getLanguage());
		
		// default parameters
		$this->loadParams("page:int=0", "order=date", "sense=desc", "filter:array");
		$this->page = $this->getParam("page");
		$this->order = $this->getParam("order");
		$this->sense = $this->getParam("sense");
		$this->filter = $this->getParam("filter");
	}
	
	function onPost() {
		// parameters
		$item_ids = $this->getParam("item_id:array");
		
		if ($this->getAction() == "first_page") {
			$this->setParam("page", 0);
		} else
		if ($this->getAction() == "prior_page") {
			$this->setParam("page", max(0, $this->page - 1));
		} else
		if ($this->getAction() == "next_page") {
			$this->setParam("page", min($this->getMaxPage(), $this->page + 1));
		} else
		if ($this->getAction() == "last_page") {
			$this->setParam("page", $this->getMaxPage());
		} else
		if ($this->getAction() == "order_by") {
			$this->setParam("order", $this->getValue("column"));
			$this->setParam("sense", $this->getValue("sense"));
			$this->setParam("page", 0);
		} else
		if ($this->getAction() == "filter_by") {
			$column = $this->getValue("column");
			$id = $this->getValue("id");
			
			$keys = array_keys($this->filter);
			$key = array_search($column, $keys);
			if (array_key_exists($column, $this->filter) && util_empty($id))
				array_splice($this->filter, $key, 1);
			else
				$this->filter[$column] = $id;
			
			$this->setParam("filter", $this->filter);
			$this->setParam("page", 0);
		} else
		if ($this->getAction() == "done") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "task", $item_id);
				$t->set("status", "done");
				$t->post();
			}
		} else
		if ($this->getAction() == "pending") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "task", $item_id);
				$t->set("status", "pending");
				$t->post();
			}
		} else
		if ($this->getAction() == "discard") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "task", $item_id);
				$t->set("status", "discard");
				$t->post();
			}
		} else
		if ($this->getAction() == "delete") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "task", $item_id);
				$t->delete();
			}
			$this->setParam("page", min($this->getMaxPage(), $this->page));
		} else
		if ($this->getAction() == "delete_item") {
			$item_id = $this->getValue();
			$t = new ActiveRecord($this->db, "task", $item_id);
			$t->delete();
			$this->setParam("page", min($this->getMaxPage(), $this->page));
		}
	}
	
	function getFilterId($name) {
		return util_getfield($this->filter, $name);
	}
	
	function getRelativeSense($order) {
		if (($this->order == $order) && ($this->sense == "asc"))
			return "desc";
		return "asc";
	}
	
	function getStringSense($order) {
		if ($this->order == $order)
			return $this->sense == "asc"? "(a..z)": "(z..a)";
		return NULL;
	}
	
	function getFilter() {
		$filter = NULL;
		$created_by_user_id = $this->getFilterId("created_by_user_id");
		$assigned_to_user_id = $this->getFilterId("assigned_to_user_id");
		$project_id = $this->getFilterId("project_id");
		$priority = $this->getFilterId("priority");
		$status = $this->getFilterId("status");
		
		if (!util_empty($created_by_user_id)) {
			$filter = util_empty($filter)? "where ": $filter."\nand ";
			$filter .= "t.created_by_user_id = ".$this->db->quote($created_by_user_id);
		}
		
		if (!util_empty($assigned_to_user_id)) {
			$filter = util_empty($filter)? "where ": $filter."\nand ";
			$filter .= "t.assigned_to_user_id = ".$this->db->quote($assigned_to_user_id);
		}
		
		if (!util_empty($project_id)) {
			$filter = util_empty($filter)? "where ": $filter."\nand ";
			$filter .= "t.project_id = ".$this->db->quote($project_id);
		}
		
		if (!util_empty($priority)) {
			$filter = util_empty($filter)? "where ": $filter."\nand ";
			$filter .= "t.priority = ".$this->db->quote($priority);
		}
		
		if (!util_empty($status)) {
			$filter = util_empty($filter)? "where ": $filter."\nand ";
			$filter .= "t.status = ".$this->db->quote($status);
		}
		
		return $filter;
	}
	
	function getPage() {
		return $this->page;
	}
	
	function getMaxPage() {
		$sql = "
		select
			count(*) as num_rows
		from task t
		inner join user u1
			on u1.id = t.created_by_user_id
		inner join user u2
			on u2.id = t.assigned_to_user_id
		inner join project p
			on p.id = t.project_id
		".$this->getFilter();
		$result = $this->db->getResult($sql);
		$row = $this->db->getRow($result);
		return max(0, floor(($row["num_rows"] - 1) / $this->num_rows_per_page));
	}
	
	function getRows() {
		$ret = array();
		
		// order
		$order = NULL;
		if ($this->order == "updated")
			$order = "t.updated_on";
		else
		if ($this->order == "date")
			$order = "t.created_on";
		else
		if ($this->order == "task")
			$order = "t.title";
		else
		if ($this->order == "created_by")
			$order = "created_by";
		else
		if ($this->order == "assigned_to")
			$order = "assigned_to";
		else
		if ($this->order == "project")
			$order = "project_name";
		else
		if ($this->order == "priority")
			$order = "t.priority";
		else
		if ($this->order == "status")
			$order = "t.status";
			
		// sense
		$sense = $this->sense == "asc"? NULL: " desc";
		
		// sql
		$sql = "
		select
			t.id,
			t.created_on,
			t.attached_file,
			t.title,
			u1.username as created_by,
			u2.username as assigned_to,
			p.title as project_name,
			p.status as project_status,
			t.priority,
			t.status
		from task t
		inner join user u1
			on u1.id = t.created_by_user_id
		inner join user u2
			on u2.id = t.assigned_to_user_id
		inner join project p
			on p.id = t.project_id
		inner join project_vs_user pu
			on pu.project_id = p.id
			and pu.user_id = ".$this->db->quote($this->current_user->get("id"))."
		".$this->getFilter()."
		order by ".$order.$sense.", t.id desc
		limit ".($this->page*$this->num_rows_per_page).", ".$this->num_rows_per_page;
		$result = $this->db->getResult($sql);
		while ($row = $this->db->getRow($result))
			array_push($ret, $row);
		
		return $ret;
	}
	
	function getUsers() {
		return Util::getUsers($this->db);
	}
	
	function getProjects() {
		$ret = array();
		
		$sql = "
		select
			p.id,
			p.created_on,
			p.title,
			p.status
		from project p
		inner join project_vs_user pu
			on pu.project_id = p.id
			and pu.user_id = ".$this->db->quote($this->current_user->get("id"))."
		order by p.title";
		$result = $this->db->getResult($sql);
		while ($row = $this->db->getRow($result))
			array_push($ret, $row);
		
		return $ret;
	}
	
	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;
	}
}
?>
Return current item: Todo Manager