diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadata-controller.php index c95d0a310..3a06086d9 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -488,8 +488,6 @@ class REST_Metadata_Controller extends REST_Controller { $metadatum = $this->metadatum_repository->fetch($metadatum_id); - $error_message = __('Metadata with this ID was not found', 'tainacan'); - if ($metadatum) { // These conditions are for verify if endpoints are used correctly if(!$collection_id && $metadatum->get_collection_id() !== 'default') { @@ -498,14 +496,14 @@ class REST_Metadata_Controller extends REST_Controller { return new \WP_REST_Response( [ 'error_message' => $error_message, 'metadatum_id' => $metadatum_id - ] ); + ], 400 ); } elseif ($collection_id && $metadatum->get_collection_id() === 'default'){ $error_message = __('This metadata is not a collection metadata', 'tainacan'); return new \WP_REST_Response( [ 'error_message' => $error_message, 'metadatum_id' => $metadatum_id - ] ); + ], 400 ); } if (isset($request['repository_level']) && $confirm_repository) { @@ -515,7 +513,7 @@ class REST_Metadata_Controller extends REST_Controller { $prepared_metadata = $this->prepare_item_for_updating($metadatum, $attributes); if($prepared_metadata->validate()){ - $updated_metadata = $this->metadatum_repository->update($prepared_metadata); + $updated_metadata = $this->metadatum_repository->update($prepared_metadata, $attributes); $response = $this->prepare_item_for_response($updated_metadata, $request); @@ -529,6 +527,7 @@ class REST_Metadata_Controller extends REST_Controller { ], 400); } + $error_message = __('Metadata with this ID was not found', 'tainacan'); return new \WP_REST_Response( [ 'error_message' => $error_message, 'metadatum_id' => $metadatum_id diff --git a/src/classes/entities/class-tainacan-metadatum.php b/src/classes/entities/class-tainacan-metadatum.php index ea415eba7..c28592eeb 100644 --- a/src/classes/entities/class-tainacan-metadatum.php +++ b/src/classes/entities/class-tainacan-metadatum.php @@ -450,7 +450,10 @@ class Metadatum extends Entity { * @param [string] $value * @return void */ - function set_metadata_section_id( $value) { + function set_metadata_section_id($value) { + if( !is_array($value) ) { + $value = [$value]; + } return $this->set_mapped_property('metadata_section_id', $value); } @@ -497,6 +500,15 @@ class Metadatum extends Entity { return $this->get_required() === 'yes'; } + /** + * Return true if is repository level, else return false + * + * @return boolean + */ + function is_repository_level() { + return $this->get_collection_id() === 'default'; + } + /** * {@inheritdoc } * diff --git a/src/classes/repositories/class-tainacan-metadata-sections.php b/src/classes/repositories/class-tainacan-metadata-sections.php index 1b351071c..80a25f759 100644 --- a/src/classes/repositories/class-tainacan-metadata-sections.php +++ b/src/classes/repositories/class-tainacan-metadata-sections.php @@ -377,10 +377,36 @@ class Metadata_Sections extends Repository { } public function get_default_section_metadata_object_list (Entities\Collection $collection, $args = []) { + $metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); $metadata_sections_ids = $this->fetch_ids(); + $collection_metadata_sections_id = array_diff(array_map(function($el) {return $el->get_id();} , $this->fetch_by_collection($collection)), [\Tainacan\Entities\Metadata_Section::$default_section_slug]); + + $args_exclude = array_merge( + array( + 'meta_query' => array( + 'relation' => 'AND', + array( + array( + 'key' => 'collection_id', + 'value' => 'default', + 'compare' => '=' + ), + array( + 'key' => 'metadata_section_id', + 'value' => $collection_metadata_sections_id, + 'compare' => 'IN' + ) + ) + ) + ) + ); + $list_exclude = $metadata_repository->fetch_by_collection($collection, $args_exclude); + $not_post_ids = array_map(function($el) { return $el->get_id(); }, $list_exclude); + $args = array_merge( $args, array( + 'post__not_in' => $not_post_ids, 'meta_query' => array( array( 'relation' => 'OR', @@ -399,12 +425,11 @@ class Metadata_Sections extends Repository { 'key' => 'metadata_section_id', 'value' => $metadata_sections_ids, 'compare' => 'NOT IN' - ), + ) ) ) ) ); - $metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); $metadata_list = $metadata_repository->fetch_by_collection($collection, $args); return $metadata_list; } diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index 5cfd4a5ca..725ff111c 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -227,7 +227,7 @@ class Metadata extends Repository { 'default' => 'no' ], 'metadata_section_id' => [ - 'map' => 'meta', + 'map' => 'meta_multi', 'title' => __( 'Metadata section', 'tainacan' ), 'type' => ['integer', 'string', 'array'], 'description' => __( 'The metadata section ID', 'tainacan' ), @@ -743,6 +743,30 @@ class Metadata extends Repository { return $new_metadatum; } + public function pre_update_metadata_repository_level($metadatum, $attributes) { + if (isset($attributes['target_collection_id']) && is_numeric($attributes['target_collection_id']) && $metadatum->is_repository_level()) { + $collection = \tainacan_collections()->fetch($attributes['target_collection_id'], 'OBJECT'); + + $new_metadata_section_id = $metadatum->get_metadata_section_id(); + $new_metadata_section_id = is_array($new_metadata_section_id) ? $new_metadata_section_id[0] : $new_metadata_section_id; + + if($collection instanceof \Tainacan\Entities\Collection) { + $collection_metadata_sections_id = array_filter( + array_map(function($el) {return $el->get_id();} , \tainacan_metadata_sections()->fetch_by_collection($collection)), + function($el) {return $el != \Tainacan\Entities\Metadata_Section::$default_section_slug;} + ); + $old_value = get_post_meta($metadatum->get_id(), 'metadata_section_id'); + $new_value = array_diff($old_value, $collection_metadata_sections_id); + $new_value[] = (string)$new_metadata_section_id; + $metadatum->set_metadata_section_id($new_value); + if(!$metadatum->validate()) { + throw new \Exception( $metadatum->get_errors() ); + } + } + } + return $metadatum; + } + /** * @param $object * @param $new_values @@ -751,6 +775,7 @@ class Metadata extends Repository { * @throws \Exception */ public function update( $object, $new_values = null ) { + $object = $this->pre_update_metadata_repository_level($object, $new_values); return $this->insert( $object ); } diff --git a/src/views/admin/components/lists/collection-metadata-list.vue b/src/views/admin/components/lists/collection-metadata-list.vue index dbafa9973..8219b3351 100644 --- a/src/views/admin/components/lists/collection-metadata-list.vue +++ b/src/views/admin/components/lists/collection-metadata-list.vue @@ -636,7 +636,7 @@ export default { this.updateMetadatum({ collectionId: this.collectionId, metadatumId: event.added.element.id, - isRepositoryLevel: false, + isRepositoryLevel: event.added.element.collection_id === 'default', index: event.added.newIndex, options: {}, includeOptionsAsHtml: true, diff --git a/src/views/admin/js/store/modules/metadata/actions.js b/src/views/admin/js/store/modules/metadata/actions.js index 089021e6e..094c4794f 100644 --- a/src/views/admin/js/store/modules/metadata/actions.js +++ b/src/views/admin/js/store/modules/metadata/actions.js @@ -111,10 +111,12 @@ export const updateMetadatum = ({commit}, {collectionId, metadatumId, isReposito return new Promise((resolve, reject) => { let endpoint = ''; - if (!isRepositoryLevel) + if (!isRepositoryLevel) { endpoint = '/collection/' + collectionId + '/metadata/' + metadatumId; - else + } else { endpoint = '/metadata/' + metadatumId; + options['target_collection_id'] = collectionId; + } endpoint += '?context=edit';