Get response from different types of metadata (ref. #94)

This commit is contained in:
eduardohumberto 2018-08-16 21:52:39 -03:00
parent ea172d452b
commit f6208ba3a9
2 changed files with 91 additions and 21 deletions

View File

@ -2,6 +2,8 @@
namespace Tainacan\API\EndPoints;
use Tainacan\Repositories;
use Tainacan\Entities;
use \Tainacan\API\REST_Controller;
class REST_Facets_Controller extends REST_Controller {
@ -23,21 +25,23 @@ class REST_Facets_Controller extends REST_Controller {
$this->collection_repository = Repositories\Collections::get_instance();
$this->metadatum_repository = Repositories\Metadata::get_instance();
$this->filter_repository = Repositories\Filters::get_instance();
$this->filter_repository = Repositories\Filters::get_instance();
$this->terms_repository = Repositories\Terms::get_instance();
$this->taxonomy_repository = Repositories\Taxonomies::get_instance();
}
public function register_routes() {
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<facet_id>[\d]+)', array(
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadatum_id>[\d]+)', array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params()
)
));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<facet_id>[\d]+)', array(
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<metadatum_id>[\d]+)', array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
@ -47,16 +51,6 @@ class REST_Facets_Controller extends REST_Controller {
));
}
/**
* @param mixed $item
* @param \WP_REST_Request $request
*
* @return mixed|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
}
/**
* @param \WP_REST_Request $request
*
@ -64,14 +58,58 @@ class REST_Facets_Controller extends REST_Controller {
*/
public function get_item( $request ) {
$filter_id = $request['facet_id'];
$filter = $this->filter_repository->fetch($filter_id);
// TODO: get facets for the filter, if collection is specified retrieve
// only values from collection
$metadatum_id = $request['metadatum_id'];
$metadatum = $this->metadatum_repository->fetch($metadatum_id);
return new \WP_REST_Response($prepared, 200);
}
$response = $this->prepare_item_for_response($metadatum, $request );
return new \WP_REST_Response($response, 200);
}
/**
*
* 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 = [];
if( !empty($metadatum) ){
$metadatum_type = $metadatum->get_metadata_type();
$options = $metadatum->get_metadata_type_options();
$args = $this->prepare_filters($request);
if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ){
$response = $this->items_repository->fetch($args, $options['collection_id'], 'OBJECT');
} else if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){
$taxonomy = $this->taxonomy_repository->fetch($options['taxonomy_id']);
$terms = $this->terms_repository->fetch($args, $taxonomy);
foreach ($terms as $term) {
array_push($response, $this->prepare_term_for_response( $term, $request ));
}
} else {
if($request['collection_id']) {
$response = $this->metadatum_repository->fetch_all_metadatum_values( $request['collection_id'], $metadatum->get_id() );
} else {
$response = $this->metadatum_repository->fetch_all_metadatum_values( null, $metadatum->get_id() );
}
}
}
return $response;
}
/**
* @param \WP_REST_Request $request
@ -81,6 +119,37 @@ class REST_Facets_Controller extends REST_Controller {
public function get_items_permissions_check( $request ) {
return true;
}
/**
* @param mixed $item
* @param \WP_REST_Request $request
*
* @return array|mixed|\WP_Error|\WP_REST_Response
*/
public function prepare_term_for_response( $item, $request ) {
if(!empty($item)){
if(!isset($request['fetch_only'])) {
$item_arr = $item->_toArray();
$children = get_terms([
'taxonomy' => $item_arr['taxonomy'],
'parent' => $item_arr['id'],
'fields' => 'ids',
'hide_empty' => false,
]);
$item_arr['total_children'] = count($children);
} else {
$attributes_to_filter = $request['fetch_only'];
$item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter);
}
return $item_arr;
}
return $item;
}
}
?>

View File

@ -18,6 +18,7 @@ $rest_background_processes_controller = new \Tainacan\API\EndPoints\REST_Back
$rest_bulkedit_controller = new \Tainacan\API\EndPoints\REST_Bulkedit_Controller();
new \Tainacan\API\EndPoints\REST_Export_Controller();
new \Tainacan\API\EndPoints\REST_Metadatum_Mappers_Controller();
$rest_facets_controller = new \Tainacan\API\EndPoints\REST_Facets_Controller();
// Add here other endpoints imports
?>