public function CSVUpdateForm::readCsvProductsToUpdate in Commerce Smart Importer 8
Reads and formats products for update.
File
- src/
Form/ CSVUpdateForm.php, line 333
Class
- CSVUpdateForm
- Class CSVUpdateForm.
Namespace
Drupal\commerce_smart_importer\FormCode
public function readCsvProductsToUpdate($uri, $fields, $offset, ImportingParameters $parameters, $external_folders, $save, &$context) {
$identifiersIndex = $this
->getIdentifiersIndex($fields);
$file = fopen($uri, 'r');
if (!array_key_exists('last_stop', $context['results'])) {
$import_name = explode('/', $save);
$context['results']['import_name'] = end($import_name);
$context['results']['current_row'] = 3;
fgetcsv($file);
fgetcsv($file);
if ($parameters->createProduct === FALSE) {
touch($save . '/log.json');
file_put_contents($save . '/field_definitions.json', json_encode($fields, JSON_UNESCAPED_UNICODE));
}
}
else {
fseek($file, $context['results']['last_stop']);
}
if ($parameters->createProduct === FALSE) {
$log = file_get_contents($save . '/log.json');
if (!empty($log)) {
$log = json_decode($log, TRUE);
}
else {
$log = [];
}
}
$counter = 0;
while (($line = fgetcsv($file)) !== FALSE) {
$temp_log = [
'product' => [
'has_log' => FALSE,
'initial' => TRUE,
],
'variations' => [
[
'has_log' => FALSE,
'initial' => TRUE,
],
],
];
if ($counter == $offset) {
break;
}
$entity_variation = FALSE;
if (!empty($fields['variation'])) {
$entity_variation = $this
->loadVariationByIndex($fields['variation'], $identifiersIndex['variation'], $line);
if ($entity_variation !== FALSE) {
$this
->reformatLine($line, $fields['variation']);
$has_log = $this->smartImporterService
->updateProduct($entity_variation, $fields['variation'], $line, $external_folders, $parameters);
if ($has_log !== TRUE) {
$temp_log['variations'][0] = $has_log;
$temp_log['product']['has_log'] = TRUE;
}
}
}
if (!empty($fields['product'])) {
if (!$this
->isEmptyLine($fields['product'], $line)) {
$entity = $this
->loadProductByIndex($fields['product'], $identifiersIndex['product'], $line, $entity_variation);
if ($entity !== FALSE) {
$this
->reformatLine($line, $fields['product']);
$temp_log['product'] = $this->smartImporterService
->updateProduct($entity, $fields['product'], $line, $external_folders, $parameters);
}
}
}
$log[$context['results']['current_row']] = $temp_log;
$context['results']['last_stop'] = ftell($file);
$context['results']['current_row']++;
$counter++;
}
if ($parameters->createProduct === FALSE) {
file_put_contents($save . '/log.json', json_encode($log, JSON_UNESCAPED_UNICODE));
}
}