1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Nicklas\Comment\User; |
8
|
|
|
use \Nicklas\Comment\Comment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Form to update an item. |
12
|
|
|
*/ |
13
|
|
|
class EditCommentForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container and the id to update. |
17
|
|
|
* |
18
|
|
|
* @param Anax\DI\DIInterface $di a service container |
19
|
|
|
* @param integer $id to update |
20
|
|
|
*/ |
21
|
|
|
public function __construct(DIInterface $di, $id) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($di); |
24
|
|
|
$this->comment = $this->getCommentDetails($id); |
|
|
|
|
25
|
|
|
$this->form->create( |
26
|
|
|
[ |
27
|
|
|
"id" => __CLASS__, |
28
|
|
|
"legend" => "Update details of the (comment {$this->comment->id})", |
29
|
|
|
"fieldset" => true |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
"comment" => [ |
33
|
|
|
"type" => "textarea", |
34
|
|
|
"validation" => ["not_empty"], |
35
|
|
|
"value" => $this->comment->comment, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"submit" => [ |
39
|
|
|
"type" => "submit", |
40
|
|
|
"value" => "Save", |
41
|
|
|
"callback" => [$this, "callbackSubmit"] |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"reset" => [ |
45
|
|
|
"type" => "reset", |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
"delete" => [ |
49
|
|
|
"type" => "submit", |
50
|
|
|
"value" => "Delete", |
51
|
|
|
"callback" => [$this, "callBackDelete"] |
52
|
|
|
], |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get details on item to load form with. |
61
|
|
|
* |
62
|
|
|
* @param integer $id get details on item with id. |
63
|
|
|
* |
64
|
|
|
* @return object true if okey, false if something went wrong. |
65
|
|
|
*/ |
66
|
|
|
public function getCommentDetails($id) |
67
|
|
|
{ |
68
|
|
|
$comment = new Comment(); |
69
|
|
|
$comment->setDb($this->di->get("db")); |
70
|
|
|
$comment->find("id", $id); |
71
|
|
|
return $comment; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* get details for the user based on sessio |
77
|
|
|
* |
78
|
|
|
* |
79
|
|
|
* @return object the user |
80
|
|
|
*/ |
81
|
|
|
public function controlAuthority() |
82
|
|
|
{ |
83
|
|
|
// Get logged in users details, based on session |
84
|
|
|
$userName = $this->di->get("session")->get("user"); |
85
|
|
|
$user = new User(); |
86
|
|
|
$user->setDb($this->di->get("db")); |
87
|
|
|
$user->find("name", $userName); |
88
|
|
|
|
89
|
|
|
// IF AUTHORITY == admin, then continue |
90
|
|
|
if ($user->authority != "admin") { |
91
|
|
|
return ($user->name == $this->comment->user); |
92
|
|
|
} |
93
|
|
|
return true; |
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 void |
101
|
|
|
*/ |
102
|
|
|
public function callbackDelete() |
103
|
|
|
{ |
104
|
|
|
// Form values |
105
|
|
|
$comment = $this->comment; |
106
|
|
|
|
107
|
|
|
if (!$this->controlAuthority($comment)) { |
|
|
|
|
108
|
|
|
$this->form->addOutput("You can't edit comment #$comment->id"); |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$comment->delete(); |
113
|
|
|
$this->di->get("response")->redirect("comment"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Callback for submit-button which should return true if it could |
118
|
|
|
* carry out its work and false if something failed. |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function callbackSubmit() |
123
|
|
|
{ |
124
|
|
|
$comment = $this->comment; |
125
|
|
|
|
126
|
|
|
if (!$this->controlAuthority($comment)) { |
|
|
|
|
127
|
|
|
$this->form->addOutput("You can't edit comment #$comment->id"); |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$comment->comment = $this->form->value("comment"); |
132
|
|
|
$comment->save(); |
133
|
|
|
$this->di->get("response")->redirect("comment/edit/{$comment->id}"); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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: