diff --git a/src/classes/tainacan-creator.php b/src/classes/tainacan-creator.php index e435f7f72..0228cd4f3 100644 --- a/src/classes/tainacan-creator.php +++ b/src/classes/tainacan-creator.php @@ -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; diff --git a/src/importer/class-tainacan-importer-handler.php b/src/importer/class-tainacan-importer-handler.php index ec6588ca2..af1ed33c1 100644 --- a/src/importer/class-tainacan-importer-handler.php +++ b/src/importer/class-tainacan-importer-handler.php @@ -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) { diff --git a/src/importer/class-tainacan-term-importer.php b/src/importer/term-importer/class-tainacan-term-importer.php similarity index 70% rename from src/importer/class-tainacan-term-importer.php rename to src/importer/term-importer/class-tainacan-term-importer.php index b51170e4a..a85937263 100644 --- a/src/importer/class-tainacan-term-importer.php +++ b/src/importer/term-importer/class-tainacan-term-importer.php @@ -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 { -
- +
+ @@ -76,18 +82,18 @@ class Term_Importer extends Importer {
- + fetch( [ 'status' => [ - 'auto-draft', + //'auto-draft', 'draft', 'publish', 'private' ] ], 'OBJECT' ); - foreach( $taxonomies as $taxonomie) { ?> @@ -97,6 +103,11 @@ class Term_Importer extends Importer {
+ + +
+ +
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; + } + } \ No newline at end of file diff --git a/src/importer/term-importer/js/term.js b/src/importer/term-importer/js/term.js new file mode 100644 index 000000000..6502e49c9 --- /dev/null +++ b/src/importer/term-importer/js/term.js @@ -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); + } +} \ No newline at end of file