add option to create a taxonomy on import terms #23

This commit is contained in:
vnmedeiros 2018-09-26 15:14:42 -03:00
parent 53f4544a08
commit 428ab42f93
4 changed files with 63 additions and 10 deletions

View File

@ -55,6 +55,10 @@ function tainacan_autoload($class_name){
if( isset( $class_path[1] ) && $class_path[1] === 'Importer' ){ if( isset( $class_path[1] ) && $class_path[1] === 'Importer' ){
$dir = TAINACAN_IMPORTER_DIR; $dir = TAINACAN_IMPORTER_DIR;
$dir_import = strtolower(str_replace('_', '-' , $class_name));
if (file_exists("$dir$dir_import/")) {
$dir .= "$dir_import/";
}
} else if( isset( $class_path[1] ) && $class_path[1] === 'Exposers' ){ } else if( isset( $class_path[1] ) && $class_path[1] === 'Exposers' ){
$dir = TAINACAN_EXPOSERS_DIR; $dir = TAINACAN_EXPOSERS_DIR;
if(count($class_path) > 3) $dir .= strtolower($class_path[2]).DIRECTORY_SEPARATOR; if(count($class_path) > 3) $dir .= strtolower($class_path[2]).DIRECTORY_SEPARATOR;

View File

@ -54,6 +54,12 @@ class Importer_Handler {
do_action('tainacan_register_importers'); do_action('tainacan_register_importers');
add_action( 'tainacan-enqueue-admin-scripts', array($this, 'enqueue_scripts') );
}
function enqueue_scripts() {
global $TAINACAN_BASE_URL;
wp_enqueue_script('import_term_csv_script', $TAINACAN_BASE_URL . '/importer/term-importer/js/term.js', false, false, true);
} }
function add_to_queue(\Tainacan\Importer\Importer $importer_object) { function add_to_queue(\Tainacan\Importer\Importer $importer_object) {

View File

@ -14,6 +14,11 @@ use \Tainacan\Entities;
class Term_Importer extends Importer { class Term_Importer extends Importer {
protected $steps = [ protected $steps = [
[
'name' => 'Create Taxonomy',
'progress_label' => 'Creating taxonomy',
'callback' => 'create_taxonomy'
],
[ [
'name' => 'Import Terms', 'name' => 'Import Terms',
'progress_label' => 'Creating terms', 'progress_label' => 'Creating terms',
@ -28,7 +33,8 @@ class Term_Importer extends Importer {
$this->remove_import_method('url'); $this->remove_import_method('url');
$this->set_default_options([ $this->set_default_options([
'delimiter' => ',' 'delimiter' => ',',
'new_taxonomy' => ''
]); ]);
} }
@ -57,8 +63,8 @@ class Term_Importer extends Importer {
</div> </div>
</div> </div>
<div class="field"> <div class="field import_term_csv_taxonomies">
<label class="label"><?php _e('Taxonomies:', 'tainacan'); ?></label> <label class="label"><?php _e('Select a taxonomies:', 'tainacan'); ?></label>
<span class="help-wrapper"> <span class="help-wrapper">
<a class="help-button has-text-secondary"> <a class="help-button has-text-secondary">
<span class="icon is-small"> <span class="icon is-small">
@ -76,18 +82,18 @@ class Term_Importer extends Importer {
</span> </span>
<div class="control is-clearfix"> <div class="control is-clearfix">
<div class="select"> <div class="select">
<select name="taxonomies"> <select name="select_taxonomy" class="select_taxonomy">
<option disabled selected></option>
<?php <?php
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$taxonomies = $Tainacan_Taxonomies->fetch( [ $taxonomies = $Tainacan_Taxonomies->fetch( [
'status' => [ 'status' => [
'auto-draft', //'auto-draft',
'draft', 'draft',
'publish', 'publish',
'private' 'private'
] ]
], 'OBJECT' ); ], 'OBJECT' );
foreach( $taxonomies as $taxonomie) { foreach( $taxonomies as $taxonomie) {
?> ?>
<option value="<?php echo $taxonomie->get_db_identifier();?>"><?php echo $taxonomie->get_name() ?> </option> <option value="<?php echo $taxonomie->get_db_identifier();?>"><?php echo $taxonomie->get_name() ?> </option>
@ -97,6 +103,11 @@ class Term_Importer extends Importer {
</select> </select>
</div> </div>
</div> </div>
<label class="label"><?php _e('or create a new:', 'tainacan'); ?></label>
<div class="control is-clearfix">
<input class="input new_taxonomy" type="text" name="new_taxonomy" value="<?php echo $this->get_option('new_taxonomy'); ?>" onchange="changeValueTaxonomy(this.value)">
</div>
</div> </div>
<?php <?php
@ -120,6 +131,7 @@ class Term_Importer extends Importer {
if ($parent == null) $parent = array(); if ($parent == null) $parent = array();
$position = $this->get_transient('position') == null ? 0: $this->get_transient('position'); $position = $this->get_transient('position') == null ? 0: $this->get_transient('position');
$last_term = $this->get_transient('last_term') == null ? 0: $this->get_transient('last_term'); $last_term = $this->get_transient('last_term') == null ? 0: $this->get_transient('last_term');
$id_taxonomy= $this->get_transient('new_taxonomy') == null ? $this->get_option('select_taxonomy') : $this->get_transient('new_taxonomy');
$position_file = $this->get_in_step_count(); $position_file = $this->get_in_step_count();
fseek($file, $position_file); fseek($file, $position_file);
@ -142,7 +154,7 @@ class Term_Importer extends Importer {
$term = new \Tainacan\Entities\Term(); $term = new \Tainacan\Entities\Term();
$term->set_name($values[$position]); $term->set_name($values[$position]);
$term->set_description($values[$position+1]); $term->set_description($values[$position+1]);
$term->set_taxonomy($this->get_option('taxonomies')); $term->set_taxonomy($id_taxonomy);
$term_repo = \Tainacan\Repositories\Terms::get_instance(); $term_repo = \Tainacan\Repositories\Terms::get_instance();
if(end($parent)) if(end($parent))
@ -166,4 +178,28 @@ class Term_Importer extends Importer {
return true; return true;
} }
} }
public function create_taxonomy() {
if ($this->get_option('new_taxonomy') == '')
return false;
$tax1 = new Entities\Taxonomy();
$tax1->set_name($this->get_option('new_taxonomy'));
$tax1->set_allow_insert('yes');
$tax1->set_status('publish');
if ($tax1->validate()) {
$tax_repo = \Tainacan\Repositories\Taxonomies::get_instance();
$tax1 = $tax_repo->insert($tax1);
$name = $tax1->get_name();
$this->add_transient('new_taxonomy', $tax1->get_db_identifier());
$this->add_log("taxonomy $name Created.");
return true;
} else {
$this->add_error_log('Error creating taxonomy');
$this->add_error_log($tax1->get_errors());
$this->abort();
}
return false;
}
} }

View File

@ -0,0 +1,7 @@
changeValueTaxonomy = function(value) {
if (value == '') {
$('.import_term_csv_taxonomies select.select_taxonomy').prop('disabled', false);
} else {
$('.import_term_csv_taxonomies select.select_taxonomy').prop('disabled', true);
}
}