refactor filter metadatum_id getters and setters

This commit is contained in:
Leo Germani 2019-06-03 11:41:44 -03:00
parent ec8d584299
commit 58dc05317f
13 changed files with 156 additions and 49 deletions

View File

@ -136,25 +136,25 @@ class REST_Filters_Controller extends REST_Controller {
$metadatum_id = $request['metadatum_id'];
$filter_obj->set_collection_id( $collection_id );
$filter_obj->set_metadatum( $metadatum_id );
$filter_obj->set_metadatum_id( $metadatum_id );
} elseif (isset($request['collection_id'])){
$collection_id = $request['collection_id'];
$filter_obj->set_collection_id( $collection_id );
if(!isset($body['metadatum'])){
if(!isset($body['metadatum_id'])){
throw new \InvalidArgumentException('You need provide a metadatum id');
}
$filter_obj->set_metadatum($body['metadatum']);
$filter_obj->set_metadatum_id($body['metadatum_id']);
} else {
$filter_obj->set_collection_id( 'filter_in_repository' );
if(!isset($body['metadatum'])){
if(!isset($body['metadatum_id'])){
throw new \InvalidArgumentException('You need provide a metadatum id');
}
$filter_obj->set_metadatum($body['metadatum']);
$filter_obj->set_metadatum_id($body['metadatum_id']);
}
$filter_obj->set_filter_type($filter_type);

View File

@ -288,9 +288,10 @@ class REST_Metadata_Controller extends REST_Controller {
$item_arr['metadata_type_object'] = $item->get_metadata_type_object()->_toArray();
if(isset($item_arr['metadata_type_options']) && isset($item_arr['metadata_type_options']['taxonomy_id'])){
$taxonomy = new Entities\Taxonomy($item_arr['metadata_type_options']['taxonomy_id']);
$item_arr['metadata_type_options']['taxonomy'] = $taxonomy->get_db_identifier();
$taxonomy = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id( $item_arr['metadata_type_options']['taxonomy_id'] );
//$taxonomy = new Entities\Taxonomy($item_arr['metadata_type_options']['taxonomy_id']);
//$item_arr['metadata_type_options']['taxonomy'] = $taxonomy->get_db_identifier();
$item_arr['metadata_type_options']['taxonomy'] = $taxonomy;
}
if($request['context'] === 'edit'){

View File

@ -16,6 +16,7 @@ class Filter extends Entity {
$order,
$color,
$metadatum,
$metadatum_id,
$max_options,
$filter_type,
$filter_type_options;
@ -40,7 +41,7 @@ class Filter extends Entity {
*/
public function _toArray(){
$filter_array = parent::_toArray();
$metadatum_id = $filter_array['metadatum'];
$metadatum_id = $filter_array['metadatum_id'];
$metadatum = $this->get_metadatum();
$filter_array['metadatum'] = [];
@ -106,14 +107,33 @@ class Filter extends Entity {
}
/**
* Return the metadatum
* Return the metadatum ID
*
* @return Metadatum
* @return integer Metadatum ID
*/
function get_metadatum_id() {
return $this->get_mapped_property('metadatum_id');
}
/**
* Return the metadatum object
*
* @return Metadatum | null
* @throws \Exception
*/
function get_metadatum() {
$id = $this->get_mapped_property('metadatum');
return new Metadatum( $id );
if (isset($this->metadatum)) {
return $this->metadatum;
}
$id = $this->get_metadatum_id();
return new Metadatum($id);
$metadatum = \Tainacan\Repositories\Metadata::get_instance()->fetch((int) $id);
if ($metadatum instanceof Metadatum) {
$this->metadatum = $metadatum;
return $metadatum;
} else {
return null;
}
}
/**
@ -192,15 +212,27 @@ class Filter extends Entity {
}
/**
* Define the filter metadatum
* Define the filter metadatum passing an object
*
* @param \Tainacan\Entities\Metadatum
* @return void
*/
function set_metadatum( $value ){
$id = ( $value instanceof Metadatum ) ? $value->get_id() : $value;
function set_metadatum( \Tainacan\Entities\Metadatum $value ){
$id = $value->get_id();
$this->set_mapped_property('metadatum', $id);
$this->set_metadatum_id($id);
$this->metadatum = $value;
}
/**
* Define the filter metadatum passing an ID
*
* @param int $value the metadatum ID
* @return void
*/
function set_metadatum_id( $value ){
unset($this->metadatum);
$this->set_mapped_property('metadatum_id', $value);
}
/**

View File

@ -64,8 +64,7 @@ class Checkbox extends Filter_Type {
public function render( $filter ){
return '<tainacan-filter-checkbox name="'.$filter->get_name().'"
filter_type="'.$filter->get_metadatum()->get_metadata_type().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-selectbox>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-selectbox>';
}
}

View File

@ -89,7 +89,7 @@ class Numeric extends Filter_Type {
step="' . $this->get_option('step') . '"
name="'.$filter->get_name().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-custom-interval>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-custom-interval>';
}

View File

@ -32,8 +32,7 @@ class Selectbox extends Filter_Type {
public function render( $filter ){
return '<tainacan-filter-selectbox name="'.$filter->get_name().'"
filter_type="'.$filter->get_metadatum()->get_metadata_type().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-selectbox>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-selectbox>';
}
}

View File

@ -62,8 +62,7 @@ class Taginput extends Filter_Type {
public function render( $filter ){
return '<tainacan-filter-taginput name="'.$filter->get_name().'"
filter_type="'.$filter->get_metadatum()->get_metadata_type().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-taginput>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taginput>';
}
}

View File

@ -64,8 +64,7 @@ class TaxonomyCheckbox extends Filter_Type {
public function render( $filter ){
return '<tainacan-filter-taxonomy-checkbox name="'.$filter->get_name().'"
filter_type="'.$filter->get_metadatum()->get_metadata_type().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-taxonomy-checkbox>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taxonomy-checkbox>';
}
}

View File

@ -62,8 +62,7 @@ class TaxonomyTaginput extends Filter_Type {
public function render( $filter ){
return '<tainacan-filter-taxonomy-taginput name="'.$filter->get_name().'"
filter_type="'.$filter->get_metadatum()->get_metadata_type().'"
collection_id="'.$filter->get_collection_id().'"
metadatum_id="'.$filter->get_metadatum()->get_id().'"></tainacan-filter-taxonomy-taginput>';
metadatum_id="'.$filter->get_metadatum_id().'"></tainacan-filter-taxonomy-taginput>';
}
}

View File

@ -80,10 +80,10 @@ class Filters extends Repository {
'description' => __( 'Filter color', 'tainacan' ),
'validation' => ''
],
'metadatum' => [
'metadatum_id' => [
'map' => 'meta',
'title' => __( 'Metadata', 'tainacan' ),
'type' => 'array/object/string',
'type' => 'integer',
'description' => __( 'Filter metadata', 'tainacan' ),
'validation' => ''
],

View File

@ -338,6 +338,16 @@ class Migrations {
flush_rewrite_rules(false);
}
static function update_filters_definition() {
global $wpdb;
$wpdb->query("UPDATE $wpdb->postmeta SET meta_key = 'metadatum_id' WHERE
meta_key = 'metadatum' AND post_id IN (
SELECT ID FROM $wpdb->posts WHERE post_type = 'tainacan-filter'
)");
}
}

View File

@ -70,8 +70,6 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
)
);
$filter_type = $this->tainacan_filter_factory->create_filter('custom_interval');
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -79,7 +77,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
'collection' => $collection,
'description' => 'descricao',
'metadatum' => $metadatum,
'filter_type' => $filter_type,
'filter_type' => 'Tainacan\Filter_Types\Custom_Interval',
),
true
);
@ -296,7 +294,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
'description' => 'Description of 2x Filter',
'status' => 'publish'
],
'metadatum' => $metadatum2->get_id()
'metadatum_id' => $metadatum2->get_id()
]);
$filter_attr2 = json_encode([
@ -306,7 +304,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
'description' => 'Description of 4x Filter',
'status' => 'publish'
],
'metadatum' => $metadatum->get_id()
'metadatum_id' => $metadatum->get_id()
]);
#### CREATE A FILTER IN REPOSITORY ####

View File

@ -65,7 +65,6 @@ class Filters extends TAINACAN_UnitTestCase {
true
);
$filter_list_type = $this->tainacan_filter_factory->create_filter('selectbox');
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
@ -74,7 +73,7 @@ class Filters extends TAINACAN_UnitTestCase {
'collection' => $collection,
'description' => 'descricao',
'metadatum' => $metadatum,
'filter_type' => $filter_list_type
'filter_type' => 'Tainacan\Filter_Types\Selectbox'
),
true
);
@ -89,9 +88,8 @@ class Filters extends TAINACAN_UnitTestCase {
$this->assertEquals( 'filtro', $test->get_name() );
$this->assertEquals( $collection->get_id(), $test->get_collection_id() );
$this->assertEquals( $metadatum->get_id(), $test->get_metadatum()->get_id() );
$objClass = get_class( $filter_list_type );
$storedObjClass = get_class( $test->get_filter_type_object() );
$this->assertEquals($objClass , $storedObjClass );
$this->assertEquals('Tainacan\Filter_Types\Selectbox' , $storedObjClass );
}
@ -131,8 +129,6 @@ class Filters extends TAINACAN_UnitTestCase {
true
);
$autocomplete = $this->tainacan_filter_factory->create_filter('autocomplete');
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
@ -140,7 +136,7 @@ class Filters extends TAINACAN_UnitTestCase {
'collection' => $collection,
'description' => 'descricao',
'metadatum' => $metadatum2,
'filter_type' => $autocomplete
'filter_type' => 'Tainacan\Filter_Types\Autocomplete'
),
true
);
@ -149,14 +145,12 @@ class Filters extends TAINACAN_UnitTestCase {
$this->assertEquals( 'Tainacan\Filter_Types\Autocomplete', $test->get_filter_type());
$custom_interval = $this->tainacan_filter_factory->create_filter('custom_interval');
$filter2 = new \Tainacan\Entities\Filter();
$filter2->set_name('filter 2');
$filter2->set_collection($collection);
$filter2->set_description('description');
$filter2->set_metadatum($metadatum2);
$filter2->set_filter_type($custom_interval);
$filter2->set_filter_type('Tainacan\Filter_Types\Custom_Interval');
$this->assertFalse($filter2->validate(), 'filter with a metadatum with unsupported primitive type should not validate');
@ -186,7 +180,7 @@ class Filters extends TAINACAN_UnitTestCase {
'name' => 'filter default',
'collection_id' => 'filter_in_repository',
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
'metadatum' => $meta_repo->get_id(),
'metadatum_id' => $meta_repo->get_id(),
'status' => 'publish'
),
true
@ -216,7 +210,7 @@ class Filters extends TAINACAN_UnitTestCase {
'name' => 'filter grandfather',
'collection_id' => $collection_grandfather->get_id(),
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
'metadatum' => $meta_grand->get_id(),
'metadatum_id' => $meta_grand->get_id(),
'status' => 'publish'
),
true
@ -247,7 +241,7 @@ class Filters extends TAINACAN_UnitTestCase {
'name' => 'filter father',
'collection_id' => $collection_father->get_id(),
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
'metadatum' => $meta_father->get_id(),
'metadatum_id' => $meta_father->get_id(),
'status' => 'publish'
),
true
@ -281,7 +275,7 @@ class Filters extends TAINACAN_UnitTestCase {
'name' => 'filter son',
'collection_id' => $collection_son->get_id(),
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
'metadatum' => $meta_son->get_id(),
'metadatum_id' => $meta_son->get_id(),
'status' => 'publish'
),
true
@ -295,5 +289,82 @@ class Filters extends TAINACAN_UnitTestCase {
$this->assertEquals( 4, sizeof( $retrieve_filters ) );
$this->assertEquals( 4, sizeof( $retrieve_filters_ids ) );
}
function test_metadatum_getter_setter(){
$Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Collection filtered',
'description' => 'Is filtered',
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Other filtered',
'description' => 'Is filtered',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'collection_id' => $collection->get_id()
),
true
);
$filter2 = new \Tainacan\Entities\Filter();
$filter2->set_name('filter 2');
$filter2->set_collection($collection);
$filter2->set_description('description');
$filter2->set_metadatum($metadatum2);
$filter2->set_filter_type('Tainacan\Filter_Types\Selectbox');
$filter2->validate();
$filter = $Tainacan_Filters->insert($filter2);
$this->assertEquals($metadatum2->get_id(), $filter->get_metadatum_id());
$this->assertEquals($metadatum2->get_name(), $filter->get_metadatum()->get_name());
$filter3 = new \Tainacan\Entities\Filter();
$filter3->set_name('filter 3');
$filter3->set_collection($collection);
$filter3->set_description('description');
$filter3->set_metadatum_id($metadatum2->get_id());
$filter3->set_filter_type('Tainacan\Filter_Types\Selectbox');
$filter3->validate();
$filter = $Tainacan_Filters->insert($filter3);
$this->assertEquals($metadatum2->get_id(), $filter->get_metadatum_id());
$this->assertEquals($metadatum2->get_name(), $filter->get_metadatum()->get_name());
$metadatum3 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'Other filtered',
'description' => 'Is filtered',
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'collection_id' => $collection->get_id()
),
true
);
$filter4 = new \Tainacan\Entities\Filter();
$filter4->set_name('filter 4');
$filter4->set_metadatum_id($metadatum3->get_id());
$this->assertEquals($metadatum3->get_id(), $filter4->get_metadatum_id());
$this->assertEquals($metadatum3->get_name(), $filter4->get_metadatum()->get_name());
$filter4->set_metadatum($metadatum2);
$this->assertEquals($metadatum2->get_id(), $filter4->get_metadatum_id());
$this->assertEquals($metadatum2->get_name(), $filter4->get_metadatum()->get_name());
}
}