From 9ccf4f60837ab4d9f69cb161d4b829ff25cc5f76 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 18 Jun 2020 12:33:02 +0300 Subject: [PATCH] import commands 2 --- application/Espo/Controllers/Import.php | 17 ++------- .../Espo/Core/Console/Commands/Import.php | 30 +++++++++++++++ application/Espo/Services/Import.php | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/application/Espo/Controllers/Import.php b/application/Espo/Controllers/Import.php index d614211937..30a2c76ed8 100644 --- a/application/Espo/Controllers/Import.php +++ b/application/Espo/Controllers/Import.php @@ -78,24 +78,13 @@ class Import extends \Espo\Core\Controllers\Record return $this->getContainer()->get('entityManager'); } - public function actionUploadFile($params, $data, $request) + public function postActionUploadFile($params, $data) { $contents = $data; - if (!$request->isPost()) { - throw new BadRequest(); - } + $attachmentId = $this->getService('Import')->uploadFile($contents); - $attachment = $this->getEntityManager()->getEntity('Attachment'); - $attachment->set('type', 'text/csv'); - $attachment->set('role', 'Import File'); - $attachment->set('name', 'import-file.csv'); - $attachment->set('contents', $contents); - $this->getEntityManager()->saveEntity($attachment); - - return [ - 'attachmentId' => $attachment->id - ]; + return ['attachmentId' => $attachmentId]; } public function actionRevert($params, $data, $request) diff --git a/application/Espo/Core/Console/Commands/Import.php b/application/Espo/Core/Console/Commands/Import.php index 1b4a4d43cb..a219a0c385 100644 --- a/application/Espo/Core/Console/Commands/Import.php +++ b/application/Espo/Core/Console/Commands/Import.php @@ -34,12 +34,42 @@ class Import extends Base public function run($options, $flagList, $argumentList) { $id = $options['id'] ?? null; + $filePath = $options['file'] ?? null; + $paramsId = $options['paramsId'] ?? null; $service = $this->getContainer()->get('serviceFactory')->create('Import'); $forceResume = in_array('r', $flagList); $revert = in_array('u', $flagList); + if (!$id && $filePath) { + if (!$paramsId) { + $this->out("You need to specify --params-id option.\n"); + return; + } + + if (!file_exists($filePath)) { + $this->out("File not found.\n"); + return; + } + + $contents = file_get_contents($filePath); + + try { + $results = $service->importFileWithParamsId($contents, $paramsId); + + $resultId = $results['id']; + $countCreated = $results['countCreated']; + $countUpdated = $results['countUpdated']; + } catch (\Throwable $e) { + $this->out("Error occured: ".$e->getMessage()."\n"); + return; + } + + $this->out("Finished. Import ID: {$resultId}. Created: {$countCreated}. Updated: {$countUpdated}.\n"); + return; + } + if ($id && $revert) { $this->out("Reverting import...\n"); diff --git a/application/Espo/Services/Import.php b/application/Espo/Services/Import.php index 6d896896cf..44729522b3 100644 --- a/application/Espo/Services/Import.php +++ b/application/Espo/Services/Import.php @@ -152,6 +152,18 @@ class Import extends \Espo\Services\Record ]; } + public function uploadFile($contents) + { + $attachment = $this->getEntityManager()->getEntity('Attachment'); + $attachment->set('type', 'text/csv'); + $attachment->set('role', 'Import File'); + $attachment->set('name', 'import-file.csv'); + $attachment->set('contents', $contents); + $this->getEntityManager()->saveEntity($attachment); + + return $attachment->id; + } + protected function readCsvString(&$string, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { $o = []; @@ -368,6 +380,32 @@ class Import extends \Espo\Services\Record return $this->import($entityType, $attributeList, $import->get('fileId'), $params, $id); } + public function importFileWithParamsId(string $contents, string $importParamsId) + { + if (!$contents) { + throw new Error("File contents is empty."); + } + + $source = $this->getEntityManager()->getEntity('Import', $importParamsId); + + if (!$source) { + throw new Error("Import {$importParamsId} not found."); + } + + $entityType = $source->get('entityType'); + $attributeList = $source->get('attributeList') ?? []; + + $params = $source->get('params') ?? (object) []; + $params = json_decode(json_encode($params), true); + + unset($params['idleMode']); + unset($params['manualMode']); + + $attachmentId = $this->uploadFile($contents); + + return $this->import($entityType, $attributeList, $attachmentId, $params); + } + /** * @param array $params [ * 'delimiter' => (string),