1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.models.product.behaviors
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* Class ProductParametersBehavior
|
12
|
|
|
* Поведение для работы с параметрами продукта
|
13
|
|
|
*
|
14
|
|
|
* @property Product $owner
|
15
|
|
|
*/
|
16
|
|
|
class ProductParametersBehavior extends CModelBehavior
|
17
|
|
|
{
|
18
|
|
|
/**
|
19
|
|
|
* @var ProductParameterName[]
|
20
|
|
|
*/
|
21
|
|
|
protected $parameters;
|
22
|
|
|
|
23
|
|
|
protected $basketParameter;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* @param null $key
|
27
|
|
|
* @param CDbCriteria $groupCriteria критерия группы параметров
|
28
|
|
|
* @param CDbCriteria|null|false $criteria критерия параметров
|
29
|
|
|
*
|
30
|
|
|
* @return ProductParameterName[]
|
31
|
|
|
*/
|
32
|
|
|
public function getParameters($key = null, CDbCriteria $groupCriteria = null, $criteria = null)
|
33
|
|
|
{
|
34
|
|
|
if( !isset($this->parameters) )
|
35
|
|
|
{
|
36
|
|
|
$productParamNames = ProductParameterName::model();
|
37
|
|
|
if( !is_null($groupCriteria) )
|
38
|
|
|
$productParamNames->setGroupCriteria($groupCriteria);
|
39
|
|
|
|
40
|
|
|
if( empty($this->owner->parent) )
|
41
|
|
|
$productParamNames->addAssignmentCondition(array('section_id' => $this->owner->section->id));
|
42
|
|
|
|
43
|
|
|
if( $criteria === null )
|
44
|
|
|
{
|
45
|
|
|
$criteria = new CDbCriteria();
|
46
|
|
|
$criteria->compare('t.product', '1');
|
47
|
|
|
$criteria->compare('t.key', ProductParameter::BASKET_KEY, false, 'OR');
|
48
|
|
|
}
|
49
|
|
|
$criteria->addInCondition('t.id', $this->getCurrentProductParameterNameIds());
|
50
|
|
|
|
51
|
|
|
$this->parameters = $productParamNames->search($criteria);
|
52
|
|
|
|
53
|
|
|
foreach($this->parameters as $parameter)
|
54
|
|
|
$parameter->setProductId($this->owner->id);
|
55
|
|
|
|
56
|
|
|
ProductParameter::model()->setParameterValues($this->parameters);
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
return isset($key) ? Arr::filter($this->parameters, array('groupKey', 'key'), $key) : $this->parameters;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* @param array $parameters
|
64
|
|
|
*
|
65
|
|
|
* @return $this
|
66
|
|
|
*/
|
67
|
|
|
public function setParameters($parameters = array())
|
68
|
|
|
{
|
69
|
|
|
$this->parameters = $parameters;
|
70
|
|
|
return $this;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* @param $parameter
|
75
|
|
|
*/
|
76
|
|
|
public function addParameter($parameter)
|
77
|
|
|
{
|
78
|
|
|
$this->parameters[] = $parameter;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* @return ProductParameterName[]
|
83
|
|
|
*/
|
84
|
|
|
public function getProductOneParameters()
|
85
|
|
|
{
|
86
|
|
|
return $this->getParametersByAttributes(array('product' => 1), true);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @return ProductParameterName[]
|
91
|
|
|
*/
|
92
|
|
|
public function getProductLineParameters()
|
93
|
|
|
{
|
94
|
|
|
return $this->getParametersByAttributes(array('section_list' => 1), true);
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* @return ProductParameterName[]
|
99
|
|
|
*/
|
100
|
|
|
public function getProductTabletParameters()
|
101
|
|
|
{
|
102
|
|
|
return $this->getParametersByAttributes(array('section' => 1), true);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* @return ProductParameterName|null
|
107
|
|
|
*/
|
108
|
|
|
public function getBasketParameter()
|
109
|
|
|
{
|
110
|
|
|
if( is_null($this->basketParameter) )
|
111
|
|
|
{
|
112
|
|
|
$this->basketParameter = $this->getParameterByKey(ProductParameter::BASKET_KEY);
|
113
|
|
|
}
|
114
|
|
|
return $this->basketParameter;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* @param string|array $key
|
119
|
|
|
* @param bool $notEmptyOnly
|
120
|
|
|
*
|
121
|
|
|
* @return null|ProductParameterName
|
122
|
|
|
*/
|
123
|
|
|
public function getParameterByKey($key, $notEmptyOnly = true)
|
124
|
|
|
{
|
125
|
|
|
$parameters = array();
|
126
|
|
|
|
127
|
|
|
foreach($this->getParameters() as $parameter)
|
128
|
|
|
{
|
129
|
|
|
if( (is_array($key) && in_array($parameter->key, $key)) || ($parameter->key == $key) )
|
130
|
|
|
{
|
131
|
|
|
if( $notEmptyOnly && empty($parameter->value) )
|
132
|
|
|
continue;
|
133
|
|
|
|
134
|
|
|
$parameters[] = $parameter;
|
135
|
|
|
}
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
return is_array($key) ? $parameters : Arr::reset($parameters);
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/**
|
142
|
|
|
* @param $id
|
143
|
|
|
* @param bool $noEmpty
|
144
|
|
|
*
|
145
|
|
|
* @return null|ProductParameterName
|
146
|
|
|
*/
|
147
|
|
|
public function getParameterById($id, $noEmpty = true)
|
148
|
|
|
{
|
149
|
|
|
foreach($this->getParameters() as $parameter)
|
150
|
|
|
{
|
151
|
|
|
if( $parameter->id == $id )
|
152
|
|
|
{
|
153
|
|
|
if( $noEmpty && empty($parameter->value) )
|
154
|
|
|
break;
|
155
|
|
|
|
156
|
|
|
return $parameter;
|
157
|
|
|
}
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
return null;
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
View Code Duplication |
private function getCurrentProductParameterNameIds()
|
|
|
|
|
164
|
|
|
{
|
165
|
|
|
$criteria = new CDbCriteria();
|
166
|
|
|
$criteria->compare('product_id', $this->owner->primaryKey);
|
167
|
|
|
$criteria->select = 'param_id';
|
168
|
|
|
|
169
|
|
|
$command = Yii::app()->db->commandBuilder->createFindCommand(ProductParameter::model()->tableName(), $criteria);
|
170
|
|
|
|
171
|
|
|
return $command->queryColumn();
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
/**
|
175
|
|
|
* @param string $name
|
176
|
|
|
* @param string $value
|
177
|
|
|
* @param string $key
|
178
|
|
|
*
|
179
|
|
|
* @return ProductParameter|stdClass
|
180
|
|
|
*/
|
181
|
|
|
private function createFakeParameter($name, $value, $key = 'fake')
|
|
|
|
|
182
|
|
|
{
|
183
|
|
|
$parameter = new stdClass();
|
184
|
|
|
$parameter->name = $name;
|
185
|
|
|
$parameter->value = $value;
|
186
|
|
|
$parameter->key = $key;
|
187
|
|
|
$parameter->id = null;
|
188
|
|
|
|
189
|
|
|
return $parameter;
|
190
|
|
|
}
|
191
|
|
|
|
192
|
|
|
private function getParametersByAttributes(array $attributes, $notEmptyValue = true, $exceptionKeys = array())
|
193
|
|
|
{
|
194
|
|
|
$parameters = array();
|
195
|
|
|
|
196
|
|
|
foreach($this->getParameters() as $parameter)
|
197
|
|
|
{
|
198
|
|
|
if( $notEmptyValue && empty($parameter->value) )
|
199
|
|
|
continue;
|
200
|
|
|
|
201
|
|
|
if( in_array($parameter->key, $exceptionKeys) || in_array($parameter->getGroupKey(), $exceptionKeys) )
|
202
|
|
|
continue;
|
203
|
|
|
|
204
|
|
|
if( !empty($attributes) )
|
205
|
|
|
{
|
206
|
|
|
$attributesSuccess = false;
|
207
|
|
|
foreach($attributes as $attribute => $value)
|
208
|
|
|
{
|
209
|
|
|
if( isset($parameter->{$attribute}) && $parameter->{$attribute} == $value )
|
210
|
|
|
$attributesSuccess = true;
|
211
|
|
|
else
|
212
|
|
|
{
|
213
|
|
|
$attributesSuccess = false;
|
214
|
|
|
break;
|
215
|
|
|
}
|
216
|
|
|
}
|
217
|
|
|
if( $attributesSuccess )
|
218
|
|
|
$parameters[] = $parameter;
|
219
|
|
|
}
|
220
|
|
|
else
|
221
|
|
|
$parameters[] = $parameter;
|
222
|
|
|
}
|
223
|
|
|
|
224
|
|
|
return $parameters;
|
225
|
|
|
}
|
226
|
|
|
}
|
227
|
|
|
|
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.