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([
'name' => 'Vocabularies',
'description' => __('Importa a terms - vocabularies', 'tainacan'),
'slug' => 'term_vocabularies',
'name' => 'Terms',
'description' => __('Import a terms', 'tainacan'),
'slug' => 'terms',
'class_name' => '\Tainacan\Importer\Term_Importer',
'manual_collection' => false,
'manual_mapping' => false,

View File

@ -55,7 +55,7 @@ class Term_Importer extends Importer {
</a>
<div class="help-tooltip">
<div class="help-tooltip-header">
<h5><?php _e('Taxonomies', 'tainacan'); ?></h5>
<h5><?php _e('taxonomy', 'tainacan'); ?></h5>
</div>
<div class="help-tooltip-body">
<p><?php _e('The taxonomies where imported term will be added.', 'tainacan'); ?></p>
@ -64,27 +64,78 @@ class Term_Importer extends Importer {
</span>
<div class="control is-clearfix">
<select class="input" type="text" name="taxonomies">
<option>tax-1</option>
<option>tax-2</option>
<option>tax-3</option>
</select>
<?php
$request = new \WP_REST_Request(
'GET', $this->namespace . '/taxonomies'
);
$response = $this->server->dispatch($request);
echo $response;
<?php
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$taxonomies = $Tainacan_Taxonomies->fetch( [
'status' => [
'auto-draft',
'draft',
'publish',
'private'
]
], 'OBJECT' );
foreach( $taxonomies as $taxonomie) {
?>
<option value="<?php echo $taxonomie->get_db_identifier();?>"><?php echo $taxonomie->get_name() ?> </option>
<?php
}
?>
</select>
</div>
</div>
<?php
return ob_get_clean();
}
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;
}
}