diff --git a/src/classes/importer/class-tainacan-bg-importer.php b/src/classes/importer/class-tainacan-bg-importer.php index e8789128a..899bc2e60 100644 --- a/src/classes/importer/class-tainacan-bg-importer.php +++ b/src/classes/importer/class-tainacan-bg-importer.php @@ -31,7 +31,7 @@ class Background_Importer extends Background_Process { $data = $batch->data; $key = $batch->key; - define('TAINACAN_DOING_IMPORT', true); + if (!defined('TAINACAN_DOING_IMPORT')) define('TAINACAN_DOING_IMPORT', true); $className = $data['class_name']; if (class_exists($className)) { diff --git a/src/classes/importer/class-tainacan-csv.php b/src/classes/importer/class-tainacan-csv.php index a9ff00104..19a5ac767 100644 --- a/src/classes/importer/class-tainacan-csv.php +++ b/src/classes/importer/class-tainacan-csv.php @@ -51,7 +51,13 @@ class CSV extends Importer { $this->set_option('item_comment_status_index', $index); } } else { - $columns[] = $rawColumn; + if ( preg_match ('/.*\|compound\(.*\)/', $rawColumn ) ) { + $data = preg_split("/[()]+/", $rawColumn, -1, PREG_SPLIT_NO_EMPTY); + $parent = $data[0] . ( isset($data[2]) ? $data[2] : '' ); + $columns[] = [$parent => explode($this->get_option('delimiter'), $data[1])]; + } else { + $columns[] = $rawColumn; + } } } return $columns; @@ -105,7 +111,16 @@ class CSV extends Importer { */ public function process_item( $index, $collection_definition ) { $processedItem = []; - $headers = $this->raw_source_metadata(); + $compoundHeaders = []; + $headers = array_map(function ($header) use (&$compoundHeaders) { + if ( preg_match ('/.*\|compound\(.*\)/', $header ) ) { + $data = preg_split("/[()]+/", $header, -1, PREG_SPLIT_NO_EMPTY); + $header = $data[0] . ( isset($data[2]) ? $data[2] : '' ); + $compoundHeaders[$header] = $data[1]; + return $header; + } + return $header; + }, $this->raw_source_metadata()); $item_line = (int) $index + 2; @@ -157,9 +172,9 @@ class CSV extends Importer { foreach ( $collection_definition['mapping'] as $metadatum_id => $header) { $column = null; foreach ( $headers as $indexRaw => $headerRaw ) { - if( $headerRaw === $header ) { + if( (is_array($header) && $headerRaw === key($header)) || ($headerRaw === $header) ) { $column = $indexRaw; - } + } } if(is_null($column)) @@ -168,10 +183,33 @@ class CSV extends Importer { $valueToInsert = $this->handle_encoding( $values[ $column ] ); $metadatum = new \Tainacan\Entities\Metadatum($metadatum_id); - $processedItem[ $header ] = ( $metadatum->is_multiple() ) ? - explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert; - } + if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Compound' ) { + $valueToInsert = $metadatum->is_multiple() + ? explode( $this->get_option('multivalued_delimiter'), $valueToInsert) + : [$valueToInsert]; + $key = key($header); + $returnValue = []; + foreach($valueToInsert as $index => $metadatumValue) { + $childrenHeaders = str_getcsv($compoundHeaders[$key], $this->get_option('delimiter'), $this->get_option('enclosure')); + $childrenValue = str_getcsv($metadatumValue, $this->get_option('delimiter'), $this->get_option('enclosure')); + + if ( sizeof($childrenHeaders) != sizeof($childrenValue) ) { + $this->add_error_log(' Mismatch count headers childrens and row columns '); + return false; + } + $tmp = []; + foreach($childrenValue as $i => $value ) { + $tmp[ $childrenHeaders[$i] ] = $value; + } + $returnValue[] = $tmp; + } + $processedItem[ $key ] = $returnValue; + } else { + $processedItem[ $header ] = ( $metadatum->is_multiple() ) ? + explode( $this->get_option('multivalued_delimiter'), $valueToInsert) : $valueToInsert; + } + } if( !empty( $this->get_option('document_index') ) ) $processedItem['special_document'] = ''; if( !empty( $this->get_option('attachment_index') ) ) $processedItem['special_attachments'] = ''; if( !empty( $this->get_option('item_status_index') ) ) $processedItem['special_item_status'] = ''; @@ -245,154 +283,167 @@ class CSV extends Importer { public function options_form() { ob_start(); ?> -