item metadata value_to_html() structure and applied to cat field type

This commit is contained in:
Leo Germani 2018-04-09 11:32:20 -03:00
parent 2784e4ad58
commit 39c86691aa
10 changed files with 267 additions and 41 deletions

View File

@ -103,6 +103,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$item_array['metadata'][$slug]['name'] = $field->get_name(); $item_array['metadata'][$slug]['name'] = $field->get_name();
$item_array['metadata'][$slug]['value'] = $item_metadata_array['value']; $item_array['metadata'][$slug]['value'] = $item_metadata_array['value'];
$item_array['metadata'][$slug]['value_as_html'] = $item_metadata_array['value_as_html'];
$item_array['metadata'][$slug]['value_as_string'] = $item_metadata_array['value_as_string'];
$item_array['metadata'][$slug]['multiple'] = $field->get_multiple(); $item_array['metadata'][$slug]['multiple'] = $field->get_multiple();
} }

View File

@ -275,6 +275,7 @@ class Entity {
public function add_error($type, $message) { public function add_error($type, $message) {
$this->errors[] = [$type => $message]; $this->errors[] = [$type => $message];
$this->set_validated(false);
} }
/** /**

View File

@ -16,6 +16,13 @@ class Item_Metadata_Entity extends Entity {
*/ */
protected $repository = 'Item_Metadata'; protected $repository = 'Item_Metadata';
protected
$item,
$field,
$parent_meta_id,
$meta_id,
$value;
/** /**
* *
* @param Item $item Item Entity * @param Item $item Item Entity
@ -38,25 +45,99 @@ class Item_Metadata_Entity extends Entity {
} }
public function __toString(){ public function __value_to_html(){
return 'Hello, I\'m the Item Field Entity'; $field = $this->get_field();
if (is_object($field)) {
$fto = $field->get_field_type_object();
if (is_object($fto)) {
if ( method_exists($fto, '__value_to_html') ) {
return $fto->__value_to_html($this);
}
}
}
$value = $this->get_value();
$return = '';
if ( $this->is_multiple() ) {
$total = sizeof($value);
$count = 0;
foreach ($value as $v) {
$return .= (string) $v;
$count ++;
if ($count <= $total)
$return .= ', ';
}
} else {
$return = (string) $value;
}
return $return;
}
public function __value_to_string() {
return strip_tags($this->__value_to_html());
}
public function __value_to_array() {
$field = $this->get_field();
if (is_object($field)) {
$fto = $field->get_field_type_object();
if (is_object($fto)) {
if ( method_exists($fto, '__value_to_array') ) {
return $fto->__value_to_array($this);
}
}
}
$value = $this->get_value();
if ( $this->is_multiple() ) {
$return = [];
foreach ($value as $v) {
if ( $v instanceof Term || $v instanceof ItemMetadataEntity ) {
$return[] = $v->__toArray();
} else {
$return[] = $v;
}
}
} else {
$return = '';
if ( $value instanceof Term || $value instanceof ItemMetadataEntity ) {
$return = $value->__toArray();
} else {
$return = $value;
}
}
return $return;
} }
public function __toArray(){ public function __toArray(){
$value = $this->get_value(); $as_array = [];
if(is_array($value) && isset($value[0]) && $value[0] instanceof Term){ $as_array['value'] = $this->__value_to_array();
$values_arr = []; $as_array['value_as_html'] = $this->__value_to_html();
$as_array['value_as_string'] = $this->__value_to_string();
foreach ($value as $val){
$values_arr[] = $val->__toArray();
}
$as_array['value'] = $values_arr;
} else {
$as_array['value'] = $this->get_value();
}
$as_array['item'] = $this->get_item()->__toArray(); $as_array['item'] = $this->get_item()->__toArray();
$as_array['field'] = $this->get_field()->__toArray(); $as_array['field'] = $this->get_field()->__toArray();
@ -258,7 +339,7 @@ class Item_Metadata_Entity extends Entity {
} else { } else {
if( is_array($value) ){ if( is_array($value) ){
$this->add_error('not_multiple', $field->get_name() . ' do not accept array as value'); $this->add_error('not_multiple', $field->get_name() . ' do not accept array as value');
return false; return false;
} }

View File

@ -172,4 +172,28 @@ class Term extends Entity {
function set_taxonomy($value) { function set_taxonomy($value) {
$this->set_mapped_property('taxonomy', $value); $this->set_mapped_property('taxonomy', $value);
} }
public function __toHtml() {
$return = '';
$id = $this->get_id();
if ( $id ) {
$link = get_term_link( (int) $id );
if (is_string($link)) {
$return = "<a data-linkto='term' data-id='$id' href='$link'>";
$return.= $this->get_name();
$return .= "</a>";
}
}
return $return;
}
} }

View File

@ -120,8 +120,8 @@ class Category extends Field_Type {
$terms = array($terms); $terms = array($terms);
foreach ($terms as $term) { foreach ($terms as $term) {
if (is_object($term) && $term instanceof \WP_Term) { if (is_object($term) && $term instanceof \Tainacan\Entities\Term) {
$term = $term->term_id; $term = $term->get_id();
} }
if (!term_exists($term)) { if (!term_exists($term)) {
@ -136,4 +136,40 @@ class Category extends Field_Type {
} }
public function __value_to_html(Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
if ( $item_metadata->is_multiple() ) {
$count = 1;
$total = sizeof($value);
foreach ( $value as $term ) {
if ( $term instanceof \Tainacan\Entities\Term ) {
$return .= $term->__toHtml();
}
$count ++;
if ( $count <= $total ) {
$return .= ', ';
}
}
} else {
if ( $value instanceof \Tainacan\Entities\Term ) {
$return .= $value->__toHtml();
}
}
return $return;
}
} }

View File

@ -28,7 +28,12 @@ class Item_Metadata extends Repository {
public function insert($item_metadata) { public function insert($item_metadata) {
$unique = !$item_metadata->is_multiple(); if ( ! $item_metadata->get_validated() ) {
throw new \Exception( 'Entities must be validated before you can save them' );
// TODO: Throw Warning saying you must validate object before insert()
}
$unique = !$item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object(); $field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) { if ($field_type->get_core()) {
@ -52,22 +57,22 @@ class Item_Metadata extends Repository {
* and not update an existing. This is the case of a multiple compound field. * and not update an existing. This is the case of a multiple compound field.
*/ */
if ( $item_metadata->get_field()->get_parent() > 0 && is_null($item_metadata->get_meta_id()) ) { if ( $item_metadata->get_field()->get_parent() > 0 && is_null($item_metadata->get_meta_id()) ) {
$added_meta_id = add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); $added_meta_id = add_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id(), wp_slash( $item_metadata->get_value() ) );
$added_compound = $this->add_compound_value($item_metadata, $added_meta_id); $added_compound = $this->add_compound_value($item_metadata, $added_meta_id);
} else { } else {
update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); update_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id(), wp_slash( $item_metadata->get_value() ) );
} }
} }
} else { } else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id()); delete_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id());
if (is_array($item_metadata->get_value())){ if (is_array($item_metadata->get_value())){
$values = $item_metadata->get_value(); $values = $item_metadata->get_value();
foreach ($values as $value){ foreach ($values as $value){
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value )); add_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id(), wp_slash( $value ));
} }
} }
} }
@ -222,8 +227,12 @@ class Item_Metadata extends Repository {
$terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug ); $terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug );
if ($unique) if ($unique) {
$terms = reset($terms); $terms = reset($terms);
if (false !== $terms)
$terms = new Entities\Term($terms);
}
if(is_array($terms)){ if(is_array($terms)){
$terms_array = []; $terms_array = [];
@ -240,7 +249,7 @@ class Item_Metadata extends Repository {
global $wpdb; global $wpdb;
$rows = $wpdb->get_results( $rows = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $item_metadata->get_item()->get_id(), $item_metadata->field->get_id()), $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id()),
ARRAY_A ); ARRAY_A );
$return_value = []; $return_value = [];
@ -268,7 +277,7 @@ class Item_Metadata extends Repository {
return $value->meta_value; return $value->meta_value;
} }
} else { } else {
return get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique); return get_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_field()->get_id(), $unique);
} }
} }

View File

@ -364,6 +364,9 @@ abstract class Repository {
return $post; return $post;
} }
if (!$post instanceof \WP_Post)
return false;
$post_type = $post->post_type; $post_type = $post->post_type;
return self::get_entity_by_post_type( $post_type, $post ); return self::get_entity_by_post_type( $post_type, $post );

View File

@ -105,7 +105,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$pending = $log->get_value(); $pending = $log->get_value();
$this->assertEquals('TestValuesSuggestion_metadado', $pending->value); $this->assertEquals('TestValuesSuggestion_metadado', $pending->get_value());
wp_set_current_user($this->user_id); wp_set_current_user($this->user_id);
@ -165,7 +165,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$pending = $log->get_value(); $pending = $log->get_value();
$this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->value); $this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->get_value());
wp_set_current_user($this->user_id); wp_set_current_user($this->user_id);

View File

@ -55,6 +55,17 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
true true
); );
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'collection' => $collection,
'status' => 'publish',
),
true
);
$field2 = $this->tainacan_entity_factory->create_entity( $field2 = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
@ -68,16 +79,7 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
); );
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'collection' => $collection,
'status' => 'publish',
),
true
);
$term = $this->tainacan_entity_factory->create_entity( $term = $this->tainacan_entity_factory->create_entity(
@ -118,7 +120,7 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field); $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field);
$this->assertEquals('WP_Term', get_class($check_item_metadata->get_value())); $this->assertEquals('Tainacan\Entities\Term', get_class($check_item_metadata->get_value()));
// test 2 fields with same category // test 2 fields with same category
$field2->set_field_type_options([ $field2->set_field_type_options([
@ -202,6 +204,72 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
}
function test_values_and_html() {
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_test',
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'orange',
'collection' => $collection,
'status' => 'publish'
),
true
);
$field = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Category',
'status' => 'publish',
'field_type_options' => [
'taxonomy_id' => $tax->get_id(),
'allow_new_terms' => true
]
),
true
);
$meta = new \Tainacan\Entities\Item_Metadata_Entity($item, $field);
$meta->set_value('new_term');
$meta->validate();
$meta = $Tainacan_ItemMetadata->insert($meta);
$this->assertInternalType( 'string', $meta->__value_to_html() );
$this->assertInternalType( 'string', $meta->__value_to_string() );
$this->assertInternalType( 'integer', strpos($meta->__value_to_html(), '<a ') );
$this->assertFalse( strpos($meta->__value_to_string(), '<a ') );
} }
} }

View File

@ -59,7 +59,9 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test); $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $test);
$item_metadata->set_value('teste_value'); $item_metadata->set_value('teste_value');
$item_metadata->validate();
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$this->assertEquals('teste_value', $item_metadata->get_value()); $this->assertEquals('teste_value', $item_metadata->get_value());