Location: PHPKode > projects > CenterFlow > app/models/task.php
<?php
define('ZERO_DATE','0000-00-00 00:00:00');

class Task extends AppModel
{
	var $name = 'Task';
	/**
	 * For sending emails in the afterSave function
	 *
	 * @var bool
	 */
	var $send_email = false;
	var $original_data = array();
	var $message_action;
	var $validate = array( 'title'  => VALID_NOT_EMPTY );
	var $belongsTo = array( 'Project' 	=>  array('className'	=>'Project'),
	'Person'	=>	array('className'	=>'Person'));

	function set_deleted($id)
	{
		$this->id = $id;
		$this->saveField('status','deleted');
	}
	
	function undelete($id)
	{
		$this->id = $id;
		$this->saveField('status','new');
	}
	
	function setOrder($id,$order)
	{
		$this->id = $id;
		$this->data = $this->read();
		$this->saveField('priority',$order);
	}

	function beforeSave()
	{
		$current_data = $this->read();
		if(in_array($this->data['Task']['status'],array('in progress','completed','ready')) &&
		empty($this->data['Task']['person_id']))
		{
			$this->invalidate('person_id');
			return false;
		}
		//Set message action
		if($this->id == 0)
		{
			$message_action = 'added';
		}

		if($this->data['Task']['status'] == 'in progress')
		{
			if($current_data['Task']['start'] == ZERO_DATE || //If this is a zero date
			$current_data['Task']['status'] == 'new') //If this is moving from new to inprogress
			{
				//print 'Noting start date';
				$this->data['Task']['start'] = date("Y-m-d H:i:s");
			}
			$this->data['Task']['finished'] = ZERO_DATE;
		}
		elseif($this->data['Task']['status'] == 'completed')
		{
			if($current_data['Task']['finished'] == ZERO_DATE || //If this is a zero date
			$current_data['Task']['status'] == 'in progress') //If this is moving from in progress to completed
			{
				//print 'Noting finish date';
				$this->data['Task']['finished'] = date("Y-m-d H:i:s");
				$message_action = 'completed';
			}
		}
		elseif($this->data['Task']['status'] == 'new')
		{
			$this->data['Task']['start'] 	= ZERO_DATE;
			$this->data['Task']['finished'] = ZERO_DATE;
		}
		elseif($this->data['Task']['status'] == 'deleted')
		{
			$message_action = 'deleted';
		}
		if(empty($current_data['Task']['author']))
		{
			$this->data['Task']['author'] = '';
		}
		$this->message_action = @ifsetor($message_action,'updated');

		$this->original_data = $current_data;
		return true;
	}

	function afterSave()
	{
		$current_data = $this->findById($this->id);

		$note = array();
		$email_body = array();
		$skip_keys = array('modified','created','start','finished','id');
		if($this->message_action == 'added')
		{
			$current_data = $this->read();
			foreach($current_data['Task'] as $key => $val)
			{
				if(in_array($key, $skip_keys)) continue;
				if($key == 'person_id')
				{
					$note[] = 'Assigned to '.$current_data['Person']['name'];
				}
				elseif ($key == 'project_id')
				{
					$note[] = 'Project:'.$current_data['Project']['name'];
				}
				else
				{
					$note[] = "$key: $val";
				}
			}
		}
		else
		{
			foreach($current_data['Task'] as $key => $val)
			{
				$original_val = $this->original_data['Task'][$key];
				if(in_array($key, $skip_keys)) continue;
				if($original_val != $val)
				{
					if($key == 'person_id')
					{
						$val = $current_data['Person']['name'];
						$note[] = "Assigned to $val";
					}
					elseif ($key == 'project_id')
					{
						$note[] = 'Project:'.$current_data['Project']['name'];
					}
					elseif($key == 'author' && $val != 0)
					{
						$note[] = "Requested by $val";
					}
					elseif($key == 'description')
					{
						$note[] = "Description: $val";
					}
					else
					{
						$note[] = "$key changed from <em>$original_val</em> to <em>$val</em>";
					}
				}
			}
		}

		if($this->send_email)
		{
			$email_to = $current_data['Person']['email'];
			$email_body[] = "Project:\n\t".$current_data['Project']['name'];
			$email_body[] = "Task:\n\t".$current_data['Task']['title'];
			$email_body[] = "Status:\n\t".$current_data['Task']['status'];
			$email_body[] = "Description:\n\t".$current_data['Task']['description'];
			$email_body = implode("\r\n\n",$email_body);
			$my_domain = $_SERVER['HTTP_HOST'];
			mail($email_to,"centerFLOW: $this->message_action",$email_body,"From: centerflow@$my_domain");
		}

		//If nothing has changed, we don't need to log the saving.
		if(empty($note))
		{
			return;
		}
		else
		{
			$note = implode("\r\n",$note);
		}
		$this->id = $current_data['Task']['id'];
		$message = array(
		'subject' 	=> $current_data['Project']['name'].': '.$current_data['Task']['title'],
		'category' 	=> $this->name,
		'action'	=> $this->message_action,
		'message'	=> $note);
		if($this->message_action = 'completed')
		{
			$message['person'] = $current_data['Task']['person_id'];
		}
		$this->requestAction('/messages/log_change',array('Message'=>$message));
	}
}


?>
Return current item: CenterFlow