1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package backend.modules.product.models
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
1 |
|
Yii::import('backend.modules.product.models.behaviors.*');
|
11
|
|
|
|
12
|
|
|
/**
|
13
|
|
|
* @method static BProduct model(string $class = __CLASS__)
|
14
|
|
|
*
|
15
|
|
|
* @property string $id
|
16
|
|
|
* @property integer $parent
|
17
|
|
|
* @property integer $position
|
18
|
|
|
* @property string $url
|
19
|
|
|
* @property string $name
|
20
|
|
|
* @property string $articul
|
21
|
|
|
* @property string $price
|
22
|
|
|
* @property string $price_old
|
23
|
|
|
* @property string $notice
|
24
|
|
|
* @property string $content
|
25
|
|
|
*
|
26
|
|
|
* @property integer $gift
|
27
|
|
|
* @property integer $visible
|
28
|
|
|
* @property integer $spec
|
29
|
|
|
* @property integer $novelty
|
30
|
|
|
* @property integer $discount
|
31
|
|
|
* @property integer $main
|
32
|
|
|
* @property integer $dump
|
33
|
|
|
* @property integer $archive
|
34
|
|
|
* @property integer $xml
|
35
|
|
|
|
36
|
|
|
* @property integer $section_id
|
37
|
|
|
* @property integer $type_id
|
38
|
|
|
* @property integer $category_id
|
39
|
|
|
* @property integer $collection_id
|
40
|
|
|
*
|
41
|
|
|
* @property BProductAssignment $assignment
|
42
|
|
|
* @property BAssociation[] $associations
|
43
|
|
|
*/
|
44
|
|
|
class BProduct extends BActiveRecord
|
45
|
|
|
{
|
46
|
24 |
|
public function __get($name)
|
47
|
|
|
{
|
48
|
24 |
|
$fields = BProductAssignment::model()->getFields();
|
49
|
|
|
|
50
|
24 |
|
if( isset($fields[$name]) )
|
51
|
24 |
|
{
|
52
|
9 |
|
$relation = str_replace('_id', '', $name);
|
53
|
|
|
|
54
|
9 |
|
if( is_array($this->$relation) )
|
55
|
9 |
|
$value = CHtml::listData($this->$relation, 'id', 'id');
|
56
|
|
|
else if( isset($this->$relation->id) )
|
57
|
9 |
|
$value = $this->$relation->id;
|
58
|
|
|
else
|
59
|
8 |
|
$value = null;
|
60
|
9 |
|
}
|
61
|
|
|
else
|
62
|
|
|
{
|
63
|
24 |
|
$value = parent::__get($name);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
24 |
|
return $value;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
20 |
|
public function __set($name, $value)
|
70
|
|
|
{
|
71
|
20 |
|
$fields = BProductAssignment::model()->getFields();
|
72
|
|
|
|
73
|
20 |
|
if( isset($fields[$name]) )
|
74
|
20 |
|
$this->$name = $value;
|
75
|
|
|
else
|
76
|
19 |
|
parent::__set($name, $value);
|
77
|
20 |
|
}
|
78
|
|
|
|
79
|
10 |
|
public function __isset($name)
|
80
|
|
|
{
|
81
|
10 |
|
$fields = BProductAssignment::model()->getFields();
|
82
|
|
|
|
83
|
10 |
|
if( isset($fields[$name]) )
|
84
|
10 |
|
return true;
|
85
|
|
|
else
|
86
|
10 |
|
return parent::__isset($name);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
24 |
|
public function rules()
|
90
|
|
|
{
|
91
|
|
|
return array(
|
92
|
24 |
|
array('url, name, articul', 'required'),
|
93
|
24 |
|
array('url, articul', 'unique'),
|
94
|
24 |
|
array('parent, position, visible, spec, novelty, main, dump, discount, archive, xml', 'numerical', 'integerOnly' => true),
|
95
|
24 |
|
array('url, name, articul', 'length', 'max' => 255),
|
96
|
24 |
|
array('notice, content, video, rating', 'safe'),
|
97
|
24 |
|
array('price, price_old', 'numerical'),
|
98
|
|
|
|
99
|
24 |
|
array('price_old', 'filter', 'filter' => function($value) {
|
100
|
5 |
|
if( PriceHelper::isEmpty($value) )
|
101
|
5 |
|
return null;
|
102
|
|
|
return $value;
|
103
|
24 |
|
}),
|
104
|
24 |
|
array('price_old', 'compare', 'allowEmpty' => true, 'compareAttribute' => 'price', 'operator' => '>'),
|
105
|
|
|
|
106
|
24 |
|
array('url', 'SUriValidator'),
|
107
|
24 |
|
array('name, url, articul', 'filter', 'filter' => array(Yii::app()->format, 'trim')),
|
108
|
24 |
|
array('url', 'filter', 'filter' => array(Yii::app()->format, 'toLower')),
|
109
|
|
|
|
110
|
24 |
|
array('section_id', 'required', 'except' => BModificationBehavior::SCENARIO_MODIFICATION),
|
111
|
24 |
|
array(implode(", ", array_keys(BProductAssignment::model()->getFields())), 'safe'),
|
112
|
24 |
|
);
|
113
|
|
|
}
|
114
|
|
|
|
115
|
24 |
|
public function behaviors()
|
116
|
|
|
{
|
117
|
|
|
return array(
|
118
|
|
|
'uploadBehavior' => array(
|
119
|
24 |
|
'class' => 'UploadBehavior',
|
120
|
|
|
'validAttributes' => 'product_img'
|
121
|
24 |
|
),
|
122
|
|
|
'facetedSearchBehavior' => array(
|
123
|
|
|
'class' => 'BFacetedSearchBehavior'
|
124
|
24 |
|
),
|
125
|
|
|
'associatedFilter' => array(
|
126
|
24 |
|
'class' => 'AssociatedFilterBehavior',
|
127
|
24 |
|
),
|
128
|
24 |
|
'modificationBehavior' => array('class' => 'BModificationBehavior')
|
129
|
24 |
|
);
|
130
|
|
|
}
|
131
|
|
|
|
132
|
1 |
|
public function relations()
|
133
|
|
|
{
|
134
|
|
|
return array(
|
|
|
|
|
135
|
1 |
|
'assignment' => array(self::HAS_MANY, 'BProductAssignment', 'product_id'),
|
136
|
1 |
|
'associations' => array(self::HAS_MANY, 'BAssociation', 'src_id', 'on' => 'src="bproduct"'),
|
137
|
1 |
|
'products' => array(self::HAS_MANY, 'BProduct', 'dst_id', 'on' => 'dst="product"', 'through' => 'associations'),
|
138
|
1 |
|
'section' => array(self::HAS_ONE, 'BProductSection', 'section_id', 'through' => 'assignment'),
|
139
|
1 |
|
'category' => array(self::HAS_ONE, 'BProductCategory', 'category_id', 'through' => 'assignment'),
|
140
|
1 |
|
'collection' => array(self::HAS_ONE, 'BProductCollection', 'collection_id', 'through' => 'assignment'),
|
141
|
1 |
|
'type' => array(self::HAS_ONE, 'BProductType', 'type_id', 'through' => 'assignment'),
|
142
|
1 |
|
);
|
143
|
|
|
}
|
144
|
|
|
|
145
|
4 |
|
public function beforeSave()
|
146
|
|
|
{
|
147
|
4 |
|
if( parent::beforeSave() )
|
148
|
4 |
|
{
|
149
|
4 |
|
if( empty($this->articul) )
|
150
|
4 |
|
$this->articul = null;
|
151
|
|
|
|
152
|
4 |
|
return true;
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
return false;
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
public function getImageTypes()
|
159
|
|
|
{
|
160
|
|
|
return array(
|
161
|
|
|
'main' => 'Основное',
|
162
|
|
|
'gallery' => 'Галерея'
|
163
|
|
|
);
|
164
|
|
|
}
|
165
|
|
|
|
166
|
|
|
public function getParameterVariants($key)
|
167
|
|
|
{
|
168
|
|
|
$variants = array();
|
169
|
|
|
$parameter = BProductParamName::model()->findByAttributes(array('key' => $key));
|
170
|
|
|
|
171
|
|
|
if( $parameter )
|
172
|
|
|
$variants = $parameter->variants;
|
173
|
|
|
|
174
|
|
|
return $variants;
|
175
|
|
|
}
|
176
|
|
|
|
177
|
3 |
|
public function attributeLabels()
|
178
|
|
|
{
|
179
|
3 |
|
return CMap::mergeArray(parent::attributeLabels(), array(
|
180
|
3 |
|
'product_img' => 'Изображения',
|
181
|
3 |
|
'BProduct' => 'Продукты',
|
182
|
3 |
|
'spec' => 'Лидер продаж',
|
183
|
3 |
|
'price_raw' => 'Цена',
|
184
|
3 |
|
));
|
185
|
|
|
}
|
186
|
|
|
|
187
|
|
|
/**
|
188
|
|
|
* @param CDbCriteria $criteria
|
189
|
|
|
*
|
190
|
|
|
* @return CDbCriteria
|
191
|
|
|
*/
|
192
|
5 |
|
protected function getSearchCriteria(CDbCriteria $criteria)
|
193
|
|
|
{
|
194
|
5 |
|
$criteria->together = true;
|
195
|
5 |
|
$criteria->distinct = true;
|
196
|
|
|
|
197
|
5 |
|
$criteria->with = array('assignment' => [
|
198
|
5 |
|
'select' => false,
|
199
|
5 |
|
]);
|
200
|
|
|
|
201
|
5 |
|
foreach($this->getFilteredGridSettings() as $filteredGridSetting)
|
202
|
|
|
{
|
203
|
5 |
|
if( property_exists($this, $filteredGridSetting->name) || isset($this->getMetaData()->columns[$filteredGridSetting->name]) )
|
204
|
5 |
|
{
|
205
|
5 |
|
if( $filteredGridSetting->filter == BGridSettings::FILTER_COMPARE )
|
206
|
5 |
|
{
|
207
|
5 |
|
$criteria->compare('t.'.$filteredGridSetting->name, trim($this->{$filteredGridSetting->name}));
|
208
|
5 |
|
}
|
209
|
|
|
else if( $filteredGridSetting->filter == BGridSettings::FILTER_CONTAIN )
|
210
|
|
|
{
|
211
|
|
|
$criteria->compare('t.'.$filteredGridSetting->name, trim($this->{$filteredGridSetting->name}), true);
|
212
|
|
|
}
|
213
|
5 |
|
}
|
214
|
5 |
|
}
|
215
|
|
|
|
216
|
5 |
|
foreach(BProductAssignment::model()->getFields() as $key => $field)
|
217
|
5 |
|
if( !is_array($this->$key) )
|
218
|
5 |
|
$criteria->compare('assignment.'.$key, $this->$key);
|
219
|
|
|
|
220
|
5 |
|
return $criteria;
|
221
|
|
|
}
|
222
|
|
|
|
223
|
5 |
|
protected function getSearchParams()
|
224
|
|
|
{
|
225
|
5 |
|
return array('sort' => array('defaultOrder' => 't.id ASC'));
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
/**
|
229
|
|
|
* @return BGridSettings[]
|
230
|
|
|
*/
|
231
|
5 |
|
protected function getFilteredGridSettings()
|
232
|
|
|
{
|
233
|
5 |
|
Yii::import('backend.modules.settings.models.BGridSettings');
|
234
|
|
|
|
235
|
5 |
|
return BGridSettings::model()->visible()->filter()->findAll();
|
236
|
|
|
}
|
237
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.