diff --git a/src/class-tainacan.php b/src/class-tainacan.php index e607e7579..23363e544 100644 --- a/src/class-tainacan.php +++ b/src/class-tainacan.php @@ -10,6 +10,7 @@ Version: 10.9.8.7.6.5.4 const ENTITIES_DIR = __DIR__ . '/classes/entities/'; const FIELD_TYPES_DIR = __DIR__ . '/classes/field-types/'; +const FILTER_TYPES_DIR = __DIR__ . '/classes/filter-types/'; const REPOSITORIES_DIR = __DIR__ . '/classes/repositories/'; const TRAITS_DIR = __DIR__ . '/classes/traits/'; const CLASSES_DIR = __DIR__ . '/classes/'; @@ -17,7 +18,8 @@ const CLASSES_DIR = __DIR__ . '/classes/'; const DIRS = [ CLASSES_DIR, ENTITIES_DIR, - FIELD_TYPES_DIR, + FIELD_TYPES_DIR, + FILTER_TYPES_DIR, REPOSITORIES_DIR, TRAITS_DIR, ]; @@ -44,6 +46,9 @@ $Tainacan_Item_Metadata = new Tainacan_Item_Metadata(); global $Tainacan_Metadatas; $Tainacan_Metadatas = new Tainacan_Metadatas(); +global $Tainacan_Filters; +$Tainacan_Filters = new Tainacan_Filters(); + global $Tainacan_Taxonomies; $Tainacan_Taxonomies = new Tainacan_Taxonomies(); diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index a52c8f697..3a14ef89b 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -38,6 +38,11 @@ class Tainacan_Filter extends Entity { return $this->get_mapped_property('order'); } + + function get_color() { + return $this->get_mapped_property('color'); + } + function get_widget( $output = 'object' ){ if( $output === 'object'){ return unserialize( $this->get_mapped_property('option') ); @@ -46,7 +51,23 @@ class Tainacan_Filter extends Entity { } } - //Setters + // Setters + function set_name($value) { + return $this->set_mapped_property('name', $value); + } + + function set_order($value) { + return $this->set_mapped_property('order', $value); + } + + function set_description($value) { + return $this->set_mapped_property('description', $value); + } + + function set_color($value) { + return $this->set_mapped_property('parent', $value); + } + function set_widget($value){ if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Filter_Type' ) ){ $this->set_option( $value ); diff --git a/src/classes/field-types/class-tainacan-checkbox-field-type.php b/src/classes/field-types/class-tainacan-checkbox-field-type.php new file mode 100644 index 000000000..c2c89f257 --- /dev/null +++ b/src/classes/field-types/class-tainacan-checkbox-field-type.php @@ -0,0 +1,23 @@ +primitive_type = 'date'; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-date-field-type.php b/src/classes/field-types/class-tainacan-date-field-type.php new file mode 100644 index 000000000..a8110346d --- /dev/null +++ b/src/classes/field-types/class-tainacan-date-field-type.php @@ -0,0 +1,23 @@ +primitive_type = 'date'; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-field-type.php b/src/classes/field-types/class-tainacan-field-type.php index 4f91de2e0..54bfbb756 100644 --- a/src/classes/field-types/class-tainacan-field-type.php +++ b/src/classes/field-types/class-tainacan-field-type.php @@ -8,6 +8,8 @@ if ( ! defined( 'ABSPATH' ) ) { */ abstract class Tainacan_Field_Type { + var $primitive_type; + abstract function render( $metadata ); function validate($value) { diff --git a/src/classes/field-types/class-tainacan-numeric-field-type.php b/src/classes/field-types/class-tainacan-numeric-field-type.php new file mode 100644 index 000000000..e7433c368 --- /dev/null +++ b/src/classes/field-types/class-tainacan-numeric-field-type.php @@ -0,0 +1,23 @@ +primitive_type = 'float'; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-radio-field-type.php b/src/classes/field-types/class-tainacan-radio-field-type.php new file mode 100644 index 000000000..2ce19e1aa --- /dev/null +++ b/src/classes/field-types/class-tainacan-radio-field-type.php @@ -0,0 +1,23 @@ +primitive_type = ''; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-relationship-field-type.php b/src/classes/field-types/class-tainacan-relationship-field-type.php new file mode 100644 index 000000000..1b688f2cd --- /dev/null +++ b/src/classes/field-types/class-tainacan-relationship-field-type.php @@ -0,0 +1,23 @@ +primitive_type = ''; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-selectbox-field-type.php b/src/classes/field-types/class-tainacan-selectbox-field-type.php new file mode 100644 index 000000000..7b716019a --- /dev/null +++ b/src/classes/field-types/class-tainacan-selectbox-field-type.php @@ -0,0 +1,23 @@ +primitive_type = ''; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/field-types/class-tainacan-text-field-type.php b/src/classes/field-types/class-tainacan-text-field-type.php index ba2af2962..8ea57ee7a 100644 --- a/src/classes/field-types/class-tainacan-text-field-type.php +++ b/src/classes/field-types/class-tainacan-text-field-type.php @@ -8,6 +8,10 @@ if ( ! defined( 'ABSPATH' ) ) { */ class Tainacan_Text_Field_Type extends Tainacan_Field_Type { + function __construct(){ + $this->primitive_type = 'string'; + } + /** * @param $metadata * @return string diff --git a/src/classes/field-types/class-tainacan-textarea-field-type.php b/src/classes/field-types/class-tainacan-textarea-field-type.php new file mode 100644 index 000000000..74ec1a037 --- /dev/null +++ b/src/classes/field-types/class-tainacan-textarea-field-type.php @@ -0,0 +1,23 @@ +primitive_type = 'string'; + } + + /** + * @param $metadata + * @return string + */ + + function render( $metadata ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/filter-types/class-tainacan-filter-type.php b/src/classes/filter-types/class-tainacan-filter-type.php index dcd6544d0..d493ac79f 100644 --- a/src/classes/filter-types/class-tainacan-filter-type.php +++ b/src/classes/filter-types/class-tainacan-filter-type.php @@ -5,13 +5,7 @@ if ( ! defined( 'ABSPATH' ) ) { abstract class Tainacan_Filter_Type extends Entity { + var $supported_types = []; + abstract function render( $metadata ); - - function validate( $value ) { - return true; - } - - function get_validation_errors() { - return []; - } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index a4bc8bb66..fb0ae2dec 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -28,26 +28,25 @@ class Tainacan_Filters { 'map' => 'meta', 'validation' => '' ], - 'mask' => [ - 'map' => 'meta', - 'validation' => '' - ], - 'option' => [ - 'map' => 'meta', + 'option' => [ + 'map' => 'meta', 'validation' => '' ], 'collection_id' => [ 'map' => 'meta', 'validation' => '' ], + 'color' => [ + 'map' => 'meta', + 'validation' => '' + ], ]; function __construct(){ add_action('init', array(&$this, 'register_post_type')); } - function register_post_type() - { + function register_post_type(){ $labels = array( 'name' => 'Filter', 'singular_name' => 'Filter', @@ -82,4 +81,74 @@ class Tainacan_Filters { ); register_post_type(self::POST_TYPE, $args); } + + + /** + * @param Tainacan_Metadata $metadata + * @return int + */ + function insert( Tainacan_Filter $metadata ) { + // First iterate through the native post properties + $map = $this->map; + foreach ($map as $prop => $mapped) { + if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { + $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop); + } + } + + // save post and get its ID + $metadata->WP_Post->post_type = self::POST_TYPE; + $metadata->WP_Post->post_status = 'publish'; + $id = wp_insert_post($metadata->WP_Post); + $metadata->WP_Post = get_post($id); + + // Now run through properties stored as postmeta + foreach ($map as $prop => $mapped) { + if ($mapped['map'] == 'meta') { + update_post_meta($id, $prop, $metadata->get_mapped_property($prop)); + } elseif ($mapped['map'] == 'meta_multi') { + $values = $metadata->get_mapped_property($prop); + delete_post_meta($id, $prop); + if (is_array($values)) + foreach ($values as $value) + add_post_meta($id, $prop, $value); + } + } + + // return a brand new object + return new Tainacan_Filter($metadata->WP_Post); + } + + + /** + * @param ( Tainacan_Collection ) $collection_id + * @param array $args + * @return WP_Query + */ + function get_filter_by_collection( $collection, $args = array()) { + + // TODO: get filters from parent collections + + $collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection; + + $args = array_merge([ + 'post_type' => self::POST_TYPE, + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'meta_key' => 'collection_id', + 'meta_value' => $collection_id + ], $args); + + $wp_query = new WP_Query($args); + + return $wp_query; + } + + /** + * @param int $id + * @return Tainacan_Filter + */ + function get_filter_by_id($id) { + return new Tainacan_Filter($id); + } } \ No newline at end of file diff --git a/tests/test-filters.php b/tests/test-filters.php new file mode 100644 index 000000000..40e56e3ad --- /dev/null +++ b/tests/test-filters.php @@ -0,0 +1,57 @@ +set_name('teste'); + $collection = $Tainacan_Collections->insert($collection); + + //setando os valores na classe do metadado + $filter->set_name('metadado'); + $filter->set_description('descricao'); + $filter->set_collection( $collection ); + + //inserindo o metadado + $metadata = $Tainacan_Metadatas->insert($metadata); + + $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + + $i = new Tainacan_Item(); + + $i->set_title('item teste'); + $i->set_description('adasdasdsa'); + $i->set_collection($collection); + + global $Tainacan_Items; + $item = $Tainacan_Items->insert($i); + + $item = $Tainacan_Items->get_item_by_id($item->get_id()); + + + + $value = 'teste_val'; + + $item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata); + $item_metadata->set_value($value); + $this->assertTrue($item_metadata->validate()); + $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); + + $n_item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata); + $n_item_metadata->set_value($value); + $this->assertFalse($n_item_metadata->validate()); + } +} \ No newline at end of file