Skip to content

Commit

Permalink
Added configuration option to silently ignore not mapped fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Aug 24, 2015
1 parent 60b1783 commit 33d7f0d
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions controller/jobs/src/Controller/Jobs/Product/Import/Csv/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function run()
* @see controller/jobs/product/import/csv/mapping
* @see controller/jobs/product/import/csv/skip-lines
* @see controller/jobs/product/import/csv/converter
* @see controller/jobs/product/import/csv/strict
* @see controller/jobs/product/import/csv/backup
* @see controller/common/product/import/csv/max-size
*/
Expand Down Expand Up @@ -134,6 +135,7 @@ public function run()
* @see controller/jobs/product/import/csv/domains
* @see controller/jobs/product/import/csv/skip-lines
* @see controller/jobs/product/import/csv/converter
* @see controller/jobs/product/import/csv/strict
* @see controller/jobs/product/import/csv/backup
* @see controller/common/product/import/csv/max-size
*/
Expand Down Expand Up @@ -195,6 +197,7 @@ public function run()
* @see controller/jobs/product/import/csv/domains
* @see controller/jobs/product/import/csv/mapping
* @see controller/jobs/product/import/csv/skip-lines
* @see controller/jobs/product/import/csv/strict
* @see controller/jobs/product/import/csv/backup
* @see controller/common/product/import/csv/max-size
*/
Expand Down Expand Up @@ -236,12 +239,37 @@ public function run()
* @see controller/jobs/product/import/csv/domains
* @see controller/jobs/product/import/csv/mapping
* @see controller/jobs/product/import/csv/converter
* @see controller/jobs/product/import/csv/strict
* @see controller/jobs/product/import/csv/backup
* @see controller/common/product/import/csv/max-size
*/
$skiplines = (int) $config->get( 'controller/jobs/product/import/csv/skip-lines', 0 );


/** controller/jobs/product/import/csv/skip-lines
* Log all columns from the file that are not mapped and therefore not imported
*
* Depending on the mapping, there can be more columns in the CSV file
* than those which will be imported. This can be by purpose if you want
* to import only selected columns or if you've missed to configure one
* or more columns. This configuration option will log all columns that
* have not been imported if set to true. Otherwise, the left over fields
* in the imported line will be silently ignored.
*
* @param boolen True if not imported columns should be logged, false if not
* @since 2015.08
* @category User
* @category Developer
* @see controller/jobs/product/import/csv/domains
* @see controller/jobs/product/import/csv/mapping
* @see controller/jobs/product/import/csv/skip-lines
* @see controller/jobs/product/import/csv/converter
* @see controller/jobs/product/import/csv/backup
* @see controller/common/product/import/csv/max-size
*/
$strict = (bool) $config->get( 'controller/jobs/product/import/csv/strict', true );


/** controller/jobs/product/import/csv/backup
* Name of the backup for sucessfully imported files
*
Expand Down Expand Up @@ -269,6 +297,7 @@ public function run()
* @see controller/jobs/product/import/csv/mapping
* @see controller/jobs/product/import/csv/skip-lines
* @see controller/jobs/product/import/csv/converter
* @see controller/jobs/product/import/csv/strict
* @see controller/common/product/import/csv/max-size
*/
$backup = $config->get( 'controller/jobs/product/import/csv/backup' );
Expand Down Expand Up @@ -305,7 +334,7 @@ public function run()
{
$data = $this->_convertData( $convlist, $data );
$products = $this->_getProducts( array_keys( $data ), $domains );
$errcnt = $this->_import( $products, $data, $mappings['item'], $processor );
$errcnt = $this->_import( $products, $data, $mappings['item'], $processor, $strict );
$chunkcnt = count( $data );

$msg = 'Imported product lines from "%1$s": %2$d/%3$d (%4$s)';
Expand Down Expand Up @@ -448,11 +477,12 @@ protected function _getContainer()
* @param array $data Associative list of import data as index/value pairs
* @param array $mappings Associative list of positions and domain item keys
* @param Controller_Common_Product_Import_Csv_Processor_Interface $processor Processor object
* @param boolean $strict Log columns not mapped or silently ignore them
* @return integer Number of products that couldn't be imported
* @throws Controller_Jobs_Exception
*/
protected function _import( array $products, array $data, array $mapping,
Controller_Common_Product_Import_Csv_Processor_Interface $processor )
Controller_Common_Product_Import_Csv_Processor_Interface $processor, $strict )
{
$errors = 0;
$context = $this->_getContext();
Expand Down Expand Up @@ -494,7 +524,7 @@ protected function _import( array $products, array $data, array $mapping,
$errors++;
}

if( !empty( $remaining ) ) {
if( $strict && !empty( $remaining ) ) {
$context->getLogger()->log( 'Not imported: ' . print_r( $remaining, true ) );
}
}
Expand Down

0 comments on commit 33d7f0d

Please sign in to comment.