|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\models; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use app\modules\config\components\ConfigurationSaveEvent; |
|
7
|
|
|
use app\modules\config\helpers\ConfigurationUpdater; |
|
8
|
|
|
use app\modules\config\models\Configurable; |
|
9
|
|
|
use app\modules\shop\ShopModule; |
|
10
|
|
|
use yii; |
|
11
|
|
|
use yii\base\Model; |
|
12
|
|
|
|
|
13
|
|
|
class GoogleFeed extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
/** @property array $attrStorage */ |
|
16
|
|
|
private $attrStorage = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritdoc |
|
20
|
|
|
*/ |
|
21
|
|
|
public function rules() |
|
22
|
|
|
{ |
|
23
|
|
|
$rules = [ |
|
24
|
|
|
[ |
|
25
|
|
|
[ |
|
26
|
|
|
'shop_host', |
|
27
|
|
|
'shop_name', |
|
28
|
|
|
'shop_description', |
|
29
|
|
|
'shop_main_currency', |
|
30
|
|
|
'shop_delivery_price', |
|
31
|
|
|
'item_delivery_cost', |
|
32
|
|
|
'feed_file_name', |
|
33
|
|
|
], |
|
34
|
|
|
'required' |
|
35
|
|
|
], |
|
36
|
|
|
[['item_delivery_cost'], 'integer'], |
|
37
|
|
|
[['shop_name'], 'string', 'length' => [1, 255]], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
return $rules; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function attributes() |
|
47
|
|
|
{ |
|
48
|
|
|
return array_keys($this->attrStorage); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function attributeLabels() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
'shop_host' => Yii::t('app', 'Shop host'), |
|
58
|
|
|
'shop_title' => Yii::t('app', 'Shop title'), |
|
59
|
|
|
'shop_description' => Yii::t('app', 'Shop description'), |
|
60
|
|
|
'shop_main_currency' => Yii::t('app', 'Main currency'), |
|
61
|
|
|
'shop_delivery_price' => Yii::t('app', 'Shop delivery price'), |
|
62
|
|
|
'item_price' => Yii::t('app', 'Price'), |
|
63
|
|
|
'item_condition' => Yii::t('app', 'Condition'), |
|
64
|
|
|
'item_title' => Yii::t('app', 'Title'), |
|
65
|
|
|
'item_description' => Yii::t('app', 'Description'), |
|
66
|
|
|
'feed_handlers' => Yii::t('app', 'Handlers'), |
|
67
|
|
|
'feed_file_name' => Yii::t('app', 'File Name'), |
|
68
|
|
|
'item_gtin' => Yii::t('app', 'Google gtin'), |
|
69
|
|
|
'item_brand' => Yii::t('app', 'Brand'), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function scenarios() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
return parent::scenarios(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritdoc |
|
83
|
|
|
*/ |
|
84
|
|
|
public function init() |
|
85
|
|
|
{ |
|
86
|
|
|
parent::init(); |
|
87
|
|
|
|
|
88
|
|
|
$this->attrStorage = [ |
|
89
|
|
|
'shop_host' => Yii::$app->request->getHostInfo(), |
|
90
|
|
|
'shop_name' => 'shop name', |
|
91
|
|
|
'shop_description' => 'shop description', |
|
92
|
|
|
'shop_delivery_price' => 0, |
|
93
|
|
|
'item_price' => ['type' => 'field', 'key' => 'price'], |
|
94
|
|
|
'item_condition' => 'new', |
|
95
|
|
|
'item_title' => ['type' => 'field', 'key' => 'name'], |
|
96
|
|
|
'item_description' => ['type' => 'field', 'key' => 'announce'], |
|
97
|
|
|
'item_gtin' => ['type' => 'field', 'key' => 'sku'], |
|
98
|
|
|
'item_brand' => ['type' => 'field', 'key' => 'name'], |
|
99
|
|
|
'shop_main_currency' => 0, |
|
100
|
|
|
'item_delivery_cost' => 0, |
|
101
|
|
|
'feed_handlers' => yii\helpers\Json::encode([ |
|
102
|
|
|
'app\modules\shop\components\GoogleMerchants\DefaultHandler' |
|
103
|
|
|
]), |
|
104
|
|
|
'feed_file_name' => 'feed.xml', |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function __get($name) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
if (isset($this->attrStorage[$name])) { |
|
114
|
|
|
return $this->attrStorage[$name]; |
|
115
|
|
|
} |
|
116
|
|
|
if (in_array($name, $this->attributes())) { |
|
117
|
|
|
return ''; |
|
118
|
|
|
} |
|
119
|
|
|
return parent::__get($name); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
if (is_array($value)) { |
|
128
|
|
|
$value = array_map('trim', $value); |
|
129
|
|
|
} elseif (is_string($value)) { |
|
130
|
|
|
$value = trim($value); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (isset($this->attrStorage[$name])) { |
|
134
|
|
|
$this->attrStorage[$name] = $value; |
|
135
|
|
|
return true; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return parent::__set($name, $value); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return array |
|
|
|
|
|
|
143
|
|
|
*/ |
|
144
|
|
|
public function getOfferElements() |
|
145
|
|
|
{ |
|
146
|
|
|
return [ |
|
147
|
|
|
'item_price', |
|
148
|
|
|
'item_title', |
|
149
|
|
|
'item_description', |
|
150
|
|
|
'item_gtin', |
|
151
|
|
|
'item_brand' |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
public function saveConfig() |
|
159
|
|
|
{ |
|
160
|
|
|
/** @var ShopModule $module */ |
|
161
|
|
|
if (null === $module = Yii::$app->getModule('shop')) { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
/** @var Configurable $configurable */ |
|
165
|
|
|
if (null === $configurable = Configurable::findOne(['module' => $module->id])) { |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
/** @var ConfigConfigurationModel $configurableModel */ |
|
169
|
|
|
$configurableModel = $configurable->getConfigurableModel(); |
|
170
|
|
|
$config = $this->attrStorage; |
|
171
|
|
|
|
|
172
|
|
|
yii\base\Event::on( |
|
173
|
|
|
$configurableModel::className(), |
|
174
|
|
|
$configurableModel->configurationSaveEvent(), |
|
175
|
|
|
function (ConfigurationSaveEvent $event) use ($config) { |
|
176
|
|
|
/** @var ConfigConfigurationModel $model */ |
|
177
|
|
|
$model = $event->configurableModel; |
|
178
|
|
|
$model->googleFeedConfig = $config; |
|
179
|
|
|
} |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
$models = [$configurable]; |
|
183
|
|
|
return ConfigurationUpdater::updateConfiguration($models, false); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
View Code Duplication |
public function loadConfig() |
|
190
|
|
|
{ |
|
191
|
|
|
/** @var ShopModule $module */ |
|
192
|
|
|
if (null === $module = Yii::$app->getModule('shop')) { |
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
if (empty($module->googleFeedConfig)) { |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
$this->attrStorage = $module->googleFeedConfig; |
|
199
|
|
|
|
|
200
|
|
|
return static::validate(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.