Import
in package
Import Class Class which handles the main import operations
Do not use this. Use ViewGenerator instead.
Tags
Table of Contents
Properties
- $fieldNames : array<string|int, mixed>
- $file : File|null
- $pairs : array<string|int, mixed>
- $parser : Csv|null
Methods
- cancel() : bool
- Cancels the import operation
- getFinalData() : array<string|int, mixed>
- getFinalData
- getUploadedFileFromUploader() : bool|string
- Get uploaded csv file by using uploader id
- importToEntity() : void
- Imports an entity based on the given mapping and data
- initFieldSelectTemplate() : void
- Sets the template for the field selection
- initFileSelectTemplate() : void
- Sets the template for the file selection
- uploadFinished() : array<string|int, mixed>
- Uploader callback function
- getDataFields() : array<string|int, mixed>
- Gets the fieldnames of the importing file
- parseMapping() : array<string|int, mixed>
- Parses data based on the mapping of fields from the file to import a single entity
- setFieldPairs() : void
- Sets the field Pairs
- setFile() : void
- Parses the file
Properties
$fieldNames
protected
array<string|int, mixed>
$fieldNames
= array()
$file
protected
File|null
$file
= null
$pairs
protected
array<string|int, mixed>
$pairs
= array()
$parser
protected
Csv|null
$parser
= null
Methods
cancel()
Cancels the import operation
public
cancel() : bool
Return values
boolgetFinalData()
getFinalData
public
getFinalData(array<string|int, mixed> $fields) : array<string|int, mixed>
This function returns the associated fields and values.
Parameters
- $fields : array<string|int, mixed>
-
Name of the fields
Return values
array<string|int, mixed>getUploadedFileFromUploader()
Get uploaded csv file by using uploader id
public
getUploadedFileFromUploader(string $uploaderId) : bool|string
Parameters
- $uploaderId : string
-
Uploader id
Return values
bool|string —File path when file exists, false otherwise
importToEntity()
Imports an entity based on the given mapping and data
public
importToEntity(string $entityClassName, array<string|int, mixed> $mapping, array<string|int, mixed> $importData) : void
This should not be public!
Parameters
- $entityClassName : string
-
Name of the entity class to create
- $mapping : array<string|int, mixed>
-
Mapping as specified by
parseMapping() - $importData : array<string|int, mixed>
-
Key/value list of fields with their values as present in the import file
Tags
initFieldSelectTemplate()
Sets the template for the field selection
public
initFieldSelectTemplate(object &$tpl, mixed $given_fields) : void
Parses the given file and sets the template and values for the field selection.
Parameters
- $tpl : object
-
The template object (by reference)
- $given_fields : mixed
initFileSelectTemplate()
Sets the template for the file selection
public
initFileSelectTemplate(object &$tpl) : void
Sets the template and all neede variables for the file selection.
Parameters
- $tpl : object
-
The template object (by reference)
uploadFinished()
Uploader callback function
public
static uploadFinished(string $tempPath, string $tempWebPath, array<string|int, mixed> $data, string $uploaderId, array<string|int, mixed> $fileInfos, object $response) : array<string|int, mixed>
Parameters
- $tempPath : string
-
Temp path
- $tempWebPath : string
-
Temp web path
- $data : array<string|int, mixed>
-
Uploader data
- $uploaderId : string
-
Uploader id
- $fileInfos : array<string|int, mixed>
-
Info about the file
- $response : object
-
\Cx\Core_Modules\Uploader\Controller\UploadResponse
Return values
array<string|int, mixed> —temp path and temp web path
getDataFields()
Gets the fieldnames of the importing file
protected
getDataFields() : array<string|int, mixed>
Return values
array<string|int, mixed> —$fields
parseMapping()
Parses data based on the mapping of fields from the file to import a single entity
protected
parseMapping(array<string|int, mixed> $mapping, array<string|int, mixed> $importData) : array<string|int, mixed>
$mapping is a list of entity properties. For each property you can specify how the value for it is generated based on the import data. The following options are available for the mapping:
- 'fields': A list of fields that need to be present in the import file. To make fields optional, set a default value for them, see 'defaultValue'.
- 'defaultValue': An optional list of fields specified in 'fields' as key with the respective default value as value.
- 'format': A string with '<...>'-style placeholders for the fields specified in 'fields'. This can be used to combine multiple fields to one.
- 'entity': Foreign entity for this field. See 'type' "entity".
- 'type': If set a type conversion will be applied. Known types are:
"int": Result will be converted to
int"DateTime": Result will be converted to\DateTime"entity": Result will be used as an argument to$em->find()on the repository for the entity specified in 'entity'. - 'callback': Optional callback to do custom stuff. The callback is applied last, just before the type conversion and will be given the result of previous conversions as first param and all data fields to import as second param.
Either 'fields' or 'format' must be specified. If more than one field is specified, 'format' is mandatory.
$mapping = array(
<entityProperty> => array(
'fields' => array(...),
'format' => '',
'type' => '',
'entity' => '',
'defaultValue' => array(...),
'callback' => function(string $formattedString, array $fields) {
return $formattedString;
}
),
...
)
Parameters
- $mapping : array<string|int, mixed>
-
Mapping according to the description above
- $importData : array<string|int, mixed>
-
Data to import
Return values
array<string|int, mixed> —Parsed data to import
setFieldPairs()
Sets the field Pairs
protected
setFieldPairs(array<string|int, mixed> $left_fields, array<string|int, mixed> $right_fields) : void
Parameters
- $left_fields : array<string|int, mixed>
- $right_fields : array<string|int, mixed>
setFile()
Parses the file
protected
setFile(string $file) : void
Parameters
- $file : string
-
Path to the csv file