|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.writesdown.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
|
5
|
|
|
* @license http://www.writesdown.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace common\models; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\db\ActiveRecord; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This is the model class for table "{{%post_comment}}". |
|
15
|
|
|
* |
|
16
|
|
|
* @property integer $id |
|
17
|
|
|
* @property string $author |
|
18
|
|
|
* @property string $email |
|
19
|
|
|
* @property string $url |
|
20
|
|
|
* @property string $ip |
|
21
|
|
|
* @property string $date |
|
22
|
|
|
* @property string $content |
|
23
|
|
|
* @property string $status |
|
24
|
|
|
* @property string $agent |
|
25
|
|
|
* @property integer $parent |
|
26
|
|
|
* @property integer $user_id |
|
27
|
|
|
* |
|
28
|
|
|
* @author Agiel K. Saputra <[email protected]> |
|
29
|
|
|
* @since 0.1.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class BaseComment extends ActiveRecord |
|
32
|
|
|
{ |
|
33
|
|
|
const STATUS_APPROVED = "approved"; |
|
34
|
|
|
const STATUS_NOT_APPROVED = "unapproved"; |
|
35
|
|
|
const STATUS_TRASHED = "trashed"; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var BaseComment[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public $child; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function scenarios() |
|
46
|
|
|
{ |
|
47
|
|
|
$scenarios = parent::scenarios(); |
|
48
|
|
|
$scenarios['reply'] = $scenarios['default']; |
|
49
|
|
|
|
|
50
|
|
|
return $scenarios; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function rules() |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
|
|
[ |
|
60
|
|
|
['author', 'email'], |
|
61
|
|
|
'required', |
|
62
|
|
|
'when' => function () { |
|
63
|
|
|
return Option::get('require_name_email') && Yii::$app->user->isGuest ? true : false; |
|
64
|
|
|
}, |
|
65
|
|
|
], |
|
66
|
|
|
['email', 'filter', 'filter' => 'trim'], |
|
67
|
|
|
['email', 'email'], |
|
68
|
|
|
['content', 'required'], |
|
69
|
|
|
['status', 'default', 'value' => self::STATUS_NOT_APPROVED], |
|
70
|
|
|
[ |
|
71
|
|
|
'status', |
|
72
|
|
|
'in', |
|
73
|
|
|
'range' => [self::STATUS_APPROVED, self::STATUS_NOT_APPROVED, self::STATUS_TRASHED], |
|
74
|
|
|
], |
|
75
|
|
|
[['parent', 'user_id'], 'integer'], |
|
76
|
|
|
['parent', 'default', 'value' => 0], |
|
77
|
|
|
[['author', 'content'], 'string'], |
|
78
|
|
|
['date', 'safe'], |
|
79
|
|
|
[['email', 'ip'], 'string', 'max' => 100], |
|
80
|
|
|
['agent', 'string', 'max' => 255], |
|
81
|
|
|
['url', 'url'], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function attributeLabels() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
'id' => Yii::t('writesdown', 'ID'), |
|
92
|
|
|
'author' => Yii::t('writesdown', 'Name'), |
|
93
|
|
|
'email' => Yii::t('writesdown', 'Email'), |
|
94
|
|
|
'url' => Yii::t('writesdown', 'URL'), |
|
95
|
|
|
'ip' => Yii::t('writesdown', 'IP'), |
|
96
|
|
|
'date' => Yii::t('writesdown', 'Date'), |
|
97
|
|
|
'content' => Yii::t('writesdown', 'Content'), |
|
98
|
|
|
'status' => Yii::t('writesdown', 'Status'), |
|
99
|
|
|
'agent' => Yii::t('writesdown', 'Agent'), |
|
100
|
|
|
'parent' => Yii::t('writesdown', 'Parent'), |
|
101
|
|
|
'user_id' => Yii::t('writesdown', 'User ID'), |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return \yii\db\ActiveQuery |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getCommentPost() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->hasOne(Post::className(), ['id' => 'post_id']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get comment status in array |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getStatuses() |
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
121
|
|
|
self::STATUS_APPROVED => Yii::t('writesdown', 'Approved'), |
|
122
|
|
|
self::STATUS_NOT_APPROVED => Yii::t('writesdown', 'Not Approved'), |
|
123
|
|
|
self::STATUS_TRASHED => Yii::t('writesdown', 'Trashed'), |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
|
|
public function beforeSave($insert) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
if (parent::beforeSave($insert)) { |
|
133
|
|
|
if ($this->isNewRecord) { |
|
134
|
|
|
if (!Yii::$app->user->isGuest) { |
|
135
|
|
|
$this->user_id = Yii::$app->user->id; |
|
136
|
|
|
$this->email = Yii::$app->user->identity->email; |
|
137
|
|
|
$this->author = Yii::$app->user->identity->display_name; |
|
138
|
|
|
} |
|
139
|
|
|
$this->agent = $_SERVER['HTTP_USER_AGENT']; |
|
140
|
|
|
$this->ip = $_SERVER['REMOTE_ADDR']; |
|
141
|
|
|
$this->date = date('Y-m-d H:i:s'); |
|
142
|
|
|
$this->status = self::STATUS_APPROVED; |
|
143
|
|
|
if (Option::get('comment_moderation') && Yii::$app->user->isGuest) { |
|
144
|
|
|
if (Option::get('comment_whitelist') && Option::get('require_name_email')) { |
|
145
|
|
|
$hasComment = static::find() |
|
146
|
|
|
->andWhere(['email' => $this->email]) |
|
147
|
|
|
->andWhere(['status' => self::STATUS_APPROVED]) |
|
148
|
|
|
->count(); |
|
149
|
|
|
if (!$hasComment) { |
|
150
|
|
|
$this->status = self::STATUS_NOT_APPROVED; |
|
151
|
|
|
} |
|
152
|
|
|
} else { |
|
153
|
|
|
$this->status = self::STATUS_NOT_APPROVED; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: