1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seb\Profile\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Seb\Profile\Profile; |
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 Psr\Container\ContainerInterface $di a service container |
|
|
|
|
18
|
|
|
* @param integer $id to update |
19
|
|
|
*/ |
20
|
2 |
|
public function __construct(ContainerInterface $di, $id) |
21
|
|
|
{ |
22
|
2 |
|
parent::__construct($di); |
23
|
2 |
|
$profile = $this->getProfileDetails($id); |
24
|
2 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
2 |
|
"id" => __CLASS__, |
27
|
2 |
|
"legend" => "Update {$profile->acronym}", |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
"id" => [ |
31
|
2 |
|
"type" => "hidden", |
32
|
|
|
"validation" => ["not_empty"], |
33
|
|
|
"readonly" => true, |
34
|
2 |
|
"value" => $profile->id, |
35
|
|
|
], |
36
|
|
|
|
37
|
|
|
"acronym" => [ |
38
|
2 |
|
"type" => "hidden", |
39
|
|
|
"validation" => ["not_empty"], |
40
|
|
|
"readonly" => true, |
41
|
2 |
|
"value" => $profile->acronym, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"password" => [ |
45
|
2 |
|
"type" => "hidden", |
46
|
|
|
"validation" => ["not_empty"], |
47
|
|
|
"readonly" => true, |
48
|
2 |
|
"value" => $profile->password, |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
"country" => [ |
52
|
2 |
|
"type" => "text", |
53
|
2 |
|
"value" => $profile->country, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
"city" => [ |
57
|
2 |
|
"type" => "text", |
58
|
2 |
|
"value" => $profile->city, |
59
|
|
|
], |
60
|
|
|
|
61
|
|
|
"email" => [ |
62
|
2 |
|
"type" => "text", |
63
|
2 |
|
"value" => $profile->email, |
64
|
|
|
], |
65
|
|
|
|
66
|
|
|
"score" => [ |
67
|
2 |
|
"type" => "hidden", |
68
|
|
|
"readonly" => true, |
69
|
2 |
|
"value" => $profile->score, |
70
|
|
|
], |
71
|
|
|
|
72
|
|
|
"submit" => [ |
73
|
2 |
|
"type" => "submit", |
74
|
2 |
|
"value" => "Save", |
75
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
76
|
|
|
], |
77
|
|
|
] |
78
|
|
|
); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get details on item to load form with. |
83
|
|
|
* |
84
|
|
|
* @param integer $id get details on item with id. |
85
|
|
|
* |
86
|
|
|
* @return Book |
|
|
|
|
87
|
|
|
*/ |
88
|
2 |
|
public function getProfileDetails($id) : object |
89
|
|
|
{ |
90
|
2 |
|
$profile = new Profile(); |
91
|
2 |
|
$profile->setDb($this->di->get("dbqb")); |
92
|
2 |
|
$profile->find("id", $id); |
93
|
2 |
|
return $profile; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback for submit-button which should return true if it could |
98
|
|
|
* carry out its work and false if something failed. |
99
|
|
|
* |
100
|
|
|
* @return bool true if okey, false if something went wrong. |
101
|
|
|
*/ |
102
|
|
|
public function callbackSubmit() : bool |
103
|
|
|
{ |
104
|
|
|
$profile = new Profile(); |
105
|
|
|
$profile->setDb($this->di->get("dbqb")); |
106
|
|
|
$profile->find("id", $this->form->value("id")); |
107
|
|
|
$profile->acronym = $this->form->value("acronym"); |
108
|
|
|
$profile->password = $this->form->value("password"); |
109
|
|
|
$profile->country = $this->form->value("country"); |
110
|
|
|
$profile->city = $this->form->value("city"); |
111
|
|
|
$profile->email = $this->form->value("email"); |
112
|
|
|
$profile->score = $this->form->value("score"); |
113
|
|
|
$profile->save(); |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|