diff --git a/application/Espo/Controllers/Import.php b/application/Espo/Controllers/Import.php index 863752c0db..159c72c19f 100644 --- a/application/Espo/Controllers/Import.php +++ b/application/Espo/Controllers/Import.php @@ -133,6 +133,10 @@ class Import extends \Espo\Core\Controllers\Record 'action' => $data['action'], ); + if (array_key_exists('updateBy', $data)) { + $importParams['updateBy'] = $data['updateBy']; + } + $attachmentId = $data['attachmentId']; if (!$this->getAcl()->check($data['entityType'], 'edit')) { diff --git a/application/Espo/Resources/i18n/en_US/Import.json b/application/Espo/Resources/i18n/en_US/Import.json index cb18894526..1a427fb957 100644 --- a/application/Espo/Resources/i18n/en_US/Import.json +++ b/application/Espo/Resources/i18n/en_US/Import.json @@ -14,7 +14,11 @@ "Remove Duplicates": "Remove Duplicates", "importedCount": "Imported (count)", "duplicateCount": "Duplicates (count)", - "updatedCount": "Updated (count)" + "updatedCount": "Updated (count)", + "Create Only": "Create Only", + "Create and Update": "Create & Update", + "Update Only": "Update Only", + "Update by": "Update by" }, "messages": { "utf8": "Should be UTF-8 encoded", diff --git a/application/Espo/Services/Import.php b/application/Espo/Services/Import.php index 0174adf73a..6dd3f0a0c3 100644 --- a/application/Espo/Services/Import.php +++ b/application/Espo/Services/Import.php @@ -342,25 +342,33 @@ class Import extends \Espo\Services\Record $action = $params['action']; } - if (in_array($action, ['createAndUpdate', 'update']) && in_array('id', $fields)) { - $i = array_search('id', $fields); - $id = $row[$i]; - if (empty($id)) { - $id = null; + + if (in_array($action, ['createAndUpdate', 'update'])) { + if (!empty($params['updateBy']) && is_array($params['updateBy'])) { + $updateByFieldList = []; + $whereClause = array(); + foreach ($params['updateBy'] as $i) { + if (array_key_exists($i, $fields)) { + $updateByFieldList[] = $fields[$i]; + $whereClause[$fields[$i]] = $row[$i]; + } + } } } $recordService = $this->getRecordService($scope); if (in_array($action, ['createAndUpdate', 'update'])) { - if (!$id) { + if (!count($updateByFieldList)) { return; } - $entity = $this->getEntityManager()->getEntity($scope, $id); + $entity = $this->getEntityManager()->getRepository($scope)->where($whereClause)->findOne(); if (!$entity) { if ($action == 'createAndUpdate') { $entity = $this->getEntityManager()->getEntity($scope); - $entity->set('id', $id); + if (array_key_exists('id', $whereClause)) { + $entity->set('id', $whereClause['id']); + } } else { return; } diff --git a/frontend/client/res/templates/import/step-1.tpl b/frontend/client/res/templates/import/step-1.tpl index 601d566c6d..3b780b9614 100644 --- a/frontend/client/res/templates/import/step-1.tpl +++ b/frontend/client/res/templates/import/step-1.tpl @@ -25,6 +25,7 @@ diff --git a/frontend/client/src/views/import/step2.js b/frontend/client/src/views/import/step2.js index 86e0aa0f8a..1c839948a1 100644 --- a/frontend/client/src/views/import/step2.js +++ b/frontend/client/src/views/import/step2.js @@ -107,6 +107,11 @@ Espo.define('Views.Import.Step2', 'View', function (Dep) { $row.append($cell); $cell = $('').html(this.translate('First Row Value', 'labels', 'Import')); $row.append($cell); + + if (~['update', 'createAndUpdate'].indexOf(this.formData.action)) { + $cell = $('').html(this.translate('Update by', 'labels', 'Import')); + $row.append($cell); + } $table.append($row); this.mapping.forEach(function (d, i) { @@ -122,6 +127,15 @@ Espo.define('Views.Import.Step2', 'View', function (Dep) { $cell = $('').html(d.value); $row.append($cell); + if (~['update', 'createAndUpdate'].indexOf(this.formData.action)) { + var $checkbox = $('').attr('type', 'checkbox').attr('id', 'update-by-' + i.toString()); + if (d.name == 'id') { + $checkbox.attr('checked', true); + } + $cell = $('').append($checkbox); + $row.append($cell); + } + $table.append($row); }, this); @@ -303,9 +317,19 @@ Espo.define('Views.Import.Step2', 'View', function (Dep) { fields.push($('#column-' + i).val()); }, this); - this.formData.fields = fields; + if (~['update', 'createAndUpdate'].indexOf(this.formData.action)) { + var updateBy = []; + this.mapping.forEach(function (d, i) { + if ($('#update-by-' + i).get(0).checked) { + updateBy.push(i); + } + }, this); + this.formData.updateBy = updateBy; + } + + this.getParentView().formData = this.formData; this.disableButtons();