diff --git a/src/admin/components/lists/items-list.vue b/src/admin/components/lists/items-list.vue index b84ad1ef9..c2caac3a0 100644 --- a/src/admin/components/lists/items-list.vue +++ b/src/admin/components/lists/items-list.vue @@ -40,7 +40,7 @@ {{ $i18n.get('label_edit_selected_items') }} @@ -119,7 +119,8 @@ v-if="item.current_user_can_edit" class="actions-area" :label="$i18n.get('label_actions')"> - @@ -203,7 +204,8 @@ v-if="item.current_user_can_edit" class="actions-area" :label="$i18n.get('label_actions')"> - @@ -273,7 +275,8 @@ v-if="item.current_user_can_edit" class="actions-area" :label="$i18n.get('label_actions')"> - @@ -388,8 +391,8 @@ autoHide: false, placement: 'auto-start' }" - v-for="(column, index) in tableMetadata" - :key="index" + v-for="(column, columnIndex) in tableMetadata" + :key="columnIndex" v-if="collectionId != undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')" @click="onClickItem($event, item, index)" v-html="item.metadata != undefined ? renderMetadata(item.metadata, column) : ''" /> @@ -400,8 +403,8 @@ autoHide: false, placement: 'auto-start' }" - v-for="(column, index) in tableMetadata" - :key="index" + v-for="(column, columnIndex) in tableMetadata" + :key="columnIndex" v-if="collectionId == undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')" @click="onClickItem($event, item, index)" v-html="item.title != undefined ? item.title : ''" /> @@ -411,7 +414,8 @@ v-if="item.current_user_can_edit" class="actions-area" :label="$i18n.get('label_actions')"> - @@ -607,7 +611,8 @@ class="actions-cell" :label="$i18n.get('label_actions')">
- @@ -858,10 +863,19 @@ export default { }); }, onClickItem($event, item, index) { - if ($event.ctrlKey) { + if ($event.ctrlKey || $event.shiftKey) { this.$set(this.selectedItems, index, !this.selectedItems[index]); } else { - this.$router.push(this.$routerHelper.getItemPath(item.collection_id, item.id)); + if(this.isOnTrash){ + this.$toast.open({ + duration: 3000, + message: this.$i18n.get('info_warning_remove_from_trash_first'), + position: 'is-bottom', + type: 'is-warning' + }); + } else { + this.$router.push(this.$routerHelper.getItemPath(item.collection_id, item.id)); + } } }, goToItemEditPage(item) { diff --git a/src/admin/components/other/checkbox-filter-modal.vue b/src/admin/components/other/checkbox-filter-modal.vue index b91d1de24..342a87d2e 100644 --- a/src/admin/components/other/checkbox-filter-modal.vue +++ b/src/admin/components/other/checkbox-filter-modal.vue @@ -438,8 +438,6 @@ applyFilter() { this.$parent.close(); - let selectedOptions = []; - if(this.isTaxonomy){ this.$eventBusSearch.$emit('input', { filter: 'checkbox', @@ -448,24 +446,7 @@ metadatum_id: this.metadatum_id, collection_id: this.collection_id, terms: this.selected - }); - - for (let selected of this.selected) { - for(let i in this.finderColumns){ - let valueIndex = this.finderColumns[i].findIndex(option => option.value == selected); - - if (valueIndex >= 0) { - selectedOptions.push(this.finderColumns[i][valueIndex]); - } - } - } - - this.$eventBusSearch.$emit('sendValuesToTags', { - filterId: this.filter.id, - value: selectedOptions, - }); - - this.$emit('appliedCheckBoxModal', selectedOptions); + }); } else { this.$eventBusSearch.$emit('input', { filter: 'checkbox', @@ -474,36 +455,9 @@ collection_id: this.collection_id ? this.collection_id : this.filter.collection_id, value: this.selected, }); - - // if(!isNaN(this.selected[0])){ - // for (let option of this.options) { - // let valueIndex = this.selected.findIndex(item => item == option.value); - - // if (valueIndex >= 0) { - // selectedOptions.push(this.options[valueIndex].label); - // } - // } - // } - if(Array.isArray(this.selected)){ - for (let aSelected of this.selected) { - let valueIndex = this.options.findIndex(option => option.value == aSelected); - - if (valueIndex >= 0) { - selectedOptions.push(this.options[valueIndex]); - } - } - } else { - let valueIndex = this.options.findIndex(option => option.value == this.selected); - - if (valueIndex >= 0) { - selectedOptions.push(this.options[valueIndex]); - } - } - - this.$emit('appliedCheckBoxModal', selectedOptions); } - + this.$emit('appliedCheckBoxModal'); } } } diff --git a/src/admin/pages/lists/items-page.vue b/src/admin/pages/lists/items-page.vue index 0ef94e3d1..79ed7a410 100644 --- a/src/admin/pages/lists/items-page.vue +++ b/src/admin/pages/lists/items-page.vue @@ -271,7 +271,7 @@
- +
diff --git a/src/admin/tainacan-admin-i18n.php b/src/admin/tainacan-admin-i18n.php index b889e6ad4..5627e3b6a 100644 --- a/src/admin/tainacan-admin-i18n.php +++ b/src/admin/tainacan-admin-i18n.php @@ -385,6 +385,7 @@ return apply_filters( 'tainacan-admin-i18n', [ 'info_showing_taxonomies' => __( 'Showing taxonomies ', 'tainacan' ), 'info_showing_events' => __( 'Showing events ', 'tainacan' ), 'info_showing_processes' => __( 'Showing processes ', 'tainacan' ), + 'info_warning_remove_from_trash_first' => __( 'Remove this item from trash first'), 'info_to' => __( ' to ', 'tainacan' ), 'info_of' => __( ' of ', 'tainacan' ), 'info_created_by' => __( 'Created by: ', 'tainacan' ), diff --git a/src/api/endpoints/class-tainacan-rest-facets-controller.php b/src/api/endpoints/class-tainacan-rest-facets-controller.php index 2a9a01fbc..8f6d6e182 100644 --- a/src/api/endpoints/class-tainacan-rest-facets-controller.php +++ b/src/api/endpoints/class-tainacan-rest-facets-controller.php @@ -90,6 +90,8 @@ class REST_Facets_Controller extends REST_Controller { $options = $metadatum->get_metadata_type_options(); $args = $this->prepare_filters($request); + // handle filter with relationship metadata + if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ){ $selected = $this->getRelationshipSelectedValues($request, $metadatum->get_id()); @@ -99,14 +101,13 @@ class REST_Facets_Controller extends REST_Controller { $args['posts_per_page'] = $request['number']; } - if( $selected ){ - //$args['postin'] = $selected; - } - $items = $this->items_repository->fetch($args, $options['collection_id'], 'WP_Query'); + $ids = []; + if ($items->have_posts()) { while ( $items->have_posts() ) { $items->the_post(); + $ids[] = (string) $items->post->ID; $item = new Entities\Item($items->post); $prepared_item = $restItemsClass->prepare_item_for_response($item, $request); @@ -117,10 +118,34 @@ class REST_Facets_Controller extends REST_Controller { wp_reset_postdata(); } + // retrieve selected items + + if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){ + foreach( $selected as $index => $item_id ){ + + if( in_array($item_id,$ids) ){ + continue; + } + + $item = new Entities\Item($item_id); + $prepared_item = $restItemsClass->prepare_item_for_response($item, $request); + $response[$index] = $prepared_item; + + if( isset($request['number']) && ($index+1) >= $request['number']){ + break; + } + } + } + $this->total_items = $items->found_posts; $this->total_pages = ceil($this->total_items / (int) $items->query_vars['posts_per_page']); - } else if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){ + } + + // handle filter with Taxonomy metadata + + else if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){ + $this->taxonomy = $this->taxonomy_repository->fetch($options['taxonomy_id']); $selected = $this->getTaxonomySelectedValues($request, $options['taxonomy_id']); @@ -131,22 +156,42 @@ class REST_Facets_Controller extends REST_Controller { } else { - if( $selected ){ - //$args['include'] = $selected; + $terms = $this->terms_repository->fetch($args, $this->taxonomy); + + // retrieve selected items + + if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){ + $ids = $this->get_terms_ids( $terms ); + + foreach( $selected as $index => $term_id ){ + + if( in_array($term_id,$ids) ){ + continue; + } + + $term_selected = $this->terms_repository->fetch($term_id, $this->taxonomy); + array_unshift($terms, $term_selected); + + if( isset($request['number']) && ($index+1) >= $request['number']){ + break; + } + } } - $terms = $this->terms_repository->fetch($args, $this->taxonomy); $restTermClass = new REST_Terms_Controller(); } - foreach ($terms as $term) { array_push($response, $restTermClass->prepare_item_for_response( $term, $request )); } $this->set_pagination_properties_term_type( $args, $response ); - } else { + } + + // handle filter with Text metadata + + else { $metadatum_id = $metadatum->get_id(); $offset = ''; @@ -174,10 +219,25 @@ class REST_Facets_Controller extends REST_Controller { } } - if($selected){ - //foreach( $selected as $value ){ - //$response[] = ['mvalue' => $value]; - //} + // retrieve selected items + + if( $selected && $request['getSelected'] && $request['getSelected'] === '1'){ + $rawValues = $this->get_values( $response ); + + foreach( $selected as $index => $value ){ + + if( in_array($value,$rawValues) ){ + continue; + } + + $row = ['mvalue' => $value, 'metadatum_id' => $metadatum_id ]; + $response[$index] = $row; + + if( isset($request['number']) && ($index+1) >= $request['number']){ + break; + } + } + } $this->set_pagination_properties_text_type( $collection_id, $metadatum_id, ($request['search']) ? $request['search'] : '' , $offset, $number ); @@ -343,7 +403,7 @@ class REST_Facets_Controller extends REST_Controller { if( isset($request['current_query']['metaquery']) ){ foreach( $request['current_query']['metaquery'] as $metaquery ){ - if( $metaquery['key'] === $metadatum_id ){ + if( $metaquery['key'] == $metadatum_id ){ return $metaquery['value']; @@ -368,7 +428,7 @@ class REST_Facets_Controller extends REST_Controller { if( isset($request['current_query']['metaquery']) ){ foreach( $request['current_query']['metaquery'] as $metaquery ){ - if( $metaquery['key'] === $metadatum_id ){ + if( $metaquery['key'] == $metadatum_id ){ return $metaquery['value']; @@ -379,6 +439,32 @@ class REST_Facets_Controller extends REST_Controller { return []; } + + /** + * + */ + private function get_terms_ids( $terms ){ + $ids = []; + + foreach( $terms as $term ){ + $ids[] = (string) $term->WP_Term->term_id; + } + + return $ids; + } + + /** + * + */ + private function get_values( $rows ){ + $values = []; + + foreach( $rows as $row ){ + $values[] = $row['mvalue']; + } + + return $values; + } } ?> \ No newline at end of file diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index de30eabdd..66892f292 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -139,8 +139,24 @@ class REST_Items_Controller extends REST_Controller { public function prepare_item_for_response( $item, $request ) { if(!empty($item)){ + /** + * Use this filter to add additional post_meta to the api response + * Use the $request object to get the context of the request and other variables + * For example, id context is edit, you may want to add your meta or not. + * + * Also take care to do any permissions verification before exposing the data + */ + $extra_metadata = apply_filters('tainacan-api-response-item-meta', [], $request); + $extra_metadata_values = []; + + foreach ($extra_metadata as $extra_meta) { + $extra_metadata_values[$extra_meta] = get_post_meta($item->get_id(), $extra_meta, true); + } + if(!isset($request['fetch_only'])) { $item_arr = $item->_toArray(); + + $item_arr = array_merge($extra_metadata_values, $item_arr); if ( $request['context'] === 'edit' ) { $item_arr['current_user_can_edit'] = $item->can_edit(); @@ -168,6 +184,8 @@ class REST_Items_Controller extends REST_Controller { } $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); + + $item_arr = array_merge($extra_metadata_values, $item_arr); if(is_array($attributes_to_filter) && array_key_exists('meta', $attributes_to_filter)){ @@ -183,19 +201,6 @@ class REST_Items_Controller extends REST_Controller { $item_arr['url'] = get_permalink( $item_arr['id'] ); $item_arr['exposer_urls'] = \Tainacan\Exposers\Exposers::get_exposer_urls(get_rest_url(null, "{$this->namespace}/{$this->rest_base}/{$item->get_id()}/")); - /** - * Use this filter to add additional post_meta to the api response - * Use the $request object to get the context of the request and other variables - * For example, id context is edit, you may want to add your meta or not. - * - * Also take care to do any permissions verification before exposing the data - */ - $extra_metadata = apply_filters('tainacan-api-response-item-meta', [], $request); - - foreach ($extra_metadata as $extra_meta) { - $item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true); - } - return $item_arr; } diff --git a/src/classes/filter-types/checkbox/Checkbox.vue b/src/classes/filter-types/checkbox/Checkbox.vue index 01c2f2936..735ff1b01 100644 --- a/src/classes/filter-types/checkbox/Checkbox.vue +++ b/src/classes/filter-types/checkbox/Checkbox.vue @@ -1,7 +1,7 @@