1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alvo\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \Alvo\Comment\HTMLForm\AddCommentForm; |
8
|
|
|
use \Alvo\Comment\HTMLForm\EditCommentForm; |
9
|
|
|
use \Alvo\User\User; |
10
|
|
|
|
11
|
|
|
class CommentController implements InjectionAwareInterface |
12
|
|
|
{ |
13
|
|
|
use InjectionAwareTrait; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
public function init() |
18
|
|
|
{ |
19
|
1 |
|
$this->db = $this->di->get("db"); |
|
|
|
|
20
|
1 |
|
$this->view = $this->di->get("view"); |
|
|
|
|
21
|
1 |
|
$this->pageRender = $this->di->get("pageRender"); |
|
|
|
|
22
|
1 |
|
$this->response = $this->di->get("response"); |
|
|
|
|
23
|
|
|
// $this->comment = new Comment(); |
24
|
1 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function getIndex() |
29
|
|
|
{ |
30
|
|
|
$title = "Comments"; |
31
|
|
|
|
32
|
|
|
$comment = new Comment(); |
33
|
|
|
$comment->setDb($this->di->get("db")); |
34
|
|
|
$posts = $comment->getAllComments() ? $comment->getAllComments() : []; |
35
|
|
|
// debug($posts); |
36
|
|
|
|
37
|
|
|
$user = $this->di->get("user"); |
38
|
|
|
$user->setDb($this->db); |
39
|
|
|
$user = $user->getUser(); |
40
|
|
|
|
41
|
|
|
$form = new AddCommentForm($this->di); |
42
|
|
|
$form->check(); |
43
|
|
|
|
44
|
|
|
$this->view->add("comments/index", [ |
45
|
|
|
"posts" => $posts, |
46
|
|
|
"form" => $form->getHTML(), |
47
|
|
|
"user" => $user |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function editItem($id) |
54
|
|
|
{ |
55
|
|
|
$title = "Edit comment"; |
56
|
|
|
|
57
|
|
|
// $user = $this->di->get("user"); |
58
|
|
|
// $user->setDb($this->db); |
59
|
|
|
// $user = $user->getUser(); |
60
|
|
|
|
61
|
|
|
$form = new EditCommentForm($this->di, $id); |
62
|
|
|
$form->check(); |
63
|
|
|
|
64
|
|
|
$this->view->add("comments/edit", [ |
65
|
|
|
"form" => $form->getHTML(), |
66
|
|
|
// "user" => $user |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function deleteItem($id) |
73
|
|
|
{ |
74
|
|
|
$comment = new Comment(); |
75
|
|
|
$comment->setDb($this->db); |
76
|
|
|
$comment->find("id", $id); |
77
|
|
|
$comment->deleted = date("Y-m-d H:i:s"); |
|
|
|
|
78
|
|
|
$comment->save(); |
79
|
|
|
|
80
|
|
|
$this->response->redirect("comments"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// public function loginRequired() |
84
|
|
|
// { |
85
|
|
|
// $user = $this->di->get("user"); |
86
|
|
|
// if (!$user->isLoggedIn()) { |
87
|
|
|
// $this->response->redirect("user/login"); |
88
|
|
|
// } |
89
|
|
|
// } |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
// TODO: |
94
|
|
|
// Users CRUD |
95
|
|
|
|