1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace execut\import\models; |
4
|
|
|
|
5
|
|
|
use execut\actions\action\adapter\viewRenderer\DynaGridRow; |
6
|
|
|
use execut\crudFields\Behavior; |
7
|
|
|
use execut\crudFields\BehaviorStub; |
8
|
|
|
use execut\crudFields\fields\Date; |
9
|
|
|
use execut\crudFields\fields\Field; |
10
|
|
|
use execut\crudFields\fields\HasManyMultipleInput; |
11
|
|
|
use execut\crudFields\fields\HasOneSelect2; |
12
|
|
|
use execut\crudFields\fields\RadiobuttonGroup; |
13
|
|
|
use execut\crudFields\ModelsHelperTrait; |
14
|
|
|
use execut\import\components\ToArrayConverter; |
15
|
|
|
use execut\crudFields\fields\File as FileField; |
16
|
|
|
|
17
|
|
|
use yii\behaviors\TimestampBehavior; |
18
|
|
|
use yii\db\Exception; |
19
|
|
|
use yii\db\Expression; |
20
|
|
|
use yii\db\mysql\Schema; |
21
|
|
|
use yii\helpers\ArrayHelper; |
22
|
|
|
use yii\helpers\FileHelper; |
23
|
|
|
use yii\web\UploadedFile; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is the model class for table "import_files". |
27
|
|
|
* |
28
|
|
|
* @property integer $id |
29
|
|
|
* @property string $created |
30
|
|
|
* @property string $updated |
31
|
|
|
* @property string $name |
32
|
|
|
* @property resource $content |
33
|
|
|
* @property string $md5 |
34
|
|
|
* @property string $import_files_source_id |
35
|
|
|
* @property string $use_id |
36
|
|
|
* @property UploadedFile $contentFile |
37
|
|
|
* |
38
|
|
|
* @property \execut\import\models\FilesSource $importFilesSource |
39
|
|
|
* @property \execut\import\models\User $use |
40
|
|
|
*/ |
41
|
|
|
class File extends base\File implements DynaGridRow |
42
|
|
|
{ |
43
|
|
|
use ModelsHelperTrait, BehaviorStub; |
44
|
|
|
|
45
|
|
|
const MODEL_NAME = '{n,plural,=0{Files} =1{File} other{Files}}'; |
46
|
|
|
public $eventsCount = null; |
47
|
|
|
protected $rows = null; |
48
|
|
|
public $contentFile = null; |
49
|
|
|
public $preview = null; |
50
|
|
|
public $progress = null; |
51
|
|
|
|
52
|
|
|
public function setTime_complete_calculated() { |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function rules() |
60
|
|
|
{ |
61
|
|
|
return $this->getBehavior('fields')->rules(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function behaviors() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'fields' => [ |
68
|
|
|
'class' => Behavior::class, |
69
|
|
|
'module' => 'import', |
70
|
|
|
'fields' => $this->getStandardFields(['visible', 'name'], [ |
71
|
|
|
'contentFile' => [ |
72
|
|
|
'class' => FileField::class, |
73
|
|
|
'attribute' => 'contentFile', |
74
|
|
|
'md5Attribute' => 'md5', |
75
|
|
|
'dataAttribute' => 'content', |
76
|
|
|
'downloadUrl' => [ |
77
|
|
|
'/import/files/download' |
78
|
|
|
], |
79
|
|
|
'allowedExtensions' => $this->getAllowedExtensions(), |
80
|
|
|
], |
81
|
|
|
'import_setting_id' => [ |
82
|
|
|
'class' => HasOneSelect2::class, |
83
|
|
|
'required' => true, |
84
|
|
|
'attribute' => 'import_setting_id', |
85
|
|
|
'relation' => 'setting', |
86
|
|
|
'url' => [ |
87
|
|
|
'/import/settings' |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
'import_files_statuse_id' => [ |
91
|
|
|
'class' => RadiobuttonGroup::class, |
92
|
|
|
'attribute' => 'import_files_statuse_id', |
93
|
|
|
'relation' => 'statuse', |
94
|
|
|
'required' => true, |
95
|
|
|
'defaultValue' => FilesStatuse::getIdByKey(FilesStatuse::NEW), |
96
|
|
|
'data' => function () { |
97
|
|
|
return $this->getAllowedStatusesList(); |
98
|
|
|
}, |
99
|
|
|
'rules' => [ |
100
|
|
|
'validateStatus' => ['import_files_statuse_id', 'in', 'range' => function () { |
101
|
|
|
return array_keys($this->getAllowedStatusesList()); |
102
|
|
|
}], |
103
|
|
|
], |
104
|
|
|
// 'rules' => [ |
105
|
|
|
// 'defaultValueOnForm' => [ |
106
|
|
|
// 'import_files_statuse_id', |
107
|
|
|
// 'default', |
108
|
|
|
// 'value' => FilesStatuse::getIdByKey(FilesStatuse::NEW), |
109
|
|
|
// 'on' => Field::SCENARIO_FORM, |
110
|
|
|
// ], |
111
|
|
|
// ], |
112
|
|
|
], |
113
|
|
|
'import_files_source_id' => [ |
114
|
|
|
'class' => HasOneSelect2::class, |
115
|
|
|
'attribute' => 'import_files_source_id', |
116
|
|
|
'required' => true, |
117
|
|
|
'relation' => 'source', |
118
|
|
|
// 'defaultValue' => FilesStatuse::getIdByKey('new'), |
119
|
|
|
], |
120
|
|
|
'rows_count' => [ |
121
|
|
|
'attribute' => 'rows_count', |
122
|
|
|
'displayOnly' => true, |
123
|
|
|
], |
124
|
|
|
'rows_errors' => [ |
125
|
|
|
'attribute' => 'rows_errors', |
126
|
|
|
'displayOnly' => true, |
127
|
|
|
], |
128
|
|
|
'rows_success' => [ |
129
|
|
|
'attribute' => 'rows_success', |
130
|
|
|
'displayOnly' => true, |
131
|
|
|
], |
132
|
|
|
'time_complete_calculated' => [ |
133
|
|
|
'attribute' => 'time_complete_calculated', |
134
|
|
|
'displayOnly' => true, |
135
|
|
|
'scope' => false, |
136
|
|
|
'column' => [ |
137
|
|
|
'filter' => false, |
138
|
|
|
// 'value' => function () { |
139
|
|
|
// return $this->getTime_complete_calculated(); |
140
|
|
|
// } |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
// 'eventsCount' => [ |
144
|
|
|
// 'attribute' => 'eventsCount', |
145
|
|
|
// 'column' => [ |
146
|
|
|
// 'filter' => [ |
147
|
|
|
// '0' => '0', |
148
|
|
|
// '1' => '>0', |
149
|
|
|
// ], |
150
|
|
|
// ], |
151
|
|
|
// 'scope' => function ($q, $model) { |
152
|
|
|
// $q->byEventsCount($model->eventsCount) |
153
|
|
|
// ->withEventsCount(); |
154
|
|
|
// }, |
155
|
|
|
// 'displayOnly' => true, |
156
|
|
|
// ], |
157
|
|
|
'progress' => [ |
158
|
|
|
'attribute' => 'progress', |
159
|
|
|
'field' => [ |
160
|
|
|
'format' => ['percent', 2], |
161
|
|
|
], |
162
|
|
|
'column' => [ |
163
|
|
|
'format' => ['percent', 2], |
164
|
|
|
], |
165
|
|
|
'displayOnly' => true, |
166
|
|
|
'rules' => false, |
167
|
|
|
], |
168
|
|
|
'errorsPercent' => [ |
169
|
|
|
'attribute' => 'errorsPercent', |
170
|
|
|
'field' => [ |
171
|
|
|
'format' => ['percent', 2], |
172
|
|
|
], |
173
|
|
|
'column' => [ |
174
|
|
|
'format' => ['percent', 2], |
175
|
|
|
], |
176
|
|
|
'displayOnly' => true, |
177
|
|
|
'rules' => false, |
178
|
|
|
], |
179
|
|
|
'start_date' => [ |
180
|
|
|
'class' => Date::class, |
181
|
|
|
'attribute' => 'start_date', |
182
|
|
|
'displayOnly' => true, |
183
|
|
|
], |
184
|
|
|
'end_date' => [ |
185
|
|
|
'class' => Date::class, |
186
|
|
|
'attribute' => 'end_date', |
187
|
|
|
'displayOnly' => true, |
188
|
|
|
], |
189
|
|
|
'logsGrouped' => [ |
190
|
|
|
'class' => HasManyMultipleInput::class, |
191
|
|
|
'order' => 115, |
192
|
|
|
'attribute' => 'logsGrouped', |
193
|
|
|
'relation' => 'logsGrouped', |
194
|
|
|
'isGridForOldRecords' => true, |
195
|
|
|
'scope' => false, |
196
|
|
|
'column' => false, |
197
|
|
|
'field' => false, |
198
|
|
|
'gridOptions' => [ |
199
|
|
|
'responsiveWrap' => false, |
200
|
|
|
'showPageSummary' => true, |
201
|
|
|
], |
202
|
|
|
], |
203
|
|
|
'logs' => [ |
204
|
|
|
'class' => HasManyMultipleInput::class, |
205
|
|
|
'order' => 115, |
206
|
|
|
'attribute' => 'logs', |
207
|
|
|
'relation' => 'logs', |
208
|
|
|
'isGridForOldRecords' => true, |
209
|
|
|
'scope' => false, |
210
|
|
|
'column' => false, |
211
|
|
|
'field' => false, |
212
|
|
|
'gridOptions' => [ |
213
|
|
|
'responsiveWrap' => false, |
214
|
|
|
'showPageSummary' => true, |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
'process_id' => [ |
218
|
|
|
'attribute' => 'process_id', |
219
|
|
|
'displayOnly' => true |
220
|
|
|
], |
221
|
|
|
]), |
222
|
|
|
'plugins' => \yii::$app->getModule('import')->getFilesCrudFieldsPlugins(), |
223
|
|
|
], |
224
|
|
|
'date' => [ |
225
|
|
|
'class' => TimestampBehavior::class, |
226
|
|
|
'createdAtAttribute' => 'created', |
227
|
|
|
'updatedAtAttribute' => 'updated', |
228
|
|
|
'value' => new Expression('NOW()'), |
229
|
|
|
], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getOldStatuse() { |
234
|
|
|
if ($attributes = $this->getDirtyAttributes(['import_files_statuse_id'])) { |
|
|
|
|
235
|
|
|
if (empty($this->oldAttributes['import_files_statuse_id'])) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$id = $this->oldAttributes['import_files_statuse_id']; |
240
|
|
|
} else { |
241
|
|
|
$id = $this->import_files_statuse_id; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ($id) { |
245
|
|
|
return FilesStatuse::findOne($id); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function getAllowedStatusesList() { |
250
|
|
|
$q = FilesStatuse::find(); |
251
|
|
|
if ($this->scenario === 'form') { |
252
|
|
|
if ($statuse = $this->getOldStatuse()) { |
253
|
|
|
$q->isAllowedForKey($statuse->key); |
254
|
|
|
} else { |
255
|
|
|
$q->byKey(FilesStatuse::NEW); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $q->forSelect(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function getTime_complete_calculated() { |
263
|
|
|
$secondsElapse = false; |
264
|
|
|
if (!$this->isLoading()) { |
265
|
|
|
$startTime = strtotime($this->start_date); |
266
|
|
|
$endTime = strtotime($this->end_date); |
267
|
|
|
|
268
|
|
|
$secondsElapse = $endTime - $startTime; |
269
|
|
|
} else { |
270
|
|
|
|
271
|
|
|
$startTime = strtotime($this->start_date); |
272
|
|
|
$currentTime = time(); |
273
|
|
|
|
274
|
|
|
$totalRows = ($this->rows_success + $this->rows_errors); |
275
|
|
|
if ($totalRows > 0) { |
276
|
|
|
$secondsElapse = ($currentTime - $startTime) / $totalRows * $this->rows_count; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if ($secondsElapse) { |
|
|
|
|
282
|
|
|
return date('H:i:s', $startTime + $secondsElapse) . ' (' . gmdate("H:i:s", $secondsElapse) . ')'; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function search() { |
287
|
|
|
$dp = $this->getBehavior('fields')->search(); |
288
|
|
|
$q = $dp->query; |
289
|
|
|
|
290
|
|
|
$select = $this->attributes(); |
291
|
|
|
unset($select[array_search('content', $select)]); |
292
|
|
|
|
293
|
|
|
$sort = $dp->sort; |
294
|
|
|
// $sort->attributes['eventsCount'] = [ |
295
|
|
|
// 'asc' => ['eventsCount' => SORT_ASC], |
296
|
|
|
// 'desc' => ['eventsCount' => SORT_DESC], |
297
|
|
|
// ]; |
298
|
|
|
|
299
|
|
|
$sort->attributes['progress'] = [ |
300
|
|
|
'asc' => ['progress' => SORT_ASC], |
301
|
|
|
'desc' => ['progress' => SORT_DESC], |
302
|
|
|
]; |
303
|
|
|
|
304
|
|
|
$sort->attributes['errorsPercent'] = [ |
305
|
|
|
'asc' => ['errorsPercent' => SORT_ASC], |
306
|
|
|
'desc' => ['errorsPercent' => SORT_DESC], |
307
|
|
|
]; |
308
|
|
|
|
309
|
|
|
unset($q->select[array_search('*', $q->select)]); |
310
|
|
|
$q->select = array_merge($q->select, $select); |
311
|
|
|
|
312
|
|
|
return $dp; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
public function beforeValidate() |
317
|
|
|
{ |
318
|
|
|
if (!empty($this->contentFile)) { |
319
|
|
|
$this->name = $this->md5 = $this->extension = $this->mime_type = null; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return parent::beforeValidate(); // TODO: Change the autogenerated stub |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function isLoading() { |
326
|
|
|
return $this->import_files_statuse_id === FilesStatuse::getIdByKey(FilesStatuse::LOADING); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function isError() { |
330
|
|
|
return $this->import_files_statuse_id === FilesStatuse::getIdByKey(FilesStatuse::ERROR); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function isComplete() { |
334
|
|
|
return $this->import_files_statuse_id === FilesStatuse::getIdByKey(FilesStatuse::LOADED); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
public function getAllowedExtensions() { |
339
|
|
|
return [ |
340
|
|
|
'rar', |
341
|
|
|
'zip', |
342
|
|
|
'xls', |
343
|
|
|
'xlt', |
344
|
|
|
'xlsx', |
345
|
|
|
'xlsm', |
346
|
|
|
'xltx', |
347
|
|
|
'xltm', |
348
|
|
|
'ods', |
349
|
|
|
'ots', |
350
|
|
|
'slk', |
351
|
|
|
'xml', |
352
|
|
|
'csv', |
353
|
|
|
'txt', |
354
|
|
|
'gnumeric', |
355
|
|
|
]; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public static function find() |
359
|
|
|
{ |
360
|
|
|
$q = new \execut\import\models\queries\File(__CLASS__); |
361
|
|
|
return $q->withErrorsPercent()->withProgress(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function isStop() { |
365
|
|
|
return File::find()->isStop()->byId($this->id)->count() > 0; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function isCancelLoading() { |
369
|
|
|
return File::find()->isCancelLoading()->byId($this->id)->count() > 0; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function triggerStop() { |
373
|
|
|
$this->setStatus(FilesStatuse::STOPED); |
374
|
|
|
$this->save(false); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function triggerDeleting() { |
378
|
|
|
$this->setStatus(FilesStatuse::DELETING); |
379
|
|
|
$this->save(); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function triggerLoading() { |
383
|
|
|
$this->deleteLogs(); |
384
|
|
|
$this->process_id = getmypid(); |
|
|
|
|
385
|
|
|
$this->start_date = date('Y-m-d H:i:s'); |
386
|
|
|
$this->rows_errors = 0; |
387
|
|
|
$this->rows_success = 0; |
388
|
|
|
$this->setStatus(FilesStatuse::LOADING); |
389
|
|
|
$this->save(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function getCalculatedSetsCount() { |
393
|
|
|
return count($this->getSettings()) * $this->getCalculatedRowsCount(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function getErrorsPercent() { |
397
|
|
|
$percent = 0; |
398
|
|
|
if (($this->rows_success || $this->rows_errors) && ($this->rows_success + $this->rows_errors) > 0) { |
399
|
|
|
$percent = $this->rows_errors / ($this->rows_success + $this->rows_errors); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $percent; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
public function triggerLoaded() { |
406
|
|
|
$this->end_date = date('Y-m-d H:i:s'); |
407
|
|
|
$this->setStatus(FilesStatuse::LOADED); |
408
|
|
|
$this->save(false, ['end_date', 'import_files_statuse_id']); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function triggerErrorRow() { |
412
|
|
|
$this->rows_errors += count($this->getSettings()); |
413
|
|
|
$this->triggerCompleteRow(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
public function triggerException() { |
417
|
|
|
$this->triggerErrorRow(); |
418
|
|
|
$this->end_date = date('Y-m-d H:i:s'); |
419
|
|
|
$this->setStatus(FilesStatuse::ERROR); |
420
|
|
|
$this->save(); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
public function triggerSuccessRow() { |
424
|
|
|
$this->rows_success++; |
425
|
|
|
$this->triggerCompleteRow(); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function scenarios() |
429
|
|
|
{ |
430
|
|
|
return array_merge(parent::scenarios(), [ |
431
|
|
|
'import' => [ |
432
|
|
|
'import_files_statuse_id', |
433
|
|
|
'start_date', |
434
|
|
|
'rows_count', |
435
|
|
|
'rows_errors', |
436
|
|
|
'rows_success', |
437
|
|
|
], |
438
|
|
|
]); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
protected $currentStep = 0; |
442
|
|
|
protected $saveStep = 10; |
443
|
|
|
public function triggerCompleteRow() { |
444
|
|
|
if ($this->currentStep === $this->saveStep || $this->completeRows == $this->calculatedRowsCount) { |
|
|
|
|
445
|
|
|
$this->currentStep = 0; |
446
|
|
|
$this->save(false, ['rows_errors', 'rows_success']); |
447
|
|
|
} else { |
448
|
|
|
$this->currentStep++; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function getCompleteRows() { |
453
|
|
|
return $this->rows_errors + $this->rows_success; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
public function getCalculatedRowsCount() { |
457
|
|
|
return count($this->getRows()); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
public function getRows() { |
461
|
|
|
if ($this->rows !== null) { |
462
|
|
|
return $this->rows; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$fileHandler = $this->content; |
466
|
|
|
$isUnlink = false; |
467
|
|
|
if (self::getDb()->schema instanceof Schema) { |
468
|
|
|
$file = tempnam(sys_get_temp_dir(), 'import_'); |
469
|
|
|
file_put_contents($file, $fileHandler); |
470
|
|
|
$fileHandler = $file; |
471
|
|
|
$isUnlink = true; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
$setting = $this->setting; |
|
|
|
|
475
|
|
|
$mimeType = $this->detectMimeType(); |
476
|
|
|
$converter = new ToArrayConverter([ |
477
|
|
|
'file' => $fileHandler, |
478
|
|
|
'trim' => '\'', |
479
|
|
|
'encoding' => $setting->filesEncoding->key, |
480
|
|
|
'mimeType' => $mimeType, |
481
|
|
|
]); |
482
|
|
|
if (!empty($setting->csv_delimiter)) { |
483
|
|
|
$converter->delimiter = $setting->csv_delimiter; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
if (!empty($setting->csv_enclosure)) { |
487
|
|
|
$converter->enclosure = $setting->csv_enclosure; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$data = $converter->convert(); |
491
|
|
|
$startFrom = $setting->ignored_lines; |
492
|
|
|
|
493
|
|
|
$data = array_splice($data, $startFrom); |
494
|
|
|
if ($isUnlink) { |
495
|
|
|
unlink($file); |
|
|
|
|
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
return $this->rows = $data; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
protected function detectMimeType() { |
502
|
|
|
if ($this->setting->is_check_mime_type) { |
|
|
|
|
503
|
|
|
return; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$mimeType = FileHelper::getMimeTypeByExtension($this->name); |
507
|
|
|
|
508
|
|
|
return $mimeType; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
public function isCheckExtension() { |
512
|
|
|
if (!$this->setting->is_check_mime_type) { |
|
|
|
|
513
|
|
|
return true; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
if (empty($this->mime_type)) { |
517
|
|
|
return false; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
$extensionsByMimeType = FileHelper::getExtensionsByMimeType($this->mime_type); |
521
|
|
|
|
522
|
|
|
if (!in_array($this->extension, $extensionsByMimeType, true)) { |
523
|
|
|
return false; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
return true; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @param $attributes |
531
|
|
|
*/ |
532
|
|
|
public function logError($attributes) |
533
|
|
|
{ |
534
|
|
|
$attributes['import_file_id'] = $this->id; |
535
|
|
|
$log = new Log($attributes); |
536
|
|
|
if (!$log->save()) { |
537
|
|
|
// var_dump($attributes); |
538
|
|
|
// exit; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
public function deleteLogs() { |
543
|
|
|
for ($tryCount = 0; $tryCount < 4; $tryCount++) { |
544
|
|
|
try { |
545
|
|
|
Log::deleteAll(['import_file_id' => $this->id]); |
546
|
|
|
|
547
|
|
|
return true; |
548
|
|
|
} catch (Exception $e) { |
549
|
|
|
if (strpos($e->getMessage(), 'Deadlock detected') !== false && $tryCount == 3) { |
550
|
|
|
throw $e; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
sleep(10); |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
public function checkWhatSheetsIsNotEmpty() { |
559
|
|
|
if ($this->setting) { |
|
|
|
|
560
|
|
|
if (empty($this->setting->settingsSheets)) { |
561
|
|
|
$this->addError('import_setting_id', 'You must add at least one sheet to selected setting'); |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
public function beforeDelete() |
567
|
|
|
{ |
568
|
|
|
$this->deleteLogs(); |
569
|
|
|
return parent::beforeDelete(); // TODO: Change the autogenerated stub |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* @param $key |
574
|
|
|
*/ |
575
|
|
|
protected function setStatus($key) |
576
|
|
|
{ |
577
|
|
|
$this->import_files_statuse_id = FilesStatuse::getIdByKey($key); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
public function getSettings() { |
581
|
|
|
return $this->setting->settingsSheets[0]->getSettings(); |
|
|
|
|
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public function getLogsGrouped() |
585
|
|
|
{ |
586
|
|
|
$result = parent::getLogs(); // TODO: Change the autogenerated stub |
587
|
|
|
$result->modelClass = LogGrouped::class; |
588
|
|
|
|
589
|
|
|
return $result->select([ |
590
|
|
|
'message', |
591
|
|
|
'category', |
592
|
|
|
'logsCount' => new Expression('count(category)'), |
593
|
|
|
])->groupBy([ |
594
|
|
|
'message', |
595
|
|
|
'category', |
596
|
|
|
])->orderBy('logsCount DESC'); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
public function getLogs() |
600
|
|
|
{ |
601
|
|
|
$result = parent::getLogs(); // TODO: Change the autogenerated stub |
602
|
|
|
$result->modelClass = Log::class; |
603
|
|
|
|
604
|
|
|
return $result; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
// public function getDataProvider() { |
608
|
|
|
// $attributes = $this->attributes(); |
609
|
|
|
// unset($attributes[array_search('content', $attributes)]); |
610
|
|
|
// unset($attributes[array_search('id', $attributes)]); |
611
|
|
|
// unset($attributes[array_search('created', $attributes)]); |
612
|
|
|
// unset($attributes[array_search('updated', $attributes)]); |
613
|
|
|
// unset($attributes[array_search('name', $attributes)]); |
614
|
|
|
// unset($attributes[array_search('import_files_source_id', $attributes)]); |
615
|
|
|
// unset($attributes[array_search('import_setting_id', $attributes)]); |
616
|
|
|
// unset($attributes[array_search('start_date', $attributes)]); |
617
|
|
|
// unset($attributes[array_search('end_date', $attributes)]); |
618
|
|
|
// $attributes[] = 'import_files.import_setting_id'; |
619
|
|
|
// $attributes[] = 'import_files.start_date'; |
620
|
|
|
// $attributes[] = 'import_files.end_date'; |
621
|
|
|
// $attributes[] = 'import_files.id'; |
622
|
|
|
// $attributes[] = 'import_files.created'; |
623
|
|
|
// $attributes[] = 'import_files.updated'; |
624
|
|
|
// $attributes[] = 'import_files.name'; |
625
|
|
|
// $attributes[] = 'import_files.import_files_source_id'; |
626
|
|
|
// /** |
627
|
|
|
// * @var \execut\import\models\queries\File $q |
628
|
|
|
// */ |
629
|
|
|
// $q = self::find()->select($attributes)->withErrorsPercent()->withProgress(); |
630
|
|
|
// $q->with([ |
631
|
|
|
// 'importFilesStatuse', |
632
|
|
|
// 'importSetting.schedulerEvents' |
633
|
|
|
// ]); |
634
|
|
|
// $q->joinWith('importSetting'); |
635
|
|
|
// $provider = new ActiveDataProvider([ |
636
|
|
|
// 'query' => $q, |
637
|
|
|
// ]); |
638
|
|
|
// |
639
|
|
|
// $sort = $provider->sort; |
640
|
|
|
// $sort->attributes['import_setting_id'] = [ |
641
|
|
|
// 'asc' => ['import_settings.name' => SORT_ASC], |
642
|
|
|
// 'desc' => ['import_settings.name' => SORT_DESC], |
643
|
|
|
// ]; |
644
|
|
|
// |
645
|
|
|
// $sort->attributes['eventsCount'] = [ |
646
|
|
|
// 'asc' => ['eventsCount' => SORT_ASC], |
647
|
|
|
// 'desc' => ['eventsCount' => SORT_DESC], |
648
|
|
|
// ]; |
649
|
|
|
// |
650
|
|
|
// $sort->attributes['progress'] = [ |
651
|
|
|
// 'asc' => ['progress' => SORT_ASC], |
652
|
|
|
// 'desc' => ['progress' => SORT_DESC], |
653
|
|
|
// ]; |
654
|
|
|
// |
655
|
|
|
// $sort->attributes['errorsPercent'] = [ |
656
|
|
|
// 'asc' => ['errorsPercent' => SORT_ASC], |
657
|
|
|
// 'desc' => ['errorsPercent' => SORT_DESC], |
658
|
|
|
// ]; |
659
|
|
|
// |
660
|
|
|
// $vsSql = '(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id)'; |
661
|
|
|
// |
662
|
|
|
// $q->select['eventsCount'] = $vsSql; |
663
|
|
|
// |
664
|
|
|
// if ($this->eventsCount === '1') { |
665
|
|
|
// $q->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) > 0'); |
666
|
|
|
// } else if ($this->eventsCount === '0') { |
667
|
|
|
// $q->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) = 0'); |
668
|
|
|
// } |
669
|
|
|
// |
670
|
|
|
// $equalsAttributes = ['import_setting_id', 'import_files_statuse_id']; |
671
|
|
|
// foreach ($equalsAttributes as $attribute) { |
672
|
|
|
// $q->andFilterWhere([ |
673
|
|
|
// $attribute => $this->$attribute, |
674
|
|
|
// ]); |
675
|
|
|
// } |
676
|
|
|
// |
677
|
|
|
// $likeAttributes = ['name', 'extension', 'mime_type', 'md5']; |
678
|
|
|
// if ($this->end_date) { |
679
|
|
|
// $parts = explode(' - ', $this->end_date); |
680
|
|
|
// if (!empty($parts[0])) { |
681
|
|
|
// $q->andFilterWhere([ |
682
|
|
|
// '>=', |
683
|
|
|
// 'end_date', |
684
|
|
|
// $parts[0] . ' 0:00:00' |
685
|
|
|
// ]); |
686
|
|
|
// } |
687
|
|
|
// |
688
|
|
|
// if (!empty($parts[1])) { |
689
|
|
|
// $q->andFilterWhere([ |
690
|
|
|
// '<=', |
691
|
|
|
// 'end_date', |
692
|
|
|
// $parts[1] . ' 23:59:59' |
693
|
|
|
// ]); |
694
|
|
|
// } |
695
|
|
|
// } |
696
|
|
|
// |
697
|
|
|
// foreach ($likeAttributes as $attribute) { |
698
|
|
|
// $q->andFilterWhere([ |
699
|
|
|
// 'ILIKE', |
700
|
|
|
// $attribute, |
701
|
|
|
// $this->$attribute, |
702
|
|
|
// ]); |
703
|
|
|
// } |
704
|
|
|
// |
705
|
|
|
// return $provider; |
706
|
|
|
// } |
707
|
|
|
|
708
|
|
|
// public function attributeLabels() |
709
|
|
|
// { |
710
|
|
|
// return [ |
711
|
|
|
// 'import_setting_id' => 'Настройки', |
712
|
|
|
// 'import_files_statuse_id' => 'Статус', |
713
|
|
|
// 'name' => 'Название', |
714
|
|
|
// 'extension' => 'Расширение', |
715
|
|
|
// 'mime_type' => 'Тип', |
716
|
|
|
// 'created' => 'Создано', |
717
|
|
|
// 'updated' => 'Обновлено', |
718
|
|
|
// 'rows_count' => 'Всего', |
719
|
|
|
// 'rows_errors' => 'Ошибок', |
720
|
|
|
// 'rows_success' => 'Успешно', |
721
|
|
|
// 'start_date' => 'Начало', |
722
|
|
|
// 'end_date' => 'Конец', |
723
|
|
|
// 'progress' => 'Прогресс', |
724
|
|
|
// 'errorsPercent' => '% ошибок', |
725
|
|
|
// 'eventsCount' => 'События', |
726
|
|
|
// ]; |
727
|
|
|
// } |
728
|
|
|
// |
729
|
|
|
// public function getFormFields() { |
730
|
|
|
// $config = [ |
731
|
|
|
// [ |
732
|
|
|
// 'type' => DetailView::INPUT_SELECT2, |
733
|
|
|
// 'attribute' => 'import_setting_id', |
734
|
|
|
// 'value' => function ($row, $detailView) { |
735
|
|
|
// $row = $detailView->model; |
736
|
|
|
// if ($row->import_setting_id) { |
737
|
|
|
// return Html::a($row->importSetting->name, [ |
738
|
|
|
// '/catalog/import/import-settings/update', |
739
|
|
|
// 'id' => $row->import_setting_id, |
740
|
|
|
// ]); |
741
|
|
|
// } |
742
|
|
|
// }, |
743
|
|
|
// 'widgetOptions' => [ |
744
|
|
|
// 'pluginOptions' => [ |
745
|
|
|
// 'allowClear' => true, |
746
|
|
|
// ], |
747
|
|
|
// 'data' => ArrayHelper::merge(['' => ''], ArrayHelper::map(Setting::find()->orderBy('name')->all(), 'id', 'name')), |
748
|
|
|
// ], |
749
|
|
|
// 'format' => 'html', |
750
|
|
|
// ], |
751
|
|
|
// [ |
752
|
|
|
// 'type' => DetailView::INPUT_SELECT2, |
753
|
|
|
// 'attribute' => 'import_files_statuse_id', |
754
|
|
|
// 'value' => function ($row, $detailView) { |
755
|
|
|
// $row = $detailView->model; |
756
|
|
|
// if ($row->importFilesStatuse) { |
757
|
|
|
// return $row->importFilesStatuse->name; |
758
|
|
|
// } |
759
|
|
|
// }, |
760
|
|
|
// 'widgetOptions' => [ |
761
|
|
|
// 'pluginOptions' => [ |
762
|
|
|
// 'allowClear' => true, |
763
|
|
|
// ], |
764
|
|
|
// 'data' => ArrayHelper::merge(['' => ''], ArrayHelper::map(FilesStatuse::find()->all(), 'id', 'name')), |
765
|
|
|
// ], |
766
|
|
|
// 'format' => 'html', |
767
|
|
|
// ], |
768
|
|
|
// [ |
769
|
|
|
// 'type' => DetailView::INPUT_FILE, |
770
|
|
|
// 'attribute' => 'contentFile', |
771
|
|
|
// ], |
772
|
|
|
// ]; |
773
|
|
|
// |
774
|
|
|
// if (!$this->isNewRecord) { |
775
|
|
|
// $config = ArrayHelper::merge($config, [ |
776
|
|
|
// [ |
777
|
|
|
// 'attribute' => 'name', |
778
|
|
|
// 'value' => function () { |
779
|
|
|
// return Html::a($this->name, [ |
780
|
|
|
// 'download', |
781
|
|
|
// 'id' => $this->id, |
782
|
|
|
// ]); |
783
|
|
|
// }, |
784
|
|
|
// 'format' => 'raw', |
785
|
|
|
// 'displayOnly' => true, |
786
|
|
|
// ], |
787
|
|
|
// [ |
788
|
|
|
// 'attribute' => 'extension', |
789
|
|
|
// 'displayOnly' => true, |
790
|
|
|
// ], |
791
|
|
|
// [ |
792
|
|
|
// 'attribute' => 'mime_type', |
793
|
|
|
// 'displayOnly' => true, |
794
|
|
|
// ], |
795
|
|
|
// [ |
796
|
|
|
// 'attribute' => 'md5', |
797
|
|
|
// 'displayOnly' => true, |
798
|
|
|
// ], |
799
|
|
|
// [ |
800
|
|
|
// 'attribute' => 'created', |
801
|
|
|
// 'displayOnly' => true, |
802
|
|
|
// ], |
803
|
|
|
// [ |
804
|
|
|
// 'attribute' => 'updated', |
805
|
|
|
// 'displayOnly' => true, |
806
|
|
|
// ], |
807
|
|
|
// [ |
808
|
|
|
// 'attribute' => 'rows_count', |
809
|
|
|
// 'displayOnly' => true, |
810
|
|
|
// ], |
811
|
|
|
// [ |
812
|
|
|
// 'attribute' => 'rows_errors', |
813
|
|
|
// 'displayOnly' => true, |
814
|
|
|
// ], |
815
|
|
|
// [ |
816
|
|
|
// 'attribute' => 'rows_success', |
817
|
|
|
// 'displayOnly' => true, |
818
|
|
|
// ], |
819
|
|
|
// [ |
820
|
|
|
// 'attribute' => 'progress', |
821
|
|
|
// 'format' => ['percent', 2], |
822
|
|
|
// 'displayOnly' => true, |
823
|
|
|
// ], |
824
|
|
|
// [ |
825
|
|
|
// 'attribute' => 'errorsPercent', |
826
|
|
|
// 'format' => ['percent', 2], |
827
|
|
|
// 'displayOnly' => true, |
828
|
|
|
// ], |
829
|
|
|
// [ |
830
|
|
|
// 'attribute' => 'start_date', |
831
|
|
|
// 'displayOnly' => true, |
832
|
|
|
// ], |
833
|
|
|
// [ |
834
|
|
|
// 'attribute' => 'end_date', |
835
|
|
|
// 'displayOnly' => true, |
836
|
|
|
// ], |
837
|
|
|
//// [ |
838
|
|
|
//// 'type' => DetailView::INPUT_TEXT, |
839
|
|
|
//// 'attribute' => 'preview', |
840
|
|
|
//// 'value' => function ($row, $detailView) { |
841
|
|
|
//// $row = $detailView->model; |
842
|
|
|
//// if ($row->importSetting) { |
843
|
|
|
//// $data = $row->getRows(); |
844
|
|
|
//// $data = array_splice($data, 0, 10); |
845
|
|
|
//// $dataProvider = new ArrayDataProvider([ |
846
|
|
|
//// 'allModels' => $data, |
847
|
|
|
//// ]); |
848
|
|
|
//// return GridView::widget([ |
849
|
|
|
//// 'dataProvider' => $dataProvider, |
850
|
|
|
//// ]); |
851
|
|
|
//// } |
852
|
|
|
//// }, |
853
|
|
|
//// 'format' => 'html', |
854
|
|
|
//// 'displayOnly' => true, |
855
|
|
|
//// ], |
856
|
|
|
// ]); |
857
|
|
|
// } |
858
|
|
|
// |
859
|
|
|
// return $config; |
860
|
|
|
// } |
861
|
|
|
// |
862
|
|
|
// public function getGridColumns() { |
863
|
|
|
// return [ |
864
|
|
|
// 'id', |
865
|
|
|
// [ |
866
|
|
|
// 'attribute' => 'import_setting_id', |
867
|
|
|
// 'format' => 'raw', |
868
|
|
|
// 'value' => function ($row) { |
869
|
|
|
// if ($row->import_setting_id) { |
870
|
|
|
// return Html::a($row->importSetting->name, [ |
871
|
|
|
// '/catalog/import/import-settings/update', |
872
|
|
|
// 'id' => $row->import_setting_id, |
873
|
|
|
// ]); |
874
|
|
|
// } |
875
|
|
|
// }, |
876
|
|
|
// 'filter' => Setting::find()->forSelect(), |
877
|
|
|
// ], |
878
|
|
|
// [ |
879
|
|
|
// 'attribute' => 'import_files_statuse_id', |
880
|
|
|
// 'value' => 'importFilesStatuse.name', |
881
|
|
|
// 'filter' => FilesStatuse::find()->forSelect(), |
882
|
|
|
// ], |
883
|
|
|
// [ |
884
|
|
|
// 'attribute' => 'name', |
885
|
|
|
// 'format' => 'raw', |
886
|
|
|
// 'value' => function ($row) { |
887
|
|
|
// return Html::a($row->name, [ |
888
|
|
|
// 'download', |
889
|
|
|
// 'id' => $row->id, |
890
|
|
|
// ]); |
891
|
|
|
// } |
892
|
|
|
// ], |
893
|
|
|
// 'extension', |
894
|
|
|
// 'mime_type', |
895
|
|
|
// 'md5', |
896
|
|
|
// 'created', |
897
|
|
|
// 'updated', |
898
|
|
|
// 'rows_count', |
899
|
|
|
// 'rows_errors', |
900
|
|
|
// 'rows_success', |
901
|
|
|
// [ |
902
|
|
|
// 'attribute' => 'eventsCount', |
903
|
|
|
// 'filter' => [ |
904
|
|
|
// '0' => '0', |
905
|
|
|
// '1' => '>0', |
906
|
|
|
// ], |
907
|
|
|
// ], |
908
|
|
|
// [ |
909
|
|
|
// 'attribute' => 'progress', |
910
|
|
|
// 'format' => ['percent', 2], |
911
|
|
|
// ], |
912
|
|
|
// [ |
913
|
|
|
// 'attribute' => 'errorsPercent', |
914
|
|
|
// 'format' => ['percent', 2], |
915
|
|
|
// ], |
916
|
|
|
// 'start_date', |
917
|
|
|
// [ |
918
|
|
|
// 'attribute' => 'end_date', |
919
|
|
|
// 'filter' => DateRangePicker::widget([ |
920
|
|
|
// 'attribute' => 'end_date', |
921
|
|
|
// 'model' => $this, |
922
|
|
|
// 'convertFormat'=>true, |
923
|
|
|
// 'pluginOptions'=>[ |
924
|
|
|
// 'timePicker'=>true, |
925
|
|
|
// 'timePickerIncrement'=>15, |
926
|
|
|
// 'locale'=>['format'=>'Y-m-d'] |
927
|
|
|
// ] |
928
|
|
|
// ]), |
929
|
|
|
// ], |
930
|
|
|
// 'eventStatus', |
931
|
|
|
// [ |
932
|
|
|
// 'class' => ActionColumn::class, |
933
|
|
|
// 'buttons' => [ |
934
|
|
|
//// 'update' => function () {}, |
935
|
|
|
// 'view' => function () {}, |
936
|
|
|
// 'delete' => function () {}, |
937
|
|
|
// ], |
938
|
|
|
// ], |
939
|
|
|
// ]; |
940
|
|
|
// } |
941
|
|
|
// |
942
|
|
|
// public function search() { |
943
|
|
|
// return $this->getDataProvider(); |
944
|
|
|
// } |
945
|
|
|
|
946
|
|
|
public function __toString() |
947
|
|
|
{ |
948
|
|
|
return '#' . $this->id . ' ' . $this->name; |
949
|
|
|
} |
950
|
|
|
} |
951
|
|
|
|