1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm\Admin; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Nicklas\Comment\Modules\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class EditUserForm 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, $id) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($di); |
22
|
|
|
$this->user = new User(); |
|
|
|
|
23
|
|
|
$this->user->setDb($this->di->get("db")); |
24
|
|
|
$this->user->find("id", $id); |
25
|
|
|
$user = $this->user; |
26
|
|
|
$this->form->create( |
27
|
|
|
[ |
28
|
|
|
"id" => __CLASS__, |
29
|
|
|
"fieldset" => true, |
30
|
|
|
"legend" => "Update user: $user->name" |
31
|
|
|
], |
32
|
|
|
[ |
33
|
|
|
|
34
|
|
|
"name" => [ |
35
|
|
|
"type" => "text", |
36
|
|
|
"readonly" => true, |
37
|
|
|
"value" => $user->name, |
38
|
|
|
], |
39
|
|
|
"email" => [ |
40
|
|
|
"type" => "text", |
41
|
|
|
"value" => $user->email, |
42
|
|
|
], |
43
|
|
|
"select" => [ |
44
|
|
|
"type" => "select", |
45
|
|
|
"label" => "Select authority", |
46
|
|
|
"options" => ["$user->authority" => $user->authority, "admin" => "admin", "user" => "user"], |
47
|
|
|
], |
48
|
|
|
"submit" => [ |
49
|
|
|
"type" => "submit", |
50
|
|
|
"value" => "Update", |
51
|
|
|
"callback" => [$this, "callbackSubmit"] |
52
|
|
|
], |
53
|
|
|
"reset" => [ |
54
|
|
|
"type" => "reset", |
55
|
|
|
], |
56
|
|
|
"delete" => [ |
57
|
|
|
"type" => "submit", |
58
|
|
|
"value" => "Delete", |
59
|
|
|
"onclick"=>"return confirm('Vill du verkligen radera användaren?');", |
60
|
|
|
"callback" => [$this, "callBackDelete"] |
61
|
|
|
], |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function callbackDelete() |
71
|
|
|
{ |
72
|
|
|
$this->user->delete(); |
73
|
|
|
$this->di->get("response")->redirect("admin/user"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Callback for submit-button which should return true if it could |
78
|
|
|
* carry out its work and false if something failed. |
79
|
|
|
* |
80
|
|
|
* @return boolean true if okey, false if something went wrong. |
81
|
|
|
*/ |
82
|
|
|
public function callbackSubmit() |
83
|
|
|
{ |
84
|
|
|
// Get values from the submitted form |
85
|
|
|
$email = $this->form->value("email"); |
86
|
|
|
|
87
|
|
|
if (strpos($email, '%') !== false) { |
88
|
|
|
$this->form->addOutput("% is not allowed"); |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$user = $this->user; |
93
|
|
|
|
94
|
|
|
$user->email = $email; |
95
|
|
|
$user->authority = $this->form->value("select") ?: "user"; |
96
|
|
|
$user->save(); |
97
|
|
|
$this->form->addOutput("Du uppdaterade användaren"); |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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: