Article.php
4.44 KB
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
<?php
namespace common\modules\calendar\models;
use Yii;
//
use thread\app\base\models\ActiveRecord;
use common\modules\calendar\Calendar as ParentModule;
/**
* Class Article
*
* @package common\modules\calendar\models
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class Article extends ActiveRecord
{
public static $commonQuery = query\ActiveQuery::class;
/**
* @return null|object|string
*/
public static function getDb()
{
return ParentModule::getDb();
}
/**
* @return string
*/
public static function tableName()
{
return '{{%calendar_article}}';
}
/**
* @return array
*/
public function rules()
{
$getFormatDate = Yii::$app->getModule('news')->getFormatDate();
return [
[['alias', 'published_time'], 'required'],
[['create_time', 'update_time'], 'integer'],
[
['published_time'],
'date',
'format' => 'php:' . $getFormatDate,
'timestampAttribute' => 'published_time'
],
[['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
[['alias', 'image_link', 'default_title'], 'string', 'max' => 255],
[['alias'], 'unique']
];
}
/**
* @return array
*/
public function scenarios()
{
return [
'published' => ['published'],
'deleted' => ['deleted'],
'backend' => ['alias', 'image_link', 'published', 'deleted', 'published_time', 'default_title'],
];
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'alias' => Yii::t('app', 'Alias'),
'image_link' => Yii::t('app', 'Image link'),
'published_time' => Yii::t('app', 'Published time'),
'created_at' => Yii::t('app', 'Create time'),
'updated_at' => Yii::t('app', 'Update time'),
'published' => Yii::t('app', 'Published'),
'deleted' => Yii::t('app', 'Deleted'),
'date_from' => Yii::t('news', 'Date from'),
'date_to' => Yii::t('news', 'Date to'),
'title' => Yii::t('app', 'Title'),
'default_title' => Yii::t('app', 'Default Title'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLang()
{
return $this->hasOne(ArticleLang::class, ['rid' => 'id']);
}
/**
* @return string
*/
public function getTitle()
{
return (isset($this->lang->title)) ? $this->lang->title : "{{$this->default_title}}";
}
/**
* @return string
*/
public function getPublishedTime()
{
$getFormatDate = Yii::$app->getModule('news')->getFormatDate();
return $this->published_time == 0 ? date($getFormatDate) : date($getFormatDate, $this->published_time);
}
/**
* ISO8601 = "Y-m-d\TH:i:sO"
* @return string
*/
public function getPublishedTimeISO()
{
return date('Y-m-d\TH:i:sO', $this->published_time);
}
/**
* ISO8601 = "Y-m-d\TH:i:sO"
* @return string
*/
public function getModifiedTimeISO()
{
return date('Y-m-d\TH:i:sO', $this->updated_at);
}
/**
* @return mixed
*/
public function getBaseUploadPath()
{
return Yii::$app->getModule('news')->getBaseUploadPath('article', $this);
}
/**
* @return mixed
*/
public function getBaseUploadUrl()
{
return Yii::$app->getModule('news')->getBaseUploadUrl('article', $this);
}
/**
* @return null|string
*/
public function getImageLink()
{
$image = null;
if (!empty($this->image_link) && is_file($this->getBaseUploadPath() . '/' . $this->image_link)) {
$image = $this->getBaseUploadUrl() . '/' . $this->image_link;
}
return $image;
}
/**
* @return null|string
*/
public function getImagePath()
{
$image = null;
if (!empty($this->image_link) && is_file($this->getBaseUploadPath() . '/' . $this->image_link)) {
$image = $this->getBaseUploadPath() . '/' . $this->image_link;
}
return $image;
}
/**
* @param $id
*/
public static function getById($id)
{
return self::find()->byID($id)->one();
}
}