Filters repository level

Adapt filters for repository level
Adapt a function of filter type mixin for repository level
Fixes some bugs and errors
Creates new route and endpoint to get field at repository level
This commit is contained in:
weryques 2018-05-29 09:28:44 -03:00 committed by Mateus Machado Luna
parent fc2ef87a60
commit 2dc057698e
18 changed files with 2634 additions and 2546 deletions

5004
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -46,8 +46,12 @@
mounted(){
let routeQueries = this.$route.query;
if(routeQueries.metaquery && Array.isArray(routeQueries.metaquery[0].value)){
if(routeQueries.metaquery &&
routeQueries.metaquery[0] &&
Array.isArray(routeQueries.metaquery[0].value)){
this.collectionsIdsToFilter = routeQueries.metaquery[0].value;
this.apply_filter();
}
},
data(){

View File

@ -3,7 +3,7 @@
<collections-filter
:open="collapsed"
:query="getQuery"
v-if="repository"/>
v-if="isRepositoryLevel"/>
<tainacan-filter-item
v-show="!isMenuCompressed"
:query="getQuery"
@ -11,7 +11,7 @@
:key="index"
:filter="filter"
:open="collapsed"
:repository="repository"/>
:is-repository-level="isRepositoryLevel"/>
</div>
</template>
@ -23,7 +23,7 @@
props: {
filters: Array,
collapsed: Boolean,
repository: Boolean,
isRepositoryLevel: Boolean,
},
methods: {
...mapGetters('search',[

View File

@ -68,7 +68,7 @@
isRepositoryLevel) || filters.length > 0)"
:filters="filters"
:collapsed="collapseAll"
:repository="isRepositoryLevel"/>
:is-repository-level="isRepositoryLevel"/>
<section
v-else
@ -463,8 +463,8 @@
slug: 'thumbnail',
id: undefined,
display: true
})
;
});
let fetchOnlyFieldIds = [];
for (let field of this.fields) {
@ -533,9 +533,9 @@
},
created() {
this.isOnTheme = (this.$route.name == null);
this.isOnTheme = (this.$route.name === null);
this.isRepositoryLevel = this.collectionId === undefined;
this.isRepositoryLevel = (this.collectionId === undefined);
this.$eventBusSearch.setCollectionId(this.collectionId);
@ -547,13 +547,17 @@
this.hasFiltered = hasFiltered;
});
this.$eventBusSearch.$on('hasToPrepareFieldsAndFilters', () => {
this.prepareFieldsAndFilters();
this.$eventBusSearch.$on('hasToPrepareFieldsAndFilters', (to) => {
/* This condition is to prevent a incorrect fetch by filter or fields when we come from items
* at collection level to items page at repository level
*/
if(this.collectionId === to.params.collectionId) {
this.prepareFieldsAndFilters();
}
});
},
mounted() {
this.prepareFieldsAndFilters();
this.localTableFields = JSON.parse(JSON.stringify(this.tableFields));

View File

@ -110,6 +110,12 @@ class REST_Fields_Controller extends REST_Controller {
'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission' => array($this, 'get_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE)
)
)
);
}
@ -120,15 +126,24 @@ class REST_Fields_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
$collection_id = $request['collection_id'];
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : false;
$field_id = $request['field_id'];
if($request['fetch'] === 'all_field_values' && $request['search']){
$results = $this->field_repository->fetch_all_field_values($collection_id, $field_id, $request['search']);
if($collection_id) {
$results = $this->field_repository->fetch_all_field_values( $collection_id, $field_id, $request['search'] );
} else {
$results = $this->field_repository->fetch_all_field_values( null, $field_id, $request['search']);
}
return new \WP_REST_Response($results, 200);
} elseif($request['fetch'] === 'all_field_values') {
$results = $this->field_repository->fetch_all_field_values($collection_id, $field_id);
if($collection_id) {
$results = $this->field_repository->fetch_all_field_values( $collection_id, $field_id );
} else {
$results = $this->field_repository->fetch_all_field_values( null, $field_id);
}
return new \WP_REST_Response($results, 200);
}
@ -145,14 +160,28 @@ class REST_Fields_Controller extends REST_Controller {
* @throws \Exception
*/
public function get_item_permissions_check( $request ) {
$collection = $this->collection_repository->fetch($request['collection_id']);
$collection_id = isset($request['collection_id']) ? $request['collection_id'] : false;
if($collection instanceof Entities\Collection) {
if ($request['context'] === 'edit' && ! $collection->can_read()) {
return false;
if($collection_id) {
$collection = $this->collection_repository->fetch( $collection_id );
if ( $collection instanceof Entities\Collection ) {
if ( $request['context'] === 'edit' && ! $collection->can_read() ) {
return false;
}
return true;
}
} elseif($request['field_id']) {
$field = $this->field_repository->fetch($request['field_id']);
return true;
if ( $field instanceof Entities\Field ) {
if ( $request['context'] === 'edit' && ! $field->can_read() ) {
return false;
}
return true;
}
}
return false;

View File

@ -116,7 +116,7 @@ class REST_Filters_Controller extends REST_Controller {
$received_type = $body['filter_type'];
if(empty($received_type)){
throw new \InvalidArgument\Exception('The type can\'t be empty');
throw new \InvalidArgumentException('The type can\'t be empty');
} elseif(!strrchr($received_type, '_')){
$received_type = ucfirst(strtolower($received_type));
} else {
@ -141,10 +141,20 @@ class REST_Filters_Controller extends REST_Controller {
$collection_id = $request['collection_id'];
$filter_obj->set_collection_id( $collection_id );
$filter_obj->set_field('');
if(!$body['field']){
throw new \InvalidArgumentException('You need provide a field id');
}
$filter_obj->set_field($body['field']);
} else {
$filter_obj->set_collection_id( 'filter_in_repository' );
$filter_obj->set_field('');
if(!$body['field']){
throw new \InvalidArgumentException('You need provide a field id');
}
$filter_obj->set_field($body['field']);
}
$filter_obj->set_filter_type($filter_type);

View File

@ -50,7 +50,14 @@
this.collection = ( this.collection_id ) ? this.collection_id : this.filter.collection_id;
this.field = ( this.field_id ) ? this.field_id : this.filter.field.field_id;
const vm = this;
axios.get('/collection/' + this.collection + '/fields/' + this.field )
let in_route = '/collection/' + this.collection + '/fields/' + this.field;
if(this.isRepositoryLevel){
in_route = '/fields/'+ this.field;
}
axios.get(in_route)
.then( res => {
let result = res.data;
if( result && result.field_type ){
@ -75,6 +82,9 @@
label: ''
}
},
props: {
isRepositoryLevel: Boolean,
},
mixins: [filter_type_mixin],
methods: {
setResults(option){
@ -101,7 +111,7 @@
promise = this.getValuesRelationship( collectionTarget );
} else {
promise = this.getValuesPlainText( this.field, query );
promise = this.getValuesPlainText( this.field, query, this.isRepositoryLevel );
}
promise.then( () => {

View File

@ -22,9 +22,9 @@
this.field = ( this.field_id ) ? this.field_id : this.filter.field.field_id ;
this.type = ( this.filter_type ) ? this.filter_type : this.filter.field.field_type;
let in_route = '/collection/' + this.collection + '/fields/' + this.field +'?context=edit';
let in_route = '/collection/' + this.isRepositoryLevel + '/fields/' + this.field +'?context=edit';
if(this.repository){
if(this.isRepositoryLevel){
in_route = '/fields?context=edit';
}
@ -57,7 +57,7 @@
query: {
type: Object // concentrate all attributes field id and type
},
repository: Boolean,
isRepositoryLevel: Boolean,
},
watch: {
selected( value ){

View File

@ -24,7 +24,7 @@
let in_route = '/collection/' + this.collection + '/fields/' + this.field +'?nopaging=1';
if(this.repository){
if(this.isRepositoryLevel){
in_route = '/fields?nopaging=1';
}
@ -42,7 +42,7 @@
});
},
props: {
repository: Boolean,
isRepositoryLevel: Boolean,
},
data(){
return {
@ -73,7 +73,7 @@
promise = this.getValuesRelationship( collectionTarget );
} else {
promise = this.getValuesPlainText( this.field );
promise = this.getValuesPlainText( this.field, null, this.isRepositoryLevel );
}
promise.then(() => {

View File

@ -68,7 +68,7 @@
let in_route = '/collection/' + this.collection + '/fields/' + this.field;
if(this.repository){
if(this.isRepositoryLevel){
in_route = '/fields/'+ this.field;
}
@ -108,7 +108,7 @@
collection_id: [Number], // not required, but overrides the filter field id if is set
id: '',
query: Object,
repository: Boolean,
isRepositoryLevel: Boolean,
},
watch: {
isTouched( val ){

View File

@ -12,9 +12,14 @@ export const filter_type_mixin = {
query: {}
},
methods: {
getValuesPlainText(fieldId, search) {
getValuesPlainText(fieldId, search, isRepositoryLevel) {
let url = '/collection/' + this.collection + '/fields/' + fieldId + '?fetch=all_field_values&nopaging=1';
if(isRepositoryLevel){
url = '/fields/' + fieldId + '?fetch=all_field_values&nopaging=1';
}
if( search ){
url += "&search=" + search;
}

View File

@ -30,7 +30,7 @@
let in_route = '/collection/' + this.collection + '/fields/' + this.field;
if(this.repository){
if(this.isRepositoryLevel){
in_route = '/fields/'+ this.field;
}
@ -48,7 +48,7 @@
});
},
props: {
repository: Boolean,
isRepositoryLevel: Boolean,
},
data(){
return {
@ -78,7 +78,7 @@
this.isLoading = true;
let promise = null;
promise = this.getValuesPlainText( this.field );
promise = this.getValuesPlainText( this.field, null, this.isRepositoryLevel );
promise.then(() => {
this.isLoading = false;

View File

@ -26,7 +26,7 @@
let in_route = '/collection/' + this.collection + '/fields/' + this.field;
if(this.repository){
if(this.isRepositoryLevel){
in_route = '/fields?nopaging=1';
}
@ -55,6 +55,9 @@
field_object: {}
}
},
props: {
isRepositoryLevel: Boolean
},
mixins: [filter_type_mixin],
watch: {
selected( value ){
@ -84,7 +87,7 @@
promise = this.getValuesRelationship( collectionTarget, query );
} else {
promise = this.getValuesPlainText( this.field, query );
promise = this.getValuesPlainText( this.field, query, this.isRepositoryLevel );
}
this.isLoading = true;
promise.then(() => {

View File

@ -22,7 +22,7 @@
:is="filter.filter_type_object.component"
:filter="filter"
:query="query"
:repository="repository"
:is-repository-level="isRepositoryLevel"
@input="listen( $event )"/>
</div>
</b-collapse>
@ -37,8 +37,8 @@
props: {
filter: Object,
query: Object,
open: false,
repository: Boolean,
isRepositoryLevel: Boolean,
open: true,
},
data(){
return {

View File

@ -642,6 +642,9 @@ class Fields extends Repository {
}
}
# TODO: Fetch all field value for repository level
/**
* Fetch all values of a field from a collection in all it collection items
*

View File

@ -289,7 +289,7 @@ class Items extends Repository {
* @return mixed|Entities\Item
*/
public function delete( $args ) {
if ( ! empty( $args[1] ) && $args[1] === true ) {
if ( ! empty( $args[1] ) && $args[1] == true ) {
$deleted = new Entities\Item( wp_delete_post( $args[0], $args[1] ) );

View File

@ -26,8 +26,8 @@ export default {
});
},
watch: {
'$route' () {
if (this.$route.params.collectionId)
'$route' (to, from) {
if (this.$route.params.collectionId)
this.collectionId = parseInt(this.$route.params.collectionId);
if (this.$route.name == null || this.$route.name == undefined || this.$route.name == 'CollectionItemsPage' || this.$route.name == 'ItemsPage') {
@ -40,9 +40,9 @@ export default {
if (this.$route.query.orderby == undefined)
this.$route.query.orderby = 'date';
this.$store.dispatch('search/set_postquery', this.$route.query);
this.$store.dispatch('search/set_postquery', this.$route.query);
this.loadItems();
this.loadItems(to);
}
}
@ -119,11 +119,11 @@ export default {
updateStoreFromURL() {
this.$store.dispatch('search/set_postquery', this.$route.query);
},
loadItems() {
loadItems(to) {
// Foreces fetch_only to be filled before any search happens
// Forces fetch_only to be filled before any search happens
if (this.$store.getters['search/getFetchOnly'] == undefined)
this.$emit( 'hasToPrepareFieldsAndFilters');
this.$emit( 'hasToPrepareFieldsAndFilters', to);
else {
this.$emit( 'isLoadingItems', true);
this.$store.dispatch('collection/fetchItems', { 'collectionId': this.collectionId, 'isOnTheme': (this.$route.name == null) })

View File

@ -9,7 +9,7 @@ export const fetchFilters = ({ commit }, {collectionId, isRepositoryLevel, isCon
else
endpoint = '/filters/';
endpoint += '?nopaging=1'
endpoint += '?nopaging=1';
if (isContextEdit) {
endpoint += '&context=edit';
@ -39,12 +39,14 @@ export const sendFilter = ( { commit }, { collectionId, fieldId, name, filterTyp
endpoint = '/collection/' + collectionId + '/field/' + fieldId +'/filters/';
else
endpoint = '/filters/';
axios.tainacan.post(endpoint + '?context=edit', {
filter_type: filterType,
filter: {
name: name,
status: status
}
},
field: fieldId,
})
.then( res => {
let filter = res.data;