allow pagination in facets endpoint (ref. #94)
This commit is contained in:
parent
323da5f0e4
commit
5cec9ccd94
|
@ -13,6 +13,8 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
*/
|
||||
public function __construct() {
|
||||
$this->rest_base = 'facets';
|
||||
$this->total_pages = 0;
|
||||
$this->total_items = 0;
|
||||
parent::__construct();
|
||||
add_action('init', array(&$this, 'init_objects'), 11);
|
||||
}
|
||||
|
@ -64,7 +66,8 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
|
||||
$rest_response = new \WP_REST_Response($response, 200);
|
||||
|
||||
$rest_response->header('X-WP-Total', (int) count($response));
|
||||
$rest_response->header('X-WP-Total', $this->total_items);
|
||||
$rest_response->header('X-WP-TotalPages', $this->total_pages);
|
||||
|
||||
return $rest_response;
|
||||
}
|
||||
|
@ -95,6 +98,10 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
|
||||
$restItemsClass = new REST_Items_Controller();
|
||||
|
||||
if(isset($request['number'])){
|
||||
$args['posts_per_page'] = $request['number'];
|
||||
}
|
||||
|
||||
$items = $this->items_repository->fetch($args, $options['collection_id'], 'WP_Query');
|
||||
if ($items->have_posts()) {
|
||||
while ( $items->have_posts() ) {
|
||||
|
@ -109,6 +116,9 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
$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' ){
|
||||
|
||||
if( isset($request['term_id']) ){
|
||||
|
@ -127,6 +137,8 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
array_push($response, $restTermClass->prepare_item_for_response( $term, $request ));
|
||||
}
|
||||
|
||||
$this->set_pagination_properties_term_type( $args, $response );
|
||||
|
||||
} else {
|
||||
$metadatum_id = $metadatum->get_id();
|
||||
$offset = '';
|
||||
|
@ -153,6 +165,7 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
$this->set_pagination_properties_text_type( $collection_id, $metadatum_id, ($request['search']) ? $request['search'] : '' , $offset, $number );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,6 +236,63 @@ class REST_Facets_Controller extends REST_Controller {
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* set attributes for text metadata
|
||||
*/
|
||||
private function set_pagination_properties_text_type( $collection_id, $metadatum_id, $search , $offset, $number ){
|
||||
$response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, $search);
|
||||
|
||||
if( $response && is_array( $response ) ){
|
||||
|
||||
if ( $offset !== '' && $number) {
|
||||
$per_page = (int) $number;
|
||||
$page = ceil( ( ( (int) $offset ) / $per_page ) + 1 );
|
||||
|
||||
$this->total_items = count( $response );
|
||||
|
||||
$max_pages = ceil( $this->total_items / $per_page );
|
||||
|
||||
$this->total_pages = (int) $max_pages ;
|
||||
} else {
|
||||
$this->total_items = count( $response );
|
||||
$this->total_pages = 1;
|
||||
}
|
||||
} else {
|
||||
$this->total_items = 0;
|
||||
$this->total_pages = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set attributes for term metadata
|
||||
*/
|
||||
private function set_pagination_properties_term_type( $args, $response ){
|
||||
|
||||
if(isset($args['number'], $args['offset'])){
|
||||
$number = $args['number'];
|
||||
$offset = $args['offset'];
|
||||
|
||||
unset( $args['number'], $args['offset'] );
|
||||
$total_terms = wp_count_terms( $this->taxonomy->get_db_identifier(), $args );
|
||||
|
||||
if ( ! $total_terms ) {
|
||||
$total_terms = 0;
|
||||
}
|
||||
|
||||
$per_page = (int) $number;
|
||||
$page = ceil( ( ( (int) $offset ) / $per_page ) + 1 );
|
||||
|
||||
$this->total_items = (int) $total_terms ;
|
||||
|
||||
$max_pages = ceil( $total_terms / $per_page );
|
||||
|
||||
$this->total_pages = (int) $max_pages ;
|
||||
} else{
|
||||
$this->total_items = count($response) ;
|
||||
$this->total_pages = 1 ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -24,7 +24,7 @@ export const filter_type_mixin = {
|
|||
let url = `/collection/${this.collection}/facets/${metadatumId}?`;
|
||||
|
||||
if(offset != undefined && number != undefined){
|
||||
url += `offset=${offset}&number=${number}`;
|
||||
url += `offset=${offset}&number=${number}&`;
|
||||
}
|
||||
|
||||
if(isRepositoryLevel){
|
||||
|
@ -32,7 +32,7 @@ export const filter_type_mixin = {
|
|||
}
|
||||
|
||||
if(search && offset != undefined && number != undefined){
|
||||
url += `&search=${search}&` + qs.stringify(query_items);
|
||||
url += `search=${search}&` + qs.stringify(query_items);
|
||||
} else if(search){
|
||||
url += `search=${search}&` + qs.stringify(query_items);
|
||||
} else {
|
||||
|
@ -107,7 +107,7 @@ export const filter_type_mixin = {
|
|||
let url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + '?';
|
||||
|
||||
if(offset != undefined && number != undefined){
|
||||
url += `offset=${offset}&perpage=${number}`;
|
||||
url += `offset=${offset}&number=${number}`;
|
||||
} else {
|
||||
url += `nopaging=1`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue