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
|
|
|
/**
|
24
|
|
|
* @var array
|
25
|
|
|
*/
|
26
|
|
|
private $cache;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @param null $key
|
30
|
|
|
* @param CDbCriteria $groupCriteria критерия группы параметров
|
31
|
|
|
* @param CDbCriteria|null|false $criteria критерия параметров
|
32
|
|
|
*
|
33
|
|
|
* @return ProductParameterName[]
|
34
|
|
|
*/
|
35
|
2 |
|
public function getParameters($key = null, CDbCriteria $groupCriteria = null, $criteria = null)
|
36
|
|
|
{
|
37
|
2 |
|
if( !isset($this->parameters) )
|
38
|
2 |
|
{
|
39
|
1 |
|
$productParamNames = ProductParameterName::model();
|
40
|
1 |
|
if( !is_null($groupCriteria) )
|
41
|
1 |
|
$productParamNames->setGroupCriteria($groupCriteria);
|
42
|
|
|
|
43
|
1 |
|
if( empty($this->owner->parent) )
|
44
|
1 |
|
$productParamNames->addAssignmentCondition(array('section_id' => $this->owner->section->id));
|
45
|
|
|
|
46
|
1 |
|
if( $criteria === null )
|
47
|
1 |
|
{
|
48
|
1 |
|
$criteria = new CDbCriteria();
|
49
|
1 |
|
$criteria->compare('t.product', '1');
|
50
|
1 |
|
$criteria->compare('t.key', ProductParameter::BASKET_KEY, false, 'OR');
|
51
|
1 |
|
}
|
52
|
1 |
|
$criteria->addInCondition('t.id', $this->getCurrentProductParameterNameIds());
|
53
|
|
|
|
54
|
1 |
|
$this->parameters = $productParamNames->search($criteria);
|
55
|
|
|
|
56
|
1 |
|
foreach($this->parameters as $parameter)
|
57
|
1 |
|
$parameter->setProductId($this->owner->id);
|
58
|
|
|
|
59
|
1 |
|
ProductParameter::model()->setParameterValues($this->parameters);
|
60
|
1 |
|
}
|
61
|
|
|
|
62
|
2 |
|
return isset($key) ? Arr::filter($this->parameters, array('groupKey', 'key'), $key) : $this->parameters;
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @param array $parameters
|
67
|
|
|
*
|
68
|
|
|
* @return $this
|
69
|
|
|
*/
|
70
|
2 |
|
public function setParameters($parameters = array())
|
71
|
|
|
{
|
72
|
2 |
|
$this->parameters = $parameters;
|
73
|
2 |
|
return $this;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @param $parameter
|
78
|
|
|
*/
|
79
|
2 |
|
public function addParameter($parameter)
|
80
|
|
|
{
|
81
|
2 |
|
$this->parameters[] = $parameter;
|
82
|
2 |
|
}
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* @param $id
|
86
|
|
|
* @param bool $notEmptyOnly
|
87
|
|
|
*
|
88
|
|
|
* @return null|ProductParameterName
|
89
|
|
|
*/
|
90
|
|
|
public function getParameterById($id, $notEmptyOnly = true)
|
91
|
|
|
{
|
92
|
|
|
return $this->getParametersByAttributes(array('id' => $id), true, $notEmptyOnly);
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* @param array $idList
|
97
|
|
|
* @param bool|true $notEmptyOnly
|
98
|
|
|
*
|
99
|
|
|
* @return null|ProductParameterName[]
|
100
|
|
|
*/
|
101
|
|
|
public function getParametersByIdList(array $idList, $notEmptyOnly = true)
|
102
|
|
|
{
|
103
|
|
|
return $this->getParametersByAttributes(array('id' => $idList), false, $notEmptyOnly);
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
/**
|
107
|
|
|
* @param string|array $key
|
108
|
|
|
* @param bool $notEmptyOnly
|
109
|
|
|
*
|
110
|
|
|
* @return null|ProductParameterName
|
111
|
|
|
*/
|
112
|
|
|
public function getParameterByKey($key, $notEmptyOnly = true )
|
113
|
|
|
{
|
114
|
|
|
return $this->getParametersByAttributes(array('key' => $key), true, $notEmptyOnly);
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* @param string|array $keys
|
119
|
|
|
* @param bool $notEmptyOnly
|
120
|
|
|
*
|
121
|
|
|
* @return null|ProductParameterName[]
|
122
|
|
|
*/
|
123
|
|
|
public function getParametersByKeys(array $keys, $notEmptyOnly = true)
|
124
|
|
|
{
|
125
|
|
|
return $this->getParametersByAttributes(array('key' => $keys), false, $notEmptyOnly);
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
/**
|
129
|
|
|
* @return ProductParameterName[]
|
130
|
|
|
*/
|
131
|
1 |
|
public function getParametersCard()
|
132
|
1 |
|
{
|
133
|
|
|
return $this->getParametersByAttributes(array('product' => 1));
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
/**
|
137
|
|
|
* @return ProductParameterName[]
|
138
|
|
|
*/
|
139
|
|
|
public function getParametersLine()
|
140
|
|
|
{
|
141
|
|
|
return $this->getParametersByAttributes(array('section_list' => 1));
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
/**
|
145
|
|
|
* @return ProductParameterName[]
|
146
|
|
|
*/
|
147
|
|
|
public function getParametersTablet()
|
148
|
|
|
{
|
149
|
|
|
return $this->getParametersByAttributes(array('section' => 1));
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
/**
|
153
|
|
|
* @return ProductParameterName|null
|
154
|
|
|
*/
|
155
|
|
|
public function getParametersBasket()
|
156
|
|
|
{
|
157
|
|
|
return $this->getParametersByKeys(array(ProductParameter::BASKET_KEY));
|
158
|
|
|
}
|
159
|
|
|
|
160
|
1 |
View Code Duplication |
private function getCurrentProductParameterNameIds()
|
|
|
|
|
161
|
|
|
{
|
162
|
1 |
|
$criteria = new CDbCriteria();
|
163
|
1 |
|
$criteria->compare('product_id', $this->owner->primaryKey);
|
164
|
1 |
|
$criteria->select = 'param_id';
|
165
|
|
|
|
166
|
1 |
|
$command = Yii::app()->db->commandBuilder->createFindCommand(ProductParameter::model()->tableName(), $criteria);
|
167
|
|
|
|
168
|
1 |
|
return $command->queryColumn();
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
/**
|
172
|
|
|
* @param string $name
|
173
|
|
|
* @param string $value
|
174
|
|
|
* @param string $key
|
175
|
|
|
*
|
176
|
|
|
* @return ProductParameter|stdClass
|
177
|
|
|
*/
|
178
|
|
|
private function createFakeParameter($name, $value, $key = 'fake')
|
|
|
|
|
179
|
|
|
{
|
180
|
|
|
$parameter = new stdClass();
|
181
|
|
|
$parameter->name = $name;
|
182
|
|
|
$parameter->value = $value;
|
183
|
|
|
$parameter->key = $key;
|
184
|
|
|
$parameter->id = null;
|
185
|
|
|
|
186
|
|
|
return $parameter;
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
/**
|
190
|
|
|
* Выбирает параметры по заданным атрибутам
|
191
|
|
|
* Пример:
|
192
|
|
|
* $this->getParametersByAttributes(array('section_list' => 1));
|
193
|
|
|
* $this->getParametersByAttributes(array('key' => array('test', 'test2')));
|
194
|
|
|
* $this->getParametersByAttributes(array('id' => 'test'), true);
|
195
|
|
|
*
|
196
|
|
|
* @param array $attributes
|
197
|
|
|
* @param bool $onlyOne только один параметр
|
198
|
|
|
* @param bool|true $notEmptyValue
|
199
|
|
|
* @param array $exceptionKeys
|
200
|
|
|
*
|
201
|
|
|
* @return array
|
202
|
|
|
* @throws CHttpException
|
203
|
|
|
*/
|
204
|
|
|
private function getParametersByAttributes(array $attributes, $onlyOne = false, $notEmptyValue = true, $exceptionKeys = array())
|
205
|
|
|
{
|
206
|
|
|
$cacheKey = serialize(func_get_args());
|
207
|
|
|
|
208
|
|
|
if( !isset($this->cache[$cacheKey]) )
|
209
|
|
|
{
|
210
|
|
|
$parameters = array();
|
211
|
|
|
|
212
|
|
|
foreach($this->getParameters() as $parameter)
|
213
|
|
|
{
|
214
|
|
|
if( $notEmptyValue && empty($parameter->value) )
|
215
|
|
|
continue;
|
216
|
|
|
|
217
|
|
|
if( !empty($exceptionKeys) && (in_array($parameter->key, $exceptionKeys) || in_array($parameter->getGroupKey(), $exceptionKeys)) )
|
218
|
|
|
continue;
|
219
|
|
|
|
220
|
|
|
if( !empty($attributes) )
|
221
|
|
|
{
|
222
|
|
|
$attributesSuccess = false;
|
223
|
|
|
|
224
|
|
|
foreach($attributes as $attribute => $value)
|
225
|
|
|
{
|
226
|
|
|
if( property_exists($parameter, $attribute) )
|
227
|
|
|
throw new CHttpException(500, "Свойство ".$attribute." не доступно в классе ".get_class($parameter));
|
228
|
|
|
|
229
|
|
|
if( (is_array($value) && in_array($parameter->{$attribute}, $value)) || $parameter->{$attribute} == $value )
|
230
|
|
|
{
|
231
|
|
|
$attributesSuccess = true;
|
232
|
|
|
}
|
233
|
|
|
else
|
234
|
|
|
{
|
235
|
|
|
$attributesSuccess = false;
|
236
|
|
|
break;
|
237
|
|
|
}
|
238
|
|
|
}
|
239
|
|
|
|
240
|
|
|
if( $attributesSuccess )
|
241
|
|
|
$parameters[] = $parameter;
|
242
|
|
|
}
|
243
|
|
|
else
|
244
|
|
|
$parameters[] = $parameter;
|
245
|
|
|
}
|
246
|
|
|
|
247
|
|
|
$this->cache[$cacheKey] = $onlyOne ? Arr::reset($parameters) : $parameters;
|
248
|
|
|
}
|
249
|
|
|
|
250
|
|
|
return $this->cache[$cacheKey];
|
251
|
|
|
}
|
252
|
|
|
}
|
253
|
|
|
|
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.