Location: PHPKode > projects > Todo Manager > todo/controllers/project.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 Project extends Controller {
	var $db;
	var $auth;
	var $current_user;
	var $order;
	var $sense;

	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", $this->current_user->getLanguage());
		
		// default parameters
		$this->loadParams("order=date", "sense=desc");
		$this->order = $this->getParam("order");
		$this->sense = $this->getParam("sense");
	}
	
	function onPost() {
		// parameters
		$item_ids = $this->getParam("item_id:array");
		
		if ($this->getAction() == "order_by") {
			$this->setParam("order", $this->getValue("column"));
			$this->setParam("sense", $this->getValue("sense"));
		} else
		if ($this->getAction() == "open") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "project", $item_id);
				$t->set("status", "open");
				$t->post();
			}
		} else
		if ($this->getAction() == "close") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "project", $item_id);
				$t->set("status", "closed");
				$t->post();
			}
		} else
		if ($this->getAction() == "delete") {
			foreach($item_ids as $item_id) {
				$t = new ActiveRecord($this->db, "project", $item_id);
				$t->delete();
			}
		} else
		if ($this->getAction() == "delete_item") {
			$item_id = $this->getValue();
			$t = new ActiveRecord($this->db, "project", $item_id);
			$t->delete();
		}
	}
	
	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 getRows() {
		$ret = array();
		
		// order
		$order = NULL;
		if ($this->order == "updated")
			$order = "updated_on";
		else
		if ($this->order == "date")
			$order = "created_on";
		else
		if ($this->order == "title")
			$order = "title";
		else
		if ($this->order == "status")
			$order = "status";
			
		// sense
		$sense = $this->sense == "asc"? NULL: " desc";
		
		// sql
		$sql = "
		select
			id,
			created_on,
			title,
			status
		from project
		order by ".$order.$sense.", id desc";
		$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;
	}
	
	function getUsers($project_id) {
		$ret = array();
		$sql = "
		select
			u.username
		from user u
		inner join project_vs_user pu
			on pu.project_id = ".$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["username"]);
		
		return implode(", ", $ret);
	}
}
?>
Return current item: Todo Manager