1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Schanihbg\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Schanihbg\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to update an item. |
11
|
|
|
*/ |
12
|
|
|
class UpdateUserForm 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
|
|
|
public function __construct(DIInterface $di, $id) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($di); |
23
|
|
|
$user = $this->getItemDetails($id); |
24
|
|
|
$this->form->create( |
25
|
|
|
[ |
26
|
|
|
"id" => __CLASS__, |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
"id" => [ |
30
|
|
|
"type" => "text", |
31
|
|
|
"validation" => ["not_empty"], |
32
|
|
|
"readonly" => true, |
33
|
|
|
"value" => $user->id, |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
"acronym" => [ |
37
|
|
|
"type" => "text", |
38
|
|
|
"validation" => ["not_empty"], |
39
|
|
|
"value" => $user->acronym, |
40
|
|
|
], |
41
|
|
|
|
42
|
|
|
"email" => [ |
43
|
|
|
"type" => "email", |
44
|
|
|
"validation" => ["not_empty"], |
45
|
|
|
"value" => $user->email, |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
"password" => [ |
49
|
|
|
"type" => "password", |
50
|
|
|
], |
51
|
|
|
|
52
|
|
|
"password-again" => [ |
53
|
|
|
"type" => "password", |
54
|
|
|
"validation" => [ |
55
|
|
|
"match" => "password" |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
|
59
|
|
|
"submit" => [ |
60
|
|
|
"type" => "submit", |
61
|
|
|
"value" => "Save", |
62
|
|
|
"callback" => [$this, "callbackSubmit"] |
63
|
|
|
], |
64
|
|
|
|
65
|
|
|
"reset" => [ |
66
|
|
|
"type" => "reset", |
67
|
|
|
], |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get details on item to load form with. |
76
|
|
|
* |
77
|
|
|
* @param integer $id get details on item with id. |
78
|
|
|
* |
79
|
|
|
* @return User |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function getItemDetails($id) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$user = new User(); |
84
|
|
|
$user->setDb($this->di->get("database")); |
85
|
|
|
$user->find("id", $id); |
86
|
|
|
return $user; |
87
|
|
|
} |
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
|
|
View Code Duplication |
public function callbackSubmit() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
// Get values from the submitted form |
100
|
|
|
$id = $this->form->value("id"); |
101
|
|
|
$acronym = $this->form->value("acronym"); |
102
|
|
|
$email = $this->form->value("email"); |
103
|
|
|
$password = $this->form->value("password"); |
104
|
|
|
$passwordAgain = $this->form->value("password-again"); |
105
|
|
|
|
106
|
|
|
// Check password matches |
107
|
|
|
if ($password !== $passwordAgain) { |
108
|
|
|
$this->form->rememberValues(); |
109
|
|
|
$this->form->addOutput("Password did not match."); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$user = new User(); |
114
|
|
|
$user->setDb($this->di->get("database")); |
115
|
|
|
$user->find("id", $id); |
116
|
|
|
|
117
|
|
|
$user->acronym = $acronym; |
118
|
|
|
$user->email = $email; |
119
|
|
|
$user->setPassword($password); |
120
|
|
|
$user->save(); |
121
|
|
|
|
122
|
|
|
$this->form->addOutput("User was updated."); |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.