Commit ce2edd20 authored by Chhengleap Soem's avatar Chhengleap Soem

Merge branch 'KIME-4583' into 'master'

[IMP] Preview error handling

refs KIME-4583

See merge request !13
parents 79dc5c5a e0b6541b
...@@ -14,6 +14,7 @@ namespace WE\SpreadsheetImport; ...@@ -14,6 +14,7 @@ namespace WE\SpreadsheetImport;
use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Mvc\ActionRequest; use TYPO3\Flow\Mvc\ActionRequest;
use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Annotations\Mapping;
use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport;
/** /**
* Service class of basic FE mapping functionality for simple usage on separate implementations. * Service class of basic FE mapping functionality for simple usage on separate implementations.
...@@ -46,6 +47,12 @@ class FrontendMappingService { ...@@ -46,6 +47,12 @@ class FrontendMappingService {
*/ */
protected $propertyMapper; protected $propertyMapper;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Validation\ValidatorResolver
*/
protected $validatorResolver;
/** /**
* @param string $context * @param string $context
* @param \TYPO3\Flow\Mvc\ActionRequest $request * @param \TYPO3\Flow\Mvc\ActionRequest $request
...@@ -93,21 +100,30 @@ class FrontendMappingService { ...@@ -93,21 +100,30 @@ class FrontendMappingService {
} }
/** /**
* @param array $mapping * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
* @param int $record * @param $record
* *
* @return array * @return array
*/ */
public function getMappingPreview($mapping, $record) { public function getMappingPreview(SpreadsheetImport $spreadsheetImport, $record) {
$mapping = $spreadsheetImport->getMapping();
$domain = $this->settings[$spreadsheetImport->getContext()]['domain'];
$record = max($record, 1); $record = max($record, 1);
$previewObject = $this->spreadsheetImportService->getObjectByRow($record); $previewObject = $this->spreadsheetImportService->getObjectByRow($record);
$preview = array(); $preview = array();
$hasErrors = FALSE;
$objectValidator = $this->validatorResolver->getBaseValidatorConjunction($domain);
$errors = $objectValidator->validate($previewObject)->getFlattenedErrors();
foreach ($mapping as $property => $columnMapping) { foreach ($mapping as $property => $columnMapping) {
/** @var Mapping $mapping */ /** @var Mapping $mapping */
$mapping = $columnMapping['mapping']; $mapping = $columnMapping['mapping'];
$getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter; $getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter;
$preview[$property] = array('value' => $previewObject->$getter(), 'mapping' => $mapping); $preview[$property] = array('value' => $previewObject->$getter(), 'mapping' => $mapping);
if (isset($errors[$property])) {
$preview[$property]['error'] = $errors[$property];
$hasErrors = TRUE;
}
} }
return $preview; return array('preview' => $preview, 'hasErrors' => $hasErrors);
} }
} }
...@@ -340,6 +340,8 @@ class SpreadsheetImportService { ...@@ -340,6 +340,8 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($object, $row) { private function setObjectPropertiesByRow($object, $row) {
// Set the arguments first as mapping property setters might be dependent on argument properties
$this->setObjectArgumentProperties($object);
$inverseSpreadsheetImportMapping = $this->getInverseSpreadsheetImportMapping(); $inverseSpreadsheetImportMapping = $this->getInverseSpreadsheetImportMapping();
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
...@@ -355,7 +357,6 @@ class SpreadsheetImportService { ...@@ -355,7 +357,6 @@ class SpreadsheetImportService {
} }
} }
} }
$this->setObjectArgumentProperties($object);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment