value as html for compound fields

This commit is contained in:
Leo Germani 2018-04-10 09:11:39 -03:00
parent ee8174682e
commit e2cd0d255e
5 changed files with 96 additions and 36 deletions

View File

@ -44,16 +44,20 @@ class Item_Metadata_Entity extends Entity {
} }
public function __value_to_html(){ /**
* Get the value as a HTML string, with markup and links
* @return string
*/
public function get_value_as_html(){
$field = $this->get_field(); $field = $this->get_field();
if (is_object($field)) { if (is_object($field)) {
$fto = $field->get_field_type_object(); $fto = $field->get_field_type_object();
if (is_object($fto)) { if (is_object($fto)) {
if ( method_exists($fto, '__value_to_html') ) { if ( method_exists($fto, 'get_value_as_html') ) {
return $fto->__value_to_html($this); return $fto->get_value_as_html($this);
} }
} }
@ -85,24 +89,19 @@ class Item_Metadata_Entity extends Entity {
} }
public function __value_to_string() { /**
return strip_tags($this->__value_to_html()); * Get the value as a plain text string
* @return string
*/
public function get_value_as_string() {
return strip_tags($this->get_value_as_html());
} }
public function __value_to_array() { /**
$field = $this->get_field(); * Get value as an array
if (is_object($field)) { * @return [type] [description]
$fto = $field->get_field_type_object(); */
if (is_object($fto)) { public function get_value_as_array() {
if ( method_exists($fto, '__value_to_array') ) {
return $fto->__value_to_array($this);
}
}
}
$value = $this->get_value(); $value = $this->get_value();
if ( $this->is_multiple() ) { if ( $this->is_multiple() ) {
@ -131,13 +130,17 @@ class Item_Metadata_Entity extends Entity {
return $return; return $return;
} }
/**
* Convert the object to an Array
* @return array the representation of this object as an array
*/
public function __toArray(){ public function __toArray(){
$as_array = []; $as_array = [];
$as_array['value'] = $this->__value_to_array(); $as_array['value'] = $this->get_value_as_array();
$as_array['value_as_html'] = $this->__value_to_html(); $as_array['value_as_html'] = $this->get_value_as_html();
$as_array['value_as_string'] = $this->__value_to_string(); $as_array['value_as_string'] = $this->get_value_as_string();
$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();

View File

@ -136,7 +136,12 @@ class Category extends Field_Type {
} }
public function __value_to_html(Item_Metadata_Entity $item_metadata) { /**
* Return the value of an Item_Metadata_Entity using a field of this field type as an html string
* @param Item_Metadata_Entity $item_metadata
* @return string The HTML representation of the value, containing one or multiple terms, separated by comma, linked to term page
*/
public function get_value_as_html(Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value(); $value = $item_metadata->get_value();

View File

@ -49,7 +49,49 @@ class Compound extends Field_Type {
return true; return true;
} }
/**
* Return the value of an Item_Metadata_Entity using a field of this field type as an html string
* @param Item_Metadata_Entity $item_metadata
* @return string The HTML representation of the value, each HTML representation of the value of each field composing this metadata
*/
public function get_value_as_html(Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
if ( $item_metadata->is_multiple() ) {
foreach ( $value as $compound_element ) {
foreach ( $compound_element as $meta ) {
if ( $meta instanceof Item_Metadata_Entity ) {
$return .= '<h4>' . $meta->get_field()->get_name() . '</h4>';
$return .= '<p>' . $meta->__value_to_html() . '</p>';
}
}
$return .= '<hr />';
}
} else {
foreach ( $value as $meta ) {
if ( $meta instanceof Item_Metadata_Entity ) {
$return .= '<h4>' . $meta->get_field()->get_name() . '</h4>';
$return .= '<p>' . $meta->__value_to_html() . '</p>';
}
}
}
return $return;
}

View File

@ -67,8 +67,12 @@ class Relationship extends Field_Type {
return true; return true;
} }
/**
public function __value_to_html(Item_Metadata_Entity $item_metadata) { * Return the value of an Item_Metadata_Entity using a field of this field type as an html string
* @param Item_Metadata_Entity $item_metadata
* @return string The HTML representation of the value, containing one or multiple items names, linked to the item page
*/
public function get_value_as_html(Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value(); $value = $item_metadata->get_value();
@ -99,14 +103,20 @@ class Relationship extends Field_Type {
// item not found // item not found
} }
} }
} else { } else {
if ( $value instanceof \Tainacan\Entities\Item ) { try {
$return .= $value->__toHtml();
$item = new \Tainacan\Entities\Item($value);
if ( $item instanceof \Tainacan\Entities\Item ) {
$return .= $item->__toHtml();
}
} catch (Exception $e) {
// item not found
} }
} }

View File

@ -263,11 +263,11 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
$meta = $Tainacan_ItemMetadata->insert($meta); $meta = $Tainacan_ItemMetadata->insert($meta);
$this->assertInternalType( 'string', $meta->__value_to_html() ); $this->assertInternalType( 'string', $meta->get_value_as_html() );
$this->assertInternalType( 'string', $meta->__value_to_string() ); $this->assertInternalType( 'string', $meta->get_value_as_string() );
$this->assertInternalType( 'integer', strpos($meta->__value_to_html(), '<a ') ); $this->assertInternalType( 'integer', strpos($meta->get_value_as_html(), '<a ') );
$this->assertFalse( strpos($meta->__value_to_string(), '<a ') ); $this->assertFalse( strpos($meta->get_value_as_string(), '<a ') );
} }