Add count items support to facets

This commit is contained in:
leogermani 2018-10-31 15:14:13 -03:00
parent 3177c91cbc
commit cdb002fc40
2 changed files with 51 additions and 4 deletions

View File

@ -66,7 +66,7 @@ class REST_Facets_Controller extends REST_Controller {
$_search = null; $_search = null;
$collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : null; $collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : null;
$query_args = $request['current_query']; $query_args = defined('TAINACAN_FACETS_FILTER_ITEMS') && true === TAINACAN_FACETS_FILTER_ITEMS ? $request['current_query'] : [];
$query_args = $this->prepare_filters($query_args); $query_args = $this->prepare_filters($query_args);
if($request['offset'] >= 0 && $request['number'] >= 1){ if($request['offset'] >= 0 && $request['number'] >= 1){
@ -106,7 +106,8 @@ class REST_Facets_Controller extends REST_Controller {
'offset' => $offset, 'offset' => $offset,
'number' => $number, 'number' => $number,
'items_filter' => $query_args, 'items_filter' => $query_args,
'include' => $include 'include' => $include,
'count_items' => defined('TAINACAN_FACETS_COUNT_ITEMS') && true === TAINACAN_FACETS_COUNT_ITEMS ? true : false
]; ];
$response = $this->metadatum_repository->fetch_all_metadatum_values( $metadatum_id, $args ); $response = $this->metadatum_repository->fetch_all_metadatum_values( $metadatum_id, $args );

View File

@ -895,6 +895,8 @@ class Metadata extends Repository {
* @type array $search String to search. It will only return values that has this string. Default '' (nothing) * @type array $search String to search. It will only return values that has this string. Default '' (nothing)
* *
* @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0 * @type array $parent_id Used by taxonomy metadata. The ID of the parent term to retrieve terms from. Default 0
*
* @type bool $count_items Include the count of items that can be found in each value (uses $items_filter as well). Default false
* *
* } * }
* *
@ -909,7 +911,8 @@ class Metadata extends Repository {
'number' => '', 'number' => '',
'include' => [], 'include' => [],
'items_filter' => [], 'items_filter' => [],
'parent_id' => 0 'parent_id' => 0,
'count_items' => false
); );
$args = wp_parse_args($args, $defaults); $args = wp_parse_args($args, $defaults);
@ -1004,15 +1007,37 @@ class Metadata extends Repository {
$count_query = $wpdb->prepare("SELECT COUNT(term_id) FROM $wpdb->term_taxonomy WHERE parent = %d", $r->term_taxonomy_id); $count_query = $wpdb->prepare("SELECT COUNT(term_id) FROM $wpdb->term_taxonomy WHERE parent = %d", $r->term_taxonomy_id);
$total_children = $wpdb->get_var($count_query); $total_children = $wpdb->get_var($count_query);
$label = $r->name;
$total_items = null;
if ( $args['count_items'] ) {
$count_items_query = $args['items_filter'];
$count_items_query['posts_per_page'] = 1;
if ( !isset($count_items_query['tax_query']) ) {
$count_items_query['tax_query'] = [];
}
$count_items_query['tax_query'][] = [
'taxonomy' => $taxonomy_slug,
'terms' => $r->term_taxonomy_id
];
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
$total_items = $count_items_results->found_posts;
//$label .= " ($total_items)";
}
$values[] = [ $values[] = [
'value' => $r->term_taxonomy_id, 'value' => $r->term_taxonomy_id,
'label' => $r->name, 'label' => $label,
'total_children' => $total_children, 'total_children' => $total_children,
'taxonomy' => $taxonomy_slug, 'taxonomy' => $taxonomy_slug,
'taxonomy_id' => $taxonomy_id, 'taxonomy_id' => $taxonomy_id,
'parent' => $r->parent, 'parent' => $r->parent,
'total_items' => $total_items,
'type' => 'Taxonomy' 'type' => 'Taxonomy'
]; ];
} }
@ -1045,11 +1070,32 @@ class Metadata extends Repository {
$label = ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) ? $label = ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) ?
get_post($r)->post_title : $r; get_post($r)->post_title : $r;
$total_items = null;
if ( $args['count_items'] ) {
$count_items_query = $args['items_filter'];
$count_items_query['posts_per_page'] = 1;
if ( !isset($count_items_query['meta_query']) ) {
$count_items_query['meta_query'] = [];
}
$count_items_query['meta_query'][] = [
'key' => $metadatum_id,
'value' => $r
];
$count_items_results = $itemsRepo->fetch($count_items_query, $args['collection_id']);
$total_items = $count_items_results->found_posts;
//$label .= " ($total_items)";
}
$values[] = [ $values[] = [
'label' => $label, 'label' => $label,
'value' => $r, 'value' => $r,
'total_items' => $total_items,
'type' => 'Text' 'type' => 'Text'
]; ];
} }
} }