Refactoring fetch all metadatatum value and fixes not removing of duplicated values

This commit is contained in:
weryques 2018-08-06 13:42:31 -03:00
parent 28f7b1cab5
commit 5b40cd1f71
4 changed files with 76 additions and 56 deletions

View File

@ -31,6 +31,14 @@
&:hover { background-color: $gray1; }
.is-small { color: $gray4; }
&.is-active { background-color: $turquoise2; }
max-width: 150px;
.media-content {
overflow-x: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
}

View File

@ -38,8 +38,7 @@ export const filter_type_mixin = {
let sResults = [];
let opts = [];
if (res.data && res.data[0]) {
for (let metadata of res.data[0]) {
for (let metadata of res.data) {
if (valuesToIgnore != undefined && valuesToIgnore.length > 0) {
let indexToIgnore = valuesToIgnore.findIndex(value => value == metadata.mvalue);
@ -70,7 +69,7 @@ export const filter_type_mixin = {
}
}
this.searchResults = sResults;

View File

@ -822,29 +822,22 @@ class Metadata extends Repository {
$title = $item->get_title();
if(!empty($search) && stristr($title, $search) !== false) {
$return[] = [ 'item_id' => $item->get_id(), 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
} elseif (empty($search)) {
$return[] = [ 'item_id' => $item->get_id(), 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ];
}
} else {
$description = $item->get_description();
if(!empty($search) && stristr($description, $search) !== false) {
$return[] = [ 'item_id' => $item->get_id(), 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
} elseif (empty($search)) {
$return[] = [ 'item_id' => $item->get_id(), 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
$return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ];
}
}
}
$results = [];
if (!empty($return)) {
$return = $this->unique_multidimensional_array($return, 'mvalue');
$results[] = $return;
}
return $results;
return $return;
}
$item_post_type = "%%{$collection_id}_item";
@ -880,7 +873,7 @@ class Metadata extends Repository {
if($collection_id) {
$sql_string = $wpdb->prepare(
"SELECT item_id, metadatum_id, mvalue
"SELECT DISTINCT metadatum_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
@ -895,7 +888,7 @@ class Metadata extends Repository {
);
} else {
$sql_string = $wpdb->prepare(
"SELECT item_id, metadatum_id, mvalue
"SELECT DISTINCT metadatum_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
@ -912,10 +905,10 @@ class Metadata extends Repository {
$pre_result = $wpdb->get_results( $sql_string, ARRAY_A );
$pre_result = $this->unique_multidimensional_array($pre_result, 'mvalue');
if (!empty($pre_result)) {
$results[] = $pre_result;
foreach ($pre_result as $pre){
$results[] = $pre;
}
}
}
} elseif ( current_user_can( $capabilities->read_private_posts) ) {
@ -929,7 +922,7 @@ class Metadata extends Repository {
if($collection_id) {
$sql_string = $wpdb->prepare(
"SELECT item_id, metadatum_id, mvalue
"SELECT DISTINCT metadatum_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
@ -944,7 +937,7 @@ class Metadata extends Repository {
);
} else {
$sql_string = $wpdb->prepare(
"SELECT item_id, metadatum_id, mvalue
"SELECT DISTINCT metadatum_id, mvalue
FROM (
SELECT ID as item_id
FROM $wpdb->posts
@ -961,15 +954,16 @@ class Metadata extends Repository {
$pre_result = $wpdb->get_results( $sql_string, ARRAY_A );
$pre_result = $this->unique_multidimensional_array($pre_result, 'mvalue');
if (!empty($pre_result)) {
$results[] = $pre_result;
foreach ($pre_result as $pre){
$results[] = $pre;
}
}
}
}
return $results;
//return $results;
return $this->unique_multidimensional_array($results, 'mvalue');
}
/**

View File

@ -439,6 +439,17 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
true
);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name3',
'description' => 'No description3',
'status' => 'private',
'collection' => $collection
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
@ -463,6 +474,13 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$item_metadata2->validate();
$Tainacan_Item_Metadata->insert($item_metadata2);
// Is repeated for test return of duplicates
$item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item3, $metadatum);
$item_metadata3->set_value('12/12/2017');
$item_metadata3->validate();
$Tainacan_Item_Metadata->insert($item_metadata3);
//=======================
$query = [
@ -485,7 +503,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data1 = $response1->get_data();
$this->assertCount(1, $data1);
$this->assertEquals('12/12/2017', $data1[0][0]['mvalue']);
$this->assertEquals('12/12/2017', $data1[0]['mvalue']);
//=======================
@ -497,7 +515,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data1 = $response1->get_data();
$this->assertCount(1, $data1);
$this->assertEquals('12/12/2017', $data1[0][0]['mvalue']);
$this->assertEquals('12/12/2017', $data1[0]['mvalue']);
//=======================
@ -508,9 +526,10 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data2 = $response2->get_data();
// Only two without duplicates
$this->assertCount(2, $data2);
$this->assertEquals('12/12/2017', $data2[0][0]['mvalue']);
$this->assertEquals('02/03/2018', $data2[1][0]['mvalue']);
$this->assertEquals('12/12/2017', $data2[0]['mvalue']);
$this->assertEquals('02/03/2018', $data2[1]['mvalue']);
}
}