begin export xml

This commit is contained in:
Jacson Passold 2018-05-02 21:55:41 -03:00
parent 3f67dad6bd
commit ad34740850
4 changed files with 320 additions and 1 deletions

View File

@ -0,0 +1,197 @@
<?php
namespace Tainacan\API\EndPoints;
use \Tainacan\API\REST_Controller;
use Tainacan\Entities;
use Tainacan\Repositories;
class REST_Export_Controller extends REST_Controller {
private $item_metadata_repository;
private $items_repository;
private $collection_repository;
private $field_repository;
public function __construct() {
$this->rest_base = 'export';
parent::__construct();
add_action('init', array(&$this, 'init_objects'), 11);
}
/**
* Initialize objects after post_type register
*
* @throws \Exception
*/
public function init_objects() {
$this->field_repository = Repositories\Fields::get_instance();
$this->item_metadata_repository = Repositories\Item_Metadata::get_instance();
$this->items_repository = Repositories\Items::get_instance();
$this->collection_repository = Repositories\Collections::get_instance();
}
/**
* If POST on field/collection/<collection_id>, then
* a field will be created in matched collection and all your item will receive this field
*
* If POST on field/item/<item_id>, then a value will be added in a field and field passed
* id body of requisition
*
* Both of GETs return the field of matched objects
*
* @throws \Exception
*/
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base. '/collection/(?P<collection_id>[\d]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE),
),
)
);
register_rest_route($this->namespace, '/' . $this->rest_base. '/item/(?P<item_id>[\d]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE),
),
)
);
register_rest_route($this->namespace, '/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(),
)
)
);
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
/*$collection_id = $request['collection_id'];
$field_id = $request['field_id'];
if($request['fetch'] === 'all_field_values'){
$results = $this->field_repository->fetch_all_field_values($collection_id, $field_id);
return new \WP_REST_Response($results, 200);
}
$result = $this->field_repository->fetch($field_id, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($result, $request);
return new \WP_REST_Response(apply_filters('tainacan-rest-response', $prepared_item, $request), 200);*/
}
/**
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
* @throws \Exception
*/
public function get_item_permissions_check( $request ) {
if(isset($request['collection_id'])) {
$collection = $this->collection_repository->fetch($request['collection_id']);
if($collection instanceof Entities\Collection) {
if (! $collection->can_read()) {
return false;
}
return true;
}
} elseif(isset($request['item_id'])) {
$item = $this->items_repository->fetch($request['item_id']);
if($item instanceof Entities\Item) {
if (! $item->can_read()) {
return false;
}
return true;
}
} else { // Exporting all
$dummy = new Entities\Collection();
return current_user_can($dummy->get_capabilities()->read); // Need to check Colletion by collection
}
return false;
}
/**
* @param \Tainacan\Entities\Entity $item
* @param \WP_REST_Request $request
*
* @return array|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$items_metadata = $item->get_fields();
$prepared_item = [];
foreach ($items_metadata as $item_metadata){
$prepared_item[] = $item_metadata->__toArray();
}
return $prepared_item;
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_items( $request ) {
$args = $this->prepare_filters($request);
$rest_response = new \WP_REST_Response([], 200); // TODO error, empty response
if(isset($request['collection_id'])) {
$collection_id = $request['collection_id'];
$items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
$response = [];
if ($items->have_posts()) {
while ( $items->have_posts() ) {
$items->the_post();
$item = new Entities\Item($items->post);
$prepared_item = $this->prepare_item_for_response($item, $request);
array_push($response, $prepared_item);
}
wp_reset_postdata();
}
$total_items = $items->found_posts;
$max_pages = ceil($total_items / (int) $items->query_vars['posts_per_page']);
$rest_response = new \WP_REST_Response(apply_filters('tainacan-rest-response', $response, $request), 200);
$rest_response->header('X-WP-Total', (int) $total_items);
$rest_response->header('X-WP-TotalPages', (int) $max_pages);
}
return $rest_response;
}
/**
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
* @throws \Exception
*/
public function get_items_permissions_check( $request ) {
return $this->get_item_permissions_check($request);
}
}
?>

View File

@ -13,6 +13,7 @@ $rest_item_metadata_controller = new \Tainacan\API\EndPoints\REST_Item_Metadata_
$rest_logs_controller = new \Tainacan\API\EndPoints\REST_Logs_Controller(); $rest_logs_controller = new \Tainacan\API\EndPoints\REST_Logs_Controller();
$rest_field_types_controller = new \Tainacan\API\EndPoints\REST_Field_Types_Controller(); $rest_field_types_controller = new \Tainacan\API\EndPoints\REST_Field_Types_Controller();
$rest_filter_types_controller = new \Tainacan\API\EndPoints\REST_Filter_Types_Controller(); $rest_filter_types_controller = new \Tainacan\API\EndPoints\REST_Filter_Types_Controller();
new \Tainacan\API\EndPoints\REST_Export_Controller();
// Add here other endpoints imports // Add here other endpoints imports
?> ?>

View File

@ -142,7 +142,11 @@ class Exposers {
} else { // array of elements } else { // array of elements
$ret = []; $ret = [];
foreach ($item_arr as $item) { foreach ($item_arr as $item) {
if(array_key_exists('field', $item)) {
$ret = array_merge($ret, $this->map($item, $mapper, $resquest) ); $ret = array_merge($ret, $this->map($item, $mapper, $resquest) );
} else {
$ret[] = $this->map($item, $mapper, $resquest);
}
} }
} }
return $ret; return $ret;

117
tests/test-api-export.php Normal file
View File

@ -0,0 +1,117 @@
<?php
namespace Tainacan\Tests;
/**
* @group api
*/
class TAINACAN_REST_Export_Controller extends TAINACAN_UnitApiTestCase {
protected function create_requirements() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeItemExport',
'description' => 'No description',
),
true,
true
);
$type = $this->tainacan_field_factory->create_field('text');
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'teste_Export',
'description' => 'descricao',
'collection' => $collection,
'field_type' => $type,
'exposer_mapping' => [
'dublin-core' => 'language'
]
),
true,
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Export',
'description' => 'adasdasdsa',
'collection' => $collection
),
true,
true
);
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $field);
$item_metadata->set_value('teste_export_field_value');
$item_metadata->validate();
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Export2',
'description' => 'adasdasdsa2',
'collection' => $collection
),
true,
true
);
$item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item2, $field);
$item_metadata2->set_value('teste_export_field_value2');
$item_metadata2->validate();
$item_metadata2 = $Tainacan_Item_Metadata->insert($item_metadata2);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_teste_Export3',
'description' => 'adasdasdsa3',
'collection' => $collection
),
true,
true
);
$item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item3, $field);
$item_metadata3->set_value('teste_export_field_value3');
$item_metadata3->validate();
$item_metadata3 = $Tainacan_Item_Metadata->insert($item_metadata3);
return ['collection' => $collection, 'items' => [$item, $item2, $item3], 'field' => $field, 'items_metadatas' => [$item_metadata, $item_metadata2, $item_metadata3]];
}
public function test_export_a_collection() {
extract($this->create_requirements());
$item_exposer_json = json_encode([
'exposer-type' => 'Xml',
'exposer-map' => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/export/collection/' . $collection->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
print_r($data);
}
}
?>