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\FormCommentUpdate; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\View; |
10
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
11
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
12
|
|
|
use Ffcms\Core\Helper\Type\Any; |
13
|
|
|
use Ffcms\Core\Network\Request; |
14
|
|
|
use Ffcms\Core\Network\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait ActionEdit |
18
|
|
|
* @package Apps\Controller\Admin\Comments |
19
|
|
|
* @property Request $request |
20
|
|
|
* @property Response $response |
21
|
|
|
* @property View $view |
22
|
|
|
*/ |
23
|
|
|
trait ActionEdit |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Commentaries and answers edit action |
27
|
|
|
* @param string $type |
28
|
|
|
* @param string $id |
29
|
|
|
* @throws NotFoundException |
30
|
|
|
* @return string |
31
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
32
|
|
|
*/ |
33
|
|
|
public function edit(string $type, string $id): ?string |
34
|
|
|
{ |
35
|
|
|
if (!Any::isInt($id) || $id < 1) { |
36
|
|
|
throw new SyntaxException('Bad arguments'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// get active record by type and id from active records |
40
|
|
|
$record = null; |
41
|
|
|
switch ($type) { |
42
|
|
|
case static::TYPE_COMMENT: |
|
|
|
|
43
|
|
|
$record = CommentPost::find($id); |
44
|
|
|
break; |
45
|
|
|
case static::TYPE_ANSWER: |
|
|
|
|
46
|
|
|
$record = CommentAnswer::find($id); |
47
|
|
|
break; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// check if response is not empty |
51
|
|
|
if (!$record) { |
52
|
|
|
throw new NotFoundException(__('Comment is not founded')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// init edit model |
56
|
|
|
$model = new FormCommentUpdate($record, $type); |
57
|
|
|
|
58
|
|
|
// check if data is submited and validated |
59
|
|
|
if ($model->send() && $model->validate()) { |
60
|
|
|
$model->make(); |
61
|
|
|
App::$Session->getFlashBag()->add('success', __('Comment or answer is successful updated')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// render view |
65
|
|
|
return $this->view->render('comments/edit', [ |
66
|
|
|
'model' => $model |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|