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

class MessagesController extends AppController
{
	var $name = 'Messages';
	var $scaffold;
	var $helpers = array('Html','Text','Javascript');
	var $uses = array('Message','Person');
	var $layout = 'app';
	
	function beforeFilter()
	{
		$this->checkSession();
	}
	
	function index()
	{
		$limit = 50;
		$filter = @ifsetor($this->params['form']['filter'],'All');
		$conditions = array();
		if($filter != 'All')
		{
			list($search_for, $search_what) = explode(':',$filter);
			$conditions["Message.$search_for"] = $search_what;
		}
		if(isset($this->data))
		{
			foreach($this->data['Message'] as $criteria => $value)
			{
				if(empty($value)) continue;
				if($criteria == 'dateTo')
				{
					//$conditions["Message.date"] = "<= $value";
					$conditions[] ="Message.date <= '$value'";
				}
				elseif($criteria == 'dateFrom')
				{
					//$conditions["Message.date"] = ">= $value ";
					$conditions[] = "Message.date >= '$value' ";
				}
				else 
				{
					$conditions["Message.$criteria"] = $value;
				}
			}
		}
		$this->set('message_list', $this->Message->findAll($conditions,null,'date desc',$limit));
		$this->set('filter',$this->requestAction("/messages/filter/$filter",array('return')));
	}

	function filter($selected)
	{
		$this->set('selected',$selected);
		$this->set('user_array',$this->Person->getUsers());
		$this->set('category_array', array('Message'=>'Message','Task'=>'Task','Project'=>'Project'));
		$this->set('action_array', array('Added'=>'added','Updated'=>'updated','Completed'=>'completed','Deleted'=>'deleted'));
		$this->layout = 'ajax';
	}

	function log_change()
	{
		$this->autoRender = false;
		$this->params['Message']['date']     = date("Y-m-d H:i:s");
		$this->params['Message']['person_id'] = @ifsetor($this->params['Message']['person'],$this->Session->read('Person.id'));
		$this->Message->id = 0;
		if(!$this->Message->save($this->params['Message']))
		{
			print 'could not save';
			pr($this);
		}
	}
	
	function delete($id)
	{
		$this->Message->del($id);
		$this->layout = 'ajax';
	}
	
	function search()
	{
		$this->set('user_array',$this->Person->generateList('active = 1','name ASC',null,'{n}.Person.id','{n}.Person.name'));
		$this->set('category_array', array('Message'=>'Message','Task'=>'Task','Project'=>'Project'));	
		$this->set('action_array', array('Added'=>'added','Updated'=>'updated','Completed'=>'completed','Deleted'=>'deleted'));	
	}
	
	function view($id)
	{
		if($id > 0) //editing existing message
		{		
			$data = $this->Message->findById($id);
		}
		else //adding new message
		{
			$data['Message'] = array('id'=>0,'subject'=>'','message'=>'');
		}
		$this->layout = 'ajax';
		$this->set('data',$data);
	}
	
	function display_one($id)
	{
		$data = $this->Message->findById($id);
		$this->layout = 'ajax';
		$this->set('data',$data);
	}
	
	function save($id)
	{
		if (!empty($this->data))
		{
			$this->Message->id 	= $id;
			$this->data['Message']['person_id']= $this->Session->read('Person.id');
			$this->data['Message']['date']	   = date("Y-m-d H:i:s");
			if($this->Message->save($this->data['Message']))
			{
				$this->display_one($id);
				$this->render('display_one');
			}
			else
			{
				$this->set('error_msg','Could not save');
				$this->view($id);
				$this->render('view');
			}
		}
	}
}
?>
Return current item: CenterFlow