1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Anax\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to update an item. |
11
|
|
|
*/ |
12
|
|
|
class UpdateForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container and the id to update. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
18
|
|
|
* @param integer $id to update |
19
|
|
|
*/ |
20
|
1 |
|
public function __construct(DIInterface $di, $id) |
21
|
|
|
{ |
22
|
1 |
|
parent::__construct($di); |
23
|
1 |
|
$user = $this->getUserData($id); |
24
|
1 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
1 |
|
"id" => __CLASS__, |
27
|
1 |
|
"legend" => "Update details of the item", |
28
|
1 |
|
], |
29
|
|
|
[ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
"id" => [ |
33
|
1 |
|
"type" => "text", |
34
|
1 |
|
"validation" => ["not_empty"], |
35
|
1 |
|
"readonly" => true, |
36
|
1 |
|
"value" => $user->id, |
37
|
1 |
|
], |
38
|
|
|
"user" => [ |
39
|
1 |
|
"type" => "text", |
40
|
1 |
|
"validation" => ["not_empty"], |
41
|
1 |
|
"value" => $user->acronym, |
42
|
1 |
|
], |
43
|
|
|
|
44
|
|
|
"email" => [ |
45
|
1 |
|
"type" => "text", |
46
|
1 |
|
"validation" => ["not_empty"], |
47
|
1 |
|
"value" => $user->email, |
48
|
1 |
|
], |
49
|
|
|
|
50
|
|
|
"password" => [ |
51
|
1 |
|
"type" => "password", |
52
|
1 |
|
"placeholder" => "Lämna tomt för att inte uppdatera lösenord", |
53
|
1 |
|
], |
54
|
|
|
|
55
|
|
|
"password-again" => [ |
56
|
1 |
|
"type" => "password", |
57
|
|
|
"validation" => [ |
58
|
|
|
"match" => "password" |
59
|
1 |
|
], |
60
|
1 |
|
], |
61
|
|
|
|
62
|
|
|
"submit" => [ |
63
|
1 |
|
"type" => "submit", |
64
|
1 |
|
"value" => "Save", |
65
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
66
|
1 |
|
], |
67
|
|
|
|
68
|
|
|
"reset" => [ |
69
|
1 |
|
"type" => "reset", |
70
|
1 |
|
], |
71
|
|
|
] |
72
|
1 |
|
); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Fetch data from the database for a specific user with id |
77
|
|
|
* |
78
|
|
|
* @param string $id id for the user. |
79
|
|
|
* |
80
|
|
|
* @return array data for the user. |
81
|
|
|
*/ |
82
|
1 |
|
public function getUserData($id) |
83
|
|
|
{ |
84
|
1 |
|
$user = new User(); |
85
|
1 |
|
$user->setDb($this->di->get("db")); |
86
|
1 |
|
$data = $user->find("id", $id); |
87
|
1 |
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Callback for submit-button which should return true if it could |
93
|
|
|
* carry out its work and false if something failed. |
94
|
|
|
* |
95
|
|
|
* @return boolean true if okey, false if something went wrong. |
96
|
|
|
*/ |
97
|
1 |
|
public function callbackSubmit() |
98
|
|
|
{ |
99
|
|
|
// Get values from the submitted form |
100
|
1 |
|
$acronym = $this->form->value("user"); |
101
|
1 |
|
$email = $this->form->value("email"); |
102
|
1 |
|
$password = $this->form->value("password"); |
103
|
1 |
|
$passwordAgain = $this->form->value("password-again"); |
104
|
|
|
|
105
|
|
|
// Check password matches |
106
|
1 |
|
if ($password !== $passwordAgain) { |
107
|
|
|
$this->form->rememberValues(); |
108
|
|
|
$this->form->addOutput("Password did not match."); |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Save to database |
113
|
|
|
// $db = $this->di->get("db"); |
114
|
|
|
// $password = password_hash($password, PASSWORD_DEFAULT); |
115
|
|
|
// $db->connect() |
116
|
|
|
// ->insert("User", ["acronym", "password"]) |
117
|
|
|
// ->execute([$acronym, $password]); |
118
|
1 |
|
$user = new User(); |
119
|
1 |
|
$user->setDb($this->di->get("db")); |
120
|
|
|
//var_dump($id); |
121
|
1 |
|
$user->find("id", $this->form->value("id")); |
122
|
1 |
|
$user->acronym = $acronym; |
123
|
1 |
|
$user->email = $email; |
124
|
1 |
|
if ($password == null) { |
125
|
1 |
|
$password = $user->password; |
|
|
|
|
126
|
1 |
|
} else { |
127
|
|
|
$user->setPassword($password); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
1 |
|
$user->save(); |
132
|
|
|
|
133
|
1 |
|
$this->form->addOutput("Användaren uppdaterades"); |
134
|
1 |
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.