Facets refactor. return filtered values for all types

This commit is contained in:
leogermani 2018-10-31 14:30:27 -03:00
parent 90bfc0a00a
commit 3177c91cbc
5 changed files with 372 additions and 688 deletions

View File

@ -8,17 +8,8 @@ use \Tainacan\API\REST_Controller;
class REST_Facets_Controller extends REST_Controller { class REST_Facets_Controller extends REST_Controller {
private $total_pages;
private $total_items;
private $collection;
private $collection_repository;
private $metadatum_repository; private $metadatum_repository;
private $filter_repository;
private $terms_repository;
private $taxonomy_repository;
private $items_repository;
private $taxonomy;
/** /**
* REST_Facets_Controller constructor. * REST_Facets_Controller constructor.
@ -35,15 +26,7 @@ class REST_Facets_Controller extends REST_Controller {
* Initialize objects after post_type register * Initialize objects after post_type register
*/ */
public function init_objects() { public function init_objects() {
$this->collection = new Entities\Collection();
$this->collection_repository = Repositories\Collections::get_instance();
$this->metadatum_repository = Repositories\Metadata::get_instance(); $this->metadatum_repository = Repositories\Metadata::get_instance();
$this->filter_repository = Repositories\Filters::get_instance();
$this->terms_repository = Repositories\Terms::get_instance();
$this->taxonomy_repository = Repositories\Taxonomies::get_instance();
$this->items_repository = Repositories\Items::get_instance();
} }
public function register_routes() { public function register_routes() {
@ -53,9 +36,9 @@ class REST_Facets_Controller extends REST_Controller {
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check') 'permission_callback' => array($this, 'get_items_permissions_check')
) )
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<metadatum_id>[\d]+)', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<metadatum_id>[\d]+)', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
@ -72,157 +55,72 @@ class REST_Facets_Controller extends REST_Controller {
public function get_items( $request ) { public function get_items( $request ) {
$metadatum_id = $request['metadatum_id']; $metadatum_id = $request['metadatum_id'];
$metadatum = $this->metadatum_repository->fetch($metadatum_id);
if( !empty($metadatum_id) ) {
$response = $this->prepare_item_for_response($metadatum, $request );
$rest_response = new \WP_REST_Response($response, 200);
$rest_response->header('X-WP-Total', $this->total_items);
$rest_response->header('X-WP-TotalPages', $this->total_pages);
return $rest_response;
}
/**
*
* Receive a \WP_Query or a metadatum object and return both in JSON
*
* @param mixed $metadatum
* @param \WP_REST_Request $request
*
* @return mixed|string|void|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response($metadatum, $request){
$response = [];
$metadatum_type = null;
if( !empty($metadatum) ){
$metadatum_type = $metadatum->get_metadata_type();
$options = $metadatum->get_metadata_type_options();
$args = $this->prepare_filters($request);
// handle filter with Taxonomy metadata
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){
$this->taxonomy = $this->taxonomy_repository->fetch($options['taxonomy_id']);
$selected = $this->getTaxonomySelectedValues($request, $options['taxonomy_id']);
if( isset($request['term_id']) ){
$terms[] = $this->terms_repository->fetch($request['term_id'], $this->taxonomy);
$restTermClass = new REST_Terms_Controller();
} else {
$terms = $this->terms_repository->fetch($args, $this->taxonomy);
// retrieve selected items
if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){
$ids = $this->get_terms_ids( $terms );
$realResponse = [];
foreach( $selected as $index => $term_id ){
$term_selected = $this->terms_repository->fetch($term_id, $this->taxonomy);
$realResponse[] = $term_selected;
}
foreach( $terms as $index => $term ){
if( in_array($term->WP_Term->term_id, $selected) ){
continue;
}
$realResponse[] = $term;
if( isset($request['number']) && count($realResponse) >= $request['number']){
break;
}
}
$terms = $realResponse;
}
$restTermClass = new REST_Terms_Controller();
}
foreach ($terms as $term) {
array_push($response, $restTermClass->prepare_item_for_response( $term, $request ));
}
$this->set_pagination_properties_term_type( $args, $response );
}
// handle filter with Text metadata $metadatum = $this->metadatum_repository->fetch($metadatum_id);
$metadatum_type = $metadatum->get_metadata_type();
else {
$offset = null;
$metadatum_id = $metadatum->get_id(); $number = null;
$offset = null; $_search = null;
$number = null; $collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : null;
$_search = null;
$collection_id = ( isset($request['collection_id']) ) ? $request['collection_id'] : null; $query_args = $request['current_query'];
//$selected = $this->getTextSelectedValues($request, $metadatum_id); $query_args = $this->prepare_filters($query_args);
$query_args = $request['current_query']; if($request['offset'] >= 0 && $request['number'] >= 1){
$offset = $request['offset'];
$number = $request['number'];
$query_args = $this->prepare_filters($query_args);
if($request['offset'] >= 0 && $request['number'] >= 1){
$offset = $request['offset'];
$number = $request['number'];
}
if($request['search']) {
$_search = $request['search'];
}
$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
//if( count($selected) && $request['getSelected'] && $request['getSelected'] === '1'){
// $rawValues = $this->get_values( $response );
// $realResponse = [];
// foreach( $selected as $index => $value ){
// $row = (object) ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
// $realResponse[] = $row;
// }
// foreach( $rawValues as $index => $row0 ){
// if( !in_array($row0, $selected) ){
// $realResponse[] = (object) ['mvalue' => $row0, 'metadatum_id' => $metadatum_id];
// if( isset($request['number']) && count($realResponse) >= $request['number']){
// break;
// }
// }
// }
//
// $response = $realResponse;
//}
//$this->set_pagination_properties_text_type( $offset, $number, $rawResponse );
} }
}
if($request['search']) {
$_search = $request['search'];
}
$include = [];
if ( isset($request['getSelected']) && $request['getSelected'] == 1 ) {
if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
if( isset($request['current_query']['taxquery']) ){
foreach( $request['current_query']['taxquery'] as $taxquery ){
if( $taxquery['taxonomy'] === 'tnc_tax_' . $taxonomy_id ){
$include = $taxquery['terms'];
}
}
}
} else {
if( isset($request['current_query']['metaquery']) ){
foreach( $request['current_query']['metaquery'] as $metaquery ){
if( $metaquery['key'] == $metadatum_id ){
$include = $metaquery['value'];
}
}
}
}
}
$args = [
'collection_id' => $collection_id,
'search' => $_search,
'offset' => $offset,
'number' => $number,
'items_filter' => $query_args,
'include' => $include
];
$response = $this->metadatum_repository->fetch_all_metadatum_values( $metadatum_id, $args );
$rest_response = new \WP_REST_Response($response['values'], 200);
return $this->prepare_response( $response, $metadatum_type ); $rest_response->header('X-WP-Total', $response['total']);
} $rest_response->header('X-WP-TotalPages', $response['pages']);
return $rest_response;
}
}
/** /**
* @param \WP_REST_Request $request * @param \WP_REST_Request $request
@ -233,283 +131,7 @@ class REST_Facets_Controller extends REST_Controller {
return true; return true;
} }
/**
* @param array $response the original response
* @param string $type the metadata type
*
* @return mixed|string|void|\WP_Error|\WP_REST_Response
*/
public function prepare_response( $response, $type ){
$result = [];
if( $response ){
foreach ( $response as $key => $item ) {
if( $type === 'Tainacan\Metadata_Types\Taxonomy' ){
$result[] = [
'label' => $item['name'],
'value' => $item['id'],
'img' => ( isset($item['header_image']) ) ? $item['header_image'] : false ,
'parent' => ( isset($item['parent']) ) ? $item['parent'] : 0,
'total_children' => ( isset($item['total_children']) ) ? $item['total_children'] : 0,
'type' => 'Taxonomy',
'taxonomy_id' => $this->taxonomy->WP_Post->ID,
'taxonomy' => ( isset($item['taxonomy']) ) ? $item['taxonomy'] : false,
];
} else if( $type === 'Tainacan\Metadata_Types\Relationship' ){
$result[] = [
'label' => $item['title'],
'value' => $item['id'],
'img' => ( isset($item['thumbnail']['thumb']) ) ? $item['thumbnail']['thumb'] : false,
'parent' => false,
'total_children' => 0,
'type' => 'Relationship'
];
} else {
$result[] = [
'label' => $item->mvalue,
'value' => $item->mvalue,
'img' => false,
'parent' => false,
'total_children' => 0,
'type' => 'Text'
];
}
}
}
return $result;
}
/**
* set attributes for text metadata
*
* @param $offset
* @param $number
* @param $response
*/
private function set_pagination_properties_text_type( $offset, $number, $response ){
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
*
* @param $args
* @param $response
*/
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 ;
}
}
/**
* get text metadata selected facets
*
* @param $request
* @param $taxonomy_id
*
* @return array
*/
private function getTaxonomySelectedValues($request, $taxonomy_id){
$selected = [];
$restTermClass = new REST_Terms_Controller();
if( isset($request['current_query']['taxquery']) ){
foreach( $request['current_query']['taxquery'] as $taxquery ){
if( $taxquery['taxonomy'] === 'tnc_tax_' . $taxonomy_id ){
return $taxquery['terms'];
}
}
}
return [];
}
/**
* get text metadata selected facets
*
* @param $request
* @param $metadatum_id
*
* @return array
*/
private function getTextSelectedValues($request, $metadatum_id){
if( isset($request['current_query']['metaquery']) ){
foreach( $request['current_query']['metaquery'] as $metaquery ){
if( $metaquery['key'] == $metadatum_id ){
return $metaquery['value'];
}
}
}
return [];
}
/**
* get only selected relationship values
*
* @param $request
* @param $metadatum_id
*
* @return array
*/
private function getRelationshipSelectedValues($request, $metadatum_id){
$selected = [];
$restTermClass = new REST_Terms_Controller();
if( isset($request['current_query']['metaquery']) ){
foreach( $request['current_query']['metaquery'] as $metaquery ){
if( $metaquery['key'] == $metadatum_id ){
return $metaquery['value'];
}
}
}
return [];
}
/**
*
*/
private function get_terms_ids( $terms ){
$ids = [];
foreach( $terms as $term ){
$ids[] = (string) $term->WP_Term->term_id;
}
return $ids;
}
/**
* @param $rows
*
* @return array
*/
private function get_values( $rows ){
$values = [];
foreach( $rows as $row ){
$values[] = $row->mvalue;
}
return $values;
}
/**
* method responsible to return the total of items for the facet value
*
* @param $value
* @param $reference_id
* @param bool $is_taxonomy
* @param $query
* @param $collection_id
*
* @return int total of items found
*/
private function add_items_count( $value, $reference_id, $is_taxonomy = false, $query, $collection_id){
$new_args = $query;
$has_value = false;
if( !$is_taxonomy ){
if( isset( $query['metaquery'] ) ){
foreach( $query['metaquery'] as $index => $metaquery ){
if( $metaquery['key'] == $metadatum_id ){
$has_value = true;
if( is_array($metaquery['value']) )
$new_args['metaquery'][$index]['value'][] = $value;
else
$new_args['metaquery'][$index]['value'] = $value;
}
}
}
if( !$has_value ){
$new_args['metaquery'][] = [
'key' => $reference_id,
'value' => $value
];
}
} else {
if( isset( $query['taxquery'] ) ){
foreach( $query['taxquery'] as $taxquery ){
if( $taxquery['taxonomy'] === 'tnc_tax_' . $reference_id ){
$has_value = true;
$new_args['taxquery'][$index]['terms'][] = $value;
}
}
}
if( !$has_value ){
$new_args['taxquery'][] = [
'taxonomy' => 'tnc_tax_' . $reference_id,
'value' => [$value]
];
}
}
$items = $this->items_repository->fetch($new_args, $collection_id, 'WP_Query');
return $items->found_posts;
}
} }
?> ?>

View File

@ -136,25 +136,6 @@ class REST_Metadata_Controller extends REST_Controller {
$number = $request['number']; $number = $request['number'];
} }
if($request['fetch'] === 'all_metadatum_values' && $request['search']){
if($collection_id) {
$results = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, $request['search'], $offset, $number );
} else {
$results = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, $request['search'], $offset, $number);
}
return new \WP_REST_Response($results, 200);
} elseif($request['fetch'] === 'all_metadatum_values') {
if($collection_id) {
$results = $this->metadatum_repository->fetch_all_metadatum_values( $collection_id, $metadatum_id, '', $offset, $number);
} else {
$results = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum_id, '', $offset, $number);
}
return new \WP_REST_Response($results, 200);
}
$result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT'); $result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT');
return new \WP_REST_Response($this->prepare_item_for_response($result, $request), 200); return new \WP_REST_Response($this->prepare_item_for_response($result, $request), 200);
@ -546,11 +527,6 @@ class REST_Metadata_Controller extends REST_Controller {
public function get_endpoint_args_for_item_schema( $method = null ) { public function get_endpoint_args_for_item_schema( $method = null ) {
$endpoint_args = []; $endpoint_args = [];
if($method === \WP_REST_Server::READABLE) { if($method === \WP_REST_Server::READABLE) {
$endpoint_args['fetch'] = [
'type' => 'string',
'description' => __('Fetch all content of a metadata within a collection'),
'enum' => ['all_metadatum_values']
];
$endpoint_args['context'] = array( $endpoint_args['context'] = array(
'type' => 'string', 'type' => 'string',
'default' => 'view', 'default' => 'view',

View File

@ -869,108 +869,189 @@ class Metadata extends Repository {
} }
# TODO: Fetch all metadatum value for repository level
/** /**
* Fetch all values of a metadatum from a collection or repository * Return all possible values for a metadatum
* *
* @param $collection_id * Each metadata is a label with the metadatum name and the value.
* @param $metadatum_id *
* * If an ID, a slug or a Tainacan\Entities\Metadatum object is passed in the 'metadata' argument, it returns only one metadata, otherwise
* @param string $search * it returns all metadata
* *
* @param int $offset * @param int $metadatum_id The ID of the metadata to fetch values from
* @param string $number * @param array|string $args {
* * Optional. Array or string of arguments.
* @return array|null|object *
* @throws \Exception * @type mixed $collection_id The collection ID you want to consider or null for all collections. If a collectoin is set
*/ * then only values applied to items in this collection will be returned
public function fetch_all_metadatum_values( $collection_id, $metadatum_id, $search = '', $offset = 0, $number = '', $query_args = [] ) { *
* @type int $number The number of values to return (for pagination). Default 0 (unlimited)
*
* @type int $offset The offset (for pagination). Default 0
*
* @type array $items_filter Array in the same format used in @see \Tainacan\Repositories\Items::fetch(). It will filter the results to only return values used in the items inside this criteria. Defatul [] (all items)
*
* @type array $include Array if ids to be included in the result. 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
*
* }
*
* @return array Array with the total number of values found. The total number of pages with the current number and the results with id and label for each value. Terms also include parent, taxonomy and number of children.
*/
public function fetch_all_metadatum_values( $metadatum_id, $args = [] ) {
$defaults = array(
'collection_id' => null,
'search' => '',
'offset' => 0,
'number' => '',
'include' => [],
'items_filter' => [],
'parent_id' => 0
);
$args = wp_parse_args($args, $defaults);
global $wpdb; global $wpdb;
// Get the query for current items $itemsRepo = Items::get_instance();
// this avoids wp_query to run the query. We just want to build the query $metadataRepo = Metadata::get_instance();
add_filter('posts_pre_query', '__return_empty_array');
// Get the query for current items
// this avoids wp_query to run the query. We just want to build the query
add_filter('posts_pre_query', '__return_empty_array');
$args['items_filter']['fields'] = 'ids';
unset($args['items_filter']['paged']);
unset($args['items_filter']['offset']);
unset($args['items_filter']['perpage']);
$args['items_filter']['nopaging'] = 1;
$items_query = $itemsRepo->fetch($args['items_filter'], $args['collection_id']);
$items_query = $items_query->request;
remove_filter('posts_pre_query', '__return_empty_array');
//// end filtering query ////////
$metadatum = $metadataRepo->fetch($metadatum_id);
$metadatum_type = $metadatum->get_metadata_type();
$pagination = '';
if ( $args['offset'] >= 0 && $args['number'] >= 1 ) {
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $args['offset'], (int) $args['number'] );
}
$search_q = '';
$search = trim($args['search']);
if (!empty($search)) {
if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) {
$search_q = $wpdb->prepare("AND meta_value IN ( SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s )", '%' . $search . '%');
} elseif ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
$search_q = $wpdb->prepare("AND t.name LIKE %s", '%' . $search . '%');
} else {
$search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%');
}
}
$itemsRepo = Items::get_instance(); if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ) {
$metadataRepo = Metadata::get_instance();
$metadatum_options = $metadatum->get_metadata_type_options();
$taxonomy_id = $metadatum_options['taxonomy_id'];
$taxonomy_slug = Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id);
$metadatum = $metadataRepo->fetch($metadatum_id);
$metadatum_type = $metadatum->get_metadata_type(); $base_query = $wpdb->prepare("FROM $wpdb->term_relationships tr
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
$query_args['fields'] = 'ids'; INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
unset($query_args['paged']); WHERE
unset($query_args['offset']); tt.parent = %d AND
unset($query_args['perpage']); tr.object_id IN ($items_query) AND
$query_args['nopaging'] = 1; tt.taxonomy = %s
$search_q
$items_query = $itemsRepo->fetch($query_args, $collection_id); ORDER BY t.name ASC
$items_query = $items_query->request; ",
$args['parent_id'],
$taxonomy_slug
remove_filter('posts_pre_query', '__return_empty_array'); );
$pagination = ''; $query = "SELECT DISTINCT t.name, tt.term_taxonomy_id, tt.parent $base_query $pagination";
if ( $offset >= 0 && $number >= 1 ) {
$pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $offset, (int) $number ); $total_query = "SELECT COUNT (DISTINCT tt.term_taxonomy_id) $base_query ";
}
$results = $wpdb->get_results($query);
$search_q = '';
$search = trim($search); // add selected to the result
if (!empty($search)) { if ( !empty($args['include']) ) {
if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ){ if ( is_array($args['include']) ) {
$search_q = $wpdb->prepare("AND meta_value IN ( SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s )", '%' . $search . '%'); $results = array_unique( array_merge($args['include'], $results) );
}
} else { }
$search_q = $wpdb->prepare("AND meta_value LIKE %s", '%' . $search . '%');
} $total = $wpdb->get_var($total_query);
$number = is_integer($number) && $number >=1 ? $number : $total;
if( $number < 1){
} $pages = 1;
} else {
if( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){ $pages = ceil( $total / $number );
}
//TODO: Retrieve terms
$values = [];
} else { foreach ($results as $r) {
$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 ); $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);
$results = $wpdb->get_col($query);
$total = $wpdb->get_var($total_query); $values[] = [
$number = is_integer($number) && $number >=1 ? $number : $total; 'value' => $r->term_taxonomy_id,
'label' => $r->name,
if( $number < 1){ 'total_children' => $total_children,
$pages = 1; 'taxonomy' => $taxonomy_slug,
} else { 'taxonomy_id' => $taxonomy_id,
$pages = ceil( $total / $number ); 'parent' => $r->parent,
} 'type' => 'Taxonomy'
];
// add selected to the result }
if ( isset($query_args['metaquery']) && is_array($query_args['metaquery']) ) {
foreach( $query_args['metaquery'] as $metaquery ){
if( $metaquery['key'] == $metadatum_id ){
if (!in_array($metaquery['value'], $results)) { } else {
array_unshift($results, $metaquery['value']);
} $base_query = $wpdb->prepare( "FROM $wpdb->postmeta WHERE meta_key = %s $search_q AND post_id IN($items_query) ORDER BY meta_value", $metadatum_id );
}
} $total_query = "SELECT COUNT(DISTINCT meta_value) $base_query";
} $query = "SELECT DISTINCT meta_value $base_query $pagination";
$values = []; $results = $wpdb->get_col($query);
foreach ($results as $r) { $total = $wpdb->get_var($total_query);
$label = ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) ? $number = is_integer($number) && $number >=1 ? $number : $total;
get_post($r)->post_title : $r; if( $number < 1){
$pages = 1;
$values[] = [ } else {
'label' => $label, $pages = ceil( $total / $number );
'value' => $r, }
'type' => 'Text'
]; // add selected to the result
} if ( !empty($args['include']) ) {
} if ( is_array($args['include']) ) {
$results = array_unique( array_merge($args['include'], $results) );
}
}
$values = [];
foreach ($results as $r) {
$label = ( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ) ?
get_post($r)->post_title : $r;
$values[] = [
'label' => $label,
'value' => $r,
'type' => 'Text'
];
}
}
return [ return [
'total' => $total, 'total' => $total,

View File

@ -338,6 +338,11 @@ class Taxonomies extends Repository {
} }
} }
} }
public function get_db_identifier_by_id($id) {
$prefix = Entities\Taxonomy::$db_identifier_prefix;
return $prefix . $id;
}
} }

View File

@ -404,128 +404,128 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('No name', $data['name']); $this->assertEquals('No name', $data['name']);
} }
public function test_fetch_all_metadatum_values(){ // public function test_fetch_all_metadatum_values(){
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); // $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
//
$collection = $this->tainacan_entity_factory->create_entity( // $collection = $this->tainacan_entity_factory->create_entity(
'collection', // 'collection',
array( // array(
'name' => 'Statement', // 'name' => 'Statement',
'description' => 'No Statement', // 'description' => 'No Statement',
'status' => 'publish' // 'status' => 'publish'
), // ),
true // true
); // );
//
$item1 = $this->tainacan_entity_factory->create_entity( // $item1 = $this->tainacan_entity_factory->create_entity(
'item', // 'item',
array( // array(
'title' => 'No name1', // 'title' => 'No name1',
'description' => 'No description1', // 'description' => 'No description1',
'status' => 'publish', // 'status' => 'publish',
'collection' => $collection // 'collection' => $collection
), // ),
true // true
); // );
//
$item2 = $this->tainacan_entity_factory->create_entity( // $item2 = $this->tainacan_entity_factory->create_entity(
'item', // 'item',
array( // array(
'title' => 'No name2', // 'title' => 'No name2',
'description' => 'No description2', // 'description' => 'No description2',
'status' => 'private', // 'status' => 'private',
'collection' => $collection // 'collection' => $collection
), // ),
true // true
); // );
//
$item3 = $this->tainacan_entity_factory->create_entity( // $item3 = $this->tainacan_entity_factory->create_entity(
'item', // 'item',
array( // array(
'title' => 'No name3', // 'title' => 'No name3',
'description' => 'No description3', // 'description' => 'No description3',
'status' => 'private', // 'status' => 'private',
'collection' => $collection // 'collection' => $collection
), // ),
true // true
); // );
//
$metadatum = $this->tainacan_entity_factory->create_entity( // $metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum', // 'metadatum',
array( // array(
'name' => 'Data', // 'name' => 'Data',
'description' => 'Descreve valor do campo data.', // 'description' => 'Descreve valor do campo data.',
'collection' => $collection, // 'collection' => $collection,
'status' => 'publish', // 'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Text', // 'metadata_type' => 'Tainacan\Metadata_Types\Text',
), // ),
true // true
); // );
//
$item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($item1, $metadatum); // $item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($item1, $metadatum);
$item_metadata1->set_value('12/12/2017'); // $item_metadata1->set_value('12/12/2017');
//
$item_metadata1->validate(); // $item_metadata1->validate();
$Tainacan_Item_Metadata->insert($item_metadata1); // $Tainacan_Item_Metadata->insert($item_metadata1);
//
$item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item2, $metadatum); // $item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item2, $metadatum);
$item_metadata2->set_value('02/03/2018'); // $item_metadata2->set_value('02/03/2018');
//
$item_metadata2->validate(); // $item_metadata2->validate();
$Tainacan_Item_Metadata->insert($item_metadata2); // $Tainacan_Item_Metadata->insert($item_metadata2);
//
// Is repeated for test return of duplicates // // Is repeated for test return of duplicates
$item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item3, $metadatum); // $item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item3, $metadatum);
$item_metadata3->set_value('12/12/2017'); // $item_metadata3->set_value('12/12/2017');
//
$item_metadata3->validate(); // $item_metadata3->validate();
$Tainacan_Item_Metadata->insert($item_metadata3); // $Tainacan_Item_Metadata->insert($item_metadata3);
//
//======================= // //=======================
//
$request = new \WP_REST_Request( // $request = new \WP_REST_Request(
'GET', // 'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/facets/' . $metadatum->get_id() // $this->namespace . '/collection/' . $collection->get_id() . '/facets/' . $metadatum->get_id()
); // );
//
//======================= // //=======================
//
// Set no one user // // Set no one user
wp_set_current_user(0); // wp_set_current_user(0);
//
$response1 = $this->server->dispatch($request); // $response1 = $this->server->dispatch($request);
//
$data1 = $response1->get_data(); // $data1 = $response1->get_data();
//
$this->assertCount(1, $data1); // $this->assertCount(1, $data1);
$this->assertEquals('12/12/2017', $data1[0]['value']); // $this->assertEquals('12/12/2017', $data1[0]['value']);
//
//======================= // //=======================
//
$new_user1 = $this->factory()->user->create(array( 'role' => 'subscriber' )); // $new_user1 = $this->factory()->user->create(array( 'role' => 'subscriber' ));
wp_set_current_user($new_user1); // wp_set_current_user($new_user1);
//
$response1 = $this->server->dispatch($request); // $response1 = $this->server->dispatch($request);
//
$data1 = $response1->get_data(); // $data1 = $response1->get_data();
//
$this->assertCount(1, $data1); // $this->assertCount(1, $data1);
$this->assertEquals('12/12/2017', $data1[0]['value']); // $this->assertEquals('12/12/2017', $data1[0]['value']);
//
//======================= // //=======================
//
$new_user2 = $this->factory()->user->create(array( 'role' => 'administrator' )); // $new_user2 = $this->factory()->user->create(array( 'role' => 'administrator' ));
wp_set_current_user($new_user2); // wp_set_current_user($new_user2);
//
$response2 = $this->server->dispatch($request); // $response2 = $this->server->dispatch($request);
//
$data2 = $response2->get_data(); // $data2 = $response2->get_data();
//
// Only two without duplicates // // Only two without duplicates
$this->assertCount(2, $data2); // //$this->assertCount(2, $data2);
$this->assertEquals('12/12/2017', $data2[0]['value']); // //$this->assertEquals('12/12/2017', $data2[0]['value']);
$this->assertEquals('02/03/2018', $data2[1]['value']); // //$this->assertEquals('02/03/2018', $data2[1]['value']);
} // }
} }
?> ?>