Merge pull request #750 from tainacan/feature/740

Feat: add title and description to repository level advanced search
This commit is contained in:
Mateus Machado Luna 2022-11-24 10:58:39 -03:00 committed by GitHub
commit 1b55a6e183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 27 deletions

View File

@ -410,7 +410,6 @@ class REST_Items_Controller extends REST_Controller {
return $rest_response; return $rest_response;
} }
/** /**
* @param array $args array of query arguments. * @param array $args array of query arguments.
* *

View File

@ -356,6 +356,10 @@ class Items extends Repository {
$args = $this->parse_relationship_metaquery($args); $args = $this->parse_relationship_metaquery($args);
} }
if ( !defined('TAINACAN_DISABLE_CORE_METADATA_ON_ADVANCED_SEARCH') || false === TAINACAN_DISABLE_CORE_METADATA_ON_ADVANCED_SEARCH ) {
$args = $this->parse_core_metadata_for_advanced_search($args, $collections_objects);
}
$args = apply_filters( 'tainacan-fetch-args', $args, 'items' ); $args = apply_filters( 'tainacan-fetch-args', $args, 'items' );
$should_filter = is_user_logged_in() && sizeof($cpt) > 1; $should_filter = is_user_logged_in() && sizeof($cpt) > 1;
@ -757,4 +761,76 @@ class Items extends Repository {
return $where; return $where;
} }
/**
* checks if there is `tainacan_core_title` or `tainacan_core_description` as a key for a meta_query,
* and replaces the ids of the metadata referring to `title_core` and `description_core`.
*
* @param array $args WP_Query args
* @param array $collections Array \Taainacan\Entities\Collection
*
* @return \WP_Query|Array;
*/
private function parse_core_metadata_for_advanced_search($args, $collections = [])
{
if (
isset($args["meta_query"]) &&
!empty($args["meta_query"]) &&
is_array($args["meta_query"])
) {
$core_title_meta_query = [];
$core_description_meta_query = [];
foreach ($args["meta_query"] as $key => $meta_query) {
// Finds a special key value, that should represent all core title or core description
if (
isset($meta_query["key"]) &&
($meta_query["key"] === "tainacan_core_title" ||
$meta_query["key"] === "tainacan_core_description")
) {
// Gets every collection to build the OR query
if( empty($collections) ) {
$collections = \Tainacan\Repositories\Collections::get_instance()->fetch(
[],
"OBJECT"
);
}
foreach ($collections as $collection) {
if ($meta_query["key"] === "tainacan_core_title") {
$title_meta = $collection->get_core_title_metadatum();
// Builds inner meta_queries for each collection, using the same settings of the special one
$core_title_meta_query[] = [
"key" => $title_meta->get_id(),
"compare" => $meta_query["compare"],
"value" => $meta_query["value"],
];
} elseif (
$meta_query["key"] === "tainacan_core_description"
) {
$description_meta = $collection->get_core_description_metadatum();
// Builds inner meta_queries for each collection, using the same settings of the special one
$core_description_meta_query[] = [
"key" => $description_meta->get_id(),
"compare" => $meta_query["compare"],
"value" => $meta_query["value"],
];
}
}
unset($args["meta_query"][$key]);
}
}
if (count($core_title_meta_query)) {
$core_title_meta_query["relation"] = "OR";
$args["meta_query"][] = $core_title_meta_query;
}
if (count($core_description_meta_query)) {
$core_description_meta_query["relation"] = "OR";
$args["meta_query"][] = $core_description_meta_query;
}
}
return $args;
}
} }

View File

@ -18,7 +18,7 @@
<span <span
style="margin-left: 10px" style="margin-left: 10px"
v-if="totalPages > 1 && allItemsOnPageSelected && items.length > 1"> v-if="totalPages > 1 && allItemsOnPageSelected && Array.isArray(items) && items.length > 1">
<b-checkbox <b-checkbox
v-model="isAllItemsSelected"> v-model="isAllItemsSelected">
{{ $i18n.getWithVariables('label_select_all_%s_items', [totalItems]) }} {{ $i18n.getWithVariables('label_select_all_%s_items', [totalItems]) }}
@ -56,7 +56,7 @@
<b-dropdown <b-dropdown
:mobile-modal="true" :mobile-modal="true"
position="is-bottom-left" position="is-bottom-left"
v-if="items.length > 0" v-if="Array.isArray(items) && items.length > 0"
:disabled="selectedItems.length <= 1" :disabled="selectedItems.length <= 1"
id="bulk-actions-dropdown" id="bulk-actions-dropdown"
aria-role="list" aria-role="list"
@ -1405,10 +1405,12 @@ export default {
return this.selectedItems.length > 0; return this.selectedItems.length > 0;
}, },
allItemsOnPageSelected() { allItemsOnPageSelected() {
for (var i = 0; i < this.items.length; i++){ if ( this.items && Array.isArray(this.items) ) {
for (let i = 0; i < this.items.length; i++){
if (this.selectedItems.indexOf(this.items[i].id) === -1) if (this.selectedItems.indexOf(this.items[i].id) === -1)
return false; return false;
} }
}
return true; return true;
}, },
itemsPerPage(){ itemsPerPage(){

View File

@ -284,9 +284,55 @@
}).then((resp) => { }).then((resp) => {
resp.request resp.request
.then((metadata) => { .then((metadata) => {
this.metadataAsArray = JSON.parse(JSON.stringify(metadata));
// In repository level, if set, we add fake options to search on every title and description
if (this.isRepositoryLevel && tainacan_plugin.tainacan_enable_core_metadata_on_advanced_search == true) {
this.metadataAsArray.unshift({
collection_id: 'default',
id: 'tainacan_core_description',
metadata_section_id: 'default_section',
metadata_type: 'Tainacan\\Metadata_Types\\Core_Description',
metadata_type_object: {
className: "Tainacan\\Metadata_Types\\Core_Description",
component: "tainacan-textarea",
core: true,
errors: null,
form_component: "tainacan-form-textarea",
name: this.$i18n.get('label_core_description'),
},
metadata_type_options: [],
name: this.$i18n.get('label_core_description'),
parent: 0,
repository_level: null,
slug: 'tainacan-core-description'
});
this.metadataAsArray.unshift({
collection_id: 'default',
id: 'tainacan_core_title',
metadata_section_id: 'default_section',
metadata_type: 'Tainacan\\Metadata_Types\\Core_Title',
metadata_type_object: {
className: "Tainacan\\Metadata_Types\\Core_Title",
component: "tainacan-text",
core: true,
errors: null,
form_component: "tainacan-form-text",
name: this.$i18n.get('label_core_title'),
},
metadata_type_options: [],
name: this.$i18n.get('label_core_title'),
parent: 0,
repository_level: null,
slug: 'tainacan-core-title'
});
}
// We create and object keyed by IDs to easily match the query params, // We create and object keyed by IDs to easily match the query params,
// but keep an array version to use the order in the select // but keep an array version to use the order in the select
this.metadataAsArray = JSON.parse(JSON.stringify(metadata));
metadata.forEach(metadatum => { metadata.forEach(metadatum => {
this.metadataAsObject[metadatum.id] = metadatum; this.metadataAsObject[metadatum.id] = metadatum;
}); });
@ -591,7 +637,7 @@
#advanced-search-container { #advanced-search-container {
width: calc(100% - (2 * var(--tainacan-one-column))); width: calc(100% - (2 * var(--tainacan-one-column)));
margin: 0 var(--tainacan-one-column) 0.875em; margin: 0 var(--tainacan-one-column) 0.875em;
background: var(--tainacan-input-background-color); background: var(--tainacan-background-color);
border: 1px solid var(--tainacan-input-border-color); border: 1px solid var(--tainacan-input-border-color);
border-radius: 1px; border-radius: 1px;
transition: height 0.2s ease; transition: height 0.2s ease;

View File

@ -327,6 +327,7 @@ class Admin {
'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE, 'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE,
'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(), 'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(),
'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key"), 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key"),
'tainacan_enable_core_metadata_on_advanced_search' => ( !defined('TAINACAN_DISABLE_CORE_METADATA_ON_ADVANCED_SEARCH') || false === TAINACAN_DISABLE_CORE_METADATA_ON_ADVANCED_SEARCH ),
'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) 'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY )
]; ];