add auto HTML link tag in text values #128 and HTML break line tag in textarea #116

This commit is contained in:
vnmedeiros 2018-09-13 17:07:32 -03:00
parent 4974066fc1
commit 108cce551c
4 changed files with 131 additions and 3 deletions

View File

@ -8,6 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
* Class TainacanMetadatumType
*/
class Text extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){
// call metadatum type constructor
@ -26,5 +27,27 @@ class Text extends Metadata_Type {
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-text>';
}
}
/**
* Get the value as a HTML string with links
* @return string
*/
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
if ( $item_metadata->is_multiple() ) {
$total = sizeof($value);
$count = 0;
foreach ( $value as $el ) {
$return .= $this->make_clickable_links($el);
$count ++;
if ($count <= $total)
$return .= ', ';
}
} else {
$return = $this->make_clickable_links($value);
}
return $return;
}
}

View File

@ -8,6 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
* Class TainacanMetadatumType
*/
class Textarea extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){
// call metadatum type constructor
@ -21,11 +22,33 @@ class Textarea extends Metadata_Type {
* @return string
*/
public function render( $itemMetadata ){
public function render( $itemMetadata ) {
return '<tainacan-textarea
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-textarea>';
}
/**
* Get the value as a HTML string with links and breakline tag.
* @return string
*/
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
if ( $item_metadata->is_multiple() ) {
$total = sizeof($value);
$count = 0;
foreach ( $value as $el ) {
$return .= nl2br($this->make_clickable_links($el));
$count ++;
if ($count <= $total)
$return .= ', ';
}
} else {
$return = nl2br($this->make_clickable_links($value));
}
return $return;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Tainacan\Traits;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class for formatter texts
* @author Vinicius Nunus - L3P/Medialab
*
*/
trait Formatter_Text {
/**
*
* @return string Texts with url's transformed in html tag <a>
*/
public function make_clickable_links($text) {
$url = '~(?:(http|https|ftp|ftps)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$text = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $text);
return $text;
}
}

View File

@ -243,5 +243,64 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
//first 2 metadata are repository metadata
$this->assertTrue( in_array('metadado', $names) );
}
}
function test_metadata_text_textarea() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item teste',
'description' => 'description',
'collection' => $collection,
'status' => 'publish'
),
true
);
$metadatum_text = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadadoText',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$metadatum_textarea = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadadoTextarea',
'description' => 'descricao',
'collection_id' => $collection->get_id(),
'metadata_type' => 'Tainacan\Metadata_Types\Textarea',
),
true
);
$value_text = 'GOOGLE: www.google.com';
$item_metadata_text = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_text);
$item_metadata_text->set_value($value_text);
$value_textarea = 'GOOGLE: www.google.com \n GOOGLE: https://www.google.com';
$item_metadata_textarea = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum_textarea);
$item_metadata_textarea->set_value($value_textarea);
$response_text = 'GOOGLE: <a href="www.google.com" target="_blank" title="www.google.com">www.google.com</a>';
$response_textarea = 'GOOGLE: <a href="www.google.com" target="_blank" title="www.google.com">www.google.com</a> \n GOOGLE: <a href="https://www.google.com" target="_blank" title="https://www.google.com">https://www.google.com</a>';
$this->assertEquals($item_metadata_text->get_value_as_html(), $response_text);
$this->assertEquals($item_metadata_textarea->get_value_as_html(), $response_textarea);
}
}