diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php index ba2c02503..547b70978 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php @@ -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[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[\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[\d]+)/' . $this->rest_base . '/(?P[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); - } - } - } - - return new \WP_REST_Response($response, 200); - } - } - return new \WP_REST_Response([ - 'error_message' => __('Body cannot be empty.', 'tainacan'), - 'item' => $request->get_body() - ], 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); } } diff --git a/src/classes/mappers/class-tainacan-mapper.php b/src/classes/mappers/class-tainacan-mapper.php index 05f1eb322..16b54689a 100644 --- a/src/classes/mappers/class-tainacan-mapper.php +++ b/src/classes/mappers/class-tainacan-mapper.php @@ -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' => '' ]; } diff --git a/src/classes/mappers/class-tainacan-mappers-handler.php b/src/classes/mappers/class-tainacan-mappers-handler.php index 3868c338c..dd91d38ad 100644 --- a/src/classes/mappers/class-tainacan-mappers-handler.php +++ b/src/classes/mappers/class-tainacan-mappers-handler.php @@ -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); } diff --git a/src/classes/mappers/class-tainacan-wiki-data.php b/src/classes/mappers/class-tainacan-wiki-data.php new file mode 100644 index 000000000..dbe88f942 --- /dev/null +++ b/src/classes/mappers/class-tainacan-wiki-data.php @@ -0,0 +1,22 @@ + 'http://purl.org/dc/elements/1.1/' + ]; + public $metadata = [ + + ]; +} \ No newline at end of file diff --git a/src/views/admin/js/store/modules/metadata/actions.js b/src/views/admin/js/store/modules/metadata/actions.js index 382e7d84a..bf8d5714d 100644 --- a/src/views/admin/js/store/modules/metadata/actions.js +++ b/src/views/admin/js/store/modules/metadata/actions.js @@ -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) => {