@@ 13-85 (lines=73) @@ | ||
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 = 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 |
@@ 13-81 (lines=69) @@ | ||
10 | /** |
|
11 | * Example of FormModel implementation. |
|
12 | */ |
|
13 | 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 |