This repository has been archived by the owner on Aug 24, 2018. It is now read-only.
/
CommentController.php
116 lines (93 loc) · 2.89 KB
/
CommentController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Epixa - Forum
*/
namespace Post\Controller;
use Epixa\Controller\AbstractController,
Post\Service\Post as PostService,
Post\Service\Comment as CommentService,
Post\Model\Comment as CommentModel,
Post\Form\Comment as CommentForm;
/**
* Manage comment functionality
*
* @category Module
* @package Post
* @subpackage Controller
* @copyright 2011 epixa.com - Court Ewing
* @license http://github.com/epixa/Forum/blob/master/LICENSE New BSD
* @author Court Ewing (court@epixa.com)
*/
class CommentController extends AbstractController
{
/**
* Create a new comment
*/
public function addAction()
{
$request = $this->getRequest();
$this->_helper->assert->isAjax($request);
$this->_helper->assert->isPost($request);
$form = new CommentForm();
if (!$form->isValid($request->getPost())) {
$this->_helper->ajax->invalid($form);
return;
}
$service = new PostService();
$post = $service->get($request->getPost('id', null));
$this->_helper->assert->isset($post);
$comment = $service->add($form->getValues(), $post);
$this->_helper->ajax->success(array(
'comment' => array(
'id' => $comment->id
)
));
}
/**
* Edit an existing comment
*/
public function editAction()
{
$request = $this->getRequest();
$this->_helper->assert->isAjax($request);
$this->_helper->assert->isPost($request);
$comment = $this->getCurrentComment();
$form = new CommentForm();
if (!$form->isValid($request->getPost())) {
$this->_helper->ajax->invalid($form);
return;
}
$service = new CommentService();
$service->edit($comment, $form->getValues());
$this->_helper->ajax->success(array(
'comment' => array(
'id' => $comment->id
)
));
}
/**
* Delete an existing comment
*/
public function deleteAction()
{
$comment = $this->getCurrentComment();
$request = $this->getRequest();
$this->_helper->assert->isAjax($request);
$this->_helper->assert->isPost($request);
$service = new CommentService();
$service->delete($comment);
$this->_helper->ajax->success();
}
/**
* Get the current comment by the id from the request
*
* @return CommentModel
*/
public function getCurrentComment()
{
$service = new CommentService();
$post = $service->get($this->getRequest()->getParam('id', null));
$this->_helper->assert->isset($post, 'Epixa\Exception\NotFoundException');
return $post;
}
}