From fb150866a72d91f8b2e2174cd83365d1d84382fc Mon Sep 17 00:00:00 2001 From: Dylan Maurits Date: Fri, 30 Apr 2021 16:03:18 +0200 Subject: [PATCH] [FEATURE][BACI-448] implemented csv import from backend --- Model/Import/Redirects.php | 289 +++++++++++++++++++++++++++++++ Model/PageNotFoundRepository.php | 23 ++- etc/import.xml | 5 + etc/module.xml | 4 + 4 files changed, 308 insertions(+), 13 deletions(-) create mode 100644 Model/Import/Redirects.php create mode 100644 etc/import.xml diff --git a/Model/Import/Redirects.php b/Model/Import/Redirects.php new file mode 100644 index 0000000..dc28e44 --- /dev/null +++ b/Model/Import/Redirects.php @@ -0,0 +1,289 @@ +jsonHelper = $jsonHelper; + $this->_importExportData = $importExportData; + $this->_resourceHelper = $resourceHelper; + $this->_dataSourceModel = $importData; + $this->resource = $resource; + $this->connection = $resource->getConnection(ResourceConnection::DEFAULT_CONNECTION); + $this->errorAggregator = $errorAggregator; + } + + /** + * Entity type code getter. + * + * @return string + */ + public function getEntityTypeCode() + { + return static::ENTITY_CODE; + } + + /** + * Get available columns + * + * @return array + */ + public function getValidColumnNames(): array + { + return $this->validColumnNames; + } + + /** + * Row validation + * + * @param array $rowData + * @param int $rowNum + * + * @return bool + */ + public function validateRow(array $rowData, $rowNum): bool + { + if (isset($this->_validatedRows[$rowNum])) { + return !$this->getErrorAggregator()->isRowInvalid($rowNum); + } + + $this->_validatedRows[$rowNum] = true; + + return !$this->getErrorAggregator()->isRowInvalid($rowNum); + } + + /** + * Import data + * + * @return bool + * + * @throws Exception + */ + protected function _importData(): bool + { + switch ($this->getBehavior()) { +case Import::BEHAVIOR_DELETE: +$this->deleteEntity(); +break; +case Import::BEHAVIOR_REPLACE: +$this->saveAndReplaceEntity(); +break; +case Import::BEHAVIOR_APPEND: +$this->saveAndReplaceEntity(); +break; +} + + return true; + } + + /** + * Delete entities + * + * @return bool + */ + private function deleteEntity(): bool + { + $rows = []; + while ($bunch = $this->_dataSourceModel->getNextBunch()) { + foreach ($bunch as $rowNum => $rowData) { + $this->validateRow($rowData, $rowNum); + + if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) { + $rowId = $rowData[static::ENTITY_ID_COLUMN]; + $rows[] = $rowId; + } + + if ($this->getErrorAggregator()->hasToBeTerminated()) { + $this->getErrorAggregator()->addRowToSkip($rowNum); + } + } + } + + if ($rows) { + return $this->deleteEntityFinish(array_unique($rows)); + } + + return false; + } + + /** + * Save and replace entities + * + * @return void + */ + private function saveAndReplaceEntity() + { + $behavior = $this->getBehavior(); + $rows = []; + while ($bunch = $this->_dataSourceModel->getNextBunch()) { + $entityList = []; + + foreach ($bunch as $rowNum => $row) { + if (!$this->validateRow($row, $rowNum)) { + continue; + } + + if ($this->getErrorAggregator()->hasToBeTerminated()) { + $this->getErrorAggregator()->addRowToSkip($rowNum); + + continue; + } + + $rowId = $row[static::ENTITY_ID_COLUMN]; + $rows[] = $rowId; + $columnValues = []; + + foreach ($this->getAvailableColumns() as $columnKey) { + $columnValues[$columnKey] = $row[$columnKey]; + } + + $entityList[$rowId][] = $columnValues; + $this->countItemsCreated += (int) !isset($row[static::ENTITY_ID_COLUMN]); + $this->countItemsUpdated += (int) isset($row[static::ENTITY_ID_COLUMN]); + } + + if (Import::BEHAVIOR_REPLACE === $behavior) { + if ($rows && $this->deleteEntityFinish(array_unique($rows))) { + $this->saveEntityFinish($entityList); + } + } elseif (Import::BEHAVIOR_APPEND === $behavior) { + $this->saveEntityFinish($entityList); + } + } + } + + /** + * Save entities + * + * @param array $entityData + * + * @return bool + */ + private function saveEntityFinish(array $entityData): bool + { + if ($entityData) { + $tableName = $this->connection->getTableName(static::TABLE); + $rows = []; + + foreach ($entityData as $entityRows) { + foreach ($entityRows as $row) { + $rows[] = $row; + } + } + + if ($rows) { + $this->connection->insertOnDuplicate($tableName, $rows, $this->getAvailableColumns()); + + return true; + } + + return false; + } + } + + /** + * Delete entities + * + * @param array $entityIds + * + * @return bool + */ + private function deleteEntityFinish(array $entityIds): bool + { + if ($entityIds) { + try { + $this->countItemsDeleted += $this->connection->delete( + $this->connection->getTableName(static::TABLE), + $this->connection->quoteInto(static::ENTITY_ID_COLUMN . ' IN (?)', $entityIds) + ); + + return true; + } catch (Exception $e) { + return false; + } + } + + return false; + } + + /** + * Get available columns + * + * @return array + */ + private function getAvailableColumns(): array + { + return $this->validColumnNames; + } +} diff --git a/Model/PageNotFoundRepository.php b/Model/PageNotFoundRepository.php index 21c67d8..2291be9 100644 --- a/Model/PageNotFoundRepository.php +++ b/Model/PageNotFoundRepository.php @@ -1,24 +1,22 @@ addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]); } } - + $sortOrders = $criteria->getSortOrders(); if ($sortOrders) { /** @var SortOrder $sortOrder */ @@ -146,7 +143,7 @@ public function getList( } $collection->setCurPage($criteria->getCurrentPage()); $collection->setPageSize($criteria->getPageSize()); - + $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($criteria); $searchResults->setTotalCount($collection->getSize()); diff --git a/etc/import.xml b/etc/import.xml new file mode 100644 index 0000000..62cad1b --- /dev/null +++ b/etc/import.xml @@ -0,0 +1,5 @@ + + + diff --git a/etc/module.xml b/etc/module.xml index e303d15..4540f70 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,8 @@ + + + +