<?php
define('VALID_PASSWORD','/^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/');
class Person extends AppModel
{
var $name = 'Person';
var $validate = array(
'name' => VALID_NOT_EMPTY,
'username' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL
);
var $hasMany = array('Tasks' => array( 'className' => 'Task', 'order'=>'priority asc' ));
function getUserIndex()
{
$list = $this->query("SELECT Person.id as id, name, username, Person.title, email, count(Task.id) as open_tasks
FROM {$this->tablePrefix}people as Person left join {$this->tablePrefix}tasks as Task on Task.person_id = Person.id and Task.status in ('new','in progress','ready')
where Person.active = 1
group by Person.id order by open_tasks desc");
foreach($list as $num => $person)
{
$list[$num] = am($person['Person'],$person[0]);
}
return $list;
}
function getUsers()
{
$list = $this->generateList('active = 1','name ASC',null,'{n}.Person.id','{n}.Person.name');
return $list;
}
function getTasks($id)
{
//We only need the person and the tasks?
$this->recursive = 1;
//Handle ALL new and in progress tasks
$this->hasMany['Tasks']['conditions']['status'] = array('new','in progress','ready','on hold');
$data = $this->findById($id);
$data_by_status = array('completed'=>array(),'new'=>array(),'in progress'=>array(),'on hold'=>array());
foreach($data['Tasks'] as $task)
{
$due = false;
if($task['due'] && $task != ZERO_DATE)
{
$due['date'] = $task['due'];
$due['status'] = (strtotime($task['due']) < time()) ? 'Past Due' : 'Upcoming';
if($due['status'] == 'Upcoming' && strtotime('-1 week',strtotime($task['due'])) > time())
{
$due['status'] = 'In Future';
}
}
$task['due'] = $due;
$data_by_status[$task['status']][$task['project_id']][] = $task;
}
//Handle completed tasks within the past week
$this->hasMany['Tasks']['conditions']['status'] = 'completed';
$this->hasMany['Tasks']['conditions'][] = "Tasks.modified >= date_sub(now(),interval 1 week) ";
$completed_data = $this->findById($id);
foreach($completed_data['Tasks'] as $task)
{
$data_by_status['completed'][$task['project_id']][] = $task;
}
$data['Tasks'] = $data_by_status;
return $data;
}
function beforeValidate()
{
//Validate password
if(!empty($this->data['Person']['password']) && !preg_match(VALID_PASSWORD,$this->data['Person']['password']))
{
$this->invalidate('password');
return false;
}
elseif(empty($this->data['Person']['password']))
{
unset($this->data['Person']['password']);
return true;
}
return true;
}
function beforeSave()
{
if(isset($this->data['Person']['password']))
{
$this->data['Person']['password'] = md5($this->data['Person']['password']);
}
return true;
}
}
?>