1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CommentFormWidget.php |
4
|
|
|
* @author Revin Roman |
5
|
|
|
* @link https://rmrevin.ru |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace rmrevin\yii\module\Comments\widgets; |
9
|
|
|
|
10
|
|
|
use rmrevin\yii\module\Comments; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CommentFormWidget |
14
|
|
|
* @package rmrevin\yii\module\Comments\widgets |
15
|
|
|
*/ |
16
|
|
|
class CommentFormWidget extends \yii\base\Widget |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var string|null */ |
20
|
|
|
public $theme; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public $entity; |
24
|
|
|
|
25
|
|
|
/** @var Comments\models\Comment */ |
26
|
|
|
public $Comment; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public $anchor = '#comment-%d'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $viewFile = 'comment-form'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Register asset bundle |
36
|
|
|
*/ |
37
|
|
|
protected function registerAssets() |
38
|
|
|
{ |
39
|
|
|
CommentFormAsset::register($this->getView()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function run() |
46
|
|
|
{ |
47
|
|
|
$this->registerAssets(); |
48
|
|
|
|
49
|
|
|
/** @var Comments\forms\CommentCreateForm $CommentCreateForm */ |
50
|
|
|
$CommentCreateFormClassData = Comments\Module::instance()->model( |
51
|
|
|
'commentCreateForm', [ |
52
|
|
|
'Comment' => $this->Comment, |
53
|
|
|
'entity' => $this->entity, |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$CommentCreateForm = \Yii::createObject($CommentCreateFormClassData); |
58
|
|
|
|
59
|
|
|
if ($CommentCreateForm->load(\Yii::$app->getRequest()->post())) { |
60
|
|
|
if ($CommentCreateForm->validate()) { |
61
|
|
|
if ($CommentCreateForm->save()) { |
62
|
|
|
\Yii::$app->getResponse() |
63
|
|
|
->refresh(sprintf($this->anchor, $CommentCreateForm->Comment->id)) |
64
|
|
|
->send(); |
65
|
|
|
|
66
|
|
|
exit; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->render($this->viewFile, [ |
72
|
|
|
'CommentCreateForm' => $CommentCreateForm, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function getViewPath() |
80
|
|
|
{ |
81
|
|
|
return empty($this->theme) |
82
|
|
|
? parent::getViewPath() |
83
|
|
|
: (\Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . $this->theme); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.