Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
class EditUser extends TPage
4
{
5
	/**
6
	 * Initializes the inputs with existing user data.
7
	 * This method is invoked by the framework when the page is being initialized.
8
	 * @param mixed event parameter
9
	 */
10
	public function onInit($param)
11
	{
12
		parent::onInit($param);
13
		if(!$this->IsPostBack)  // if the page is initially requested
14
		{
15
			// Retrieves the existing user data. This is equivalent to:
16
			// $userRecord=$this->getUserRecord();
17
			$userRecord=$this->UserRecord;
18
 
19
			// Populates the input controls with the existing user data
20
			$this->Username->Text=$userRecord->username;
21
			$this->Email->Text=$userRecord->email;
22
			$this->Role->SelectedValue=$userRecord->role;
23
			$this->FirstName->Text=$userRecord->first_name;
24
			$this->LastName->Text=$userRecord->last_name;
25
		}
26
	}
27
 
28
	/**
29
	 * Saves the user account if all inputs are valid.
30
	 * This method responds to the OnClick event of the "save" button.
31
	 * @param mixed event sender
32
	 * @param mixed event parameter
33
	 */
34
	public function saveButtonClicked($sender,$param)
35
	{
36
		if($this->IsValid)  // when all validations succeed
37
		{
38
			// Retrieves the existing user data. This is equivalent to:
39
			$userRecord=$this->UserRecord;
40
 
41
			// Fetches the input data
42
			$userRecord->username=$this->Username->Text;
43
			// update password when the input is not empty
44
			if(!empty($this->Password->Text))
45
				$userRecord->password=$this->Password->Text;
46
			$userRecord->email=$this->Email->Text;
47
			// update the role if the current user is an administrator
48
			if($this->User->IsAdmin)
49
				$userRecord->role=(int)$this->Role->SelectedValue;
50
			$userRecord->first_name=$this->FirstName->Text;
51
			$userRecord->last_name=$this->LastName->Text;
52
 
53
			// saves to the database via Active Record mechanism
54
			$userRecord->save();
55
 
56
			// redirects the browser to the homepage
57
			$this->Response->redirect($this->Service->DefaultPageUrl);
58
		}
59
	}
60
 
61
	/**
62
	 * Returns the user data to be editted.
63
	 * @return UserRecord the user data to be editted.
64
	 * @throws THttpException if the user data is not found.
65
	 */
66
	protected function getUserRecord()
67
	{
68
		// the user to be editted is the currently logged-in user
69
		$username=$this->User->Name;
70
		// if the 'username' GET var is not empty and the current user
71
		// is an administrator, we use the GET var value instead.
72
		if($this->User->IsAdmin && $this->Request['username']!==null)
73
			$username=$this->Request['username'];
74
 
75
		// use Active Record to look for the specified username
76
		$userRecord=UserRecord::finder()->findByPk($username);
77
		if(!($userRecord instanceof UserRecord))
78
			throw new THttpException(500,'Username is invalid.');
79
		return $userRecord;
80
	}
81
}
82
 
83
?>