|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Comments; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\App as AppRecord; |
|
6
|
|
|
use Apps\Model\Api\Comments\CommentAnswerAdd; |
|
7
|
|
|
use Apps\Model\Api\Comments\CommentPostAdd; |
|
8
|
|
|
use Apps\Model\Api\Comments\EntityCommentData; |
|
9
|
|
|
use Ffcms\Core\App; |
|
10
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
11
|
|
|
use Ffcms\Core\Network\Request; |
|
12
|
|
|
use Ffcms\Core\Network\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait ActionAdd |
|
16
|
|
|
* @package Apps\Controller\Api\Comments |
|
17
|
|
|
* @property Request $request |
|
18
|
|
|
* @property Response $response |
|
19
|
|
|
* @method void setJsonHeader() |
|
20
|
|
|
*/ |
|
21
|
|
|
trait ActionAdd |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Add comment or answer via ajax. |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @throws NativeException |
|
27
|
|
|
* @throws \Ffcms\Core\Exception\JsonException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function add(): ?string |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setJsonHeader(); |
|
32
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
33
|
|
|
|
|
34
|
|
|
$replayTo = (int)$this->request->request->get('replay-to'); |
|
35
|
|
|
$model = null; |
|
36
|
|
|
// check if its a answer (comment answer type) |
|
37
|
|
|
if ($replayTo > 0) { |
|
38
|
|
|
$model = new CommentAnswerAdd($configs); |
|
|
|
|
|
|
39
|
|
|
$model->replayTo = $replayTo; |
|
40
|
|
|
} else { // sounds like new comment row |
|
41
|
|
|
$model = new CommentPostAdd($configs); |
|
|
|
|
|
|
42
|
|
|
$model->appName = App::$Security->strip_tags($this->request->request->get('app_name')); |
|
43
|
|
|
$model->appId = (int)$this->request->request->get('app_id'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// pass general comment params to model |
|
47
|
|
|
$model->message = App::$Security->secureHtml((string)$this->request->request->get('message')); |
|
48
|
|
|
$model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name')); |
|
49
|
|
|
|
|
50
|
|
|
// check model conditions before add new row |
|
51
|
|
|
if (!$model|| !$model->check()) { |
|
52
|
|
|
throw new NativeException('Unknown error'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// add comment post or answer to database and get response active record row |
|
56
|
|
|
$record = $model->buildRecord(); |
|
57
|
|
|
// pass row to entity builder model |
|
58
|
|
|
$response = new EntityCommentData($record); |
|
59
|
|
|
|
|
60
|
|
|
return json_encode([ |
|
61
|
|
|
'status' => 1, |
|
62
|
|
|
'data' => $response->make() // build row to standard format |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|