tainacan csv importer - create endpoint to fetch saved ampping

This commit is contained in:
Eduardo Humberto 2018-11-01 21:01:57 -03:00
parent a5f9d8bf7d
commit e78b291021
2 changed files with 67 additions and 0 deletions

View File

@ -88,6 +88,16 @@ class REST_Importers_Controller extends REST_Controller {
));
register_rest_route($this->namespace, '/' . $this->rest_base . '/session/(?P<session_id>[0-9a-f]+)/get_mapping/(?P<collection_id>[0-9a-f]+)', array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_saved_mapping'),
'permission_callback' => array($this, 'import_permissions_check'),
),
));
register_rest_route($this->namespace, '/' . $this->rest_base . '/session/(?P<session_id>[0-9a-f]+)', array(
array(
@ -265,6 +275,26 @@ class REST_Importers_Controller extends REST_Controller {
}
public function get_saved_mapping( $request ){
$session_id = $request['session_id'];
$collection_id = $request['collection_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
$response = false;
if(!$importer) {
return new \WP_REST_Response([
'error_message' => __('Importer Session not found', 'tainacan' ),
'session_id' => $session_id
], 400);
}
if ( method_exists($importer, 'get_mapping') ) {
$response = $importer->get_mapping($collection_id);
}
return new \WP_REST_Response( $response, 200 );
}
public function get_item( $request ) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];

View File

@ -686,4 +686,41 @@ class CSV extends Importer {
return false;
}
}
/**
* @param $collection_id int the collection id
* @param $mapping array the headers-metadata mapping
*/
public function save_mapping( $collection_id, $mapping ){
update_post_meta( $collection_id, 'metadata_mapping', $mapping );
}
/**
* @param $collection_id
*
* @return array/bool false if has no mapping or associated array with metadata id and header
*/
public function get_mapping( $collection_id ){
$mapping = get_post_meta( $collection_id, 'metadata_mapping', true );
return ( $mapping ) ? $mapping : false;
}
/**
* @inheritdoc
*
* allow save mapping
*/
public function add_collection(array $collection) {
if (isset($collection['id'])) {
if( isset($collection['mapping']) && is_array($collection['mapping']) ){
$this->save_mapping( $collection['id'], $collection['mapping'] );
}
$this->remove_collection($collection['id']);
$this->collections[] = $collection;
}
}
}