Location: PHPKode > projects > CenterFlow > app/controllers/projects_controller.php
<?

class ProjectsController extends AppController
{
	var $name = 'Projects';
	var $scaffold;
	var $helpers = array('Html','Text','Javascript','Cache');
	var $uses = array('Project','Task');
	var $cacheAction = array('/projectlist'=>'1 hour' );
	var $layout = 'app';

	function index()
	{
		// initial view
		$data =  $this->Project->fetchMainProjects();
		$this->set('data',$data);
	}


	function beforeFilter()
	{
		$this->checkSession();
	}

	function beforeRender()
	{
		if($this->layout != 'ajax')
		{
			//$this->set('project_listing',$this->Project->generateList('parentid = 0', null, null, '{n}.Project.id', '{n}.Project.name'));
			$this->set('project_listing',$this->Project->generateTree());
		}
		if(!$this->pageTitle || empty($this->pageTitle))
		{
			$this->pageTitle = ucwords($this->name.' :: '.$this->action);
		}
		$this->pageTitle = '[centerFLOW] '.$this->pageTitle;
		$this->set('cfroot',CF_ROOT);
	}

	function merge($id)
	{
		if(isset($this->data))
		{
			//$json = $this->requestAction("/projects/jsonencode/$id",array('return'));
			//$this->Project->backup($id, $json);
			$old_project_id = $this->data['Project']['id'];
			$new_project_id = $this->data['Project']['merge_id'];
			$moved_tasks = $this->Project->moveProjects($old_project_id,$new_project_id);
			$moved_tasks = $this->Project->moveTasks($old_project_id,$new_project_id);
			$_SESSION['msg'] = "$moved_tasks tasks were moved to this project.";
			$this->Project->del($id);
			$this->redirect(CF_ROOT."/projects/view/$new_project_id");
			exit();
		}
		$project_list = $this->Project->generateList('parentid = 0', null, null, '{n}.Project.id', '{n}.Project.name');
		$this->set('project_list',$project_list);
		$this->set('data',$this->Project->findById($id));
	}

	function delete($id)
	{
		$data= $this->Project->findById($id);
		$parent_project = $this->Project->findById($data['Project']['parentid']);
		$this->set('parent_name',$parent_project['Project']['name']);
		$this->set('data',$data);
	}

	function edit($id)
	{
		$data= $this->Project->findById($id);
		$project_list = $this->Project->generateList('parentid = 0', null, null, '{n}.Project.id', '{n}.Project.name');
		$project_list[0] = 'No Parent Project';
		$this->set('project_list',$project_list);
		$this->set('data',$data);
	}

	function projectlist($selected = null)
	{
		$projectlist = $this->Project->generateList(null, null, null, '{n}.Project.id', '{n}.Project.name');
		$this->set('selected',$selected);
		$this->set('projectlist',$projectlist);
		$this->layout = 'ajax';
	}

	function save($id)
	{
		$action = $this->params['form']['action'];
		if($action == 'Delete')
		{
			//$json = $this->requestAction("/projects/jsonencode/$id",array('return'));
			//$this->Project->backup($id, $json);
			//$this->set('error_msg',$json);
			$project_data = $this->Project->findById($id);
			$parent_id    = $project_data['Project']['parentid'] == 0 ? '' : $project_data['Project']['parentid'];

			if($this->params['form']['move'] == 1)
			{
				if(empty($parent_id))
				{
					$parent_project = $this->Project->findById($data['Project']['parentid']);
					$error_msg = "There is no parent project to move tasks into. If you want to delete this project 
					and move the tasks somewhere else, try <a class='em' href='/projects/merge/$id'>merging</a> your project.";
					$this->set('error_msg',$error_msg);
					$this->set('parent_name',$parent_project['Project']['name']);
					$this->set('data',$project_data);
					$this->render('delete');
					exit();
				}
				$this->Project->moveTasks($id,$parent_id);
			}
			$this->Project->del($id,true);
			$_SESSION['msg'] = 'Your project was deleted successfully';
			$view = empty($parent_id) ? 'index' : 'view';
			$this->redirect(CF_ROOT."/projects/$view/".$parent_id);
		}
		else
		{
			if ($this->Project->save($this->data['Project']))
			{
				if($id == 0)
				{
					$saved_project = $this->Project->read('Project.id');
					$redirect_to   = $saved_project['Project']['id'];
				}
				elseif (isset($this->data['parentid']) && $this->data['parentid'] != 0)
				{
					$redirect_to = $this->data['parentid'];
				}
				else
				{
					$redirect_to = $id;
				}
				$this->redirect(CF_ROOT."/projects/view/$redirect_to");
				exit();
			}
			else
			{
				$project_list = $this->Project->generateList('parentid = 0', null, null, '{n}.Project.id', '{n}.Project.name');
				$project_list[0] = 'No Parent Project';
				$this->set('project_list',$project_list);

				$this->set('error_msg','This form has errors.');
				$this->set('data',$this->data);
				$this->render('edit');
			}
		}
	}

	function add($project_id)
	{
		if(!empty($this->data))
		{
			//pr($this->data);
			if ($this->Project->save($this->data['Project']))
			{
				$this->flash('Your project was added','index');
				$this->redirect(CF_ROOT."/projects/view/$project_id");
				exit();
			}
			else
			{
				$this->	set('msg','Your plan was not added properly');
			}
		}
		$this->pageTitle = 'Add Project';
		$this->Project->id = 0;
		$this->Project->parentid = $project_id;
		$this->set('parentid',$project_id);
	}

	function view($id, $mode = null)
	{
		$data= $this->Project->fetchProject($id);
		$this->pageTitle = ucwords($this->name.' :: '.$this->action).' :: '.$data['Project']['name'];
		$this->set('data',$data);
		if($mode == 'full')
		{
			$listing_action = "/tasks/full_listing/";
		}
		elseif($mode == 'undelete')
		{
			$listing_action = "/tasks/undelete_listing/";
		}
		else
		{
			$listing_action = "/tasks/listing/";
		}
		$tasks = $this->requestAction("$listing_action/$id",array('return'));
		$subtasks = array();
		foreach($data['Projects'] as $subproject)
		{
			$sub_id = $subproject['id'];
			$subtasks[$sub_id] = $this->requestAction("$listing_action/$sub_id",array('return'));
		}
		$this->set('mytasks',$tasks);
		$this->set('subtasks',$subtasks);
	}

	function reorder($id)
	{
		if(!empty($this->params['form']['order']))
		{
			parse_str($this->params['form']['order']);
			foreach($task_list as $num => $task_id)
			{
				$this->Task->setOrder($task_id,$num);
			}
			$_SESSION['msg'] = 'New order saved';
			$this->redirect(CF_ROOT."/projects/view/$id");
		}


		$data= $this->Project->fetchProject($id);
		$this->pageTitle = ucwords($this->name.' :: '.$this->action).' :: '.$data['Project']['name'];
		//$this->set('data',$data);
		//$tasks = $this->Task->findAllByProjectId($id);

		$tasks = $this->Task->generateList("project_id = $id and status in('new','in progress')",'priority ASC',null,'{n}.Task.id','{n}.Task.title');
		$this->set('project_name',$data['Project']['name']);
		$this->set('tasks',$tasks);
	}

	function quicksearch()
	{
		//pr($this->params);
		$search = $this->params['form']['search'];
		$this->pageTitle = ucwords($this->name.' :: '.$this->action);
		$search_terms = preg_split('/\s+/',$search);
		$results = $this->Project->search($search_terms);
		$this->set('terms',$search);
		$this->set('results',$results);
		$this->set('my_person_id', $this->Session->read('Person.id'));
	}

	function search($project_id = null)
	{
		$this->set('projectlist',$this->requestAction("/projects/projectlist/$project_id",array('return')));
		$this->set('userlist',$this->requestAction("/people/userlist/",array('return')));
	}

	function table($id = null)
	{
		$this->Project->recursive = 2;
		$filter = @ifsetor($this->params['form']['filter'],'All');
		$conditions = array();
		if($id != null)
		{
			$conditions['Project.id'] = $id;
		}
		if($filter != 'All')
		{
			list($search_for, $search_what) = explode(':',$filter);
			if($search_for == 'status')
			{
				$task_conditions = "Tasks.status = '$search_what'";
			}
			elseif($search_for == 'person' && $search_what != 0)
			{
				$task_conditions = "Tasks.person_id = '$search_what'";
			}
			$this->Project->hasMany['Tasks']['conditions'] = $task_conditions;
		}
		elseif(!empty($this->data))
		{
			$task_conditions = array();
			if(!empty($conditions['Project.id']))
			{
				$task_conditions[] = "project_id = $id";
			}
			else
			{
				$task_conditions[] = 'project_id = {$__cakeID__$}';
			}

			foreach($this->data['Task'] as $criteria => $value)
			{
				if(empty($value)) continue;
				if($criteria == 'dateTo')
				{
					$task_conditions[] = "Tasks.modified <= '$value' ";
					//$task_conditions[] = "date(modified) <= '$value' ";
				}
				elseif($criteria == 'dateFrom')
				{
					$task_conditions[] ="Tasks.modified >= '$value' ";
					//$task_conditions[] = "date(modified) >= '$value' ";
				}
				elseif($criteria == 'title')
				{
					$task_conditions["Tasks.title"] = "like '%$value%' ";
					//$task_conditions[] = "title like '%$value%' ";
				}
				elseif($criteria == 'project_id')
				{
					$conditions['Project.id'] = $value;
					$task_conditions['Tasks.project_id'] = $value;
					//$task_conditions[] = "project_id = '$value' ";
				}
				else
				{
					$task_conditions[] ="Tasks.$criteria= '$value'";
					//$task_conditions[] = "$criteria = '$value' ";
				}
			}
			//$where = implode(' AND ',$task_conditions);
			//$this->Project->hasMany['Tasks']['finderQuery'] = "SELECT `Tasks`.`id`, `Tasks`.`project_id`, `Tasks`.`title`, `Tasks`.`description`, `Tasks`.`author`, `Tasks`.`person_id`, `Tasks`.`status`, `Tasks`.`created`, `Tasks`.`modified`, `Tasks`.`start`, `Tasks`.`finished` FROM `sp_tasks` AS `Tasks` WHERE $where ORDER BY `status` ASC, `modified` ASC";
			$this->Project->hasMany['Tasks']['conditions'] = $task_conditions;
		}
		$data = $this->Project->findAll($conditions);
		$this->set('data',$data);
		$this->set('statusfilter',$this->requestAction("/people/statusfilter/$filter",array('return')));
	}
}
?>
Return current item: CenterFlow