Merge branch 'export' into field-mapping-interface, fix some field from export and write array to exported file

This commit is contained in:
Jacson Passold 2018-07-09 18:41:42 -03:00
commit 45b2d718ff
2 changed files with 179 additions and 22 deletions

View File

@ -6,6 +6,7 @@ use \Tainacan\API\REST_Controller;
use Tainacan\Entities;
use Tainacan\Repositories;
use Tainacan\Entities\Entity;
use Tainacan\Tests\TAINACAN_REST_Collections_Controller;
class REST_Export_Controller extends REST_Controller {
private $item_metadata_repository;
@ -80,21 +81,7 @@ class REST_Export_Controller extends REST_Controller {
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_item( $request ) {
/*$collection_id = $request['collection_id'];
$metadatum_id = $request['metadatum_id'];
if($request['fetch'] === 'all_metadatum_values'){
$results = $this->metadatum_repository->fetch_all_metadatum_values($collection_id, $metadatum_id);
return new \WP_REST_Response($results, 200);
}
$result = $this->metadatum_repository->fetch($metadatum_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);*/
}
public function get_item( $request ) { }
/**
* @param \WP_REST_Request $request
@ -212,7 +199,6 @@ class REST_Export_Controller extends REST_Controller {
$prepared_item = $this->prepare_item_for_response($item, $request);
array_push($response, $prepared_item);
file_put_contents('/tmp/2', print_r($prepared_item, true), FILE_APPEND);
}
wp_reset_postdata();
}
@ -225,12 +211,42 @@ class REST_Export_Controller extends REST_Controller {
$response = [$prepared_item];
}
} else { // Export All
$collections = $query;
$collection_controller = new REST_Collections_Controller();
if ($collections->have_posts()) {
while ($collections->have_posts()) {
$collections->the_post();
$collection_id = $collections->post->ID;
$collection = \Tainacan\Repositories\Repository::get_entity_by_post($collections->post);
$prepared_collection = $collection_controller->prepare_item_for_response($collection, $request);
$prepared_items = [];
$items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
if ($items->have_posts()) {
while ( $items->have_posts() ) { //TODO write line by line
$items->the_post();
$item = new Entities\Item($items->post);
$prepared_item = $this->prepare_item_for_response($item, $request);
array_push($prepared_items, $prepared_item);
}
wp_reset_postdata();
}
$prepared_collection['items'] = $prepared_items;
array_push($prepared_collection, $response);
}
wp_reset_postdata();
}
}
$rest_response = new \WP_REST_Response(apply_filters('tainacan-rest-response', $response, $request));
//file_put_contents($filename, $rest_response->get_data());
file_put_contents('/tmp/1', print_r($rest_response->get_data(), true));
$data = $rest_response->get_data();
file_put_contents($filename, is_string($data) ? $data : print_r($data, true));
if($background) {
$log->set_status('publish');
@ -291,7 +307,15 @@ class REST_Export_Controller extends REST_Controller {
$rest_response->header('X-WP-TotalPages', 1);
}
} else { // Export All
$Tainacan_Collection = \Tainacan\Repositories\Collections::get_instance();
$collections = $Tainacan_Collection->fetch(['post_status' => 'publish'], 'WP_Query');
$response = $this->export($request, $collections, $args);
$total_items = $collections->found_posts;
$ret = $response instanceof Entity ? $response->__toArray() : $response;
$rest_response = new \WP_REST_Response($ret, 200);
$rest_response->header('X-WP-Total', (int) $total_items);
}
return $rest_response;

View File

@ -94,7 +94,99 @@ class TAINACAN_REST_Export_Controller extends TAINACAN_UnitApiTestCase {
$item_metadata3 = $Tainacan_Item_Metadata->insert($item_metadata3);
return ['collection' => $collection, 'items' => [$item, $item2, $item3], 'metadatum' => $metadatum, 'items_metadatas' => [$item_metadata, $item_metadata2, $item_metadata3]];
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeItemExport2',
'description' => 'No description',
),
true,
true
);
$type2 = $this->tainacan_metadatum_factory->create_metadatum('text');
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'teste_Export2',
'description' => 'descricao2',
'collection' => $collection2,
'metadata_type' => $type2,
'exposer_mapping' => [
'dublin-core' => 'contributor'
]
),
true,
true
);
$item_c2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_c2_teste_Export',
'description' => 'adasdasdsa_c2',
'collection' => $collection2
),
true,
true
);
$item_c2_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item_c2, $metadatum2);
$item_c2_metadata->set_value('teste_export_metadatum_value');
$item_c2_metadata->validate();
$item_c2_metadata = $Tainacan_Item_Metadata->insert($item_c2_metadata);
$item_c2_2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_c2_teste_Export2',
'description' => 'adasdasdsa2_c2',
'collection' => $collection2
),
true,
true
);
$item_c2_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item_c2_2, $metadatum2);
$item_c2_metadata2->set_value('teste_export_metadatum_value2');
$item_c2_metadata2->validate();
$item_c2_metadata2 = $Tainacan_Item_Metadata->insert($item_c2_metadata2);
$item_c2_3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item_c2_teste_Export3',
'description' => 'adasdasdsa3_c2',
'collection' => $collection2
),
true,
true
);
$item_c2_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item_c2_3, $metadatum2);
$item_c2_metadata3->set_value('teste_export_metadatum_value3');
$item_c2_metadata3->validate();
$item_c2_metadata3 = $Tainacan_Item_Metadata->insert($item_c2_metadata3);
return [
'collection' => $collection,
'items' => [$item, $item2, $item3],
'metadatum' => $metadatum,
'items_metadatas' => [$item_metadata, $item_metadata2, $item_metadata3],
'collections' => [$collection, $collection2],
'items2' => [$item_c2, $item_c2_2, $item_c2_3],
'items_metadatas2' => [$item_c2_metadata, $item_c2_metadata2, $item_c2_metadata3],
];
}
public function test_export_a_collection() {
@ -132,6 +224,47 @@ class TAINACAN_REST_Export_Controller extends TAINACAN_UnitApiTestCase {
$i++;
}
}
/**
* @group export_all
*/
public function test_export_all() {
extract($this->create_requirements());
$item_exposer_json = json_encode([
'exposer-type' => 'Xml',
'exposer-map' => 'Value',
'export-background' => false
]);
$query = [
'orderby' => 'id',
'order' => 'asc',
];
$request = new \WP_REST_Request('GET', $this->namespace . '/export' );
$request->set_query_params($query);
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertInstanceOf('SimpleXMLElement', $xml = @simplexml_load_string($data));
var_dump($data);
/*
$this->assertEquals(3, $xml->count());
$i = 0;
foreach ($xml->children() as $xml_item ) {
$metadata = $items[$i]->get_metadata();
foreach ($metadata as $metadatum_meta) {
$metadatum = $metadatum_meta->get_metadatum();
$this->assertEquals($metadatum_meta->get_value(), $xml_item->{$metadatum->get_name()});
//echo "{$metadatum->get_name()}:{$metadatum_meta->get_value()}"; // uncomment if need debug
}
$i++;
}*/
}
}
?>