diff --git a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php index 84462256f..acb4323fd 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php @@ -5,7 +5,6 @@ namespace Tainacan\API\EndPoints; use \Tainacan\API\REST_Controller; use Tainacan\Repositories; use Tainacan\Entities; -use Tainacan\Tests\Collections; /** * Represents the Items REST Controller @@ -38,6 +37,7 @@ class REST_Items_Controller extends REST_Controller { $this->item_metadata = Repositories\Item_Metadata::get_instance(); $this->collections_repository = Repositories\Collections::get_instance(); $this->metadatum_repository = Repositories\Metadata::get_instance(); + $this->terms_repository = \Tainacan\Repositories\Terms::get_instance(); } /** @@ -897,11 +897,13 @@ class REST_Items_Controller extends REST_Controller { return $this->item_metadata->suggest( $item_metadata ); } else { + $this->submission_rollback_new_terms(); return new \WP_REST_Response( [ 'error_message' => __( 'The metadatum does not accept suggestions', 'tainacan' ), ], 400 ); } } else { + $this->submission_rollback_new_terms(); return new \WP_REST_Response( [ 'error_message' => __( 'Please verify, invalid value(s)', 'tainacan' ), 'errors' => $item_metadata->get_errors(), @@ -910,6 +912,43 @@ class REST_Items_Controller extends REST_Controller { } } + private $new_terms_ids = []; + private function submission_process_terms ($value, $taxonomy) { + if (is_numeric($value)) return $value; + $split_value = explode(">>", $value); + if(count($split_value) == 1 ) { + $exist = $this->terms_repository->term_exists($split_value[0], $taxonomy, null, true); + if ($exist) + return $split_value[0]; + $new_term = new Entities\Term(); + $new_term->set_taxonomy( $taxonomy->get_db_identifier() ); + $new_term->set('name', $split_value[0]); + if ( $new_term->validate() ) { + $new_term = $this->terms_repository->insert( $new_term ); + $this->new_terms_ids[] = ['term_id' => $new_term->get_term_id(), 'taxonomy' => $new_term->get_taxonomy()]; + } + return $new_term; + } else if (count($split_value) == 2 ) { + $new_term = new Entities\Term(); + $new_term->set_taxonomy( $taxonomy->get_db_identifier() ); + $new_term->set('name', $split_value[1]); + $new_term->set('parent', $split_value[0]); + if ( $new_term->validate() ) { + $new_term = $this->terms_repository->insert( $new_term ); + $this->new_terms_ids[] = ['term_id' => $new_term->get_term_id(), 'taxonomy' => $new_term->get_taxonomy()]; + } + return $new_term; + } + return count($split_value) > 1 ? $value : filter_var($value, FILTER_SANITIZE_STRING); + } + + private function submission_rollback_new_terms () { + foreach($this->new_terms_ids as $term) { + $remove_term = new Entities\Term($term['term_id'], $term['taxonomy']); + $this->terms_repository->delete( $remove_term, true ); + } + } + public function submission_item ($request) { $collection_id = $request['collection_id']; $item = json_decode($request->get_body(), true); @@ -936,7 +975,7 @@ class REST_Items_Controller extends REST_Controller { if ( $item->validate() ) { $item = $this->items_repository->insert( $item ); - $item_id = $item->get_id(); + foreach ( $metadata as $m ) { if ( !isset($m['value']) || $m['value'] == null ) continue; $value = $m['value']; @@ -988,6 +1027,25 @@ class REST_Items_Controller extends REST_Controller { $parent_meta_id = $item_metadata_child->get_parent_meta_id(); } } + } else if ($metadatum->get_metadata_type_object()->get_primitive_type() == 'term') { + $taxonomy_id = $metadatum->get_metadata_type_object()->get_option( 'taxonomy_id' ); + $taxonomy = new Entities\Taxonomy( $taxonomy_id ); + if (is_array($value) == true) { + $value = array_map( function($v) use ($taxonomy) { + return $this->submission_process_terms($v, $taxonomy); + }, $value); + } else { + $value = $this->submission_process_terms($value, $taxonomy); + } + if ($item_metadata->is_multiple()) { + $item_metadata->set_value( is_array($value) ? $value : [$value] ); + } else { + $item_metadata->set_value( is_array($value) ? implode(' ', $value) : $value); + } + $item_metadata = $this->submission_item_metadada($item_metadata, $request); + if ($item_metadata instanceof \WP_REST_Response) { + return $item_metadata; + } } else { if (is_array($value) == true) { $value = array_map( function($v) { return is_numeric($v) ? $v : filter_var($v, FILTER_SANITIZE_STRING); }, $value); @@ -1011,14 +1069,19 @@ class REST_Items_Controller extends REST_Controller { $fake_id = md5(uniqid(mt_rand(), true)); $id = $item->get_id(); if (set_transient('tnc_transient_submission_' . $fake_id, $id, 300) == true) { + set_transient('tnc_transient_submission_' . $fake_id . '_new_terms_ids', $this->new_terms_ids, 300); $response_item = $this->prepare_item_for_response($item, $request); $response_item['id'] = $fake_id; return new \WP_REST_Response($response_item, 201 ); - } else return new \WP_REST_Response([ - 'error_message' => __('unable create submission ID.', 'tainacan'), - ], 400); + } else { + $this->submission_rollback_new_terms(); + return new \WP_REST_Response([ + 'error_message' => __('unable create submission ID.', 'tainacan'), + ], 400); + } } else { + $this->submission_rollback_new_terms(); return new \WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $item->get_errors(), @@ -1026,6 +1089,7 @@ class REST_Items_Controller extends REST_Controller { ], 400); } } else { + $this->submission_rollback_new_terms(); return new \WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => $item->get_errors(), @@ -1033,6 +1097,7 @@ class REST_Items_Controller extends REST_Controller { ], 400); } } catch (\Exception $exception){ + $this->submission_rollback_new_terms(); return new \WP_REST_Response($exception->getMessage(), 400); } } @@ -1047,6 +1112,7 @@ class REST_Items_Controller extends REST_Controller { 'error_message' => __('submission ID not exist.', 'tainacan'), ], 400); } + $this->new_terms_ids = get_transient('tnc_transient_submission_' . $submission_id . '_new_terms_ids'); $item = $this->items_repository->fetch($item_id); $collection = $this->collections_repository->fetch($collection_id); @@ -1120,6 +1186,7 @@ class REST_Items_Controller extends REST_Controller { delete_transient('tnc_transient_submission_' . $submission_id); return new \WP_REST_Response($this->prepare_item_for_response($item, $request), 201 ); } else { + $this->submission_rollback_new_terms(); return new \WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), 'errors' => array_merge($item->get_errors(), $entities_erros), diff --git a/src/classes/entities/class-tainacan-term.php b/src/classes/entities/class-tainacan-term.php index 7d4b55f2e..28cd41598 100644 --- a/src/classes/entities/class-tainacan-term.php +++ b/src/classes/entities/class-tainacan-term.php @@ -8,14 +8,14 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); * Represents the Entity Term */ class Term extends Entity { - protected - $term_id, - $name, - $parent, - $description, - $user, + protected + $term_id, + $name, + $parent, + $description, + $user, $header_image_id, - $taxonomy; + $taxonomy; static $post_type = false; @@ -34,22 +34,22 @@ class Term extends Entity { */ function __construct($which = 0, $taxonomy = false ) { - if ($taxonomy) - $this->set_taxonomy( $taxonomy ); + if ($taxonomy) + $this->set_taxonomy( $taxonomy ); - if ( is_numeric( $which ) && $which > 0) { - $post = get_term_by('id', $which, $taxonomy); - - if ( $post instanceof \WP_Term) { - $this->WP_Term = get_term_by('id', $which, $taxonomy); - } + if ( is_numeric( $which ) && $which > 0) { + $post = get_term_by('id', $which, $taxonomy); + + if ( $post instanceof \WP_Term) { + $this->WP_Term = get_term_by('id', $which, $taxonomy); + } - } elseif ( $which instanceof \WP_Term ) { - $this->WP_Term = $which; - } else { - $this->WP_Term = new \StdClass(); - } - } + } elseif ( $which instanceof \WP_Term ) { + $this->WP_Term = $which; + } else { + $this->WP_Term = new \StdClass(); + } + } public function __toString() { return (string) apply_filters("tainacan-term-to-string", $this->get_name(), $this); @@ -70,7 +70,7 @@ class Term extends Entity { return apply_filters('tainacan-term-to-array', $term_array, $this); } - // Getters + // Getters /** * Return the unique identifier @@ -78,11 +78,11 @@ class Term extends Entity { * @return integer */ function get_id() { - return $this->get_term_id(); - } + return $this->get_term_id(); + } function get_term_id() { - return $this->get_mapped_property('term_id'); - } + return $this->get_mapped_property('term_id'); + } /** * Return the name @@ -90,8 +90,8 @@ class Term extends Entity { * @return string */ function get_name() { - return $this->get_mapped_property('name'); - } + return $this->get_mapped_property('name'); + } /** * Return the parent ID @@ -99,8 +99,8 @@ class Term extends Entity { * @return integer */ function get_parent() { - return $this->get_mapped_property('parent'); - } + return $this->get_mapped_property('parent'); + } /** * Return the description @@ -108,8 +108,8 @@ class Term extends Entity { * @return string */ function get_description() { - return $this->get_mapped_property('description'); - } + return $this->get_mapped_property('description'); + } /** * Return the user ID @@ -117,8 +117,8 @@ class Term extends Entity { * @return integer */ function get_user() { - return $this->get_mapped_property('user'); - } + return $this->get_mapped_property('user'); + } /** * Return the taxonomy @@ -126,8 +126,8 @@ class Term extends Entity { * @return integer */ function get_taxonomy() { - return $this->get_mapped_property('taxonomy'); - } + return $this->get_mapped_property('taxonomy'); + } /** * Get Header Image ID attribute @@ -155,7 +155,7 @@ class Term extends Entity { return $url; } - // Setters + // Setters /** * Define the name @@ -163,8 +163,8 @@ class Term extends Entity { * @param [string] $value */ function set_name($value) { - $this->set_mapped_property('name', $value); - } + $this->set_mapped_property('name', $value); + } /** * Define the parent ID @@ -172,8 +172,8 @@ class Term extends Entity { * @param [integer] $value */ function set_parent($value) { - $this->set_mapped_property('parent', $value); - } + $this->set_mapped_property('parent', $value); + } /** * Define the description @@ -181,8 +181,8 @@ class Term extends Entity { * @param [string] $value */ function set_description($value) { - $this->set_mapped_property('description', $value); - } + $this->set_mapped_property('description', $value); + } /** * Define the user associated @@ -190,8 +190,8 @@ class Term extends Entity { * @param [integer] $value */ function set_user($value) { - $this->set_mapped_property('user', $value); - } + $this->set_mapped_property('user', $value); + } /** * Define the taxonomy associated @@ -199,8 +199,8 @@ class Term extends Entity { * @param [integer] $value */ function set_taxonomy($value) { - $this->set_mapped_property('taxonomy', $value); - } + $this->set_mapped_property('taxonomy', $value); + } /** * Set Header Image ID diff --git a/src/views/admin/components/edition/term-edition-form.vue b/src/views/admin/components/edition/term-edition-form.vue index 805f16c19..39a0be1d6 100644 --- a/src/views/admin/components/edition/term-edition-form.vue +++ b/src/views/admin/components/edition/term-edition-form.vue @@ -227,7 +227,7 @@ + + + +