Location: PHPKode > projects > CenterFlow > app/models/project.php
<?php

class Project extends AppModel
{
	var $name = 'Project';
	var $original_data = array();
	var $message_action;
	
	var $validate = array(	'name'  => VALID_NOT_EMPTY	);

	var $hasMany = array('Tasks' =>	array(	'className'	=> 'Task', 'dependent' => true, 'order' => 'status, priority',),
	'Projects' => array( 'className' => 'Project', 'foreignKey'=>'parentid', 'dependent' => true));
	//var $belongsTo = array('Projects'=>array('className'=>'Project','foreignKey'=>'parentid'));
	
	function fetchMainProjects()
	{
		$this->recursive = 1;
		$conditions['Project.parentid'] = 0;
		$this->hasMany['Tasks']['conditions']['Tasks.status'] = array('new','in progress');
		$data = $this->findAll($conditions);
		return $data;
	}

	function moveProjects($old_parent_id, $new_parent_id)
	{
		$list = $this->execute("UPDATE {$this->tablePrefix}projects SET parent_id = $new_parent_id WHERE project_id = $old_parent_id");
		$task_count = $this->getAffectedRows();
		return $task_count;
	}

	function moveTasks($old_project_id, $new_project_id)
	{
		$list = $this->execute("UPDATE {$this->tablePrefix}tasks SET project_id = $new_project_id WHERE project_id = $old_project_id");
		$task_count = $this->getAffectedRows();
		return $task_count;
	}

	function generateTree()
	{
		$list = $this->query("SELECT Project.id as id, name, Project.parentid, count(Task.id) as open_tasks
								FROM {$this->tablePrefix}projects as Project left join {$this->tablePrefix}tasks as Task
								on Task.project_id = Project.id and Task.status in ('new','in progress')
								group by Project.id order by open_tasks desc");
		$project_tree = array();
		foreach($list as $project)
		{
			$project_id= $project['Project']['id'];
			$name 	   = $project['Project']['name'];
			$parent_id = $project['Project']['parentid'];
			$opentasks = $project[0]['open_tasks'];
			if($parent_id == 0)
			{
				$project_tree[$project_id] = array('name'=>$name,'opentasks'=>$opentasks,'subprojects'=>false);
			}
			else
			{
				$project_tree[$parent_id]['subprojects'][$project_id] = array('name'=>$name,'opentasks'=>$opentasks);
			}
		}
		return $project_tree;
	}

	function fetchProject($id)
	{
		$this->recursive = 1;
		$data = $this->findById($id);
		return $data;
	}

	function search($search_terms,$limit = 20,$page = 1)
	{
		$prefix = $this->tablePrefix;
		$ors = array();
		foreach($search_terms as $term)
		{
			$ors[] = "Task.description LIKE '%$term%'";
			$ors[] = "Task.title LIKE '%$term%'";
		}
		$ors = implode(' OR ',$ors);
		$limit_text = ($page = 1) ? "$limit" : $page * $limit." , $limit";
		$list = $this->query("SELECT * from ${prefix}tasks as Task
		left join  ${prefix}people as Person on Person.id = Task.person_id
		where ($ors) order by priority asc limit $limit_text");
		return $list;
	}
}

?>
Return current item: CenterFlow