1
|
|
|
<?php |
2
|
|
|
namespace app\modules\shop\components\yml; |
3
|
|
|
|
4
|
|
|
use app\models\ObjectStaticValues; |
5
|
|
|
use app\models\Property; |
6
|
|
|
use app\models\PropertyStaticValues; |
7
|
|
|
use app\modules\shop\events\yml\YmlOffersEvent; |
8
|
|
|
use app\modules\shop\helpers\CurrencyHelper; |
9
|
|
|
use app\modules\shop\models\Category; |
10
|
|
|
use app\modules\shop\models\Currency; |
11
|
|
|
use app\modules\shop\models\Product; |
12
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
use app\modules\shop\models\Yml as YmlModel; |
16
|
|
|
use yii\caching\TagDependency; |
17
|
|
|
use yii\db\Query; |
18
|
|
|
use yii\helpers\Json; |
19
|
|
|
use yii\helpers\Url; |
20
|
|
|
use yii\web\View; |
21
|
|
|
|
22
|
|
|
class Yml extends Component |
23
|
|
|
{ |
24
|
|
|
const PARAM_TYPE_FIELD = 'field'; |
25
|
|
|
const PARAM_TYPE_RELATION = 'relation'; |
26
|
|
|
const USE_GZIP = 1; |
27
|
|
|
const USE_OFFER_PARAM = 1; |
28
|
|
|
const USE_ADULT = 1; |
29
|
|
|
const USE_STORE = 1; |
30
|
|
|
const USE_PICKUP = 1; |
31
|
|
|
const USE_DELIVERY = 1; |
32
|
|
|
|
33
|
|
|
const EVENT_PROCESS_OFFER = 'ymlProcessOffer'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @property \app\modules\shop\models\Yml $model |
37
|
|
|
* @property string $viewFile |
38
|
|
|
* @property Currency $currency |
39
|
|
|
*/ |
40
|
|
|
protected $model = null; |
41
|
|
|
protected $viewFile = null; |
42
|
|
|
protected $currency = null; |
43
|
|
|
|
44
|
|
|
static public $_noImg = ''; |
45
|
|
|
static public $ymlEavProperties = []; |
46
|
|
|
static public $ymlStaticProperties = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function __construct(YmlModel $model, $config = []) |
52
|
|
|
{ |
53
|
|
|
$this->model = $model; |
54
|
|
|
parent::__construct($config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function init() |
61
|
|
|
{ |
62
|
|
|
parent::init(); |
63
|
|
|
|
64
|
|
|
if (false === $this->model instanceof YmlModel) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (null === $this->viewFile) { |
69
|
|
|
$this->viewFile = Yii::$app->getModule('shop')->getViewPath() . '/backend-yml/generate/yml.php'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @var YmlModel $config */ |
73
|
|
|
$config = $this->model; |
74
|
|
|
Yii::$app->urlManager->setHostInfo($config->shop_url); |
|
|
|
|
75
|
|
|
$this->currency = CurrencyHelper::findCurrencyByIso($config->currency_id); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if (static::USE_OFFER_PARAM == $config->offer_param) { |
|
|
|
|
78
|
|
|
$this->prepareProperties(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
static::$_noImg = Yii::$app->getModule('image')->noImageSrc; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function generate() |
88
|
|
|
{ |
89
|
|
|
if (false === $this->model instanceof YmlModel) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var \app\modules\shop\models\Yml $yml */ |
94
|
|
|
$config = $this->model; |
95
|
|
|
/** @var View $view */ |
96
|
|
|
$view = Yii::$app->getView(); |
97
|
|
|
|
98
|
|
|
$outputParams = []; |
99
|
|
|
$outputParams['shop'] = $this->generateSectionShop($config); |
100
|
|
|
$outputParams['offers'] = []; |
101
|
|
|
|
102
|
|
|
$eventOffer = new YmlOffersEvent(); |
103
|
|
|
$product = Yii::$container->get(Product::class); |
104
|
|
|
/** @var Product $model */ |
105
|
|
|
foreach ($product::find()->where(['active' => 1])->batch() as $offers) { |
106
|
|
|
$eventOffer |
107
|
|
|
->clearHandled() |
108
|
|
|
->setOffers(array_reduce($offers, function ($r, $i) use ($config) { |
109
|
|
|
if (null !== $o = $this->generateSingleOffer($config, $i)) { |
110
|
|
|
$r[] = $o; |
111
|
|
|
} |
112
|
|
|
return $r; |
113
|
|
|
}, [])); |
114
|
|
|
$this->trigger(static::EVENT_PROCESS_OFFER, $eventOffer); |
115
|
|
|
|
116
|
|
|
$outputParams['offers'] = array_merge($outputParams['offers'], array_column($eventOffer->getOffers(), 'result')); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$output = $view->renderFile($this->viewFile, $outputParams); |
120
|
|
|
|
121
|
|
|
$fileName = Yii::getAlias('@webroot') . '/' . $config->general_yml_filename; |
|
|
|
|
122
|
|
|
$result = static::USE_GZIP === $config->use_gzip |
|
|
|
|
123
|
|
|
? file_put_contents($fileName . '.gz', gzencode($output), 5) |
124
|
|
|
: file_put_contents($fileName, $output); |
125
|
|
|
|
126
|
|
|
return false !== $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param YmlModel $config |
131
|
|
|
* @param string $name |
132
|
|
|
* @param Product $model |
133
|
|
|
* @param string $default |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public static function getOfferValue(YmlModel $config, $name, Product $model, $default = '') |
137
|
|
|
{ |
138
|
|
|
$param = $config->$name; |
139
|
|
|
|
140
|
|
|
if (static::PARAM_TYPE_FIELD === $param['type']) { |
141
|
|
|
$field = $param['key']; |
142
|
|
|
$result = $model->$field; |
143
|
|
|
} elseif (static::PARAM_TYPE_RELATION === $param['type']) { |
144
|
|
|
$rel = call_user_func([$model, $param['key']]); |
145
|
|
|
$attr = $param['value']; |
146
|
|
|
$rel = $rel->one(); |
147
|
|
|
if (false === empty($rel)) { |
148
|
|
|
$result = $rel->$attr; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false === empty($result) ? $result : $default; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
*/ |
158
|
|
|
private function prepareProperties() |
159
|
|
|
{ |
160
|
|
|
$props = Property::getDb()->cache(function ($db) { |
|
|
|
|
161
|
|
|
return Property::find()->select([ |
162
|
|
|
'id', |
163
|
|
|
'name', |
164
|
|
|
'property_handler_id', |
165
|
|
|
'key', |
166
|
|
|
'property_group_id', |
167
|
|
|
'has_static_values', |
168
|
|
|
'is_eav', |
169
|
|
|
'handler_additional_params' |
170
|
|
|
])->all(); |
171
|
|
|
}, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Property::className()),]])); |
172
|
|
|
foreach ($props as $one) { |
173
|
|
|
$additionalParams = Json::decode($one['handler_additional_params']); |
174
|
|
|
if (false === empty($additionalParams['use_in_file'])) { |
175
|
|
|
if (1 == $one['is_eav'] && false === isset(self::$ymlEavProperties[$one['id']])) { |
176
|
|
|
self::$ymlEavProperties[$one['id']] = [ |
177
|
|
|
'name' => $one['name'], |
178
|
|
|
'unit' => empty($additionalParams['unit']) ? '' : $additionalParams['unit'], |
179
|
|
|
'key' => $one['key'], |
180
|
|
|
'group_id' => $one['property_group_id'], |
181
|
|
|
'handler_id' => $one['property_handler_id'], |
182
|
|
|
]; |
183
|
|
|
} elseif (1 == $one['has_static_values'] && false === isset(self::$ymlStaticProperties[$one['id']])) { |
184
|
|
|
self::$ymlStaticProperties[$one['id']] = [ |
185
|
|
|
'name' => $one['name'], |
186
|
|
|
'unit' => empty($additionalParams['unit']) ? '' : $additionalParams['unit'], |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param Product $model |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public static function getOfferParams(Product $model) |
199
|
|
|
{ |
200
|
|
|
$params = []; |
201
|
|
|
$eav = Yii::$app->getDb()->cache(function ($db) use ($model) { |
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @var \app\models\Object $object |
204
|
|
|
*/ |
205
|
|
|
$object = $model->object; |
|
|
|
|
206
|
|
|
return (new Query()) |
207
|
|
|
->from($object->eav_table_name) |
208
|
|
|
->select(Property::tableName() . '.id, ' . $object->eav_table_name . '.value') |
209
|
|
|
->innerJoin( |
210
|
|
|
Property::tableName(), |
211
|
|
|
Property::tableName() . '.property_group_id = ' . $object->eav_table_name . '.property_group_id' |
212
|
|
|
. ' AND ' . Property::tableName() . '.key = ' . $object->eav_table_name . '.key' |
213
|
|
|
) |
214
|
|
|
->where([ |
215
|
|
|
'object_model_id' => $model->id, |
216
|
|
|
$object->eav_table_name . '.key' => array_column(static::$ymlEavProperties, 'key'), |
217
|
|
|
$object->eav_table_name . '.property_group_id' => array_column(static::$ymlEavProperties, 'group_id'), |
|
|
|
|
218
|
|
|
Property::tableName() . '.id' => array_keys(static::$ymlEavProperties), |
219
|
|
|
]) |
220
|
|
|
->andWhere(['<>','value', '']) |
221
|
|
|
->all(); |
222
|
|
|
}); |
223
|
|
|
foreach ($eav as $prop) { |
224
|
|
|
if (false === isset($prop['id'])) { |
225
|
|
|
continue ; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$val = htmlspecialchars($prop['value']); |
229
|
|
|
switch (static::$ymlEavProperties[$prop['id']]['handler_id']) { |
230
|
|
|
case 3 : |
|
|
|
|
231
|
|
|
$val = $val == 1 ? Yii::t('yii', 'Yes') : Yii::t('yii', 'No'); |
232
|
|
|
break; |
233
|
|
|
} |
234
|
|
|
$_key = static::$ymlEavProperties[$prop['id']]['name']; |
235
|
|
|
$params[htmlspecialchars(trim($_key))] = [ |
236
|
|
|
'unit' => false === empty(static::$ymlEavProperties[$prop['id']]['unit']) |
237
|
|
|
? htmlspecialchars(trim(static::$ymlEavProperties[$prop['id']]['unit'])) |
238
|
|
|
: null, |
239
|
|
|
'value' => $val, |
240
|
|
|
]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$psv = Yii::$app->getDb()->cache(function ($db) use ($model) { |
|
|
|
|
244
|
|
|
return (new Query()) |
245
|
|
|
->from(PropertyStaticValues::tableName()) |
246
|
|
|
->innerJoin( |
247
|
|
|
ObjectStaticValues::tableName(), |
248
|
|
|
ObjectStaticValues::tableName() . '.property_static_value_id = ' . PropertyStaticValues::tableName() . '.id' |
|
|
|
|
249
|
|
|
) |
250
|
|
|
->where([ |
251
|
|
|
'object_model_id' => $model->id, |
252
|
|
|
'object_id' => $model->object->id, |
|
|
|
|
253
|
|
|
'property_id' => array_keys(static::$ymlStaticProperties) |
254
|
|
|
]) |
255
|
|
|
->andWhere(['<>','value', '']) |
256
|
|
|
->all(); |
257
|
|
|
}); |
258
|
|
|
foreach ($psv as $prop) { |
259
|
|
|
if (false === isset($prop['property_id'])) { |
260
|
|
|
continue ; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$_key = static::$ymlStaticProperties[$prop['property_id']]['name']; |
264
|
|
|
$params[htmlspecialchars(trim($_key))] = [ |
265
|
|
|
'unit' => false === empty(static::$ymlStaticProperties[$prop['property_id']]['unit']) |
266
|
|
|
? htmlspecialchars(trim(static::$ymlStaticProperties[$prop['property_id']]['unit'])) |
267
|
|
|
: null, |
268
|
|
|
'value' => htmlspecialchars(trim($prop['value'])), |
269
|
|
|
]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $params; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param YmlModel $config |
277
|
|
|
* @return array |
278
|
|
|
*/ |
279
|
|
|
private function generateSectionShop(YmlModel $config) |
280
|
|
|
{ |
281
|
|
|
return [ |
282
|
|
|
'name' => $config->shop_name, |
|
|
|
|
283
|
|
|
'company' => $config->shop_company, |
|
|
|
|
284
|
|
|
'url' => $config->shop_url, |
|
|
|
|
285
|
|
|
'currency' => $this->currency->iso_code, |
286
|
|
|
'categories' => Category::find()->where(['active' => 1])->asArray(), |
287
|
|
|
'store' => static::USE_STORE == $config->shop_store ? 'true' : 'false', |
|
|
|
|
288
|
|
|
'pickup' => static::USE_PICKUP == $config->shop_pickup ? 'true' : 'false', |
|
|
|
|
289
|
|
|
'delivery' => static::USE_DELIVERY == $config->shop_delivery ? 'true' : 'false', |
|
|
|
|
290
|
|
|
'local_delivery_cost' => $config->shop_local_delivery_cost, |
|
|
|
|
291
|
|
|
'adult' => static::USE_ADULT == $config->shop_adult ? 'true' : 'false', |
|
|
|
|
292
|
|
|
]; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param YmlModel $config |
297
|
|
|
* @param Product $model |
298
|
|
|
* @return null|array |
|
|
|
|
299
|
|
|
*/ |
300
|
|
|
private function generateSingleOffer(YmlModel $config, Product $model) |
301
|
|
|
{ |
302
|
|
|
$result = $this->offerSimplified($config, $model); |
303
|
|
|
if (null === $result) { |
304
|
|
|
return null; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return [ |
308
|
|
|
'model' => $model, |
309
|
|
|
'result' => $result, |
310
|
|
|
]; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param YmlModel $config |
315
|
|
|
* @param Product $model |
316
|
|
|
* @return OfferTag|null |
317
|
|
|
*/ |
318
|
|
|
private function offerSimplified(YmlModel $config, Product $model) |
319
|
|
|
{ |
320
|
|
|
$offer = new OfferTag('offer', [], ['id' => $model->id, 'available' => 'true',]); |
|
|
|
|
321
|
|
|
|
322
|
|
|
$price = static::getOfferValue($config, 'offer_price', $model, 0); |
323
|
|
|
$price = CurrencyHelper::convertCurrencies($price, $model->currency, $this->currency); |
|
|
|
|
324
|
|
|
if ($price <= 0 || $price >= 1000000000) { |
325
|
|
|
return null; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$values = []; |
329
|
|
|
|
330
|
|
|
$name = static::getOfferValue($config, 'offer_name', $model, null); |
331
|
|
|
if (true === empty($name)) { |
332
|
|
|
return null; |
333
|
|
|
} |
334
|
|
View Code Duplication |
if (mb_strlen($name) > 120) { |
335
|
|
|
$name = mb_substr($name, 0, 120); |
336
|
|
|
$name = mb_substr($name, 0, mb_strrpos($name, ' ')); |
337
|
|
|
} |
338
|
|
|
$values[] = new OfferTag('name', htmlspecialchars(trim(strip_tags($name)))); |
339
|
|
|
$values[] = new OfferTag('price', $price); |
340
|
|
|
$values[] = new OfferTag('currencyId', $this->currency->iso_code); |
341
|
|
|
|
342
|
|
|
/** @var Category $category */ |
343
|
|
|
if (null === $category = $model->category) { |
344
|
|
|
return null; |
345
|
|
|
} |
346
|
|
|
$values[] = new OfferTag('categoryId', $category->id); |
347
|
|
|
$values[] = new OfferTag('url', Url::toRoute([ |
348
|
|
|
'@product', |
349
|
|
|
'model' => $model, |
350
|
|
|
'category_group_id' => $category->category_group_id, |
351
|
|
|
], true)); |
352
|
|
|
|
353
|
|
|
$picture = static::getOfferValue($config, 'offer_picture', $model, static::$_noImg); |
354
|
|
|
if (static::$_noImg !== $picture) { |
355
|
|
|
$picture = htmlspecialchars(trim($picture, '/')); |
356
|
|
|
$picture = implode('/', array_map('rawurlencode', explode('/', $picture))); |
357
|
|
|
$values[] = new OfferTag('picture', trim($config->shop_url, '/') . '/' . $picture); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$description = static::getOfferValue($config, 'offer_description', $model, null); |
361
|
|
|
|
362
|
|
|
if (false === empty($description)) { |
363
|
|
|
$description = preg_replace("/([\r\n\t])/", ' ', $description); |
364
|
|
|
$description = preg_replace("/[ ]{2,}/", '', $description); |
365
|
|
|
$description = htmlspecialchars(trim(strip_tags($description))); |
366
|
|
|
|
367
|
|
View Code Duplication |
if (mb_strlen($description) > 175) { |
368
|
|
|
$description = mb_substr($description, 0, 175); |
369
|
|
|
$description = mb_substr($description, 0, mb_strrpos($description, ' ')); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$values[] = new OfferTag('description', $description); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
if (static::USE_OFFER_PARAM == $config->offer_param) { |
|
|
|
|
376
|
|
|
foreach (static::getOfferParams($model) as $k => $v) { |
377
|
|
|
$values[] = new OfferTag('param', $v['value'], ['name' => $k, 'unit' => $v['unit']]); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $offer->setValue($values); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.