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 "{{%taxonomy}}". |
16
|
|
|
* |
17
|
|
|
* @property integer $id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property string $slug |
20
|
|
|
* @property integer $hierarchical |
21
|
|
|
* @property string $singular_name |
22
|
|
|
* @property string $plural_name |
23
|
|
|
* @property integer $menu_builder |
24
|
|
|
* |
25
|
|
|
* @property PostTypeTaxonomy[] $postTypeTaxonomies |
26
|
|
|
* @property PostType[] $postTypes |
27
|
|
|
* @property Term[] $terms |
28
|
|
|
* |
29
|
|
|
* @author Agiel K. Saputra <[email protected]> |
30
|
|
|
* @since 1.0 |
31
|
|
|
*/ |
32
|
|
|
class Taxonomy extends ActiveRecord |
33
|
|
|
{ |
34
|
|
|
const HIERARCHICAL = 1; |
35
|
|
|
const NOT_HIERARCHICAL = 0; |
36
|
|
|
const MENU_BUILDER = 1; |
37
|
|
|
const NOT_MENU_BUILDER = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public static function tableName() |
43
|
|
|
{ |
44
|
|
|
return '{{%taxonomy}}'; |
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'], 'required'], |
68
|
|
|
[['hierarchical', 'menu_builder'], 'integer'], |
69
|
|
|
['hierarchical', 'in', 'range' => [self::HIERARCHICAL, self::NOT_HIERARCHICAL]], |
70
|
|
|
['hierarchical', 'default', 'value' => self::NOT_HIERARCHICAL], |
71
|
|
|
['menu_builder', 'in', 'range' => [self::MENU_BUILDER, self::NOT_MENU_BUILDER]], |
72
|
|
|
['menu_builder', 'default', 'value' => self::NOT_MENU_BUILDER], |
73
|
|
|
[['name', 'slug'], 'string', 'max' => 200], |
74
|
|
|
[['singular_name', 'plural_name'], 'string', 'max' => 255], |
75
|
|
|
[['name', 'slug'], 'unique'], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
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
|
|
|
'hierarchical' => Yii::t('writesdown', 'Is Hierarchical'), |
89
|
|
|
'singular_name' => Yii::t('writesdown', 'Singular Name'), |
90
|
|
|
'plural_name' => Yii::t('writesdown', 'Plural Name'), |
91
|
|
|
'menu_builder' => Yii::t('writesdown', 'Is Menu Builder'), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \yii\db\ActiveQuery |
97
|
|
|
*/ |
98
|
|
|
public function getPostTypeTaxonomies() |
99
|
|
|
{ |
100
|
|
|
return $this->hasMany(PostTypeTaxonomy::className(), ['taxonomy_id' => 'id']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return \yii\db\ActiveQuery |
105
|
|
|
*/ |
106
|
|
|
public function getPostTypes() |
107
|
|
|
{ |
108
|
|
|
return $this->hasMany(PostType::className(), ['id' => 'post_type_id']) |
109
|
|
|
->viaTable('{{%post_type_taxonomy}}', ['taxonomy_id' => 'id']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \yii\db\ActiveQuery |
114
|
|
|
*/ |
115
|
|
|
public function getTerms() |
116
|
|
|
{ |
117
|
|
|
return $this->hasMany(Term::className(), ['taxonomy_id' => 'id']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get array of taxonomy hierarchical for label or dropdown. |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function getHierarchies() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
self::HIERARCHICAL => Yii::t('writesdown', 'Yes'), |
129
|
|
|
self::NOT_HIERARCHICAL => Yii::t('writesdown', 'No'), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get array of menu_builder hierarchical for label or dropdown. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function getMenuBuilders() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
|
|
self::MENU_BUILDER => Yii::t('writesdown', 'Yes'), |
142
|
|
|
self::NOT_MENU_BUILDER => Yii::t('writesdown', 'No'), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.