1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin\Comments; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
6
|
|
|
use Apps\ActiveRecord\CommentPost; |
7
|
|
|
use Apps\Model\Admin\Comments\FormCommentModerate; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\View; |
10
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
11
|
|
|
use Ffcms\Core\Helper\Type\Any; |
12
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
13
|
|
|
use Ffcms\Core\Network\Request; |
14
|
|
|
use Ffcms\Core\Network\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait ActionPublish |
18
|
|
|
* @package Apps\Controller\Admin\Comments |
19
|
|
|
* @property Request $request |
20
|
|
|
* @property Response $response |
21
|
|
|
* @property View $view |
22
|
|
|
*/ |
23
|
|
|
trait ActionPublish |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Moderate guest comments and answer - make it publish |
27
|
|
|
* @param string $type |
28
|
|
|
* @param string|null $id |
29
|
|
|
* @return string |
30
|
|
|
* @throws NotFoundException |
31
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
32
|
|
|
*/ |
33
|
|
|
public function publish(string $type, ?string $id = null): ?string |
34
|
|
|
{ |
35
|
|
|
// check if it multiple accept ids |
36
|
|
|
if ($id === null || (int)$id < 1) { |
37
|
|
|
$ids = $this->request->query->get('selected'); |
38
|
|
|
if (!Any::isArray($ids) || !Arr::onlyNumericValues($ids)) { |
39
|
|
|
throw new NotFoundException('Bad conditions'); |
40
|
|
|
} |
41
|
|
|
$id = $ids; |
42
|
|
|
} else { |
43
|
|
|
$id = [$id]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// build query |
47
|
|
|
$query = null; |
48
|
|
|
switch ($type) { |
49
|
|
|
case static::TYPE_COMMENT: |
|
|
|
|
50
|
|
|
$query = CommentPost::whereIn('id', $id)->where('moderate', '=', 1); |
51
|
|
|
break; |
52
|
|
|
case static::TYPE_ANSWER: |
53
|
|
|
$query = CommentAnswer::whereIn('id', $id)->where('moderate', '=', 1); |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// check if result is not empty |
58
|
|
|
if ($query === null || $query->count() < 1) { |
59
|
|
|
throw new NotFoundException(__('No comments found for this condition')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// initialize moderation model |
63
|
|
|
$model = new FormCommentModerate($query, $type); |
64
|
|
|
|
65
|
|
|
// check if form is submited |
66
|
|
|
if ($model->send()) { |
67
|
|
|
$model->make(); |
68
|
|
|
App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published')); |
69
|
|
|
$this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->view->render('publish', [ |
73
|
|
|
'model' => $model |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.