import csv with terms

This commit is contained in:
vnmedeiros 2018-09-23 21:17:43 -03:00
parent 747e2d11dc
commit d306783e19
2 changed files with 68 additions and 17 deletions

View File

@ -44,9 +44,9 @@ class Importer_Handler {
]); ]);
$this->register_importer([ $this->register_importer([
'name' => 'Vocabularies', 'name' => 'Terms',
'description' => __('Importa a terms - vocabularies', 'tainacan'), 'description' => __('Import a terms', 'tainacan'),
'slug' => 'term_vocabularies', 'slug' => 'terms',
'class_name' => '\Tainacan\Importer\Term_Importer', 'class_name' => '\Tainacan\Importer\Term_Importer',
'manual_collection' => false, 'manual_collection' => false,
'manual_mapping' => false, 'manual_mapping' => false,

View File

@ -55,7 +55,7 @@ class Term_Importer extends Importer {
</a> </a>
<div class="help-tooltip"> <div class="help-tooltip">
<div class="help-tooltip-header"> <div class="help-tooltip-header">
<h5><?php _e('Taxonomies', 'tainacan'); ?></h5> <h5><?php _e('taxonomy', 'tainacan'); ?></h5>
</div> </div>
<div class="help-tooltip-body"> <div class="help-tooltip-body">
<p><?php _e('The taxonomies where imported term will be added.', 'tainacan'); ?></p> <p><?php _e('The taxonomies where imported term will be added.', 'tainacan'); ?></p>
@ -64,19 +64,24 @@ class Term_Importer extends Importer {
</span> </span>
<div class="control is-clearfix"> <div class="control is-clearfix">
<select class="input" type="text" name="taxonomies"> <select class="input" type="text" name="taxonomies">
<option>tax-1</option>
<option>tax-2</option>
<option>tax-3</option>
</select>
<?php <?php
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$taxonomies = $Tainacan_Taxonomies->fetch( [
'status' => [
'auto-draft',
'draft',
'publish',
'private'
]
], 'OBJECT' );
$request = new \WP_REST_Request( foreach( $taxonomies as $taxonomie) {
'GET', $this->namespace . '/taxonomies'
);
$response = $this->server->dispatch($request);
echo $response;
?> ?>
<option value="<?php echo $taxonomie->get_db_identifier();?>"><?php echo $taxonomie->get_name() ?> </option>
<?php
}
?>
</select>
</div> </div>
</div> </div>
@ -85,6 +90,52 @@ class Term_Importer extends Importer {
} }
public function process_item($index, $collection_definition) { public function process_item($index, $collection_definition) {
$this->add_log('Proccessing item index ' . $index . ' in collection ' . $collection_definition['id'] );
if (($handle = fopen($this->tmp_file, "r")) !== false) {
$file = $handle;
} else {
$this->add_error_log(' Error reading the file ');
return false;
}
$term_repo = \Tainacan\Repositories\Terms::get_instance();
$parent = array();
$position = 0;
$last_term = 0;
$auxId = 1;
$taxonomy_id = $this->get_option('taxonomies');
while (($values = fgetcsv($file, 0, $this->get_option('delimiter'), '"')) !== FALSE) {
if ($values[$position] == '') { // next degree
$position++;
array_push($parent, $last_term);
}
if ($position > 0 && $values[$position-1] != '') { // back degree
$position--;
array_pop($parent);
}
$term = new \Tainacan\Entities\Term();
$term->set_name($values[$position]);
$term->set_description($values[$position+1]);
$term->set_taxonomy($taxonomy_id);
if(end($parent))
$term->set_parent(end($parent));
if ($term->validate()) {
$term_insert = $term_repo->insert($term);
$last_term = $term_insert->get_id();
$this->add_log('Added term: id=' . $last_term . ' name=' . $term->get_name() . ' id parent=' . $term->get_parent());
} else {
$validationErrors = $term->get_errors();
$err_msg = "";
foreach($validationErrors as $err) {
$err_msg .= $err;
}
$this->add_error_log("err! = ". $err_msg);
return false;
}
}
return true;
} }
} }