1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\data\components; |
4
|
|
|
|
5
|
|
|
use app\models\BaseObject; |
6
|
|
|
use app\models\ObjectPropertyGroup; |
7
|
|
|
use app\models\ObjectStaticValues; |
8
|
|
|
use app\modules\shop\models\Product; |
9
|
|
|
use app\models\Property; |
10
|
|
|
use app\models\PropertyGroup; |
11
|
|
|
use app\models\PropertyStaticValues; |
12
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
use yii\base\InvalidParamException; |
16
|
|
|
use yii\db\ActiveQuery; |
17
|
|
|
use yii\db\ActiveRecord; |
18
|
|
|
use yii\db\Expression; |
19
|
|
|
use yii\helpers\ArrayHelper; |
20
|
|
|
|
21
|
|
|
abstract class Import extends Component |
22
|
|
|
{ |
23
|
|
|
protected $object; |
24
|
|
|
protected $properties = null; |
25
|
|
|
public $_processInternalId = true; |
26
|
|
|
public $_dataHeaderProcessor = ['app\modules\data\components\DummyDHProcessor','processHeader']; |
27
|
|
|
public $_importRoutine = 'processImport'; |
28
|
|
|
public $_exportRoutine = 'processExport'; |
29
|
|
|
public $_saveRoutine = 'save'; |
30
|
|
|
public $filename; |
31
|
|
|
public $addPropertyGroups = []; |
32
|
|
|
public $createIfNotExists = false; |
33
|
|
|
public $multipleValuesDelimiter = '|'; |
34
|
|
|
public $additionalFields = []; |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* Export method |
38
|
|
|
*/ |
39
|
|
|
abstract public function getData($header, $data); |
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* Import method |
42
|
|
|
*/ |
43
|
|
|
abstract public function setData(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $config |
47
|
|
|
* @return ImportCsv |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public static function createInstance($config) |
51
|
|
|
{ |
52
|
|
|
if (isset($config['type'])) { |
53
|
|
|
$type = $config['type']; |
54
|
|
|
unset($config['type']); |
55
|
|
|
|
56
|
|
|
switch ($type) { |
57
|
|
|
case 'csv': |
58
|
|
|
return Yii::$container->get('app\modules\data\components\ImportCsv',[],$config); |
59
|
|
|
case 'excelCsv': |
60
|
|
|
return Yii::$container->get('app\modules\data\components\ImportExcelCsv',[],$config); |
61
|
|
|
case 'xls': |
62
|
|
|
return Yii::$container->get('app\modules\data\components\ImportXlsx',[],array_merge(['fileType' => 'xls'], $config)); |
63
|
|
|
case 'xlsx': |
64
|
|
|
return Yii::$container->get('app\modules\data\components\ImportXlsx',[],array_merge(['fileType' => 'xlsx'], $config)); |
65
|
|
|
default: |
66
|
|
|
throw new \Exception('Unsupported type'); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
throw new InvalidParamException('Parameter \'type\' is not set'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $objectId |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public static function getFields($objectId) |
78
|
|
|
{ |
79
|
|
|
$fields = []; |
80
|
|
|
$object = BaseObject::findById($objectId); |
81
|
|
|
if ($object) { |
82
|
|
|
$fields['object'] = array_diff((new $object->object_class)->attributes(), ['id']); |
83
|
|
|
$fields['object'] = array_combine($fields['object'], $fields['object']); |
84
|
|
|
$fields['property'] = ArrayHelper::getColumn(static::getProperties($objectId), 'key'); |
85
|
|
|
$fields['additionalFields'] = []; |
86
|
|
|
} |
87
|
|
|
return $fields; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $objectId |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected static function getProperties($objectId) |
95
|
|
|
{ |
96
|
|
|
$properties = []; |
97
|
|
|
$groups = PropertyGroup::getForObjectId($objectId); |
98
|
|
|
foreach ($groups as $group) { |
99
|
|
|
$props = Property::getForGroupId($group->id); |
100
|
|
|
foreach ($props as $prop) { |
101
|
|
|
$properties[] = $prop; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return $properties; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return BaseObject |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public function getObject() |
111
|
|
|
{ |
112
|
|
|
return $this->object; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $config |
117
|
|
|
*/ |
118
|
|
|
public function __construct($config = []) |
119
|
|
|
{ |
120
|
|
|
if (!isset($config['object'])) { |
121
|
|
|
throw new InvalidParamException('Parameters \'object\' is not set'); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
$this->object = $config['object']; |
124
|
|
|
if (is_numeric($this->object)) { |
125
|
|
|
$this->object = BaseObject::findById($this->object); |
126
|
|
|
} elseif (!($this->object instanceof BaseObject)) { |
127
|
|
|
throw new InvalidParamException('Parameter "object" not Object or numeric'); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
unset($config['object']); |
130
|
|
|
parent::__construct($config); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $objectId |
135
|
|
|
* @param $object |
136
|
|
|
* @param array $objectFields |
137
|
|
|
* @param array $properties |
138
|
|
|
* @param array $propertiesFields |
139
|
|
|
* @param array $row |
140
|
|
|
* @param array $titleFields |
141
|
|
|
* @throws \Exception |
142
|
|
|
*/ |
143
|
|
|
protected function save($objectId, $object, $objectFields = [], $properties = [], $propertiesFields = [], $row=[], $titleFields=[], $columnsCount = null) |
144
|
|
|
{ |
145
|
|
|
if ($columnsCount === null) { |
146
|
|
|
$columnsCount = count($titleFields); |
147
|
|
|
} |
148
|
|
|
try { |
149
|
|
|
$rowFields = array_combine(array_keys($titleFields), array_slice($row, 0, $columnsCount)); |
150
|
|
|
} catch(\Exception $e) { |
151
|
|
|
echo "title fields: "; |
152
|
|
|
var_dump(array_keys($titleFields)); |
153
|
|
|
echo "\n\nRow:"; |
154
|
|
|
var_dump($row); |
155
|
|
|
echo "\n\n"; |
156
|
|
|
throw $e; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$class = $this->object->object_class; |
160
|
|
|
if ($objectId > 0) { |
161
|
|
|
/** @var ActiveRecord $objectModel */ |
162
|
|
|
$objectModel = $class::findOne($objectId); |
163
|
|
|
if (!is_object($objectModel)) { |
164
|
|
|
if ($this->createIfNotExists === true) { |
165
|
|
|
$objectModel = new $class; |
166
|
|
|
$objectModel->id = $objectId; |
167
|
|
|
} else { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$objectData = []; |
172
|
|
|
foreach ($objectFields as $field) { |
173
|
|
|
if (isset($object[$field])) { |
174
|
|
|
$objectData[$field] = $object[$field]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} else { |
178
|
|
|
/** @var ActiveRecord $objectModel */ |
179
|
|
|
$objectModel = new $class; |
180
|
|
|
$objectModel->loadDefaultValues(); |
181
|
|
|
$objectData = $object; |
182
|
|
|
} |
183
|
|
|
if ($objectModel) { |
184
|
|
|
|
185
|
|
|
if ($objectModel instanceof ImportableInterface) { |
186
|
|
|
$objectModel->processImportBeforeSave($rowFields, $this->multipleValuesDelimiter, $this->additionalFields); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($objectModel->save()) { |
190
|
|
|
|
191
|
|
|
// add PropertyGroup to object |
192
|
|
|
if (!is_array($this->addPropertyGroups)) { |
193
|
|
|
$this->addPropertyGroups = []; |
194
|
|
|
} |
195
|
|
|
foreach ($this->addPropertyGroups as $propertyGroupId) { |
196
|
|
|
$model = new ObjectPropertyGroup(); |
197
|
|
|
$model->object_id = $this->object->id; |
198
|
|
|
$model->object_model_id = $objectModel->id; |
199
|
|
|
$model->property_group_id = $propertyGroupId; |
200
|
|
|
$model->save(); |
201
|
|
|
} |
202
|
|
|
if (count($this->addPropertyGroups) > 0) { |
203
|
|
|
$objectModel->updatePropertyGroupsInformation(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$propertiesData = []; |
207
|
|
|
$objectModel->getPropertyGroups(); |
208
|
|
|
|
209
|
|
|
foreach ($propertiesFields as $propertyId => $field) { |
210
|
|
|
if (isset($properties[$field['key']])) { |
211
|
|
|
$value = $properties[$field['key']]; |
212
|
|
|
|
213
|
|
|
if (isset($field['processValuesAs'])) { |
214
|
|
|
// it is PSV in text |
215
|
|
|
// we should convert it to ids |
216
|
|
|
$staticValues = PropertyStaticValues::getValuesForPropertyId($propertyId); |
217
|
|
|
|
218
|
|
|
$representationConversions = [ |
219
|
|
|
// from -> to |
220
|
|
|
'text' => 'name', |
221
|
|
|
'value' => 'value', |
222
|
|
|
'id' => 'id', |
223
|
|
|
]; |
224
|
|
|
$attributeToGet = $representationConversions[$field['processValuesAs']]; |
225
|
|
|
$ids = []; |
226
|
|
|
foreach ($value as $initial) { |
227
|
|
|
$original = $initial; |
228
|
|
|
$initial = mb_strtolower(trim($original)); |
229
|
|
|
$added = false; |
230
|
|
|
foreach ($staticValues as $static) { |
231
|
|
|
if (mb_strtolower(trim($static[$attributeToGet])) === $initial) { |
232
|
|
|
$ids [] = $static['id']; |
233
|
|
|
$added = true; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
if (!$added) { |
237
|
|
|
// create PSV! |
238
|
|
|
$model = new PropertyStaticValues(); |
239
|
|
|
$model->property_id = $propertyId; |
240
|
|
|
$model->name = $model->value = $model->slug = $original; |
241
|
|
|
$model->sort_order = 0; |
242
|
|
|
$model->title_append = ''; |
|
|
|
|
243
|
|
|
if ($model->save()) { |
244
|
|
|
$ids[] = $model->id; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
//flush cache! |
248
|
|
|
unset(PropertyStaticValues::$identity_map_by_property_id[$propertyId]); |
249
|
|
|
|
250
|
|
|
\yii\caching\TagDependency::invalidate( |
251
|
|
|
Yii::$app->cache, |
252
|
|
|
[ |
253
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag(Property::className(), $propertyId) |
|
|
|
|
254
|
|
|
] |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
$value = $ids; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$propertiesData[$field['key']] = $value; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if (!empty($propertiesData)) { |
266
|
|
|
|
267
|
|
|
$objectModel->saveProperties( |
268
|
|
|
[ |
269
|
|
|
"Properties_{$objectModel->formName()}_{$objectModel->id}" => $propertiesData |
270
|
|
|
] |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($objectModel instanceof ImportableInterface) { |
275
|
|
|
$objectModel->processImportAfterSave($rowFields, $this->multipleValuesDelimiter, $this->additionalFields); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if ($objectModel->hasMethod('invalidateTags')) { |
279
|
|
|
$objectModel->invalidateTags(); |
280
|
|
|
} |
281
|
|
|
} else { |
282
|
|
|
throw new \Exception('Cannot save object: ' . var_export($objectModel->errors, true) . var_export($objectData, true) . var_export($objectModel->getAttributes(), true)); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param array $exportFields |
|
|
|
|
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public function getAllFields($fields = []) |
292
|
|
|
{ |
293
|
|
|
$result = []; |
294
|
|
|
|
295
|
|
|
$fields_object = isset($fields['object']) ? $fields['object'] : []; |
296
|
|
|
$fields_property = isset($fields['property']) ? $fields['property'] : []; |
297
|
|
|
$fields_additional = isset($fields['additionalFields']) ? $fields['additionalFields'] : []; |
298
|
|
|
|
299
|
|
|
$result['fields_object'] = $data_header = ($this->_processInternalId)? array_merge($fields_object, ['internal_id']) : $fields_object; |
300
|
|
|
|
301
|
|
View Code Duplication |
$result['fields_property'] = array_filter($fields_property, function($input) use (&$data_header) { |
302
|
|
|
if (1 == $input['enabled']) { |
303
|
|
|
$data_header[] = $input['key']; |
304
|
|
|
return true; |
305
|
|
|
} |
306
|
|
|
return false; |
307
|
|
|
}); |
308
|
|
|
|
309
|
|
View Code Duplication |
$result['fields_additional'] = array_filter($fields_additional, function($input) use (&$data_header) { |
310
|
|
|
if (1 == $input['enabled']) { |
311
|
|
|
$data_header[] = $input['key']; |
312
|
|
|
return true; |
313
|
|
|
} |
314
|
|
|
return false; |
315
|
|
|
}); |
316
|
|
|
|
317
|
|
|
$result['fields_header'] = Yii::$container->invoke($this->_dataHeaderProcessor,['dh'=>$data_header]); |
318
|
|
|
|
319
|
|
|
return $result; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param $objectId |
325
|
|
|
* @param $object |
326
|
|
|
* @param array $objectFields |
327
|
|
|
* @param array $properties |
328
|
|
|
* @param array $propertiesFields |
329
|
|
|
* @param array $row |
330
|
|
|
* @param array $titleFields |
331
|
|
|
* @throws \Exception |
332
|
|
|
*/ |
333
|
|
|
public function saveInvk($objectId, $object, $objectFields = [], $properties = [], $propertiesFields = [], $row=[], $titleFields=[], $columnsCount = null) |
334
|
|
|
{ |
335
|
|
|
return call_user_func([$this,$this->_saveRoutine],$objectId,$object,$objectFields,$properties,$propertiesFields,$row,$titleFields,$columnsCount); |
336
|
|
|
} |
337
|
|
|
/** |
338
|
|
|
* @param $exportFields |
339
|
|
|
* @param array $conditions |
340
|
|
|
* @param int $batchSize |
341
|
|
|
* @return bool |
342
|
|
|
* @throws \Exception |
343
|
|
|
*/ |
344
|
|
|
public function processExportInvk($exportFields = [], $conditions = [], $batchSize = 100) |
345
|
|
|
{ |
346
|
|
|
return call_user_func([$this,$this->_exportRoutine],$exportFields,$conditions,$batchSize); |
347
|
|
|
} |
348
|
|
|
/** |
349
|
|
|
* @param array $importFields |
350
|
|
|
* @return bool |
351
|
|
|
* @throws \Exception |
352
|
|
|
* @throws \yii\db\Exception |
353
|
|
|
*/ |
354
|
|
|
public function processImportInvk($importFields = []) |
355
|
|
|
{ |
356
|
|
|
return call_user_func([$this,$this->_importRoutine],$importFields); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param $exportFields |
361
|
|
|
* @param array $conditions |
362
|
|
|
* @param int $batchSize |
363
|
|
|
* @return bool |
364
|
|
|
* @throws \Exception |
365
|
|
|
*/ |
366
|
|
|
public function processExport($exportFields = [], $conditions = [], $batchSize = 100) |
367
|
|
|
{ |
368
|
|
|
$fields = $this->getAllFields($exportFields); |
369
|
|
|
|
370
|
|
|
$class = $this->object->object_class; |
371
|
|
|
/** @var $select ActiveQuery */ |
372
|
|
|
$select = $class::find(); |
373
|
|
|
|
374
|
|
|
$representationConversions = [ |
375
|
|
|
'text' => 'name', |
376
|
|
|
'value' => 'value', |
377
|
|
|
'id' => 'psv_id', |
378
|
|
|
]; |
379
|
|
|
$product = Yii::$container->get(Product::class); |
380
|
|
|
if ( |
381
|
|
|
isset($conditions['category']) && |
382
|
|
|
is_array($conditions['category']) && |
383
|
|
|
$this->object->id == BaseObject::getForClass(get_class($product))->id |
384
|
|
|
) { |
385
|
|
|
foreach ($conditions['category'] as $condition) { |
386
|
|
|
$joinTableName = 'Category'.$condition['value']; |
387
|
|
|
|
388
|
|
|
$select->innerJoin( |
389
|
|
|
"{{%product_category}} " . $joinTableName, |
390
|
|
|
"$joinTableName.object_model_id = product.id" |
391
|
|
|
); |
392
|
|
|
$select->andWhere( |
393
|
|
|
new Expression( |
394
|
|
|
'`' . $joinTableName . '`.`category_id` = "'.$condition['value'].'"' |
395
|
|
|
) |
396
|
|
|
); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
if (isset($conditions['field']) && is_array($conditions['field'])) { |
401
|
|
|
foreach ($conditions['field'] as $condition) { |
402
|
|
|
$conditionOptions = [$condition['operators'], $condition['value'], $condition['option']]; |
403
|
|
|
if ($condition['comparison'] == 'AND') { |
404
|
|
|
$select->andWhere($conditionOptions); |
405
|
|
|
} elseif ($condition['comparison'] == 'OR') { |
406
|
|
|
$select->orWhere($conditionOptions); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
if (isset($conditions['property']) && is_array($conditions['property'])) { |
411
|
|
|
foreach ($conditions['property'] as $condition) { |
412
|
|
|
$property = Property::findById($condition['value']); |
413
|
|
|
|
414
|
|
|
if ($property && isset($condition['option']) && !empty($condition['option'])) { |
415
|
|
|
if ($property->is_eav) { |
416
|
|
|
$joinTableName = 'EAVJoinTable'.$property->id; |
417
|
|
|
|
418
|
|
|
$select->innerJoin( |
419
|
|
|
$this->object->eav_table_name . " " . $joinTableName, |
420
|
|
|
"$joinTableName.object_model_id = " . |
421
|
|
|
Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id " |
422
|
|
|
); |
423
|
|
|
$select->andWhere( |
424
|
|
|
new Expression( |
425
|
|
|
'`' . $joinTableName . '`.`value` '.$condition['operators'].' "'.$condition['option'].'" AND `' . |
426
|
|
|
$joinTableName . '`.`key` = "'. $property->key.'"' |
427
|
|
|
) |
428
|
|
|
); |
429
|
|
|
} elseif ($property->has_static_values) { |
430
|
|
|
$joinTableName = 'OSVJoinTable'.$property->id; |
431
|
|
|
$propertyStaticValue = PropertyStaticValues::find()->where(['value'=>$condition['option']])->one(); |
432
|
|
|
|
433
|
|
|
if ($propertyStaticValue) { |
434
|
|
|
$select->innerJoin( |
435
|
|
|
ObjectStaticValues::tableName() . " " . $joinTableName, |
436
|
|
|
"$joinTableName.object_id = " . intval($this->object->id) . |
437
|
|
|
" AND $joinTableName.object_model_id = " . |
438
|
|
|
Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id " |
439
|
|
|
); |
440
|
|
|
|
441
|
|
|
$select->andWhere( |
442
|
|
|
new Expression( |
443
|
|
|
'`' . $joinTableName . '`.`property_static_value_id` ="'.$propertyStaticValue->id.'"' |
444
|
|
|
) |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
} else { |
448
|
|
|
throw new \Exception("Wrong property type for ".$property->id); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
$data = []; |
455
|
|
|
$batchSize = intval($batchSize) <= 0 ? 100 : intval($batchSize); |
456
|
|
|
foreach ($select->each($batchSize) as $object) { |
457
|
|
|
$row = []; |
458
|
|
|
|
459
|
|
|
foreach ($fields['fields_object'] as $field) { |
460
|
|
|
if ('internal_id' === $field && $this->_processInternalId) { |
461
|
|
|
$row[] = $object->id; |
462
|
|
|
} else { |
463
|
|
|
$row[] = isset($object->$field) ? $object->$field : ''; |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
foreach ($fields['fields_property'] as $field_id => $field) { |
468
|
|
|
$value = $object->getPropertyValuesByPropertyId($field_id); |
469
|
|
|
|
470
|
|
|
if (!is_object($value)) { |
471
|
|
|
$value = ''; |
472
|
|
|
} elseif (is_array($field) && count($value->values) >= 1 && isset($field['processValuesAs'])) { |
473
|
|
|
$attributeToGet = $representationConversions[$field['processValuesAs']]; |
474
|
|
|
$newValues = []; |
475
|
|
|
foreach ($value->values as $val) { |
476
|
|
|
$newValues[] = $val[$attributeToGet]; |
477
|
|
|
} |
478
|
|
|
$value = implode($this->multipleValuesDelimiter, $newValues); |
479
|
|
|
} else { |
480
|
|
|
$value = (string) $value; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
$row[] = $value; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
if (!empty($fields['fields_additional']) && $object->hasMethod('getAdditionalFields')) { |
487
|
|
|
$fieldsFromModel = $object->getAdditionalFields($fields['fields_additional']); |
488
|
|
|
foreach ($fields['fields_additional'] as $key => $configuration) { |
489
|
|
|
if (!isset($fieldsFromModel[$key])) { |
490
|
|
|
$fieldsFromModel[$key] = ''; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
if (!empty($fieldsFromModel[$key])) { |
494
|
|
|
$value = (array)$fieldsFromModel[$key]; |
495
|
|
|
$row[] = implode($this->multipleValuesDelimiter, $value); |
496
|
|
|
} else { |
497
|
|
|
$row[] = ''; |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$data[] = $row; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
unset($value, $row, $object, $select, $class); |
506
|
|
|
|
507
|
|
|
return $this->getData($fields['fields_header'], $data); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @param array $importFields |
512
|
|
|
* @return bool |
513
|
|
|
* @throws \Exception |
514
|
|
|
* @throws \yii\db\Exception |
515
|
|
|
*/ |
516
|
|
|
public function processImport($importFields = []) |
517
|
|
|
{ |
518
|
|
|
$fields = $this->getAllFields($importFields); |
519
|
|
|
$data = $this->setData(); |
520
|
|
|
|
521
|
|
|
$objectFields = static::getFields($this->object->id); |
522
|
|
|
$objAttributes = $objectFields['object']; |
523
|
|
|
$propAttributes = isset($objectFields['property']) ? $objectFields['property'] : []; |
524
|
|
|
|
525
|
|
|
$titleFields = array_filter( |
526
|
|
|
array_shift($data), |
527
|
|
|
function ($value) { |
528
|
|
|
return !empty($value); |
529
|
|
|
} |
530
|
|
|
); |
531
|
|
|
$titleFields = array_intersect_key(array_flip($titleFields), array_flip($fields['fields_header'])); |
532
|
|
|
|
533
|
|
|
$transaction = \Yii::$app->db->beginTransaction(); |
534
|
|
|
$columnsCount = count($titleFields); |
535
|
|
|
try { |
536
|
|
|
foreach ($data as $row) { |
537
|
|
|
$objData = []; |
538
|
|
|
$propData = []; |
539
|
|
|
foreach ($objAttributes as $attribute) { |
540
|
|
|
if (isset($titleFields[$attribute])) { |
541
|
|
|
$objData[$attribute] = $row[$titleFields[$attribute]]; |
542
|
|
|
} |
543
|
|
|
} |
544
|
|
|
foreach ($propAttributes as $attribute) { |
545
|
|
|
if (!(isset($titleFields[$attribute]))) { |
546
|
|
|
continue; |
547
|
|
|
} |
548
|
|
|
$propValue = $row[$titleFields[$attribute]]; |
549
|
|
|
if (!empty($this->multipleValuesDelimiter)) { |
550
|
|
|
if (strpos($propValue, $this->multipleValuesDelimiter) > 0) { |
551
|
|
|
$values = explode($this->multipleValuesDelimiter, $propValue); |
552
|
|
|
} elseif (strpos($this->multipleValuesDelimiter, '/') === 0) { |
553
|
|
|
$values = preg_split($this->multipleValuesDelimiter, $propValue); |
554
|
|
|
} else { |
555
|
|
|
$values = [$propValue]; |
556
|
|
|
} |
557
|
|
|
$propValue = []; |
558
|
|
|
foreach ($values as $value) { |
559
|
|
|
$value = trim($value); |
560
|
|
|
if (!empty($value)) { |
561
|
|
|
$propValue[] = $value; |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
$propData[$attribute] = $propValue; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
$objectId = isset($titleFields['internal_id']) ? $row[$titleFields['internal_id']] : 0; |
569
|
|
|
$this->saveInvk( |
570
|
|
|
$objectId, |
571
|
|
|
$objData, |
572
|
|
|
$fields['fields_object'], |
573
|
|
|
$propData, |
574
|
|
|
$fields['fields_property'], |
575
|
|
|
$row, |
576
|
|
|
$titleFields, |
577
|
|
|
$columnsCount |
578
|
|
|
); |
579
|
|
|
} |
580
|
|
|
} catch (\Exception $exception) { |
581
|
|
|
$transaction->rollBack(); |
582
|
|
|
throw $exception; |
583
|
|
|
} |
584
|
|
|
$transaction->commit(); |
585
|
|
|
|
586
|
|
|
return true; |
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.