-
Notifications
You must be signed in to change notification settings - Fork 0
/
Category.php
executable file
·209 lines (187 loc) · 5.3 KB
/
Category.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 common\models;
use common\behaviors\PositionBehavior;
use common\behaviors\CacheInvalidateBehavior;
use common\behaviors\MetaBehavior;
use common\helpers\Tree;
use common\models\behaviors\CategoryBehavior;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\caching\TagDependency;
use yii\db\Expression;
/**
* This is the model class for table "{{%article}}".
*
* @property int $id
* @property int $pid
* @property string $title
* @property string $slug
* @property string $description
* @property int $created_at
* @property int $updated_at
* @property string $module
* @property string $cover
* @property int $allow_publish
*/
class Category extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%category}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title'], 'required'],
//中文没法自动生成slug,又没必要必填
['slug', 'default', 'value' => function ($model) {
return $model->title;
}],
['module', 'safe'],
[['pid', 'sort', 'allow_publish'], 'integer'],
['pid', 'default', 'value' => 0],
[['sort'], 'default', 'value' => 0]
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => '分类名',
'slug' => '标识',
'pid' => '上级分类',
'ptitle' => '上级分类', // 非表字段,方便后台显示
'description' => '分类介绍',
'article' => '文章数', //冗余字段,方便查询
'sort' => '排序',
'module' => '支持的文档类型',
'allow_publish' => '是否允许发布内容',
'created_at' => '创建时间',
'updated_at' => '更新时间',
];
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
[
'class' => MetaBehavior::className(),
'type' => 'category'
],
CategoryBehavior::className(),
'positionBehavior' => [
'class' => PositionBehavior::className(),
'positionAttribute' => 'sort',
'groupAttributes' => [
'pid'
],
],
[
'class' => CacheInvalidateBehavior::className(),
'tags' => [
'categoryList'
]
]
];
}
public function getMetaData()
{
$model = $this->getMetaModel();
$title = $model->title ? : $this->title;
$keywords = $model->keywords;
$description =$model->description ? : $this->description;
return [$title, $keywords, $description];
}
/**
* 获取分类名
*/
public function getPtitle()
{
return static::find()->select('title')->where(['id' => $this->pid])->scalar();
}
public static function lists($module = null)
{
$list = Yii::$app->cache->get(['categoryList', $module]);
if ($list === false) {
$query = static::find();
if ($module) {
$query->where(new Expression("FIND_IN_SET('{$module}', module) "));
}
$list = $query->asArray()->all();
Yii::$app->cache->set(['categoryList', $module], $list, 0, new TagDependency(['tags' => ['categoryList']]));
}
return $list;
}
public static function getDropDownList($tree = [], &$result = [], $deep = 0, $separator = '--')
{
$deep++;
foreach($tree as $list) {
$result[$list['id']] = str_repeat($separator, $deep-1) . $list['title'];
if (isset($list['children'])) {
self::getDropDownList($list['children'], $result, $deep);
}
}
return $result;
}
public function getCategoryNameById($id)
{
$list = $this->lists();
return isset($list[$id]) ? $list[$id] : null;
}
public static function getIdByName($name)
{
$list = self::lists();
return array_search($name, $list);
}
public static function findByIdOrSlug($id)
{
if (intval($id) == 0) {
$condition = ["slug" => $id];
} else {
$condition = [
$id
];
}
return static::findOne($condition);
}
public static function getAllowPublishEnum()
{
return [
'不允许',
'只允许后台',
'允许前后台'
];
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->module = implode(',', $this->module);
return true;
} else {
return false;
}
}
public function afterFind()
{
parent::afterFind();
$this->module = explode(',', $this->module);
}
public function renderModule($separator = ',')
{
return join($separator, array_map(function ($val) {
return array_get(ArticleModule::getTypeEnum(), $val);
}, $this->module));
}
}