/
BlogPosts.php
209 lines (190 loc) · 5.83 KB
/
BlogPosts.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace linchpinstudios\blog\models;
use Yii;
use common\models\User;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use linchpinstudios\blog\models\BlogTermRelationships;
/**
* This is the model class for table "blog_posts".
*
* @property integer $id
* @property integer $user_id
* @property integer $comments
* @property string $title
* @property string $body
* @property string $thumbnail
* @property string $excerpt
* @property string $status
* @property string $slug
* @property string $date
* @property string $date_gmt
* @property string $modified
* @property string $modified_gmt
*
* @property BlogComments[] $blogComments
* @property BlogPostTags[] $blogPostTags
* @property User $author
* @property BlogCategories $category
* @property BlogMeta[] $blogMetas
* @property User $user
*/
class BlogPosts extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'modified' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'modified',
ActiveRecord::EVENT_BEFORE_UPDATE => 'modified',
],
'value' => function() {
return date('Y-m-d H:i:s');
},
],
'modifiedGMT' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'modified_gmt',
ActiveRecord::EVENT_BEFORE_UPDATE => 'modified_gmt',
],
'value' => function() {
return gmdate('Y-m-d H:i:s');
},
],
'date' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'date',
ActiveRecord::EVENT_BEFORE_UPDATE => 'date',
],
'value' => function() {
return (empty($this->date) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',strtotime($this->date)));
},
],
'dateGMT' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'date_gmt',
ActiveRecord::EVENT_BEFORE_UPDATE => 'date_gmt',
],
'value' => function() {
return (empty($this->date) ? gmdate('Y-m-d H:i:s') : gmdate('Y-m-d H:i:s',strtotime($this->date)));
},
],
'slug' => [
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'slug',
ActiveRecord::EVENT_BEFORE_UPDATE => 'slug',
],
'value' => function() {
return (empty($this->slug) ? $this->genSlug($this->title) : $this->genSlug($this->slug));
},
],
];
}
public static function genSlug($str, $replace=array(), $delimiter='-'){
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%blog_posts}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'comments', 'thumbnail'], 'integer'],
[['body', 'excerpt'], 'string'],
[['date', 'date_gmt', 'modified', 'modified_gmt'], 'safe'],
[['title'], 'string', 'max' => 555],
[['status'], 'string', 'max' => 20],
[['slug'], 'string', 'max' => 45]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'comments' => 'Comments',
'title' => 'Title',
'body' => 'Body',
'thumbnail' => 'Thumbnail',
'excerpt' => 'Excerpt',
'status' => 'Status',
'slug' => 'Slug',
'date' => 'Date',
'date_gmt' => 'Date Gmt',
'modified' => 'Modified',
'modified_gmt' => 'Modified Gmt',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBlogComments()
{
return $this->hasMany(BlogComments::className(), ['post_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBlogPostTags()
{
return $this->hasMany(BlogPostTags::className(), ['postId' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthor()
{
return $this->hasOne(User::className(), ['id' => 'authorId']);
}
public function getAuthorList()
{
$model = User::find()->asArray()->all();
return ArrayHelper::map($model, 'id', 'username');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTerms()
{
return $this->hasMany(BlogTermRelationships::className(), ['post_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBlogMetas()
{
return $this->hasMany(BlogMeta::className(), ['post_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}