Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
WE.SpreadsheetImport
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Labels
Merge Requests
0
Merge Requests
0
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
packages
WE.SpreadsheetImport
Commits
28a8edde
Commit
28a8edde
authored
Oct 26, 2016
by
Chhengleap Soem
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'KIME-4583' into 'master'
Property mapping See merge request
!2
parents
fed606d3
4dab0562
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
125 additions
and
21 deletions
+125
-21
Classes/WE/SpreadsheetImport/Annotations/Mapping.php
Classes/WE/SpreadsheetImport/Annotations/Mapping.php
+13
-0
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
+55
-0
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
+55
-17
Configuration/Testing/Settings.yaml
Configuration/Testing/Settings.yaml
+0
-1
Tests/Functional/SpreadsheetImportServiceTest.php
Tests/Functional/SpreadsheetImportServiceTest.php
+2
-3
No files found.
Classes/WE/SpreadsheetImport/Annotations/Mapping.php
View file @
28a8edde
...
...
@@ -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
;
...
...
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
0 → 100644
View file @
28a8edde
<?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
;
}
}
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
View file @
28a8edde
...
...
@@ -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.
*/
p
ublic
function
ge
tDomainMappingProperties
()
{
$
domainM
appingProperties
=
array
();
p
rivate
function
ini
tDomainMappingProperties
()
{
$
this
->
m
appingProperties
=
array
();
$properties
=
$this
->
reflectionService
->
getPropertyNamesByAnnotation
(
$this
->
context
[
'domain'
],
Mapping
::
class
);
foreach
(
$properties
as
$property
)
{
$
domainM
appingProperties
[
$property
]
=
$this
->
reflectionService
->
getPropertyAnnotation
(
$this
->
context
[
'domain'
],
$property
,
Mapping
::
class
);
$
this
->
m
appingProperties
[
$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,18 +281,18 @@ 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
];
/** @var Mapping $mapping */
$mapping
=
$domainMappingProperties
[
$property
];
$setter
=
empty
(
$mapping
->
setter
)
?
'set'
.
ucfirst
(
$property
)
:
$mapping
->
setter
;
$newObject
->
$setter
(
$cell
->
getValue
());
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
;
$newObject
->
$setter
(
$cell
->
getValue
());
}
}
}
}
...
...
Configuration/Testing/Settings.yaml
View file @
28a8edde
...
...
@@ -2,7 +2,6 @@ WE:
SpreadsheetImport
:
testing
:
domain
:
WE\SpreadsheetImport\Tests\Functional\Fixtures\ImportTarget
labelPackageKey
:
'
WE.SpreadsheetImport'
Configuration
:
userProfileType
:
setter
:
convertUserProfileTypeBySysValue
...
...
Tests/Functional/SpreadsheetImportServiceTest.php
View file @
28a8edde
...
...
@@ -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
->
get
Domain
MappingProperties
();
$properties
=
$this
->
spreadsheetImportService
->
getMappingProperties
();
$this
->
assertArrayHasKey
(
'id'
,
$properties
);
$this
->
assertArrayHasKey
(
'name'
,
$properties
);
/** @var Mapping $id */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment