|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alvo\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
|
6
|
|
|
use \Anax\DI\DIInterface; |
|
7
|
|
|
use \Alvo\User\User; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example of FormModel implementation. |
|
11
|
|
|
*/ |
|
12
|
|
|
class UpdateUserForm extends FormModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor injects with DI container. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(DIInterface $di, $user = null) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($di); |
|
22
|
|
|
|
|
23
|
|
|
$this->user = $user; |
|
|
|
|
|
|
24
|
|
|
if (!$this->user) { |
|
25
|
|
|
$this->user = $this->loadUserData($di->get('session')->get('userId')); |
|
26
|
|
|
} |
|
27
|
|
|
$this->di = $di; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->form->create( |
|
30
|
|
|
[ |
|
31
|
|
|
"id" => __CLASS__, |
|
32
|
|
|
"class" => "center form", |
|
33
|
|
|
"wrapper-element" => "div", |
|
34
|
|
|
"use_fieldset" => false, |
|
35
|
|
|
// "br-after-label" => false, |
|
36
|
|
|
], |
|
37
|
|
|
[ |
|
38
|
|
|
"email" => [ |
|
39
|
|
|
"type" => "email", |
|
40
|
|
|
"value" => esc($this->user->email), |
|
41
|
|
|
"validation" => [ |
|
42
|
|
|
// "custom_test" => [ |
|
43
|
|
|
// "message" => "User with this email is already registered", |
|
44
|
|
|
// "test" => function ($email) |
|
45
|
|
|
// { |
|
46
|
|
|
// $check = $this->user->getUser('email', $email); |
|
47
|
|
|
// return !$check; |
|
48
|
|
|
// } |
|
49
|
|
|
// ], |
|
50
|
|
|
"not_empty" |
|
51
|
|
|
] |
|
52
|
|
|
], |
|
53
|
|
|
|
|
54
|
|
|
"password" => [ |
|
55
|
|
|
"type" => "password", |
|
56
|
|
|
"validation" => [ |
|
57
|
|
|
"not_empty" |
|
58
|
|
|
], |
|
59
|
|
|
], |
|
60
|
|
|
|
|
61
|
|
|
"password-again" => [ |
|
62
|
|
|
"type" => "password", |
|
63
|
|
|
"validation" => [ |
|
64
|
|
|
"match" => "password" |
|
65
|
|
|
], |
|
66
|
|
|
], |
|
67
|
|
|
|
|
68
|
|
|
"submit" => [ |
|
69
|
|
|
"type" => "submit", |
|
70
|
|
|
"value" => "Update User", |
|
71
|
|
|
"callback" => [$this, "callbackSubmit"], |
|
72
|
|
|
"class" => "btn btn-warning" |
|
73
|
|
|
], |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
public function loadUserData($id) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$user = new User(); |
|
83
|
|
|
$user->setDb($this->di->get("db")); |
|
84
|
|
|
$user->find("id", $id); |
|
85
|
|
|
return $user; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Callback for submit-button which should return true if it could |
|
92
|
|
|
* carry out its work and false if something failed. |
|
93
|
|
|
* |
|
94
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function callbackSubmit() |
|
97
|
|
|
{ |
|
98
|
|
|
// Get values from the submitted form |
|
99
|
|
|
$email = $this->form->value("email"); |
|
100
|
|
|
$password = $this->form->value("password"); |
|
101
|
|
|
$passwordAgain = $this->form->value("password-again"); |
|
102
|
|
|
|
|
103
|
|
|
// Check password matches |
|
104
|
|
|
if ($password !== $passwordAgain) { |
|
105
|
|
|
$this->form->rememberValues(); |
|
106
|
|
|
$this->form->addOutput("Password did not match."); |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$user = new User(); |
|
111
|
|
|
$user->setDb($this->di->get("db")); |
|
112
|
|
|
$user->find("id", $this->user->id); |
|
113
|
|
|
$user->email = $email; |
|
114
|
|
|
$user->setPassword($password); |
|
115
|
|
|
$user->updated = date("Y-m-d H:i:s"); |
|
116
|
|
|
$user->save(); |
|
117
|
|
|
|
|
118
|
|
|
// $this->di->get("user")->logout(); |
|
119
|
|
|
// $t = $this->di->get("user")->login($email, $password); |
|
120
|
|
|
// debug($t); |
|
121
|
|
|
|
|
122
|
|
|
$this->form->addOutput("Profile has been updated."); |
|
123
|
|
|
|
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|