summaryrefslogtreecommitdiff
path: root/demos/time-tracker/protected/APP_CODE/ProjectDao.php
blob: 25a2845d3afb376eb504e971f54645a20e899c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

Prado::using('Application.APP_CODE.BaseDao');

class ProjectDao extends BaseDao
{
	public function createNewProject($project)
	{
		$sqlmap = $this->getConnection();
		$creator = $sqlmap->queryForObject('GetUserByName', $project->CreatorUserName);
		$manager = $sqlmap->queryForObject('GetUserByName', $project->ManagerUserName);
		$exists = $sqlmap->queryForObject('GetProjectByName', $project->Name);
		if($exists)
		{
			throw new TimeTrackerException(
				'project_exists', $project->Name);
		}
		else if(!$creator || !$manager)
		{
			throw new TimeTrackerException(
				'invalid_creator_and_manager',
				$project->Name, $project->CreatorUserName, 
				$project->ManagerUserName);	
		}
		else
		{
			$param['project'] = $project;
			$param['creator'] = $creator->ID;
			$param['manager'] = $manager->ID; 
			return $sqlmap->insert('CreateNewProject', $param);			
		}
	} 
	
	public function getProjectByID($projectID)
	{
		$sqlmap = $this->getConnection();
		return $sqlmap->queryForObject('GetProjectByID', $projectID);
	}
	
	public function addUserToProject($project, $user)
	{
		$sqlmap = $this->getConnection();
		$project = $this->getProjectByID($project->ID);
		$user = $sqlmap->queryForObject('GetUserByName', $user->Name);
		$list = $sqlmap->queryForList('GetProjectMembers', $project);
		$userExists = false;
		foreach($list as $k)
		{
			if($k->ID == $user->ID)
				$userExists = true;
		}
		if(!$project)
		{
			throw new TimeTrackerException(
				'invalid_project', $project->Name);			
		}
		else if(!$user)
		{
			throw new TimeTrackerException(
				'invalid_user', $user->Name);	
		}
		else if($userExists)
		{
			throw new TimeTrackerException(
				'project_member_exists', $projet->Name, $user->Name);
		}
		else
		{
			$param['project'] = $project;
			$param['user'] = $user;
			return $sqlmap->insert('AddUserToProject', $param);
		}	
	}
}

?>