1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oenstrom\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Oenstrom\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to update an user. |
11
|
|
|
*/ |
12
|
|
|
class ProfileForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
18
|
|
|
*/ |
19
|
3 |
|
public function __construct(DIInterface $di) |
20
|
|
|
{ |
21
|
3 |
|
parent::__construct($di); |
22
|
3 |
|
$username = $this->di->get("session")->get("username"); |
23
|
3 |
|
$this->user = $this->hasItemDetails($username); |
|
|
|
|
24
|
3 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
3 |
|
"id" => __CLASS__, |
27
|
3 |
|
"legend" => "Uppdatera profil", |
28
|
3 |
|
"use_fieldset" => false, |
29
|
3 |
|
"wrapper-element" => "div", |
30
|
3 |
|
"br-after-label" => false, |
31
|
3 |
|
], |
32
|
|
|
[ |
33
|
|
|
"email" => [ |
34
|
3 |
|
"label" => "E-postadress", |
35
|
3 |
|
"label-class" => "mdl-textfield__label", |
36
|
3 |
|
"wrapper-class" => "mdl-textfield mdl-js-textfield mdl-textfield--floating-label", |
37
|
3 |
|
"class" => "mdl-textfield__input", |
38
|
3 |
|
"type" => "text", |
39
|
3 |
|
"value" => $this->user->email, |
40
|
3 |
|
"validation" => ["email", "not_empty"], |
41
|
3 |
|
], |
42
|
|
|
|
43
|
|
|
"new-password" => [ |
44
|
3 |
|
"label" => "Nytt lösenord", |
45
|
3 |
|
"label-class" => "mdl-textfield__label", |
46
|
3 |
|
"wrapper-class" => "mdl-textfield mdl-js-textfield mdl-textfield--floating-label", |
47
|
3 |
|
"class" => "mdl-textfield__input", |
48
|
3 |
|
"type" => "password", |
49
|
3 |
|
], |
50
|
|
|
|
51
|
|
|
"new-password-again" => [ |
52
|
3 |
|
"label" => "Nytt lösenord igen", |
53
|
3 |
|
"label-class" => "mdl-textfield__label", |
54
|
3 |
|
"wrapper-class" => "mdl-textfield mdl-js-textfield mdl-textfield--floating-label", |
55
|
3 |
|
"class" => "mdl-textfield__input", |
56
|
3 |
|
"type" => "password", |
57
|
3 |
|
"validation" => ["match" => "new-password"], |
58
|
3 |
|
], |
59
|
|
|
|
60
|
|
|
"submit" => [ |
61
|
3 |
|
"class" => "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent", |
62
|
3 |
|
"type" => "submit", |
63
|
3 |
|
"value" => "Uppdatera", |
64
|
3 |
|
"callback" => [$this, "callbackSubmit"] |
65
|
3 |
|
], |
66
|
|
|
] |
67
|
3 |
|
); |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get details on item to load form with. |
74
|
|
|
* |
75
|
|
|
* @param string $username get details on item with username. |
76
|
|
|
* |
77
|
|
|
* @return boolean true if okey, false if something went wrong. |
78
|
|
|
*/ |
79
|
3 |
|
public function hasItemDetails($username) |
80
|
|
|
{ |
81
|
3 |
|
$user = $this->di->get("user"); |
82
|
3 |
|
$user->find("username", $username); |
83
|
3 |
|
return $user; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Callback for submit-button which should return true if it could |
90
|
|
|
* carry out its work and false if something failed. |
91
|
|
|
* |
92
|
|
|
* @return boolean true if okey, false if something went wrong. |
93
|
|
|
*/ |
94
|
1 |
|
public function callbackSubmit() |
95
|
|
|
{ |
96
|
1 |
|
$password = $this->form->value("new-password"); |
97
|
1 |
|
$email = $this->form->value("email"); |
98
|
1 |
|
$isOldEmail = $this->user->email === $email; |
99
|
|
|
|
100
|
1 |
|
$user = $this->di->get("user"); |
101
|
|
|
|
102
|
1 |
View Code Duplication |
if (!$isOldEmail && $user->emailExists($email) !== null) { |
|
|
|
|
103
|
|
|
$this->form->addOutput("E-postadressen är upptagen.", "error"); |
104
|
|
|
$this->di->get("response")->redirect("user/profile"); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
if ($password != "") { |
109
|
|
|
$this->user->setPassword($password); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
if (empty($email)) { |
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->user->email = $email; |
117
|
|
|
$this->user->save(); |
118
|
|
|
$this->di->get("session")->set("email", $email); |
119
|
|
|
$this->form->addOutput("Din profil har uppdaterats.", "success"); |
120
|
|
|
$this->di->get("response")->redirect("user/profile"); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: