refactor get_all_metadata_values method
This commit is contained in:
parent
276c00967b
commit
76451c6e18
|
@ -217,57 +217,60 @@ class REST_Facets_Controller extends REST_Controller {
|
||||||
$metadatum_id = $metadatum->get_id();
|
$metadatum_id = $metadatum->get_id();
|
||||||
$offset = null;
|
$offset = null;
|
||||||
$number = null;
|
$number = null;
|
||||||
$collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : false;
|
$_search = null;
|
||||||
$selected = $this->getTextSelectedValues($request, $metadatum_id);
|
$collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : null;
|
||||||
|
//$selected = $this->getTextSelectedValues($request, $metadatum_id);
|
||||||
|
|
||||||
|
$query_args = $request['current_query'];
|
||||||
|
|
||||||
|
|
||||||
|
$query_args = $this->prepare_filters($query_args);
|
||||||
|
|
||||||
|
|
||||||
if($request['offset'] >= 0 && $request['number'] >= 1){
|
if($request['offset'] >= 0 && $request['number'] >= 1){
|
||||||
$offset = $request['offset'];
|
$offset = $request['offset'];
|
||||||
$number = $request['number'];
|
$number = $request['number'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request['search']){
|
if($request['search']) {
|
||||||
if($collection_id) {
|
$_search = $request['search'];
|
||||||
$response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, $request['search'], $offset, $number );
|
|
||||||
} else {
|
|
||||||
$response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, $request['search'], $offset, $number);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if($collection_id) {
|
|
||||||
$response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, null, $offset, $number);
|
|
||||||
} else {
|
|
||||||
$response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, null, $offset, $number);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rawResponse = $response;
|
$response = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, $_search, $offset, $number, $query_args );
|
||||||
|
|
||||||
|
$this->total_items = $response['total'];
|
||||||
|
$this->total_pages = $response['pages'];
|
||||||
|
return $response['values'];
|
||||||
|
|
||||||
|
|
||||||
|
//$rawResponse = $response;
|
||||||
|
|
||||||
// retrieve selected items
|
// retrieve selected items
|
||||||
|
|
||||||
if( count($selected) && $request['getSelected'] && $request['getSelected'] === '1'){
|
//if( count($selected) && $request['getSelected'] && $request['getSelected'] === '1'){
|
||||||
$rawValues = $this->get_values( $response );
|
// $rawValues = $this->get_values( $response );
|
||||||
$realResponse = [];
|
// $realResponse = [];
|
||||||
|
|
||||||
foreach( $selected as $index => $value ){
|
// foreach( $selected as $index => $value ){
|
||||||
$row = (object) ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
|
// $row = (object) ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
|
||||||
$realResponse[] = $row;
|
// $realResponse[] = $row;
|
||||||
}
|
// }
|
||||||
|
|
||||||
foreach( $rawValues as $index => $row0 ){
|
// foreach( $rawValues as $index => $row0 ){
|
||||||
|
|
||||||
if( !in_array($row0, $selected) ){
|
// if( !in_array($row0, $selected) ){
|
||||||
$realResponse[] = (object) ['mvalue' => $row0, 'metadatum_id' => $metadatum_id];
|
// $realResponse[] = (object) ['mvalue' => $row0, 'metadatum_id' => $metadatum_id];
|
||||||
|
|
||||||
if( isset($request['number']) && count($realResponse) >= $request['number']){
|
// if( isset($request['number']) && count($realResponse) >= $request['number']){
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// $response = $realResponse;
|
||||||
|
//}
|
||||||
|
|
||||||
$response = $realResponse;
|
//$this->set_pagination_properties_text_type( $offset, $number, $rawResponse );
|
||||||
}
|
|
||||||
|
|
||||||
$this->set_pagination_properties_text_type( $offset, $number, $rawResponse );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -885,183 +885,71 @@ class Metadata extends Repository {
|
||||||
* @return array|null|object
|
* @return array|null|object
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function fetch_all_metadatum_values( $collection_id, $metadatum_id, $search = '', $offset = 0, $number = '' ) {
|
public function fetch_all_metadatum_values( $collection_id, $metadatum_id, $search = '', $offset = 0, $number = '', $query_args = [] ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
// Clear the result cache
|
// Get the query for current items
|
||||||
$wpdb->flush();
|
// this avoids wp_query to run the query. We just want to build the query
|
||||||
|
add_filter('posts_pre_query', '__return_empty_array');
|
||||||
|
|
||||||
$metadatum = new Entities\Metadatum( $metadatum_id );
|
$itemsRepo = Items::get_instance();
|
||||||
|
|
||||||
// handle core titles
|
$query_args['fields'] = 'ids';
|
||||||
if ( strpos( $metadatum->get_metadata_type(), 'Core' ) !== false && $search ) {
|
unset($query_args['paged']);
|
||||||
$collection = new Entities\Collection( $collection_id );
|
unset($query_args['offset']);
|
||||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
unset($query_args['perpage']);
|
||||||
|
$query_args['nopaging'] = 1;
|
||||||
|
|
||||||
if ( $number >= 1 && $offset >= 0 ) {
|
$items_query = $itemsRepo->fetch($query_args, $collection_id);
|
||||||
$items = $Tainacan_Items->fetch( [
|
$items_query = $items_query->request;
|
||||||
's' => $search,
|
|
||||||
'offset' => $offset,
|
|
||||||
'posts_per_page' => $number
|
|
||||||
], $collection, 'OBJECT' );
|
|
||||||
} else {
|
|
||||||
$items = $Tainacan_Items->fetch( [ 's' => $search ], $collection, 'OBJECT' );
|
|
||||||
}
|
|
||||||
|
|
||||||
$return = [];
|
remove_filter('posts_pre_query', '__return_empty_array');
|
||||||
|
|
||||||
foreach ( $items as $item ) {
|
|
||||||
if ( strpos( $metadatum->get_metadata_type(), 'Core_Title' ) !== false ) {
|
|
||||||
$title = $item->get_title();
|
|
||||||
|
|
||||||
if ( ! empty( $search ) && stristr( $title, $search ) !== false ) {
|
|
||||||
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
|
|
||||||
} elseif ( empty( $search ) ) {
|
|
||||||
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$description = $item->get_description();
|
|
||||||
|
|
||||||
if ( ! empty( $search ) && stristr( $description, $search ) !== false ) {
|
|
||||||
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
|
|
||||||
} elseif ( empty( $search ) ) {
|
|
||||||
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$item_post_type = "%%{$collection_id}_item";
|
|
||||||
|
|
||||||
$collection = new Entities\Collection( $collection_id );
|
|
||||||
$capabilities = $collection->get_capabilities();
|
|
||||||
|
|
||||||
$results = [];
|
|
||||||
|
|
||||||
$search_query = '';
|
|
||||||
if ( $search ) {
|
|
||||||
$search_param = '%' . $search . '%';
|
|
||||||
$search_query = $wpdb->prepare( "WHERE meta_value LIKE %s", $search_param );
|
|
||||||
}
|
|
||||||
|
|
||||||
$pagination = '';
|
$pagination = '';
|
||||||
if ( $offset >= 0 && $number >= 1 ) {
|
if ( $offset >= 0 && $number >= 1 ) {
|
||||||
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $offset, (int) $number );
|
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $offset, (int) $number );
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no has logged user or actual user can not read private posts
|
$search_q = '';
|
||||||
if ( get_current_user_id() === 0 || ! current_user_can( $capabilities->read_private_posts ) ) {
|
$search = trim($search);
|
||||||
$args = [
|
if (!empty($search)) {
|
||||||
'exclude_from_search' => false,
|
$search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%');
|
||||||
'public' => true,
|
}
|
||||||
'private' => false,
|
|
||||||
'internal' => false,
|
|
||||||
];
|
|
||||||
|
|
||||||
$post_statuses = get_post_stati( $args, 'names', 'and' );
|
$total_query = $wpdb->prepare( "SELECT COUNT(DISTINCT meta_value) FROM $wpdb->postmeta WHERE meta_key = %s $search_q AND post_id IN($items_query) ORDER BY meta_value", $metadatum_id );
|
||||||
|
$query = $wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s $search_q AND post_id IN($items_query) ORDER BY meta_value $pagination", $metadatum_id );
|
||||||
|
|
||||||
foreach ( $post_statuses as $post_status ) {
|
$results = $wpdb->get_col($query);
|
||||||
|
$total = $wpdb->get_var($total_query);
|
||||||
|
$number = is_integer($number) && $number >=1 ? $number : $total;
|
||||||
|
$pages = ceil( $total / $number );
|
||||||
|
|
||||||
if ( $collection_id ) {
|
// add selected to the result
|
||||||
$sql_string = $wpdb->prepare(
|
if ( isset($query_args['metaquery']) && is_array($query_args['metaquery']) ) {
|
||||||
"SELECT DISTINCT metadatum_id, mvalue
|
foreach( $query_args['metaquery'] as $metaquery ){
|
||||||
FROM (
|
if( $metaquery['key'] == $metadatum_id ){
|
||||||
SELECT ID as item_id
|
if (!in_array($metaquery['value'], $results)) {
|
||||||
FROM $wpdb->posts
|
array_unshift($results, $metaquery['value']);
|
||||||
WHERE post_type LIKE %s AND post_status = %s
|
|
||||||
) items
|
|
||||||
JOIN (
|
|
||||||
SELECT meta_key as metadatum_id, meta_value as mvalue, post_id
|
|
||||||
FROM $wpdb->postmeta $search_query
|
|
||||||
) metas
|
|
||||||
ON items.item_id = metas.post_id AND metas.metadatum_id = %d ORDER BY mvalue $pagination",
|
|
||||||
$item_post_type, $post_status, $metadatum_id
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$sql_string = $wpdb->prepare(
|
|
||||||
"SELECT DISTINCT metadatum_id, mvalue
|
|
||||||
FROM (
|
|
||||||
SELECT ID as item_id
|
|
||||||
FROM $wpdb->posts
|
|
||||||
WHERE post_status = %s
|
|
||||||
) items
|
|
||||||
JOIN (
|
|
||||||
SELECT meta_key as metadatum_id, meta_value as mvalue, post_id
|
|
||||||
FROM $wpdb->postmeta $search_query
|
|
||||||
) metas
|
|
||||||
ON items.item_id = metas.post_id AND metas.metadatum_id = %d ORDER BY mvalue $pagination",
|
|
||||||
$post_status, $metadatum_id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pre_result = $wpdb->get_results( $sql_string, OBJECT );
|
|
||||||
|
|
||||||
if ( ! empty( $pre_result ) ) {
|
|
||||||
foreach ( $pre_result as $pre ) {
|
|
||||||
$results[] = $pre;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif ( current_user_can( $capabilities->read_private_posts ) ) {
|
|
||||||
$args = [
|
|
||||||
'exclude_from_search' => false,
|
|
||||||
];
|
|
||||||
|
|
||||||
$post_statuses = get_post_stati( $args, 'names', 'and' );
|
|
||||||
|
|
||||||
foreach ( $post_statuses as $post_status ) {
|
|
||||||
|
|
||||||
if ( $collection_id ) {
|
|
||||||
$sql_string = $wpdb->prepare(
|
|
||||||
"SELECT DISTINCT metadatum_id, mvalue
|
|
||||||
FROM (
|
|
||||||
SELECT ID as item_id
|
|
||||||
FROM $wpdb->posts
|
|
||||||
WHERE post_type LIKE %s AND post_status = %s
|
|
||||||
) items
|
|
||||||
JOIN (
|
|
||||||
SELECT meta_key as metadatum_id, meta_value as mvalue, post_id
|
|
||||||
FROM $wpdb->postmeta $search_query
|
|
||||||
) metas
|
|
||||||
ON items.item_id = metas.post_id AND metas.metadatum_id = %d ORDER BY mvalue $pagination",
|
|
||||||
$item_post_type, $post_status, $metadatum_id
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$sql_string = $wpdb->prepare(
|
|
||||||
"SELECT DISTINCT metadatum_id, mvalue
|
|
||||||
FROM (
|
|
||||||
SELECT ID as item_id
|
|
||||||
FROM $wpdb->posts
|
|
||||||
WHERE post_status = %s
|
|
||||||
) items
|
|
||||||
JOIN (
|
|
||||||
SELECT meta_key as metadatum_id, meta_value as mvalue, post_id
|
|
||||||
FROM $wpdb->postmeta $search_query
|
|
||||||
) metas
|
|
||||||
ON items.item_id = metas.post_id AND metas.metadatum_id = %d ORDER BY mvalue $pagination",
|
|
||||||
$post_status, $metadatum_id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pre_result = $wpdb->get_results( $sql_string, OBJECT );
|
|
||||||
|
|
||||||
if ( ! empty( $pre_result ) ) {
|
|
||||||
foreach ( $pre_result as $pre ) {
|
|
||||||
$results[] = $pre;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$spliced = $this->unique_multidimensional_array( $results, 'mvalue' );
|
$values = [];
|
||||||
|
foreach ($results as $r) {
|
||||||
if($number > 0 && count($spliced) > $number){
|
$values[] = [
|
||||||
array_splice($spliced, (int) $number);
|
'label' => $r,
|
||||||
|
'value' => $r,
|
||||||
|
'type' => 'Text'
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $spliced;
|
return [
|
||||||
|
'total' => $total,
|
||||||
|
'pages' => $pages,
|
||||||
|
'values' => $values
|
||||||
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue