From ad347408501d267abc7497c22b8186043151957e Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Wed, 2 May 2018 21:55:41 -0300 Subject: [PATCH] begin export xml --- .../class-tainacan-rest-export-controller.php | 197 ++++++++++++++++++ src/api/tainacan-rest-creator.php | 1 + src/exposers/class-tainacan-exposers.php | 6 +- tests/test-api-export.php | 117 +++++++++++ 4 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 src/api/endpoints/class-tainacan-rest-export-controller.php create mode 100644 tests/test-api-export.php diff --git a/src/api/endpoints/class-tainacan-rest-export-controller.php b/src/api/endpoints/class-tainacan-rest-export-controller.php new file mode 100644 index 000000000..6d18844d5 --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-export-controller.php @@ -0,0 +1,197 @@ +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/, then + * a field will be created in matched collection and all your item will receive this field + * + * If POST on field/item/, 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[\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[\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); + } + +} + +?> \ No newline at end of file diff --git a/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index 45c1a18f0..fb0240d5e 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -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_field_types_controller = new \Tainacan\API\EndPoints\REST_Field_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 ?> \ No newline at end of file diff --git a/src/exposers/class-tainacan-exposers.php b/src/exposers/class-tainacan-exposers.php index 8dbe3f406..74cf66cac 100644 --- a/src/exposers/class-tainacan-exposers.php +++ b/src/exposers/class-tainacan-exposers.php @@ -142,7 +142,11 @@ class Exposers { } else { // array of elements $ret = []; foreach ($item_arr as $item) { - $ret = array_merge($ret, $this->map($item, $mapper, $resquest) ); + if(array_key_exists('field', $item)) { + $ret = array_merge($ret, $this->map($item, $mapper, $resquest) ); + } else { + $ret[] = $this->map($item, $mapper, $resquest); + } } } return $ret; diff --git a/tests/test-api-export.php b/tests/test-api-export.php new file mode 100644 index 000000000..59041e9b1 --- /dev/null +++ b/tests/test-api-export.php @@ -0,0 +1,117 @@ +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); + } +} + +?> \ No newline at end of file