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\Post; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Example of FormModel implementation. |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class EditPostForm extends FormModel |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Anax\DI\DIInterface $di a service container |
19
|
|
|
*/ |
20
|
|
|
public function __construct(DIInterface $di, $id) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($di); |
23
|
|
|
$this->post = new Post($di->get("db")); |
|
|
|
|
24
|
|
|
$this->post->find("id", $id); |
25
|
|
|
if ($this->post->id == null) { |
26
|
|
|
$di->get("response")->redirect("question"); |
27
|
|
|
} |
28
|
|
|
$this->form->create( |
29
|
|
|
[ |
30
|
|
|
"id" => __CLASS__, |
31
|
|
|
"legend" => "Här kan du uppdatera din post" |
32
|
|
|
], |
33
|
|
|
[ |
34
|
|
|
"text" => [ |
35
|
|
|
"type" => "textarea", |
36
|
|
|
"value" => $this->post->text, |
37
|
|
|
"label" => "Här kan du skriva din fråga", |
38
|
|
|
"placeholder" => "Din fråga" |
39
|
|
|
], |
40
|
|
|
"submit" => [ |
41
|
|
|
"type" => "submit", |
42
|
|
|
"value" => "Spara", |
43
|
|
|
"callback" => [$this, "callbackSubmit"] |
44
|
|
|
], |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Callback for submit-button which should return true if it could |
51
|
|
|
* carry out its work and false if something failed. |
52
|
|
|
* |
53
|
|
|
* @return boolean true if okey, false if something went wrong. |
54
|
|
|
*/ |
55
|
|
|
public function callbackSubmit() |
56
|
|
|
{ |
57
|
|
|
// Get values from the submitted form |
58
|
|
|
$text = $this->form->value("text"); |
59
|
|
|
|
60
|
|
|
if (!$this->di->get('session')->has("user")) { |
61
|
|
|
$this->form->addOutput("Du behöver logga in"); |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$user = new User($this->di->get("db")); |
66
|
|
|
if ($user->controlAuthority($this->di->get('session')->get("user"), $this->post->user) != true) { |
|
|
|
|
67
|
|
|
$this->form->addOutput("Du får inte redigera denna."); |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($text == "") { |
72
|
|
|
$this->form->addOutput("Du skrev aldrig något. Skriv gärna något."); |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->post->text = $text; |
77
|
|
|
$this->post->save(); |
78
|
|
|
$this->form->addOutput("Du har uppdaterat inlägget"); |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.