Fetch all values of a field from a collection in all it collection items

This commit is contained in:
weryques 2018-03-02 11:42:19 -03:00
parent 048227261e
commit 9cff5a736d
3 changed files with 184 additions and 1 deletions

View File

@ -46,12 +46,17 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
),
// ENDPOINT X. THIS ENDPOINT DO THE SAME THING OF ENDPOINT Z. I hope that in a brief future it function changes.
// ENDPOINT X. THIS ENDPOINT DO THE SAME THING OF ENDPOINT Z. I hope in a brief future it function changes.
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check')
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check')
)
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base,
@ -95,6 +100,41 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
);
}
/**
* @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);
}
return new WP_REST_Response([
'error_message' => __('Verify the route. A query parameter is missing', 'tainacan'),
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function get_item_permissions_check( $request ) {
if($request['context'] === 'edit' && !$this->field_repository->can_read(new Entities\Field())){
return false;
}
return true;
}
/**
* @param WP_REST_Request $request
*

View File

@ -583,4 +583,51 @@ class Fields extends Repository {
throw new \ErrorException('The entity wasn\'t validated.' . print_r( $field->get_errors(), true));
}
}
/**
* Fetch all values of a field from a collection in all it collection items
*
* @param $collection_id
* @param $field_id
*
* @return array|null|object
*/
public function fetch_all_field_values($collection_id, $field_id){
global $wpdb;
// Clear the result cache
$wpdb->flush();
$item_post_type = "%{$collection_id}_item";
$sql_string = (current_user_can( "read_private_tnc_col_{$collection_id}_items" ) && current_user_can( 'read_private_tainacan-collections' )) ? $wpdb->prepare(
"SELECT item_id, field_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
WHERE post_type LIKE %s
) items
JOIN (
SELECT meta_key as field_id, meta_value as mvalue, post_id
FROM $wpdb->postmeta
) metas
ON items.item_id = metas.post_id AND metas.field_id = %s", $item_post_type, $field_id
) : $wpdb->prepare(
"SELECT item_id, field_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
WHERE post_type LIKE %s AND post_status <> 'private'
) items
JOIN (
SELECT meta_key as field_id, meta_value as mvalue, post_id
FROM $wpdb->postmeta
) metas
ON items.item_id = metas.post_id AND metas.field_id = %s", $item_post_type, $field_id
);
$results = $wpdb->get_results($sql_string, ARRAY_A);
return $results;
}
}

View File

@ -333,6 +333,102 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('No name', $data['name']);
}
public function test_fetch_all_field_values(){
global $Tainacan_Fields, $Tainacan_Item_Metadata;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Statement',
'description' => 'No Statement',
'status' => 'publish'
),
true
);
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name1',
'description' => 'No description1',
'status' => 'publish',
'collection' => $collection
),
true
);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name2',
'description' => 'No description2',
'status' => 'private',
'collection' => $collection
),
true
);
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'Data',
'description' => 'Descreve valor do campo data.',
'collection' => $collection,
'status' => 'publish',
'field_type' => 'Tainacan\Field_Types\Text',
),
true
);
$item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($item1, $field);
$item_metadata1->set_value('12/12/2017');
$item_metadata1->validate();
$Tainacan_Item_Metadata->insert($item_metadata1);
$item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item2, $field);
$item_metadata2->set_value('02/03/2018');
$item_metadata2->validate();
$Tainacan_Item_Metadata->insert($item_metadata2);
//=======================
$query = [
'fetch' => 'all_field_values'
];
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field->get_id()
);
$request->set_query_params($query);
//=======================
$new_user1 = $this->factory()->user->create(array( 'role' => 'subscriber' ));
wp_set_current_user($new_user1);
$response1 = $this->server->dispatch($request);
$data1 = $response1->get_data();
$this->assertCount(1, $data1);
$this->assertEquals('12/12/2017', $data1[0]['mvalue']);
//=======================
$new_user2 = $this->factory()->user->create(array( 'role' => 'administrator' ));
wp_set_current_user($new_user2);
$response2 = $this->server->dispatch($request);
$data2 = $response2->get_data();
$this->assertCount(2, $data2);
$this->assertEquals('12/12/2017', $data2[0]['mvalue']);
$this->assertEquals('02/03/2018', $data2[1]['mvalue']);
}
}
?>