-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommentsWidget.php
64 lines (53 loc) · 1.45 KB
/
CommentsWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace spanjeta\comments;
use spanjeta\comments\models\Comment;
use yii\data\ActiveDataProvider;
/**
* This is just an example.
*/
class CommentsWidget extends \yii\base\Widget
{
public $disabled = false;
/**
*
* @var Model
*/
public $model;
public $readOnly = false;
protected function getRecentComments()
{
if ($this->model == null)
return null;
$query = Comment::find([
'model_type' => get_class($this->model),
'model_id' => $this->model->id
]);
return new ActiveDataProvider(['query' =>$query]);
}
protected function formModel()
{
$comment = null;
if ($this->readOnly == false) {
$comment = new Comment();
$comment->model_type = get_class($this->model);
$comment->model_id = $this->model->id;
}
return $comment;
}
public function run()
{
if ($this->disabled)
return; //Do nothing
if (isset($_POST['Comment'])) {
$comment = new Comment();
$comment->load($_POST['Comment']);
$comment->model_type = get_class($this->model);
$comment->model_id = $this->model->id;
$comment->save();
}
echo $this->render('comments', [
'comments' => $this->getRecentComments(),
'model' => $this->formModel()
]);
}
}