|
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\behaviors\SluggableBehavior; |
|
12
|
|
|
use yii\db\ActiveRecord; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is the model class for table "{{%post_type}}". |
|
16
|
|
|
* |
|
17
|
|
|
* @property integer $id |
|
18
|
|
|
* @property string $name |
|
19
|
|
|
* @property string $slug |
|
20
|
|
|
* @property string $description |
|
21
|
|
|
* @property string $icon |
|
22
|
|
|
* @property string $singular_name |
|
23
|
|
|
* @property string $plural_name |
|
24
|
|
|
* @property integer $menu_builder |
|
25
|
|
|
* @property string $permission |
|
26
|
|
|
* |
|
27
|
|
|
* @property Post[] $posts |
|
28
|
|
|
* @property PostTypeTaxonomy[] $postTypeTaxonomies |
|
29
|
|
|
* @property Taxonomy[] $taxonomies |
|
30
|
|
|
* |
|
31
|
|
|
* @author Agiel K. Saputra <[email protected]> |
|
32
|
|
|
* @since 0.1.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
class PostType extends ActiveRecord |
|
35
|
|
|
{ |
|
36
|
|
|
const MENU_BUILDER = 1; |
|
37
|
|
|
const NOT_MENU_BUILDER = 0; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function tableName() |
|
43
|
|
|
{ |
|
44
|
|
|
return '{{%post_type}}'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
[ |
|
54
|
|
|
'class' => SluggableBehavior::className(), |
|
55
|
|
|
'attribute' => 'name', |
|
56
|
|
|
'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['slug']], |
|
57
|
|
|
], |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function rules() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
[['name', 'singular_name', 'plural_name', 'permission'], 'required'], |
|
68
|
|
|
[['description'], 'string'], |
|
69
|
|
|
[['menu_builder'], 'integer'], |
|
70
|
|
|
['menu_builder', 'in', 'range' => [self::MENU_BUILDER, self::NOT_MENU_BUILDER]], |
|
71
|
|
|
['menu_builder', 'default', 'value' => self::NOT_MENU_BUILDER], |
|
72
|
|
|
[['name', 'slug', 'permission'], 'string', 'max' => 64], |
|
73
|
|
|
[['icon', 'singular_name', 'plural_name'], 'string', 'max' => 255], |
|
74
|
|
|
[['name', 'slug'], 'unique'], |
|
75
|
|
|
[['slug'], 'safe'], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function attributeLabels() |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
'id' => Yii::t('writesdown', 'ID'), |
|
86
|
|
|
'name' => Yii::t('writesdown', 'Name'), |
|
87
|
|
|
'slug' => Yii::t('writesdown', 'Slug'), |
|
88
|
|
|
'description' => Yii::t('writesdown', 'Description'), |
|
89
|
|
|
'icon' => Yii::t('writesdown', 'Icon'), |
|
90
|
|
|
'singular_name' => Yii::t('writesdown', 'Singular Name'), |
|
91
|
|
|
'plural_name' => Yii::t('writesdown', 'Plural Name'), |
|
92
|
|
|
'menu_builder' => Yii::t('writesdown', 'Is Menu Builder'), |
|
93
|
|
|
'permission' => Yii::t('writesdown', 'Permission'), |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return \yii\db\ActiveQuery |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getPosts() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->hasMany(Post::className(), ['type' => 'id']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return \yii\db\ActiveQuery |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getPostTypeTaxonomies() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->hasMany(PostTypeTaxonomy::className(), ['post_type_id' => 'id']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return \yii\db\ActiveQuery |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getTaxonomies() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->hasMany(Taxonomy::className(), ['id' => 'taxonomy_id']) |
|
119
|
|
|
->viaTable('{{%post_type_taxonomy}}', ['post_type_id' => 'id']); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get array of menu_builder hierarchical for label or dropdown. |
|
124
|
|
|
* SMB is abbreviation from Show Menu Builder. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
View Code Duplication |
public function getMenuBuilders() |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
return [ |
|
131
|
|
|
self::MENU_BUILDER => Yii::t('writesdown', 'Yes'), |
|
132
|
|
|
self::NOT_MENU_BUILDER => Yii::t('writesdown', 'No'), |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get all post type which menu_builder is true. |
|
138
|
|
|
* The return value will be used in admin sidebar menu. |
|
139
|
|
|
* |
|
140
|
|
|
* @param int $position Position of post type in admin site menu |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function getMenus($position = 2) |
|
144
|
|
|
{ |
|
145
|
|
|
/* @var $model \common\models\PostType */ |
|
146
|
|
|
$models = static::find()->all(); |
|
147
|
|
|
$items = []; |
|
148
|
|
|
foreach ($models as $model) { |
|
149
|
|
|
$items[$position] = [ |
|
150
|
|
|
'label' => $model->plural_name, |
|
151
|
|
|
'icon' => $model->icon, |
|
152
|
|
|
'items' => static::getTaxonomyMenus($model), |
|
153
|
|
|
'visible' => Yii::$app->user->can($model->permission), |
|
154
|
|
|
]; |
|
155
|
|
|
$position++; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $items; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get all taxonomies in postType to show as submenu. |
|
163
|
|
|
* |
|
164
|
|
|
* @param \common\models\PostType $postType |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
protected static function getTaxonomyMenus($postType) |
|
168
|
|
|
{ |
|
169
|
|
|
$items = []; |
|
170
|
|
|
$items[] = [ |
|
171
|
|
|
'icon' => 'fa fa-circle-o', |
|
172
|
|
|
'label' => Yii::t('app', 'All {name}', ['name' => $postType->plural_name]), |
|
173
|
|
|
'url' => ['/post/index/', 'type' => $postType->id], |
|
174
|
|
|
]; |
|
175
|
|
|
$items[] = [ |
|
176
|
|
|
'icon' => 'fa fa-circle-o', |
|
177
|
|
|
'label' => Yii::t('app', 'Add New {name}', ['name' => $postType->singular_name]), |
|
178
|
|
|
'url' => ['/post/create/', 'type' => $postType->id], |
|
179
|
|
|
]; |
|
180
|
|
|
foreach ($postType->taxonomies as $taxonomy) { |
|
181
|
|
|
$items[] = [ |
|
182
|
|
|
'icon' => 'fa fa-circle-o', |
|
183
|
|
|
'label' => $taxonomy->plural_name, |
|
184
|
|
|
'url' => ['/taxonomy/view/', 'id' => $taxonomy->id], |
|
185
|
|
|
'visible' => Yii::$app->user->can('editor'), |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
$items[] = [ |
|
189
|
|
|
'icon' => 'fa fa-circle-o', |
|
190
|
|
|
'label' => Yii::t('app', 'Comments'), |
|
191
|
|
|
'url' => ['/post-comment/index/', 'posttype' => $postType->id], |
|
192
|
|
|
'visible' => Yii::$app->user->can('editor'), |
|
193
|
|
|
]; |
|
194
|
|
|
|
|
195
|
|
|
return $items; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.