summaryrefslogtreecommitdiff
path: root/demos/time-tracker/protected/pages/TimeTracker/LogTimeEntry.php
blob: 2c8f0f3bcf3f4ad316236e980d31336fd6357b89 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

class LogTimeEntry extends TPage
{
	
	protected function getProjectDao()
	{
		return $this->Application->Modules['daos']->getDao('ProjectDao');
	}
	
	protected function getCategoryDao()
	{
		return $this->Application->Modules['daos']->getDao('CategoryDao');
	}
	
	protected function getTimeEntryDao()
	{
		return $this->Application->Modules['daos']->getDao('TimeEntryDao');
	}
		
	public function onLoad($param)
	{
		if(!$this->IsPostBack)
		{
			$projects = $this->getProjects();
			$this->projects->DataSource = $projects;
			$this->projects->dataBind();
			$this->showCategories(key($projects));
		}
	}
	
	protected function showCategories($projectID)
	{
		$categories = array();
		foreach($this->getCategoryDao()->getCategoriesByProjectID($projectID) as $cat)
		{
			$categories[$cat->ID] = $cat->Name;
		}				
		$this->category->DataSource = $categories;
		$this->category->dataBind();
		$this->showProjectUsers($projectID);
	}
	
	protected function showProjectUsers($projectID)
	{
		if($this->User->isInRole('manager'))
			$users = $this->getProjectDao()->getProjectMembers($projectID);
		else
			$users = array($this->User->Name);
		$this->projectMembers->DataSource = $users;
		$this->projectMembers->dataBind();
		if(is_int($index = array_search($this->User->Name, $users)))
		{
			$this->projectMembers->SelectedIndex = $index;
			$this->showTimeSheet();
		}
	}
	
	public function showTimeSheet()
	{
		$user = $this->projectMembers->SelectedItem->Text;
		$project = $this->projects->SelectedValue;
		$this->entryList->setProjectEntry($user,$project);
		$this->entryList->refreshEntryList();
	}
	
	protected function getProjects()
	{
		$projects = array();
		if($this->User->isInRole('admin'))
			$list = $this->getProjectDao()->getAllProjects();
		else if($this->User->isInRole('manager'))
			$list = $this->getProjectDao()->getProjectsByManagerName($this->User->Name);
		else
			$list = $this->getProjectDao()->getProjectsByUserName($this->User->Name);
		foreach($list as $project)
			$projects[$project->ID] = $project->Name;
		return $projects;
	}
	
	public function projects_Changed($sender, $param)
	{
		$this->showCategories($sender->SelectedValue);
	}
		
	public function AddNewEntry($sender, $param)
	{
		if(!$this->IsValid)
			return;
		
		if($this->projectMembers->SelectedItem)
		{
			$entry = new TimeEntryRecord;
			$entry->CreatorUserName = $this->User->Name;
			$category = new CategoryRecord;
			$category->ID = $this->category->SelectedValue;
			$entry->Category = $category;
			$entry->Description = $this->description->Text;
			$entry->Duration = floatval($this->hours->Text);
			$entry->ReportDate = $this->day->TimeStamp;
			$entry->Username = $this->projectMembers->SelectedItem->Text;
			
			$this->hours->Text = '';
			$this->description->Text = '';
			
			$this->getTimeEntryDao()->addNewTimeEntry($entry);
			$this->showTimeSheet();
		}
	}
}

?>