Category.php
6.01 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace common\modules\product\models;
use common\behaviors\RuSlug;
use common\components\artboxtree\ArtboxTreeBehavior;
use common\components\artboxtree\ArtboxTreeHelper;
use common\modules\relation\relationBehavior;
use common\modules\rubrication\behaviors\ArtboxSynonymBehavior;
use Yii;
/**
* This is the model class for table "category".
*
* @property integer $category_id
* @property string $remote_id
* @property integer $parent_id
* @property string $path
* @property integer $depth
* @property string $image
* @property string $meta_title
* @property string $meta_desc
* @property string $meta_robots
* @property string $seo_text
* @property integer $category_name_id
* @property integer $product_unit_id
* @property string $alias
* @property boolean $populary
*
* @property CategoryName $categoryName
* @property ProductUnit $productUnit
* @property CategoryName[] $categoryNames
* @property ProductCategory[] $productCategories
*/
class Category extends \yii\db\ActiveRecord
{
public $imageUpload;
public function behaviors()
{
return [
'artboxtree' => [
'class' => ArtboxTreeBehavior::className(),
'keyNameGroup' => null,
'keyNamePath' => 'path',
],
'artboxsynonym' => [
'class' => ArtboxSynonymBehavior::className(),
'keyNameValue' => 'category_name_id',
'valueModel' => CategoryName::className(),
'valueOptionId' => 'category_id',
'valueFields' => [ // postKey => DBFieldName
'name' => 'value'
],
'slug' => [
'valueKeyName' => 'value',
'slugKeyName' => 'alias',
'translit' => true
]
],
[
'class' => relationBehavior::className(),
'relations' => [
'product_categories' => 'entity2', // Products of category
'tax_group_to_category' => 'entity2',
]
],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'category';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'string'],
[['parent_id', 'depth', 'category_name_id', 'product_unit_id'], 'integer'],
[['path', 'meta_desc', 'seo_text'], 'string'],
[['meta_title'], 'string', 'max' => 255],
[['meta_robots'], 'string', 'max' => 50],
[['alias', 'name'], 'string', 'max' => 250],
[['populary'], 'boolean'],
[['group_to_category', 'remote_category'], 'safe'],
[['category_name_id'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryName::className(), 'targetAttribute' => ['category_name_id' => 'category_name_id']],
[['imageUpload'], 'safe'],
[['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
// [['product_unit_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductUnit::className(), 'targetAttribute' => ['product_unit_id' => 'product_unit_id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'category_id' => Yii::t('product', 'Category ID'),
'parent_id' => Yii::t('product', 'Parent ID'),
'path' => Yii::t('product', 'Path'),
'depth' => Yii::t('product', 'Depth'),
'image' => Yii::t('product', 'Image'),
'imageUrl' => Yii::t('product', 'Image'),
'meta_title' => Yii::t('product', 'Meta Title'),
'meta_desc' => Yii::t('product', 'Meta Desc'),
'meta_robots' => Yii::t('product', 'Meta Robots'),
'seo_text' => Yii::t('product', 'Seo Text'),
'product_unit_id' => Yii::t('product', 'Product Unit ID'),
'alias' => Yii::t('product', 'Alias'),
'populary' => Yii::t('product', 'Populary'),
'name' => Yii::t('product', 'Name'),
'remote_id' => Yii::t('product', 'Remote ID'),
];
}
public static function find()
{
return new CategoryQuery(get_called_class());
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductUnit()
{
return $this->hasOne(ProductUnit::className(), ['product_unit_id' => 'product_unit_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategoryNames()
{
return $this->hasMany(CategoryName::className(), ['category_id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductCategories()
{
return $this->hasMany(ProductCategory::className(), ['category_id' => 'category_id']);
}
public function getTaxGroups()
{
return $this->getRelations('tax_group_to_category');
}
public function getRemote_category()
{
return ArtboxTreeHelper::getArrayField($this->remote_id);
}
public function setRemote_category($value)
{
if (!empty($value) && is_array($value)) {
$this->remote_id = ArtboxTreeHelper::setArrayField($value, false);
}
}
public function getCategoryName()
{
return $this->hasOne(CategoryName::className(), ['category_name_id' => 'category_name_id']);
}
public function getName()
{
return empty($this->categoryName) ? null : $this->categoryName->value;
}
public function getImageFile() {
return empty($this->image) ? null : Yii::getAlias('@storage/category/'. $this->image);
}
public function getImageUrl()
{
return empty($this->image) ? null : '/storage/category/' . $this->image;
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if (empty($this->parent_id))
$this->parent_id = 0;
return true;
}
return false;
}
}