fix error on create a taxonomy on import.

This commit is contained in:
vnmedeiros 2018-11-01 11:21:18 -03:00
parent 49f60fb431
commit 765d559b64
1 changed files with 20 additions and 14 deletions

View File

@ -567,18 +567,14 @@ class CSV extends Importer {
if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Taxonomy' ) { if( $metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Taxonomy' ) {
if( !is_array( $values ) ) { if( !is_array( $values ) ) {
$tmp = $this->insert_hierarchy( $metadatum, $values); $tmp = $this->insert_hierarchy( $metadatum, $values);
if ($tmp == false) { if ($tmp !== false) {
$this->add_error_log('Metadata ' . $metadatum_source . ' has a term empty');
} else {
$singleItemMetadata->set_value( $tmp ); $singleItemMetadata->set_value( $tmp );
} }
} else { } else {
$terms = []; $terms = [];
foreach($values as $k => $v) { foreach($values as $k => $v) {
$tmp = $this->insert_hierarchy( $metadatum, $v); $tmp = $this->insert_hierarchy( $metadatum, $v);
if ($tmp == false) { if ($tmp !== false) {
$this->add_error_log('Metadata ' . $metadatum_source . ' has a term empty');
} else {
$terms[] = $tmp; $terms[] = $tmp;
} }
} }
@ -645,21 +641,31 @@ class CSV extends Importer {
*/ */
private function insert_hierarchy( $metadatum, $values ){ private function insert_hierarchy( $metadatum, $values ){
if (empty($values)) {
return false;
}
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance(); $Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
$taxonomy = new Entities\Taxonomy( $metadatum->get_metadata_type_options()['taxonomy_id']); $taxonomy = new Entities\Taxonomy( $metadatum->get_metadata_type_options()['taxonomy_id']);
$exploded_values = explode(">>",$values); $exploded_values = explode(">>",$values);
if (empty($exploded_values)) {
return false;
}
if( is_array($exploded_values) ) { if( is_array($exploded_values) ) {
$parent = 0; $parent = 0;
foreach ( $exploded_values as $key => $value) { foreach ( $exploded_values as $key => $value) {
$value = trim($value); $value = trim($value);
if ($value=='') { if ($value=='') {
$this->add_error_log('Malformed term hierarchy for Item ' . $this->get_current_collection_item() . '. Term skipped. Value: ' . $values);
return false; return false;
} }
$exists = term_exists( $value ,$taxonomy->get_db_identifier(), $parent ); $exists = term_exists( $value ,$taxonomy->get_db_identifier(), $parent );
if (0 !== $exists && null !== $exists && isset($exists['term_id'])) { if (0 !== $exists && null !== $exists && isset($exists['term_id'])) {
$parent = $value; $parent = $exists['term_id'];
} else { } else {
$this->add_log('New term created: ' . $value . ' in tax_id: ' . $taxonomy->get_db_identifier() . '; parent: ' . $parent);
$term = new Entities\Term(); $term = new Entities\Term();
$term->set_name( $value ); $term->set_name( $value );
$term->set_parent( $parent ); $term->set_parent( $parent );
@ -668,7 +674,7 @@ class CSV extends Importer {
$parent = $term->get_id(); $parent = $term->get_id();
} }
} }
return $parent !== 0 ? $parent : false; return $parent !== 0 ? (int)$parent : false;
} else { } else {
return false; return false;
} }