Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
weryques 2018-06-19 18:06:05 -03:00
commit 58fb746628
11 changed files with 162 additions and 48 deletions

View File

@ -15,7 +15,8 @@
:title="$i18n.get('label_source_file')"
:message="$i18n.get('info_source_file_upload')"/>
<br>
<b-upload
<b-upload
v-if="importer.tmp_file == undefined"
:value="importerFile"
@input="onUploadFile($event)"
drag-drop>
@ -43,7 +44,8 @@
:message="$i18n.get('info_target_collection_helper')"/>
<b-select
id="tainacan-select-target-collection"
v-model="form.collectionId"
:value="collectionId"
@input="onSelectCollection($event)"
:loading="isFetchingCollections"
:placeholder="$i18n.get('instruction_select_a_target_collection')">
<option
@ -62,21 +64,37 @@
<help-button
:title="$i18n.get('label_metadata_mapping')"
:message="$i18n.get('info_metadata_mapping_helper')"/>
<div class="columns">
<div class="column">
<ol v-if="importerSourceInfo != undefined">
<li
v-for="(source_metadatum, index) of importerSourceInfo.source_metadata"
:key="index">{{ source_metadatum }}</li>
</ol>
<div v-else>
Upload a source to load metadata.
</div>
</div>
<div class="column">
<div>Select collection to list metadada. (no implemented)</div>
<div
v-if="importerSourceInfo != undefined &&
importerSourceInfo != null">
<div
class="source-metadatum"
v-for="(source_metadatum, index) of importerSourceInfo.source_metadata"
:key="index"><p>{{ source_metadatum }}</p>
<b-select
v-if="collectionMetadata != undefined &&
collectionMetadata.length > 0 &&
!isFetchingCollectionMetadata"
@input="onSelectCollectionMetadata($event, source_metadatum)"
:placeholder="$i18n.get('label_select_metadatum')">
<option
v-for="(metadatum, index) of collectionMetadata"
:key="index"
:value="metadatum.id"
:disabled="checkIfMetadatumIsAvailable(metadatum.id)">{{ metadatum.name }}
</option>
</b-select>
<p v-if="collectionMetadata == undefined || collectionMetadata.length <= 0">{{ $i18n.get('info_select_collection_to_list_metadata') }}</p>
</div>
</div>
<div
v-if="importerSourceInfo == undefined ||
importerSourceInfo == null">
<p>{{ $i18n.get('info_upload_a_source_to_see_metadata') }}</p>
</div>
</b-field>
<!-- Form submit -------------------------------- -->
@ -90,8 +108,9 @@
</div>
<div class="control">
<button
:disabled="sessionId == undefined || importer == undefined"
id="button-submit-collection-creation"
@click.prevent="runImporter"
@click.prevent="onRunImporter"
class="button is-success">{{ $i18n.get('run') }}</button>
</div>
</div>
@ -115,13 +134,20 @@ export default {
isLoading: false,
isFetchingCollections: false,
form: {
collectionId: Number
},
mappedCollection: {
'id': Number,
'mapping': {},
'total_items': Number
},
importerTypes: [],
importerType: '',
importerFile: {},
importerSourceInfo: {},
importerSourceInfo: null,
collections: [],
collectionMetadata: [],
collectionId: undefined
}
},
methods: {
@ -132,10 +158,14 @@ export default {
'updateImporter',
'updateImporterFile',
'fetchImporterSourceInfo',
'updateImporterCollection',
'runImporter'
]),
...mapActions('collection', [
'fetchCollectionsForParent',
'fetchCollectionsForParent'
]),
...mapActions('metadata', [
'fetchMetadata'
]),
createImporter() {
// Puts loading on Draft Importer creation
@ -169,7 +199,7 @@ export default {
this.fetchImporterSourceInfo(this.sessionId)
.then(importerSourceInfo => {
this.importerSourceInfo = importerSourceInfo;
console.log(this.importerSourceInfo);
this.mappedCollection['total_items'] = this.importerSourceInfo.source_total_items;
})
.catch((errors) => {
this.$console.log(errors);
@ -179,14 +209,35 @@ export default {
this.$console.log(errors);
});
},
runImporter() {
this.runImporter({ sessionId: this.sessionId })
.then(backgroundProcess => {
this.$console.log(backgroundProcess);
})
.catch((errors) => {
this.$console.log(errors);
});
checkIfMetadatumIsAvailable(metadatumId) {
return this.mappedCollection['mapping'][metadatumId] != undefined;
},
onRunImporter() {
if (this.importer.manual_collection) {
this.updateImporterCollection({ sessionId: this.sessionId, collection: this.mappedCollection })
.then(updatedImporter => {
this.importer = updatedImporter;
this.runImporter(this.sessionId)
.then(backgroundProcess => {
this.$console.log(backgroundProcess);
})
.catch((errors) => {
this.$console.log(errors);
});
})
.catch((errors) => {
this.$console.log(errors);
});
} else {
this.runImporter(this.sessionId)
.then(backgroundProcess => {
this.$console.log(backgroundProcess);
})
.catch((errors) => {
this.$console.log(errors);
});
}
},
loadCollections() {
// Generates options for target collection
@ -200,11 +251,31 @@ export default {
this.$console.error(error);
this.isFetchingCollections = false;
});
},
onSelectCollection(collectionId) {
this.collectionId = collectionId;
this.mappedCollection['id'] = collectionId;
// Generates options for metadata listing
this.isFetchingCollectionMetadata = true;
this.fetchMetadata({collectionId: this.collectionId, isRepositoryLevel: false, isContextEdit: false })
.then((metadata) => {
this.collectionMetadata = metadata;
this.isFetchingCollectionMetadata = false;
})
.catch((error) => {
this.$console.error(error);
this.isFetchingCollectionMetadata = false;
});
},
onSelectCollectionMetadata(selectedMetadatum, sourceMetadatum) {
this.isFetchingCollectionMetadata = true;
this.mappedCollection['mapping'][selectedMetadatum] = sourceMetadatum;
this.isFetchingCollectionMetadata = false;
}
},
created(){
this.importerType = this.$route.params.importerSlug;
this.createImporter();
}
@ -228,6 +299,16 @@ export default {
line-height: 1.2em;
}
.source-metadatum {
padding: 2px 0;
border-bottom: 1px solid $tainacan-input-background;
width: 100%;
margin-bottom: 6px;
display: flex;
justify-content: space-between;
align-items: center;
}
.drop-inner{
padding: 1rem 3rem;
}

View File

@ -46,6 +46,7 @@ html {
// Link (hover is the same color as secondary
a:hover {
color: $secondary;
//text-decoration: underline;
}
// Page settings

View File

@ -56,7 +56,7 @@ return apply_filters('tainacan-admin-i18n',[
'new_blank_collection' => __( 'New Blank Collection', 'tainacan' ),
'split' => __( 'Split', 'tainacan' ),
'unified' => __( 'Unified', 'tainacan' ),
'add_another_search_criteria' => __( 'Add another search criteria', 'tainacan' ),
'add_more_one_search_criteria' => __( 'Add more one search criteria', 'tainacan' ),
'clear_search' => __( 'Clear search', 'tainacan' ),
'run' => __( 'Run', 'tainacan' ),
@ -148,7 +148,7 @@ return apply_filters('tainacan-admin-i18n',[
'label_yes' => __( 'Yes', 'tainacan' ),
'label_no' => __( 'No', 'tainacan' ),
'label_approved' => __( 'Approved', 'tainacan' ),
'label_related_collection' => __( 'Related Collection', 'tainacan' ),
'label_collection_related' => __( 'Collection Related', 'tainacan' ),
'label_metadata_for_search' => __( 'Metadata for search', 'tainacan' ),
'label_allow_repeated_items' => __( 'Allow repeated items', 'tainacan' ),
'label_select_taxonomy' => __( 'Select taxonomy', 'tainacan' ),
@ -226,6 +226,7 @@ return apply_filters('tainacan-admin-i18n',[
'label_target_collection' => __( 'Target Collection', 'tainacan' ),
'label_source_file' => __( 'Source file', 'tainacan' ),
'label_metadata_mapping' => __( 'Metadata mapping', 'tainacan' ),
'label_select_metadatum' => __( 'Select metadatum', 'tainacan' ),
// Instructions. More complex sentences to guide user and placeholders
'instruction_delete_selected_collections' => __( 'Delete selected collections', 'tainacan' ),
@ -284,7 +285,7 @@ return apply_filters('tainacan-admin-i18n',[
'info_warning_selected_items_delete' => __( 'Do you really want to permanently delete the selected items?', 'tainacan' ),
'info_warning_selected_items_trash' => __( 'Do you really want to trash the selected items?', 'tainacan' ),
'info_warning_selected_taxonomies_delete' => __( 'Do you really want to delete the selected taxonomies?', 'tainacan' ),
'info_warning_collection_related' => __( 'The metadata related collection is required', 'tainacan' ),
'info_warning_collection_related' => __( 'The metadata Collection related is required', 'tainacan' ),
'info_warning_no_metadata_found' => __( 'No metadata found in this collection', 'tainacan' ),
'info_showing_items' => __( 'Showing items ', 'tainacan' ),
'info_showing_collections' => __( 'Showing collections ', 'tainacan' ),
@ -322,6 +323,9 @@ return apply_filters('tainacan-admin-i18n',[
'info_target_collection_helper' => __( 'The collection to which imported data will be send.', 'tainacan' ),
'info_source_file_upload' => __( 'The file containing the data to be imported.', 'tainacan' ),
'info_metadata_mapping_helper' => __( 'Map each file metadata with the corresponding one in selected collection.', 'tainacan' ),
'info_upload_a_source_to_see_metadata' => __( 'Upload a source file to load metadata.', 'tainacan' ),
'info_select_collection_to_list_metadata' => __( 'Select a target collection to list metadata.', 'tainacan' ),
// Tainacan Metadatum Types
'tainacan-text' => __( 'Text', 'tainacan' ),
'tainacan-textarea' => __( 'Text area', 'tainacan' ),

View File

@ -56,7 +56,7 @@ class REST_Importers_Controller extends REST_Controller {
'description' => __( 'The URL to be used by the importer', 'tainacan' ),
],
'collection' => [
'type' => 'array',
'type' => 'array/object',
'description' => __( 'The array describing the destination collectino as expected by the importer', 'tainacan' ),
],
'options' => [
@ -272,7 +272,7 @@ class REST_Importers_Controller extends REST_Controller {
}
public function run() {
public function run($request) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
@ -286,8 +286,16 @@ class REST_Importers_Controller extends REST_Controller {
global $Tainacan_Importer_Handler;
$process = $Tainacan_Importer_Handler->add_to_queue($importer);
if (false === $process) {
return new \WP_REST_Response([
'error_message' => __('Error starting importer', 'tainacan' ),
'session_id' => $session_id
], 400);
}
$response = [
'bg_process_id' => $process->get_id()
'bg_process_id' => $process->ID
];
return new \WP_REST_Response( $response, 200 );

View File

@ -36,7 +36,7 @@ abstract class Background_Process extends \WP_Background_Process {
* ID of the process in the database
* @var false|int
*/
protected $ID = false;
public $ID = false;
/**
* Prefix

View File

@ -35,8 +35,12 @@ class Importer_Handler {
function add_to_queue(\Tainacan\Importer\Importer $importer_object) {
$data = $importer_object->_to_Array(true);
$importer_object = $this->bg_importer->data($data)->save()->dispatch();
return $importer_object;
$bg_process = $this->bg_importer->data($data)->save();
if ( is_wp_error($bg_process->dispatch()) ) {
return false;
}
return $bg_process;
}
/**

View File

@ -61,7 +61,7 @@ abstract class Importer {
* Example of the structure of this propery for one collection:
* 0 => [
* 'id' => 12,
* 'map' => [
* 'mapping' => [
* 30 => 'column1'
* 31 => 'column2'
* ],
@ -774,7 +774,7 @@ abstract class Importer {
$collections = $this->get_collections();
$collection_definition = isset($collections[$collection_index]) ? $collections[$collection_index] : false;
if ( !$collection_definition || !is_array($collection_definition) || !isset($collection_definition['id']) || !isset($collection_definition['map']) ) {
if ( !$collection_definition || !is_array($collection_definition) || !isset($collection_definition['id']) || !isset($collection_definition['mapping']) ) {
$this->add_error_log('Collection misconfigured');
return false;
}
@ -790,7 +790,7 @@ abstract class Importer {
if( is_array( $processed_item ) ){
foreach ( $processed_item as $metadatum_source => $values ){
$tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['map'] );
$tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['mapping'] );
$metadatum = $Tainacan_Metadata->fetch( $tainacan_metadatum_id );
if( $metadatum instanceof Entities\Metadatum ){

View File

@ -134,7 +134,7 @@ class Old_Tainacan extends Importer{
$this->add_collection([
'id' => $collection_id,
'map' => $map,
'mapping' => $map,
'total_items' => $this->get_total_items_from_source( $collection->ID ),
'source_id' => $collection->ID,
'items' => $this->get_all_items( $collection->ID )
@ -209,8 +209,8 @@ class Old_Tainacan extends Importer{
foreach( $metadata_old as $metadatum ){
if( isset($metadatum->id) && array_search($metadatum->id,$collection_id['map']) ){
$new_tainacanid = array_search($metadatum->id,$collection_id['map']);
if( isset($metadatum->id) && array_search($metadatum->id,$collection_id['mapping']) ){
$new_tainacanid = array_search($metadatum->id,$collection_id['mapping']);
$newMetadatum = new Entities\Metadatum($new_tainacanid);
$item_metadata = new Entities\Item_Metadata_Entity( $item, $newMetadatum );

View File

@ -202,7 +202,7 @@ class Test_Importer extends Importer {
$this->add_collection([
'id' => $col1->get_id(),
'map' => $col1_map,
'mapping' => $col1_map,
'total_items' => $this->get_col1_number_of_items(),
'source_id' => 'col1'
]);
@ -232,7 +232,7 @@ class Test_Importer extends Importer {
$this->add_collection([
'id' => $col2->get_id(),
'map' => $col2_map,
'mapping' => $col2_map,
'total_items' => $this->get_col2_number_of_items(),
'source_id' => 'col2'
]);

View File

@ -63,6 +63,23 @@ export const updateImporter = ( { commit }, { sessionId, options }) => {
});
};
export const updateImporterCollection = ( { commit }, { sessionId, collection }) => {
return new Promise(( resolve, reject ) => {
console.log(collection)
axios.tainacan.put('importers/session/' + sessionId, {
collection: collection
})
.then( res => {
let importer = res.data;
commit('setImporter', importer);
resolve( importer );
})
.catch(error => {
reject(error);
});
});
};
export const updateImporterFile = ( { commit }, { sessionId, file }) => {
return new Promise(( resolve, reject ) => {
@ -99,7 +116,7 @@ export const fetchImporterSourceInfo = ({ commit }, sessionId ) => {
});
};
export const runImporter = ( { commit } , { importerId }) => {
export const runImporter = ( { commit } , importerId ) => {
return new Promise(( resolve, reject ) => {
axios.tainacan.post('importers/session/' + importerId + '/run')

View File

@ -1,5 +1,4 @@
import axios from '../../../axios/axios';
import qs from 'qs';
export const fetchMetadata = ({commit}, {collectionId, isRepositoryLevel, isContextEdit}) => {
return new Promise((resolve, reject) => {