1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lyco\Post\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Lyco\Post\Post; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to update an item. |
11
|
|
|
*/ |
12
|
|
|
class UpdateForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
public $postId; |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container and the id to update. |
17
|
|
|
* |
18
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
19
|
|
|
* @param integer $postId to update |
20
|
|
|
*/ |
21
|
|
|
public function __construct(ContainerInterface $di, $postId) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($di); |
24
|
|
|
$post = $this->getItemDetails($postId); |
25
|
|
|
$this->form->create( |
26
|
|
|
[ |
27
|
|
|
"postId" => __CLASS__, |
28
|
|
|
"legend" => "Update details of the item", |
29
|
|
|
], |
30
|
|
|
[ |
31
|
|
|
"postId" => [ |
32
|
|
|
"type" => "text", |
33
|
|
|
"validation" => ["not_empty"], |
34
|
|
|
"readonly" => true, |
35
|
|
|
"value" => $post->postId, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"acronym" => [ |
39
|
|
|
"type" => "text", |
40
|
|
|
"validation" => ["not_empty"], |
41
|
|
|
"value" => $post->acronym, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"title" => [ |
45
|
|
|
"type" => "text", |
46
|
|
|
"validation" => ["not_empty"], |
47
|
|
|
"value" => $post->title, |
48
|
|
|
], |
49
|
|
|
|
50
|
|
|
"text" => [ |
51
|
|
|
"type" => "text", |
52
|
|
|
"validation" => ["not_empty"], |
53
|
|
|
"value" => $post->text, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
"submit" => [ |
57
|
|
|
"type" => "submit", |
58
|
|
|
"value" => "Save", |
59
|
|
|
"callback" => [$this, "callbackSubmit"] |
60
|
|
|
], |
61
|
|
|
|
62
|
|
|
"reset" => [ |
63
|
|
|
"type" => "reset", |
64
|
|
|
], |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get details on item to load form with. |
73
|
|
|
* |
74
|
|
|
* @param integer $postId get details on item with id. |
75
|
|
|
* |
76
|
|
|
* @return Post |
77
|
|
|
*/ |
78
|
|
|
public function getItemDetails($postId) : object |
79
|
|
|
{ |
80
|
|
|
$post = new Post(); |
81
|
|
|
$post->setDb($this->di->get("dbqb")); |
82
|
|
|
$post->find("postId", $postId); |
83
|
|
|
return $post; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Callback for submit-button which should return true if it could |
90
|
|
|
* carry out its work and false if something failed. |
91
|
|
|
* |
92
|
|
|
* @return bool true if okey, false if something went wrong. |
93
|
|
|
*/ |
94
|
|
|
public function callbackSubmit() : bool |
95
|
|
|
{ |
96
|
|
|
$post = new Post(); |
97
|
|
|
$post->setDb($this->di->get("dbqb")); |
98
|
|
|
$post->find("postId", $this->form->value("postId")); |
99
|
|
|
$post->acronym = $this->form->value("acronym"); |
100
|
|
|
$post->title = $this->form->value("title"); |
101
|
|
|
$post->text = $this->form->value("text"); |
102
|
|
|
$post->save(); |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Callback what to do if the form was successfully submitted, this |
108
|
|
|
* happen when the submit callback method returns true. This method |
109
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
110
|
|
|
*/ |
111
|
|
|
public function callbackSuccess() |
112
|
|
|
{ |
113
|
|
|
$this->di->get("response")->redirect("post")->send(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|