update field and filter type properties in entities, save options in database

This commit is contained in:
Eduardo humberto 2017-12-05 15:42:15 -02:00
parent 07bc1a6776
commit 11a40ddee9
10 changed files with 87 additions and 58 deletions

View File

@ -82,23 +82,35 @@ class Filter extends Entity {
}
/**
* Retorna o objeto filtro
* Retorna o objeto Filter Type
*
* @return Filter
* @return array || object
*/
function get_filter_type_object(){
return unserialize( base64_decode( $this->get_mapped_property('filter_type_object') ) );
$class_name = $this->get_filter_type();
$object_type = new $class_name();
$object_type->set_options( $this->get_filter_options() );
return $object_type;
}
/**
* Retorna o filtro
* Retorna o objeto field type
*
* @return string
* @return array || object
*/
function get_filter_type(){
return $this->get_mapped_property('filter_type');
}
/**
* Retorna o objeto Metadado
*
* @return array || object
*/
function get_filter_options(){
return $this->get_mapped_property('filter_type_options');
}
/**
* Atribui nome ao filtro
*
@ -157,14 +169,14 @@ class Filter extends Entity {
* @param \Tainacan\Filter_Types\Filter_Type $value
* @return void
*/
function set_filter_type_object( \Tainacan\Filter_Types\Filter_Type $value ){
// TODO: validate primitive type with filter
//if filter matches the metadata type
//if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){
$this->set_filter_type( get_class( $value ) );
$this->set_mapped_property('filter_type_object', base64_encode( serialize($value) ) );
//}
}
// function set_filter_type_object( \Tainacan\Filter_Types\Filter_Type $value ){
// // TODO: validate primitive type with filter
// //if filter matches the metadata type
// //if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){
// $this->set_filter_type( get_class( $value ) );
// $this->set_mapped_property('filter_type_object', base64_encode( serialize($value) ) );
// //}
// }
/**
* Atribui o filter type.
@ -173,7 +185,7 @@ class Filter extends Entity {
* @param string
*
*/
private function set_filter_type($value){
$this->set_mapped_property('filter_type', $value );
public function set_filter_type($value){
$this->set_mapped_property('filter_type', get_class( $value ) );
}
}

View File

@ -150,7 +150,10 @@ class Metadata extends Entity {
* @return array || object
*/
function get_field_type_object(){
return unserialize(base64_decode( $this->get_mapped_property('field_type_object') ) );
$class_name = $this->get_field_type();
$object_type = new $class_name();
$object_type->set_options( $this->get_field_options() );
return $object_type;
}
/**
@ -159,7 +162,16 @@ class Metadata extends Entity {
* @return array || object
*/
function get_field_type(){
return base64_decode($this->get_mapped_property('field_type'));
return $this->get_mapped_property('field_type');
}
/**
* Retorna o objeto Metadado
*
* @return array || object
*/
function get_field_options(){
return $this->get_mapped_property('get_field_options');
}
/**
@ -272,18 +284,13 @@ class Metadata extends Entity {
$this->set_mapped_property('default_property', $value);
}
function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){
$this->set_field_type( get_class( $value ) );
$this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal
}
/**
* Este metodo é privado, porque é utilizado apenas neste contexto pelo @method set_field_type_object()
* save the class field class name
*
* @param $value
*/
private function set_field_type($value){
$this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal
public function set_field_type($value){
$this->set_mapped_property('field_type', get_class( $value ) ) ; // Encode to avoid backslaches removal
}
// helpers

View File

@ -7,6 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
abstract class Filter_Type {
private $supported_types = [];
public $options;
abstract function render( $metadata );
@ -17,4 +18,11 @@ abstract class Filter_Type {
public function set_supported_types($supported_types){
$this->supported_types = $supported_types;
}
/**
* @param $options
*/
public function set_options( $options ){
$this->options = ( is_array( $options ) ) ? $options : unserialize( $options );
}
}

View File

@ -40,11 +40,11 @@ class Filters extends Repository {
'description'=> __('The filter description', 'tainacan'),
'validation' => ''
],
'filter_type_object' => [
'filter_type_options' => [
'map' => 'meta',
'title' => __('Type', 'tainacan'),
'title' => __('Filter type options', 'tainacan'),
'type' => 'string',
'description'=> __('The filter type object', 'tainacan'),
'description'=> __('The filter type options', 'tainacan'),
'validation' => ''
],
'filter_type' => [

View File

@ -124,11 +124,11 @@ class Metadatas extends Repository {
'type' => 'string',
'description'=> __('The value default fot the metadata', 'tainacan'),
],
'field_type_object' => [ // not showed in form
'field_type_options' => [ // not showed in form
'map' => 'meta',
'title' => __('Type', 'tainacan'),
'title' => __('Field Type options', 'tainacan'),
'type' => 'string',
'description'=> __('The object type', 'tainacan'),
'description'=> __('Options specific for field type', 'tainacan'),
// 'validation' => ''
],
'collection_id' => [ // not showed in form

View File

@ -138,10 +138,10 @@ class DevInterface {
<?php $this->collections_dropdown($value); ?>
<?php elseif ($prop == 'collections_ids'): ?>
<?php $this->collections_checkbox_list($value); ?>
<?php elseif ($prop == 'field_type'): ?>
<?php elseif ($prop == 'field_type_options'): ?>
<?php echo $value; ?>
<?php elseif ($prop == 'field_type_object'): ?>
<?php echo $this->field_type_dropdown($value); ?>
<?php elseif ($prop == 'field_type'): ?>
<?php echo $this->field_type_dropdown($post->ID,$value); ?>
<?php else: ?>
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php endif; ?>
@ -315,11 +315,11 @@ class DevInterface {
<?php
}
function field_type_dropdown($selected) {
function field_type_dropdown($id,$selected) {
global $Tainacan_Metadatas;
$class = ( $selected ) ? unserialize( base64_decode( $selected ) ) : '';
$class = ( class_exists( $selected ) ) ? new $selected() : '';
if(is_object( $class )){
$selected = str_replace('Tainacan\Field_Types\\','', get_class( $class ) );
@ -327,13 +327,15 @@ class DevInterface {
$field_types = $Tainacan_Metadatas->fetch_field_types('NAME');
?>
<select name="tnc_prop_field_type_object">
<select name="tnc_prop_field_type">
<?php foreach ($field_types as $field_type): ?>
<option value="<?php echo $field_type; ?>" <?php selected($field_type, $selected) ?>><?php echo $field_type; ?></option>
<?php endforeach; ?>
</select>
<?php
if( $class ){
$options = get_post_meta($id,'field_type_options',true);
$class->set_options($options);
echo $class->form();
}
?>
@ -388,12 +390,12 @@ class DevInterface {
if ($entity->validate_prop($prop)) {
if ($prop == 'field_type_object') {
if ($prop == 'field_type') {
$class = '\Tainacan\Field_Types\\'.$value;
$ft = new $class();
$ft->set_options( $_POST['field_type_'.strtolower( $value )] );
update_post_meta($post_id, 'field_type_object', base64_encode(serialize($ft)) );
update_post_meta($post_id, 'field_type', base64_encode(get_class( $ft )) );
update_post_meta($post_id, 'field_type_options', $_POST['field_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'field_type', wp_slash( get_class( new $class() ) ) );
}else if($prop == 'field_type_options'){
continue;
}
// we cannot user repository->insert here, it would create an infinite loop
else if ($mapped['map'] == 'meta') {

View File

@ -58,7 +58,7 @@ class Filters extends TAINACAN_UnitTestCase {
array(
'name' => 'metadado',
'collection_id' => $collection->get_id(),
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -71,7 +71,7 @@ class Filters extends TAINACAN_UnitTestCase {
'name' => 'filtro',
'collection' => $collection,
'metadata' => $metadata,
'filter_type_object' => $filter_list_type
'filter_type' => $filter_list_type
),
true
);
@ -79,7 +79,7 @@ class Filters extends TAINACAN_UnitTestCase {
$filter_range_type = $this->tainacan_filter_factory->create_filter('range');
//nao devera permitir um filtro Range para o tipo string
$this->assertTrue( $filter->set_filter_type_object( $filter_range_type ) === null );
$this->assertTrue( $filter->set_filter_type( $filter_range_type ) === null );
$test = $Tainacan_Filters->fetch( $filter->get_id() );

View File

@ -36,7 +36,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -88,7 +88,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao',
'collection' => $collection,
'required' => 'yes',
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -142,7 +142,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao',
'collection' => $collection,
'collection_key' => 'yes',
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -200,7 +200,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
'description' => 'descricao',
'collection' => $collection,
'status' => 'publish',
'field_type_object' => $type
'field_type' => $type
),
true
);

View File

@ -43,7 +43,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -54,7 +54,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -65,7 +65,7 @@ class Items extends TAINACAN_UnitTestCase {
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
'field_type_object' => $type
'field_type' => $type
),
true
);

View File

@ -35,7 +35,7 @@ class Metadata extends TAINACAN_UnitTestCase {
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -69,7 +69,7 @@ class Metadata extends TAINACAN_UnitTestCase {
'name' => 'metadado',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
'field_type_object' => $type
'field_type' => $type
),
true
);
@ -95,7 +95,7 @@ class Metadata extends TAINACAN_UnitTestCase {
array(
'name' => 'metadata default',
'collection_id' => $Tainacan_Metadatas->get_default_metadata_attribute(),
'field_type_object' => $type,
'field_type' => $type,
'status' => 'publish'
),
true
@ -114,7 +114,7 @@ class Metadata extends TAINACAN_UnitTestCase {
array(
'name' => 'metadata grandfather',
'collection_id' => $collection_grandfather->get_id(),
'field_type_object' => $type,
'field_type' => $type,
'status' => 'publish'
),
true
@ -134,7 +134,7 @@ class Metadata extends TAINACAN_UnitTestCase {
array(
'name' => 'metadata father',
'collection_id' => $collection_father->get_id(),
'field_type_object' => $type,
'field_type' => $type,
'status' => 'publish'
),
true
@ -157,7 +157,7 @@ class Metadata extends TAINACAN_UnitTestCase {
array(
'name' => 'metadata son',
'collection_id' => $collection_son->get_id(),
'field_type_object' => $type,
'field_type' => $type,
'status' => 'publish'
),
true