From a798caca911390bc48c059d47208adffaf3a9bce Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Tue, 21 Aug 2018 21:04:36 -0300 Subject: [PATCH] Adapt taxonomies filters for facets endpoints (ref. #94) --- .../class-tainacan-rest-facets-controller.php | 22 ++++++- .../filter-types/taxonomy/Checkbox.vue | 12 ---- .../filter-types/taxonomy/Selectbox.vue | 59 +++++++------------ .../filter-types/taxonomy/Taginput.vue | 58 +++++++----------- 4 files changed, 61 insertions(+), 90 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-facets-controller.php b/src/api/endpoints/class-tainacan-rest-facets-controller.php index 21a39d236..6d67c3cb0 100644 --- a/src/api/endpoints/class-tainacan-rest-facets-controller.php +++ b/src/api/endpoints/class-tainacan-rest-facets-controller.php @@ -120,11 +120,27 @@ class REST_Facets_Controller extends REST_Controller { } } else { + $metadatum_id = $metadatum->get_id(); + $offset = ''; + $number = ''; + if($request['offset'] >= 0 && $request['number'] >= 1){ + $offset = $request['offset']; + $number = $request['number']; + } - if($request['collection_id']) { - $response = $this->metadatum_repository->fetch_all_metadatum_values( $request['collection_id'], $metadatum->get_id() ); + if($request['search']){ + if($collection_id) { + $response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, $request['search'], $offset, $number ); + } else { + $response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, $request['search'], $offset, $number); + } + } else { - $response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum->get_id() ); + if($collection_id) { + $response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, '', $offset, $number); + } else { + $response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, '', $offset, $number); + } } } diff --git a/src/classes/filter-types/taxonomy/Checkbox.vue b/src/classes/filter-types/taxonomy/Checkbox.vue index ba0954649..9129ab700 100644 --- a/src/classes/filter-types/taxonomy/Checkbox.vue +++ b/src/classes/filter-types/taxonomy/Checkbox.vue @@ -98,18 +98,6 @@ } }, methods: { - getValuesTaxonomy( taxonomy ){ - return axios.get(`/taxonomy/${taxonomy}/terms?hideempty=0&order=asc&parent=0&number=${this.filter.max_options}`) - .then( res => { - for (let item of res.data) { - this.taxonomy = item.taxonomy; - this.options.push(item); - } - }) - .catch(error => { - this.$console.log(error); - }); - }, loadOptions(){ this.isLoading = true; diff --git a/src/classes/filter-types/taxonomy/Selectbox.vue b/src/classes/filter-types/taxonomy/Selectbox.vue index 9e5aabacc..9637786c3 100644 --- a/src/classes/filter-types/taxonomy/Selectbox.vue +++ b/src/classes/filter-types/taxonomy/Selectbox.vue @@ -12,8 +12,8 @@ + :label="option.label" + :value="option.value">{{ option.label }} @@ -63,44 +63,25 @@ export default { } }, methods: { - getValuesTaxonomy(taxonomy) { - return axios - .get("/taxonomy/" + taxonomy + "/terms?hideempty=0&order=asc") - .then(res => { - for (let item of res.data) { - this.taxonomy = item.taxonomy; - this.options.push(item); - } - }) - .catch(error => { - this.$console.log(error); - }); - }, loadOptions() { - let promise = null; this.isLoading = true; - axios - .get("/collection/" + this.collection + "/metadata/" + this.metadatum) - .then(res => { - let metadatum = res.data; - promise = this.getValuesTaxonomy( - metadatum.metadata_type_options.taxonomy_id - ); + axios.get('/collection/'+ this.collection +'/facets/' + this.metadatum + `?hideempty=0&order=asc`) + .then( res => { - promise - .then(() => { - this.isLoading = false; - this.selectedValues(); - }) - .catch(error => { - this.$console.log("error select", error); - this.isLoading = false; - }); - }) - .catch(error => { - this.$console.log(error); - }); + for (let item of res.data) { + this.taxonomy = item.taxonomy; + this.taxonomy_id = item.taxonomy_id; + this.options.push(item); + } + + this.isLoading = false; + this.selectedValues(); + }) + .catch(error => { + this.$console.log(error); + this.isLoading = false; + }); }, getOptions(parent, level = 0) { // retrieve only ids @@ -111,7 +92,7 @@ export default { term["level"] = level; result.push(term); const levelTerm = level + 1; - const children = this.getOptions(term.id, levelTerm); + const children = this.getOptions(term.value, levelTerm); result = result.concat(children); } } @@ -148,12 +129,12 @@ export default { }); let valueIndex = this.options.findIndex( - option => option.id == this.selected + option => option.value == this.selected ); if (valueIndex >= 0) { this.$eventBusSearch.$emit("sendValuesToTags", { filterId: this.filter.id, - value: this.options[valueIndex].name + value: this.options[valueIndex].label }); } } diff --git a/src/classes/filter-types/taxonomy/Taginput.vue b/src/classes/filter-types/taxonomy/Taginput.vue index daa6a1a7c..b7c241fed 100644 --- a/src/classes/filter-types/taxonomy/Taginput.vue +++ b/src/classes/filter-types/taxonomy/Taginput.vue @@ -124,53 +124,38 @@ }, methods: { search( query ){ - let promise = null; + this.isLoading = true; this.options = []; - const q = query; - const endpoint = this.isRepositoryLevel ? '/metadata/' + this.metadatum : '/collection/'+ this.collection +'/metadata/' + this.metadatum; + let endpoint = this.isRepositoryLevel ? '/facets/' + this.metadatum : '/collection/'+ this.collection +'/facets/' + this.metadatum; - axios.get(endpoint) - .then( res => { - let metadatum = res.data; - promise = this.getValuesTaxonomy( metadatum.metadata_type_options.taxonomy_id, q ); - this.isLoading = true; - promise.then( () => { - this.isLoading = false; - }) - .catch( error => { - this.$console.log('error select', error ); - this.isLoading = false; - }); - }) - .catch(error => { - this.$console.log(error); - }); - }, - getValuesTaxonomy( taxonomy, query ){ + endpoint += '?hideempty=0&order=asc'; let valuesToIgnore = []; - for(let val of this.selected) + for(let val of this.selected){ valuesToIgnore.push( val.value ); - - return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0&order=asc' ).then( res => { + } + + return axios.get(endpoint).then( res => { for (let term of res.data) { if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { - let indexToIgnore = valuesToIgnore.findIndex(value => value == term.id); + let indexToIgnore = valuesToIgnore.findIndex(value => value == term.value); if (indexToIgnore < 0) { - if( term.name.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ){ + if( term.label.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ){ this.taxonomy = term.taxonomy; - this.options.push({label: term.name, value: term.id}); + this.options.push({label: term.label, value: term.value}); } } } else { - if( term.name.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ){ + if( term.label.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ){ this.taxonomy = term.taxonomy; - this.options.push({label: term.name, value: term.id}); + this.options.push({label: term.label, value: term.value}); } } } + this.isLoading = false; }) .catch(error => { + this.isLoading = false; this.$console.log(error); }); }, @@ -189,13 +174,14 @@ } }, getTerm( taxonomy, id ){ - return axios.get('/taxonomy/' + taxonomy + '/terms/' + id + '?order=asc&hideempty=0' ) - .then( res => { - this.selected.push({ label: res.data.name, value: res.data.id }) - }) - .catch(error => { - this.$console.log(error); - }); + //getting a specific value from api, does not need be in fecat api + return axios.get('/taxonomy/' + taxonomy + '/terms/' + id + '?order=asc&hideempty=0' ) + .then( res => { + this.selected.push({ label: res.data.name, value: res.data.id }) + }) + .catch(error => { + this.$console.log(error); + }); } } }