From 39c86691aa1936991e95089cc11a6eb2d79862fb Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 9 Apr 2018 11:32:20 -0300 Subject: [PATCH] item metadata value_to_html() structure and applied to cat field type --- .../class-tainacan-rest-items-controller.php | 2 + .../entities/class-tainacan-entity.php | 1 + .../class-tainacan-item-metadata-entity.php | 115 +++++++++++++++--- src/classes/entities/class-tainacan-term.php | 24 ++++ .../category/class-tainacan-category.php | 40 +++++- .../class-tainacan-item-metadata.php | 25 ++-- .../class-tainacan-repository.php | 3 + tests/test-api-item-metadata.php | 4 +- tests/test-category-field-types.php | 90 ++++++++++++-- tests/test-item-metadata.php | 4 +- 10 files changed, 267 insertions(+), 41 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index a949a3867..4c2e83b7b 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -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]['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(); } diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index 9f601f91e..8599ffb21 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -275,6 +275,7 @@ class Entity { public function add_error($type, $message) { $this->errors[] = [$type => $message]; + $this->set_validated(false); } /** diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index 916684aeb..e94c875f4 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -16,6 +16,13 @@ class Item_Metadata_Entity extends Entity { */ protected $repository = 'Item_Metadata'; + protected + $item, + $field, + $parent_meta_id, + $meta_id, + $value; + /** * * @param Item $item Item Entity @@ -38,25 +45,99 @@ class Item_Metadata_Entity extends Entity { } - public function __toString(){ - return 'Hello, I\'m the Item Field Entity'; + public function __value_to_html(){ + $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(){ - $value = $this->get_value(); - - if(is_array($value) && isset($value[0]) && $value[0] instanceof Term){ - $values_arr = []; - - foreach ($value as $val){ - $values_arr[] = $val->__toArray(); - } - - $as_array['value'] = $values_arr; - } else { - $as_array['value'] = $this->get_value(); - } - + $as_array = []; + + $as_array['value'] = $this->__value_to_array(); + $as_array['value_as_html'] = $this->__value_to_html(); + $as_array['value_as_string'] = $this->__value_to_string(); $as_array['item'] = $this->get_item()->__toArray(); $as_array['field'] = $this->get_field()->__toArray(); @@ -258,7 +339,7 @@ class Item_Metadata_Entity extends Entity { } else { 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; } diff --git a/src/classes/entities/class-tainacan-term.php b/src/classes/entities/class-tainacan-term.php index 3fdd713cd..8ce7a521c 100644 --- a/src/classes/entities/class-tainacan-term.php +++ b/src/classes/entities/class-tainacan-term.php @@ -172,4 +172,28 @@ class Term extends Entity { function set_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 = ""; + $return.= $this->get_name(); + $return .= ""; + + } + + } + + return $return; + + } } diff --git a/src/classes/field-types/category/class-tainacan-category.php b/src/classes/field-types/category/class-tainacan-category.php index d09000f01..e64b6fbcd 100644 --- a/src/classes/field-types/category/class-tainacan-category.php +++ b/src/classes/field-types/category/class-tainacan-category.php @@ -120,8 +120,8 @@ class Category extends Field_Type { $terms = array($terms); foreach ($terms as $term) { - if (is_object($term) && $term instanceof \WP_Term) { - $term = $term->term_id; + if (is_object($term) && $term instanceof \Tainacan\Entities\Term) { + $term = $term->get_id(); } 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; + + } + } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 65d80ecc9..41ccd98b7 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -28,7 +28,12 @@ class Item_Metadata extends Repository { 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(); 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. */ 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); } 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 { - 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())){ $values = $item_metadata->get_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 ); - if ($unique) + if ($unique) { $terms = reset($terms); + + if (false !== $terms) + $terms = new Entities\Term($terms); + } if(is_array($terms)){ $terms_array = []; @@ -240,7 +249,7 @@ class Item_Metadata extends Repository { global $wpdb; $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 ); $return_value = []; @@ -268,7 +277,7 @@ class Item_Metadata extends Repository { return $value->meta_value; } } 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); } } diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index d677cb464..b684b4e4a 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -364,6 +364,9 @@ abstract class Repository { return $post; } + if (!$post instanceof \WP_Post) + return false; + $post_type = $post->post_type; return self::get_entity_by_post_type( $post_type, $post ); diff --git a/tests/test-api-item-metadata.php b/tests/test-api-item-metadata.php index e6e7efdda..a70eb7d26 100644 --- a/tests/test-api-item-metadata.php +++ b/tests/test-api-item-metadata.php @@ -105,7 +105,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase { $pending = $log->get_value(); - $this->assertEquals('TestValuesSuggestion_metadado', $pending->value); + $this->assertEquals('TestValuesSuggestion_metadado', $pending->get_value()); wp_set_current_user($this->user_id); @@ -165,7 +165,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase { $pending = $log->get_value(); - $this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->value); + $this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->get_value()); wp_set_current_user($this->user_id); diff --git a/tests/test-category-field-types.php b/tests/test-category-field-types.php index ef026a256..70821b061 100644 --- a/tests/test-category-field-types.php +++ b/tests/test-category-field-types.php @@ -55,6 +55,17 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase { 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( 'field', 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( @@ -118,7 +120,7 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase { $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 $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(), 'assertFalse( strpos($meta->__value_to_string(), 'set_value('teste_value'); - + + $item_metadata->validate(); + $item_metadata = $Tainacan_Item_Metadata->insert($item_metadata); $this->assertEquals('teste_value', $item_metadata->get_value());