feat: change mappers API

This commit is contained in:
vnmedeiros 2023-09-21 22:17:52 -03:00
parent 7caa526e47
commit 4959a7dd7b
5 changed files with 164 additions and 85 deletions

View File

@ -7,14 +7,14 @@ use Tainacan\Entities\Metadatum;
class REST_Metadatum_Mappers_Controller extends REST_Controller {
protected function get_schema() {
return "TODO:get_schema";
}
return "TODO:get_schema";
}
/**
* REST_Metadatum_Mappers_Controller constructor.
*/
public function __construct() {
$this->rest_base = 'metadatum-mappers';
$this->rest_base = 'mappers';
parent::__construct();
}
@ -26,11 +26,43 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'),
),
)
);
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<slug>[a-z0-9-_]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_items_permissions_check'),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'),
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base ,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
),
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<slug>[a-z0-9-_]+)' ,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_items_permissions_check'),
),
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'),
),
)
);
}
@ -43,7 +75,7 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
*/
public function prepare_item_for_response( $mapper, $request ) {
$metadatum_arr = $mapper->_toArray();
$metadatum_arr = $mapper->_toArray();
return $metadatum_arr;
}
@ -55,11 +87,11 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_metadatum_for_response( $item, $request ) {
if(!empty($item)){
$item_arr = ['field_id' => $item->get_id(), 'exposer_mapping' => $item->get('exposer_mapping')];
return $item_arr;
}
return $item;
if(!empty($item)){
$item_arr = ['field_id' => $item->get_id(), 'exposer_mapping' => $item->get('exposer_mapping')];
return $item_arr;
}
return $item;
}
/**
@ -68,13 +100,36 @@ 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');
$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($metadatum_mapper->show_ui) {
$mapper = $this->prepare_item_for_response($metadatum_mapper, $request);
$mapper['disabled'] = in_array($metadatum_mapper->slug, $disabled_mappers) ? true : false;
array_push($prepared, $mapper);
}
}
return new \WP_REST_Response($prepared, 200);
}
/**
* @param \WP_REST_Request $request
*
* @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));
}
return new \WP_REST_Response($prepared, 200);
@ -98,21 +153,21 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
* @throws \Exception
*/
public function update_item_permissions_check( $request ) {
$body = json_decode( $request->get_body(), true );
if(
is_array($body) &&
array_key_exists('metadata_mappers', $body) &&
is_array($body['metadata_mappers']) &&
count($body['metadata_mappers']) > 0 &&
\Tainacan\Mappers_Handler::get_mapper_from_request($request)
) {
$metadatum_mapper = $body['metadata_mappers'][0];
$metadatum = \Tainacan\Repositories\Repository::get_entity_by_post($metadatum_mapper['metadatum_id']);
if($metadatum instanceof \Tainacan\Entities\Metadatum && $metadatum->can_edit()) {
return true;
}
}
return false;
$body = json_decode( $request->get_body(), true );
if(
is_array($body) &&
array_key_exists('metadata_mappers', $body) &&
is_array($body['metadata_mappers']) &&
count($body['metadata_mappers']) > 0 &&
\Tainacan\Mappers_Handler::get_mapper_from_request($request)
) {
$metadatum_mapper = $body['metadata_mappers'][0];
$metadatum = \Tainacan\Repositories\Repository::get_entity_by_post($metadatum_mapper['metadatum_id']);
if($metadatum instanceof \Tainacan\Entities\Metadatum && $metadatum->can_edit()) {
return true;
}
}
return false;
}
/**
@ -121,58 +176,58 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function update_item( $request ) {
$Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$body = json_decode( $request->get_body(), true );
if($mapper = $Tainacan_Mappers::get_mapper_from_request($request)) {
if(count($body['metadata_mappers']) > 0) {
$response = [];
$saved = [];
foreach ($body['metadata_mappers'] as $metadatum_mapper) {
$metadatum_mapper['metadatum_id'] = intval($metadatum_mapper['metadatum_id']);
if(is_array($metadatum_mapper['mapper_metadata'])) {
foreach ($metadatum_mapper['mapper_metadata'] as $k => $tag) {
$metadatum_mapper['mapper_metadata'][$k] = esc_attr($tag);
}
} else {
$metadatum_mapper['mapper_metadata'] = sanitize_text_field($metadatum_mapper['mapper_metadata']);
}
$isSaved = false;
$metadatum = $Tainacan_Metadata->fetch($metadatum_mapper['metadatum_id']);
$exposer_mapping = $metadatum->get('exposer_mapping');
if($metadatum_mapper['mapper_metadata'] == '') {
if(array_key_exists($mapper->slug, $exposer_mapping) ) {
unset($exposer_mapping[$mapper->slug]);
$isSaved = true;
}
} else {
if( !array_key_exists($mapper->slug, $exposer_mapping) || $exposer_mapping[$mapper->slug] != $metadatum_mapper['mapper_metadata']) {
$exposer_mapping[$mapper->slug] = $metadatum_mapper['mapper_metadata'];
$isSaved = true;
}
}
if($isSaved) {
$metadatum->set('exposer_mapping', $exposer_mapping);
if($metadatum->validate()) {
$Tainacan_Metadata->update($metadatum);
$response[] = $this->prepare_metadatum_for_response($metadatum, $request);
} else {
return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared->get_errors(),
'metadatum' => $this->prepare_item_for_response($prepared, $request),
], 400);
}
}
}
$Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$body = json_decode( $request->get_body(), true );
if($mapper = $Tainacan_Mappers::get_mapper_from_request($request)) {
if(count($body['metadata_mappers']) > 0) {
$response = [];
$saved = [];
foreach ($body['metadata_mappers'] as $metadatum_mapper) {
$metadatum_mapper['metadatum_id'] = intval($metadatum_mapper['metadatum_id']);
if(is_array($metadatum_mapper['mapper_metadata'])) {
foreach ($metadatum_mapper['mapper_metadata'] as $k => $tag) {
$metadatum_mapper['mapper_metadata'][$k] = esc_attr($tag);
}
} else {
$metadatum_mapper['mapper_metadata'] = sanitize_text_field($metadatum_mapper['mapper_metadata']);
}
$isSaved = false;
$metadatum = $Tainacan_Metadata->fetch($metadatum_mapper['metadatum_id']);
$exposer_mapping = $metadatum->get('exposer_mapping');
if($metadatum_mapper['mapper_metadata'] == '') {
if(array_key_exists($mapper->slug, $exposer_mapping) ) {
unset($exposer_mapping[$mapper->slug]);
$isSaved = true;
}
} else {
if( !array_key_exists($mapper->slug, $exposer_mapping) || $exposer_mapping[$mapper->slug] != $metadatum_mapper['mapper_metadata']) {
$exposer_mapping[$mapper->slug] = $metadatum_mapper['mapper_metadata'];
$isSaved = true;
}
}
if($isSaved) {
$metadatum->set('exposer_mapping', $exposer_mapping);
if($metadatum->validate()) {
$Tainacan_Metadata->update($metadatum);
$response[] = $this->prepare_metadatum_for_response($metadatum, $request);
} else {
return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared->get_errors(),
'metadatum' => $this->prepare_item_for_response($prepared, $request),
], 400);
}
}
}
return new \WP_REST_Response($response, 200);
}
}
return new \WP_REST_Response([
'error_message' => __('Body cannot be empty.', 'tainacan'),
'item' => $request->get_body()
], 400);
return new \WP_REST_Response($response, 200);
}
}
return new \WP_REST_Response([
'error_message' => __('Body cannot be empty.', 'tainacan'),
'item' => $request->get_body()
], 400);
}
}

View File

@ -44,7 +44,8 @@ abstract class Mapper {
'metadata' => $this->metadata,
'prefix' => $this->prefix,
'sufix' => $this->sufix,
'header' => $this->header
'header' => $this->header,
'add_meta_form' => ''
];
}

View File

@ -37,6 +37,7 @@ class Mappers_Handler {
function init() {
$this->register_mapper('Tainacan\Mappers\Dublin_Core');
$this->register_mapper('Tainacan\Mappers\Wiki_Data');
do_action('tainacan-register-mappers', $this);
}

View File

@ -0,0 +1,22 @@
<?php
namespace Tainacan\Mappers;
/**
* Support Dublin Core Mapping
* http://purl.org/dc/elements/1.1/
*
*/
class Wiki_Data extends Mapper {
public $slug = 'wiki-data';
public $name = 'WikiData';
public $allow_extra_metadata = true;
public $context_url = 'http://??????';
public $header = '??????';
public $prefixes = [
'dc' => 'http://purl.org/dc/elements/1.1/'
];
public $metadata = [
];
}

View File

@ -261,7 +261,7 @@ export const updateMetadatumTypes = ({commit}, metadatumTypes) => {
// METADATA MAPPERS
export const fetchMappers = ({commit}) => {
return new Promise((resolve, reject) => {
axios.tainacan.get('/metadatum-mappers')
axios.tainacan.get('/mappers')
.then((res) => {
let mappers = res.data;
commit('setMappers', mappers);
@ -280,7 +280,7 @@ export const updateMapper = ({ dispatch }, {metadataMapperMetadata, mapper}) =>
metadata_mappers: metadataMapperMetadata,
};
param[tainacan_plugin.exposer_mapper_param] = mapper;
axios.tainacan.post('/metadatum-mappers', param).then((res) => {
axios.tainacan.post('/mappers', param).then((res) => {
resolve(res.data);
})
.catch((error) => {