fix: get disabled mappers in collections #783

This commit is contained in:
vnmedeiros 2023-09-25 12:13:51 -03:00
parent 4959a7dd7b
commit d3efbdaf93
4 changed files with 26 additions and 9 deletions

View File

@ -100,8 +100,8 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function get_items( $request ) {
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : '';
$disabled_mappers = get_post_meta($collection_id, 'disabled_mappers');
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : 'default';
$disabled_mappers = get_post_meta($collection_id, 'disabled_mappers', false);
$Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance();
$metadatum_mappers = $Tainacan_Mappers->get_mappers( 'OBJECT' );
@ -110,7 +110,7 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
foreach ($metadatum_mappers as $metadatum_mapper){
if($metadatum_mapper->show_ui) {
$mapper = $this->prepare_item_for_response($metadatum_mapper, $request);
$mapper['disabled'] = in_array($metadatum_mapper->slug, $disabled_mappers) ? true : false;
$mapper['disabled'] = !empty($disabled_mappers) && in_array($metadatum_mapper->slug, $disabled_mappers) ? true : false;
array_push($prepared, $mapper);
}
}
@ -123,16 +123,29 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
$Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance();
$metadatum_mappers = $Tainacan_Mappers->get_mappers( 'OBJECT' );
$prepared = [];
foreach ($metadatum_mappers as $metadatum_mapper){
if($metadatum_mapper->show_ui) array_push($prepared, $this->prepare_item_for_response($metadatum_mapper, $request));
if( !isset( $request['slug'] ) ) {
return new \WP_REST_Response([
'error_message' => __('Slug mapper not informed', 'tainacan'),
], 422);
}
return new \WP_REST_Response($prepared, 200);
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : 'default';
$disabled_mappers = get_post_meta($collection_id, 'disabled_mappers', false);
$Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance();
$metadatum_mappers = $Tainacan_Mappers->get_mappers( 'OBJECT' );
$slug_mapper = $request['slug'];
$mapper_key = array_search($slug_mapper, array_column($metadatum_mappers, 'slug') );
if( $mapper_key === false ) {
return new \WP_REST_Response([
'error_message' => __('Mapper not found', 'tainacan'),
], 400);
}
$mapper = $metadatum_mappers[$mapper_key];
$prepared_mapper = $this->prepare_item_for_response($mapper, $request);
$prepared_mapper['disabled'] = !empty($disabled_mappers) && in_array($slug_mapper, $disabled_mappers) ? true : false;
return new \WP_REST_Response($prepared_mapper, 200);
}
/**

View File

@ -10,6 +10,7 @@ namespace Tainacan\Mappers;
class Dublin_Core extends Mapper {
public $slug = 'dublin-core';
public $name = 'Dublin Core';
public $description = 'The mapper of the Dublin Core standard';
public $allow_extra_metadata = true;
public $context_url = 'http://dublincore.org/documents/dcmi-terms/';
public $header = '<?xml version="1.0"?><!DOCTYPE rdf:RDF SYSTEM "http://dublincore.org/2000/12/01-dcmes-xml-dtd.dtd"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" ></rdf:RDF>';

View File

@ -4,7 +4,8 @@ namespace Tainacan\Mappers;
abstract class Mapper {
public $slug = null; // Slug of Mapper, used as option on api call
public $name = null; // Public name do mapper
public $name = null; // Public name of the mapper
public $description = null; // Public descript of the mapper
public $allow_extra_metadata = true; // Allow more metadatum to be register
public $context_url = null; // URL of mapper documentation
public $type = ''; // The Class of the ontology that this mapping refers to. For example `CreativeWork`, which is a class of Schema.org if applied
@ -39,6 +40,7 @@ abstract class Mapper {
return [
'slug' => $this->slug,
'name' => $this->name,
'description' => $this->description,
'allow_extra_metadata' => $this->allow_extra_metadata,
'context_url' => $this->context_url,
'metadata' => $this->metadata,

View File

@ -10,6 +10,7 @@ namespace Tainacan\Mappers;
class Wiki_Data extends Mapper {
public $slug = 'wiki-data';
public $name = 'WikiData';
public $description = 'The model mapper using the WikiData source';
public $allow_extra_metadata = true;
public $context_url = 'http://??????';
public $header = '??????';