diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index ae0ab76e9..a4c8ed921 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -105,7 +105,17 @@ class REST_Items_Controller extends REST_Controller { 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array($this, 'duplicate_item'), 'permission_callback' => array($this, 'create_item_permissions_check'), - 'args' => $this->get_collection_params(), + 'args' => array( + 'copies' => array( + 'description' => __('Number of copies to be created', 'tainacan'), + 'default' => 1, + 'type' => 'integer' + ), + 'status' => array( + 'description' => __('Try to assign the informed status to the duplicates if they validate. By default it will save them as drafts.', 'tainacan'), + 'type' => 'string' + ), + ) ), ) ); @@ -648,49 +658,85 @@ class REST_Items_Controller extends REST_Controller { $item_id = $request['item_id']; $item = $this->items_repository->fetch($item_id); + + $defaults = [ + 'copies' => 1, + 'status' => 'draft' + ]; + + $body = json_decode($request->get_body(), true); + + if (!is_array($body)) { + $body = []; + } + + $args = array_merge($defaults, $body); + if ($item) { - $new_item = new Entities\Item(); - $items_repo = Repositories\Items::get_instance(); + $response = [ + 'items' => [] + ]; - $new_item->set_title( $item->get_title() ); - $new_item->set_description( $item->get_description() ); - $new_item->set_status( $item->get_status() ); - $new_item->set_collection_id( $item->get_collection_id() ); - - if ( $new_item->validate() ) { + for ($i=1; $i<=$args['copies']; $i++) { - $new_item = $items_repo->insert($new_item); + $new_item = new Entities\Item(); + $items_repo = Repositories\Items::get_instance(); - $metadata = $item->get_metadata(); + $new_item->set_status( 'draft' ); - $errors = []; + $new_item->set_title( $item->get_title() ); + $new_item->set_description( $item->get_description() ); + $new_item->set_collection_id( $item->get_collection_id() ); - foreach ($metadata as $item_metadatum) { + if ( $new_item->validate() ) { - $new_item_metadatum = new Entities\Item_Metadata_Entity( $new_item, $item_metadatum->get_metadatum() ); - $new_item_metadatum->set_value( $item_metadatum->get_value() ); + $new_item = $items_repo->insert($new_item); - if ( $new_item_metadatum->validate() ) { - Repositories\Item_Metadata::get_instance()->insert( $new_item_metadatum ); - } else { - $errors[] = $new_item_metadatum->get_errors(); + $metadata = $item->get_metadata(); + + $errors = []; + + foreach ($metadata as $item_metadatum) { + + $new_item_metadatum = new Entities\Item_Metadata_Entity( $new_item, $item_metadatum->get_metadatum() ); + $new_item_metadatum->set_value( $item_metadatum->get_value() ); + + if ( $new_item_metadatum->validate() ) { + Repositories\Item_Metadata::get_instance()->insert( $new_item_metadatum ); + } else { + $errors[] = $new_item_metadatum->get_errors(); + } + } + if ($args['status'] != 'draft') { + + $new_item->set_status( $args['status'] ); + + if ( $new_item->validate() ) { + $new_item = $items_repo->insert($new_item); + } else { + $new_item->set_status( 'draft' ); + } + + } + + $response['items'][] = $this->prepare_item_for_response($new_item, $request); + + do_action('tainacan-api-item-duplicated', $item, $new_item); + + } else { + return new \WP_REST_Response([ + 'error_message' => __('Error duplicating item', 'tainacan'), + 'errors' => $new_item->get_errors(), + 'item' => $this->prepare_item_for_response($item, $request) + ], 400); } - } else { - return new \WP_REST_Response([ - 'error_message' => __('Error duplicating item', 'tainacan'), - 'errors' => $new_item->get_errors(), - 'item' => $this->prepare_item_for_response($item, $request) - ], 400); } - - do_action('tainacan-api-item-duplicated', $item, $new_item); - - return new \WP_REST_Response($this->prepare_item_for_response($new_item, $request), 201); + return new \WP_REST_Response($response, 201); } diff --git a/tests/test-api-items.php b/tests/test-api-items.php index 591a4fe1b..2fc5cf371 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -609,11 +609,11 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $data = $response->get_data(); - $this->assertEquals('Lean Startup', $data['title']); + $this->assertEquals('Lean Startup', $data['items'][0]['title']); $metadata_1 = $item1->get_metadata(); - $duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $data['id'] ); + $duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $data['items'][0]['id'] ); $metadata_2 = $duplicated->get_metadata(); @@ -621,6 +621,82 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( $m->get_value(), $metadata_2[$k]->get_value() ); } + + $request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate'); + + $params = json_encode([ + 'copies' => 4, + 'status' => 'publish' + ]); + + $request->set_body($params); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals(4, count($data['items'])); + + foreach ( $data['items'] as $created_item ) { + $duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $created_item['id'] ); + + $this->assertEquals('publish', $created_item['status']); + + $metadata_2 = $duplicated->get_metadata(); + + foreach( $metadata_1 as $k => $m ) { + $this->assertEquals( $m->get_value(), $metadata_2[$k]->get_value() ); + } + } + + + // Create a required metadata + $required_meta = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'required_meta', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'multiple' => 'no', + 'required' => 'yes', + 'status' => 'publish' + ), + true + ); + + // $metas = \Tainacan\Repositories\Metadata::get_instance()->fetch_by_collection($collection, [], 'OBJECT'); + // + // foreach ($metas as $m) { + // var_dump($m->get_name()); + // } + + $request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate'); + + $params = json_encode([ + 'copies' => 3, + 'status' => 'publish' + ]); + + $request->set_body($params); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals(3, count($data['items'])); + + foreach ( $data['items'] as $created_item ) { + $duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $created_item['id'] ); + + // item does not validate because required meta is empty and stays draft + $this->assertEquals('draft', $duplicated->get_status()); + $this->assertEquals('draft', $created_item['status']); + + } + + + + } }