-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBlogComment.php
184 lines (166 loc) · 4.7 KB
/
BlogComment.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Project: yii2-blog for internal using
* Author: akiraz2
* Copyright (c) 2018.
*/
namespace akiraz2\blog\models;
use akiraz2\blog\Module;
use akiraz2\blog\traits\IActiveStatus;
use akiraz2\blog\traits\ModuleTrait;
use akiraz2\blog\traits\StatusTrait;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Html;
use yii\helpers\StringHelper;
use yii\helpers\Url;
/**
* This is the model class for table "blog_comment".
*
* @property integer $id
* @property integer $post_id
* @property string $content
* @property string $author
* @property string $email
* @property string $url
* @property string $captcha
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*
* @property BlogPost $post
*/
class BlogComment extends \yii\db\ActiveRecord
{
use StatusTrait, ModuleTrait;
const SCENARIO_ADMIN = 'admin';
const SCENARIO_USER = 'user';
public $captcha;
private $_status;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%blog_comment}}';
}
/**
* created_at, updated_at to now()
* crate_user_id, update_user_id to current login user id
*/
public function behaviors()
{
return [
'class' => TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_ADMIN] = ['post_id', 'content', 'author', 'email', 'status', 'url'];
$scenarios[self::SCENARIO_USER] = ['content', 'author', 'email', 'captcha'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['post_id', 'content', 'author', 'email'], 'required'],
['email', 'email'],
[['author', 'content'], 'filter', 'filter' => 'strip_tags'],
[['author', 'captcha', 'email'], 'trim'],
[['post_id', 'status'], 'integer'],
[['content'], 'string'],
[['author', 'email', 'url'], 'string', 'max' => 128],
['captcha', 'captcha', 'captchaAction' => Url::to('/blog/default/captcha'), 'on' => self::SCENARIO_USER],
['captcha', 'required', 'on' => self::SCENARIO_USER]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Module::t('blog', 'ID'),
'post_id' => Module::t('blog', 'Post ID'),
'content' => Module::t('blog', 'Content'),
'author' => Module::t('blog', 'Author'),
'email' => Module::t('blog', 'Email'),
'url' => Module::t('blog', 'Url'),
'status' => Module::t('blog', 'Status'),
'created_at' => Module::t('blog', 'Created At'),
'updated_at' => Module::t('blog', 'Updated At'),
'captcha' => Module::t('blog', 'Verify code'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPost()
{
return $this->hasOne(BlogPost::className(), ['id' => 'post_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBlogPost()
{
return $this->hasOne(BlogPost::className(), ['id' => 'post_id']);
}
/**
* @return string
*/
public function getAuthorLink()
{
if (!empty($this->url)) {
return Html::a(Html::encode($this->author), $this->url);
} else {
return Html::encode($this->author);
}
}
/**
* @param null $post
* @return string
*/
public function getUrl($post = null)
{
if ($post === null) {
$post = $this->post;
}
return $post->url . '#c' . $this->id;
}
/**
* @param int $limit
* @return array|\yii\db\ActiveRecord[]
*/
public static function findRecentComments($limit = 10)
{
return self::find()->joinWith('blogPost')->where([
'{{%blog_comment}}.status' => IActiveStatus::STATUS_ACTIVE,
'{{%blog_post}}.status' => IActiveStatus::STATUS_ACTIVE,
])->orderBy([
'created_at' => SORT_DESC
])->limit($limit)->all();
}
/**
* @return string
*/
public function getMaskedEmail()
{
list($email_username, $email_domain) = explode('@', $this->email);
$masked_email_username = preg_replace('/(.)./', "$1*", $email_username);
return implode('@', array($masked_email_username, $email_domain));
}
/**
* @return string
*/
public function getContent()
{
return ($this->status == IActiveStatus::STATUS_ACTIVE) ? $this->content : StringHelper::truncate($this->content, 15);
}
}