1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Comments; |
4
|
|
|
|
5
|
|
|
use Apps\Model\Api\Comments\CommentAnswerAdd; |
6
|
|
|
use Apps\Model\Api\Comments\CommentPostAdd; |
7
|
|
|
use Apps\Model\Api\Comments\EntityCommentData; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Exception\NativeException; |
10
|
|
|
use Ffcms\Core\Network\Request; |
11
|
|
|
use Ffcms\Core\Network\Response; |
12
|
|
|
use Apps\ActiveRecord\App as AppRecord; |
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\SyntaxException |
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->pathway = App::$Security->strip_tags($this->request->request->get('pathway')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// pass general comment params to model |
46
|
|
|
$model->message = App::$Security->secureHtml((string)$this->request->request->get('message')); |
47
|
|
|
$model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name')); |
48
|
|
|
|
49
|
|
|
// check model conditions before add new row |
50
|
|
|
if ($model === null || !$model->check()) { |
51
|
|
|
throw new NativeException('Unknown error'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// add comment post or answer to database and get response active record row |
55
|
|
|
$record = $model->buildRecord(); |
56
|
|
|
// pass row to entity builder model |
57
|
|
|
$response = new EntityCommentData($record); |
58
|
|
|
|
59
|
|
|
return json_encode([ |
60
|
|
|
'status' => 1, |
61
|
|
|
'data' => $response->make() // build row to standard format |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.