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

@ -54,7 +54,11 @@ function tainacan_autoload($class_name){
$sliced = array_slice($class_path, 1, count($class_path) -2);
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' ){
$dir = TAINACAN_EXPOSERS_DIR;
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');
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) {

View File

@ -14,6 +14,11 @@ use \Tainacan\Entities;
class Term_Importer extends Importer {
protected $steps = [
[
'name' => 'Create Taxonomy',
'progress_label' => 'Creating taxonomy',
'callback' => 'create_taxonomy'
],
[
'name' => 'Import Terms',
'progress_label' => 'Creating terms',
@ -28,7 +33,8 @@ class Term_Importer extends Importer {
$this->remove_import_method('url');
$this->set_default_options([
'delimiter' => ','
'delimiter' => ',',
'new_taxonomy' => ''
]);
}
@ -57,8 +63,8 @@ class Term_Importer extends Importer {
</div>
</div>
<div class="field">
<label class="label"><?php _e('Taxonomies:', 'tainacan'); ?></label>
<div class="field import_term_csv_taxonomies">
<label class="label"><?php _e('Select a taxonomies:', 'tainacan'); ?></label>
<span class="help-wrapper">
<a class="help-button has-text-secondary">
<span class="icon is-small">
@ -76,18 +82,18 @@ class Term_Importer extends Importer {
</span>
<div class="control is-clearfix">
<div class="select">
<select name="taxonomies">
<select name="select_taxonomy" class="select_taxonomy">
<option disabled selected></option>
<?php
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$taxonomies = $Tainacan_Taxonomies->fetch( [
'status' => [
'auto-draft',
//'auto-draft',
'draft',
'publish',
'private'
]
], 'OBJECT' );
foreach( $taxonomies as $taxonomie) {
?>
<option value="<?php echo $taxonomie->get_db_identifier();?>"><?php echo $taxonomie->get_name() ?> </option>
@ -97,6 +103,11 @@ class Term_Importer extends Importer {
</select>
</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>
<?php
@ -118,8 +129,9 @@ class Term_Importer extends Importer {
$parent = $this->get_transient('parent');
if ($parent == null) $parent = array();
$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');
$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');
$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();
fseek($file, $position_file);
@ -142,7 +154,7 @@ class Term_Importer extends Importer {
$term = new \Tainacan\Entities\Term();
$term->set_name($values[$position]);
$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();
if(end($parent))
@ -166,4 +178,28 @@ class Term_Importer extends Importer {
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);
}
}