1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Nicklas\Comment\Modules\User; |
8
|
|
|
use \Nicklas\Comment\Modules\Comment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Form to update an item. |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
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 = new Comment($di->get("db")); |
|
|
|
|
25
|
|
|
$this->comment->find("id", $id); |
26
|
|
|
if ($this->comment->id == null) { |
27
|
|
|
$di->get("response")->redirect("question"); |
28
|
|
|
} |
29
|
|
|
$this->form->create( |
30
|
|
|
[ |
31
|
|
|
"id" => __CLASS__, |
32
|
|
|
"legend" => "Här kan du uppdatera din kommentar" |
33
|
|
|
], |
34
|
|
|
[ |
35
|
|
|
"text" => [ |
36
|
|
|
"type" => "textarea", |
37
|
|
|
"validation" => ["not_empty"], |
38
|
|
|
"value" => $this->comment->text, |
39
|
|
|
], |
40
|
|
|
|
41
|
|
|
"submit" => [ |
42
|
|
|
"type" => "submit", |
43
|
|
|
"value" => "Spara", |
44
|
|
|
"callback" => [$this, "callbackSubmit"] |
45
|
|
|
], |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Callback for submit-button which should return true if it could |
55
|
|
|
* carry out its work and false if something failed. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function callbackSubmit() |
60
|
|
|
{ |
61
|
|
|
// Get values from the submitted form |
62
|
|
|
$text = $this->form->value("text"); |
63
|
|
|
|
64
|
|
|
if (!$this->di->get('session')->has("user")) { |
65
|
|
|
$this->form->addOutput("Du behöver logga in"); |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$user = new User($this->di->get("db")); |
70
|
|
|
if ($user->controlAuthority($this->di->get('session')->get("user"), $this->comment->user) != true) { |
|
|
|
|
71
|
|
|
$this->form->addOutput("Du får inte redigera denna."); |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($text == "") { |
76
|
|
|
$this->form->addOutput("Du skrev aldrig något. Skriv gärna något."); |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->comment->text = $text; |
81
|
|
|
$this->comment->save(); |
82
|
|
|
$this->form->addOutput("Du har uppdaterat kommentaren"); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.