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

@ -333,9 +333,9 @@ class REST_Items_Controller extends REST_Controller {
if (! $item instanceof Entities\Item) {
return new \WP_REST_Response([
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
}
$response = $this->prepare_item_for_response($item, $request);
@ -355,9 +355,9 @@ class REST_Items_Controller extends REST_Controller {
if (! $item instanceof Entities\Item) {
return new \WP_REST_Response([
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
}
$args = $this->prepare_filters($request);
@ -410,7 +410,6 @@ class REST_Items_Controller extends REST_Controller {
return $rest_response;
}
/**
* @param array $args array of query arguments.
*
@ -850,10 +849,10 @@ class REST_Items_Controller extends REST_Controller {
$collection = $this->collections_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
return current_user_can($collection->get_items_capabilities()->edit_posts);
}
return current_user_can($collection->get_items_capabilities()->edit_posts);
}
return false;
return false;
}
/**
@ -869,9 +868,9 @@ class REST_Items_Controller extends REST_Controller {
if (! $item instanceof Entities\Item) {
return new \WP_REST_Response([
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
}
if($permanently == true) {

View File

@ -143,13 +143,13 @@ class Items extends Repository {
'title' => __( 'Thumbnail', 'tainacan' ),
'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' )
],
'comment_status' => [
'map' => 'comment_status',
'title' => __( 'Comment Status', 'tainacan' ),
'type' => 'string',
'description' => __( 'Item comment status: "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ),
'default' => get_default_comment_status(Entities\Collection::get_post_type()),
'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )),
'comment_status' => [
'map' => 'comment_status',
'title' => __( 'Comment Status', 'tainacan' ),
'type' => 'string',
'description' => __( 'Item comment status: "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ),
'default' => get_default_comment_status(Entities\Collection::get_post_type()),
'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )),
],
] );
}
@ -356,6 +356,10 @@ class Items extends Repository {
$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' );
$should_filter = is_user_logged_in() && sizeof($cpt) > 1;
@ -757,4 +761,76 @@ class Items extends Repository {
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
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
v-model="isAllItemsSelected">
{{ $i18n.getWithVariables('label_select_all_%s_items', [totalItems]) }}
@ -56,7 +56,7 @@
<b-dropdown
:mobile-modal="true"
position="is-bottom-left"
v-if="items.length > 0"
v-if="Array.isArray(items) && items.length > 0"
:disabled="selectedItems.length <= 1"
id="bulk-actions-dropdown"
aria-role="list"
@ -1405,9 +1405,11 @@ export default {
return this.selectedItems.length > 0;
},
allItemsOnPageSelected() {
for (var i = 0; i < this.items.length; i++){
if (this.selectedItems.indexOf(this.items[i].id) === -1)
return false;
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)
return false;
}
}
return true;
},

View File

@ -284,9 +284,55 @@
}).then((resp) => {
resp.request
.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,
// but keep an array version to use the order in the select
this.metadataAsArray = JSON.parse(JSON.stringify(metadata));
metadata.forEach(metadatum => {
this.metadataAsObject[metadatum.id] = metadatum;
});
@ -591,7 +637,7 @@
#advanced-search-container {
width: calc(100% - (2 * var(--tainacan-one-column)));
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-radius: 1px;
transition: height 0.2s ease;

View File

@ -327,6 +327,7 @@ class Admin {
'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE,
'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(),
'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 )
];