Commit 28a8edde authored by Chhengleap Soem's avatar Chhengleap Soem
Browse files

Merge branch 'KIME-4583' into 'master'

Property mapping



See merge request !2
parents fed606d3 4dab0562
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -20,16 +20,29 @@ namespace WE\SpreadsheetImport\Annotations;
final class Mapping {

	/**
	 * Label id for the property mapping
	 *
	 * @var string
	 */
	public $labelId = '';

	/**
	 * Flag if property is handled as an identifier for updates
	 *
	 * @var boolean
	 */
	public $identifier = FALSE;

	/**
	 * Overwrite the default getter for previews
	 *
	 * @var string
	 */
	public $getter;

	/**
	 * Overwrite the default setter
	 *
	 * @var string
	 */
	public $setter;
+55 −0
Original line number Diff line number Diff line
<?php
namespace WE\SpreadsheetImport;

/*                                                                        *
 * This script belongs to the Flow package "SpreadsheetImport".           *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

use TYPO3\Flow\Mvc\ActionRequest;
use WE\SpreadsheetImport\Annotations\Mapping;

/**
 * Utility class of basic FE mapping functionality for simple usage on separate implementations.
 */
class FrontendMappingUtility {

	/**
	 * @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService
	 * @param \TYPO3\Flow\Mvc\ActionRequest $request
	 *
	 * @return array
	 */
	public static function getSpreadsheetImportMappingByRequest(SpreadsheetImportService $spreadsheetImportService, ActionRequest $request) {
		$mappings = array();
		$domainMappingProperties = $spreadsheetImportService->getMappingProperties();
		foreach ($domainMappingProperties as $property => $mapping) {
			$column = $request->getArgument($property);
			$mappings[$property] = $column;
		}
		return $mappings;
	}

	/**
	 * @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService
	 * @param int $record
	 *
	 * @return array
	 */
	public static function getMappingPreview(SpreadsheetImportService $spreadsheetImportService, $record) {
		$domainMappingProperties = $spreadsheetImportService->getMappingProperties();
		$previewObject = $spreadsheetImportService->getObjectByRow($record);
		$preview = array();
		/** @var Mapping $mapping */
		foreach ($domainMappingProperties as $property => $mapping) {
			$getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter;
			$preview[$property] = $previewObject->$getter();
		}
		return $preview;
	}
}
+55 −17
Original line number Diff line number Diff line
@@ -30,6 +30,16 @@ class SpreadsheetImportService {
	 */
	protected $context;

	/**
	 * @var array
	 */
	protected $mappingProperties;

	/**
	 * @var array
	 */
	protected $columnPropertyMapping;

	/**
	 * @Flow\InjectConfiguration
	 * @var array
@@ -68,25 +78,54 @@ class SpreadsheetImportService {
	public function init(SpreadsheetImport $spreadsheetImport) {
		$this->spreadsheetImport = $spreadsheetImport;
		$this->context = $this->settings[$spreadsheetImport->getContext()];
		$this->initDomainMappingProperties();
		$this->initColumnPropertyMapping();

		return $this;
	}

	/**
	 * @return array
	 * Initializes the properties declared by annotations.
	 */
	public function getDomainMappingProperties() {
		$domainMappingProperties = array();
	private function initDomainMappingProperties() {
		$this->mappingProperties = array();
		$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
		foreach ($properties as $property) {
			$domainMappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class);
			$this->mappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class);
		}
		return $domainMappingProperties;
	}

	/**
	 * Flip mapping and return it as a 2-dim array in case the same column is assigned to multiple properties
	 */
	private function initColumnPropertyMapping() {
		$this->columnPropertyMapping = array();
		foreach ($this->spreadsheetImport->getMapping() as $property => $column) {
			$this->columnPropertyMapping[$column][] = $property;
		}
	}

	/**
	 * Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases
	 * flexibility for dynamic property mapping.
	 *
	 * @param array $additionalMappingProperties
	 */
	public function addAdditionalMappingProperties(array $additionalMappingProperties) {
		$this->mappingProperties = array_merge($this->mappingProperties, $additionalMappingProperties);
	}

	/**
	 * @return array
	 */
	public function getDomainMappingIdentifierProperties() {
	public function getMappingProperties() {
		return $this->mappingProperties;
	}

	/**
	 * @return array
	 */
	private function getDomainMappingIdentifierProperties() {
		$domainMappingProperties = array();
		$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
		foreach ($properties as $property) {
@@ -141,7 +180,6 @@ class SpreadsheetImportService {
	 * @return void
	 */
	public function import() {
		// TODO: This simply creates the objects for now without update or delete
		$totalInserted = 0;
		$totalUpdated = 0;
		$totalDeleted = 0;
@@ -210,7 +248,7 @@ class SpreadsheetImportService {
	private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) {
		$query = $this->getDomainRepository()->createQuery();
		$constraints = array();
		$spreadsheetImportMapping = array_flip($this->spreadsheetImport->getMapping());
		$spreadsheetImportMapping = $this->spreadsheetImport->getMapping();
		/** @var Mapping $mapping */
		foreach ($identifierProperties as $property => $mapping) {
			$column = $spreadsheetImportMapping[$property];
@@ -243,14 +281,13 @@ class SpreadsheetImportService {
	 * @param \PHPExcel_Worksheet_Row $row
	 */
	private function setObjectPropertiesByRow($newObject, $row) {
		// TODO: Cache $domainMappingProperties and $mappings
		$domainMappingProperties = $this->getDomainMappingProperties();
		$mappings = $this->spreadsheetImport->getMapping();
		$domainMappingProperties = $this->mappingProperties;
		/** @var \PHPExcel_Cell $cell */
		foreach ($row->getCellIterator() as $cell) {
			$column = $cell->getColumn();
			if (array_key_exists($column, $mappings)) {
				$property = $mappings[$column];
			if (array_key_exists($column, $this->columnPropertyMapping)) {
				$properties = $this->columnPropertyMapping[$column];
				foreach ($properties as $property) {
					/** @var Mapping $mapping */
					$mapping = $domainMappingProperties[$property];
					$setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter;
@@ -259,3 +296,4 @@ class SpreadsheetImportService {
			}
		}
	}
}
+0 −1
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ WE:
  SpreadsheetImport:
    testing:
      domain: WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget
      labelPackageKey: 'WE.SpreadsheetImport'
      Configuration:
        userProfileType:
          setter: convertUserProfileTypeBySysValue
+2 −3
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ namespace WE\SpreadsheetImport\Tests\Functional;
 *                                                                        */

use TYPO3\Flow\Reflection\ReflectionService;
use TYPO3\Flow\Resource\Resource;
use TYPO3\Flow\Resource\ResourceManager;
use TYPO3\Flow\Tests\FunctionalTestCase;
use WE\SpreadsheetImport\Annotations\Mapping;
@@ -63,7 +62,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
		$spreadsheetImport->setContext('testing');
		$resource = $this->resourceManager->importResource(__DIR__ . '/Fixtures/Resources/sample.xlsx');
		$spreadsheetImport->setFile($resource);
		$spreadsheetImport->setMapping(array('C' => 'id', 'A' => 'name'));
		$spreadsheetImport->setMapping(array('id' => 'C', 'name' => 'A'));
		$this->spreadsheetImportService->init($spreadsheetImport);
	}

@@ -71,7 +70,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
	 * @test
	 */
	public function getMappingPropertiesReturnsPropertiesWithMappingAnnotation() {
		$properties = $this->spreadsheetImportService->getDomainMappingProperties();
		$properties = $this->spreadsheetImportService->getMappingProperties();
		$this->assertArrayHasKey('id', $properties);
		$this->assertArrayHasKey('name', $properties);
		/** @var Mapping $id */