Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2018-09-13 22:54:02 -03:00
commit 9bcf796512
15 changed files with 302 additions and 135 deletions

View File

@ -40,7 +40,7 @@
</button> </button>
<b-dropdown-item <b-dropdown-item
v-if="$route.params.collectionId && $userCaps.hasCapability('edit_others_posts')" v-if="$route.params.collectionId && $userCaps.hasCapability('edit_others_posts') && !isOnTrash"
@click="openBulkEditionModal()"> @click="openBulkEditionModal()">
{{ $i18n.get('label_edit_selected_items') }} {{ $i18n.get('label_edit_selected_items') }}
</b-dropdown-item> </b-dropdown-item>
@ -120,6 +120,7 @@
class="actions-area" class="actions-area"
:label="$i18n.get('label_actions')"> :label="$i18n.get('label_actions')">
<a <a
v-if="!isOnTrash"
id="button-edit" id="button-edit"
:aria-label="$i18n.getFrom('items','edit_item')" :aria-label="$i18n.getFrom('items','edit_item')"
@click.prevent.stop="goToItemEditPage(item)"> @click.prevent.stop="goToItemEditPage(item)">
@ -204,6 +205,7 @@
class="actions-area" class="actions-area"
:label="$i18n.get('label_actions')"> :label="$i18n.get('label_actions')">
<a <a
v-if="!isOnTrash"
id="button-edit" id="button-edit"
:aria-label="$i18n.getFrom('items','edit_item')" :aria-label="$i18n.getFrom('items','edit_item')"
@click.prevent.stop="goToItemEditPage(item)"> @click.prevent.stop="goToItemEditPage(item)">
@ -274,6 +276,7 @@
class="actions-area" class="actions-area"
:label="$i18n.get('label_actions')"> :label="$i18n.get('label_actions')">
<a <a
v-if="!isOnTrash"
id="button-edit" id="button-edit"
:aria-label="$i18n.getFrom('items','edit_item')" :aria-label="$i18n.getFrom('items','edit_item')"
@click.prevent.stop="goToItemEditPage(item)"> @click.prevent.stop="goToItemEditPage(item)">
@ -388,8 +391,8 @@
autoHide: false, autoHide: false,
placement: 'auto-start' placement: 'auto-start'
}" }"
v-for="(column, index) in tableMetadata" v-for="(column, columnIndex) in tableMetadata"
:key="index" :key="columnIndex"
v-if="collectionId != undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')" v-if="collectionId != undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')"
@click="onClickItem($event, item, index)" @click="onClickItem($event, item, index)"
v-html="item.metadata != undefined ? renderMetadata(item.metadata, column) : ''" /> v-html="item.metadata != undefined ? renderMetadata(item.metadata, column) : ''" />
@ -400,8 +403,8 @@
autoHide: false, autoHide: false,
placement: 'auto-start' placement: 'auto-start'
}" }"
v-for="(column, index) in tableMetadata" v-for="(column, columnIndex) in tableMetadata"
:key="index" :key="columnIndex"
v-if="collectionId == undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')" v-if="collectionId == undefined && column.display && column.metadata_type_object != undefined && (column.metadata_type_object.related_mapped_prop == 'title')"
@click="onClickItem($event, item, index)" @click="onClickItem($event, item, index)"
v-html="item.title != undefined ? item.title : ''" /> v-html="item.title != undefined ? item.title : ''" />
@ -412,6 +415,7 @@
class="actions-area" class="actions-area"
:label="$i18n.get('label_actions')"> :label="$i18n.get('label_actions')">
<a <a
v-if="!isOnTrash"
id="button-edit" id="button-edit"
:aria-label="$i18n.getFrom('items','edit_item')" :aria-label="$i18n.getFrom('items','edit_item')"
@click.prevent.stop="goToItemEditPage(item)"> @click.prevent.stop="goToItemEditPage(item)">
@ -608,6 +612,7 @@
:label="$i18n.get('label_actions')"> :label="$i18n.get('label_actions')">
<div class="actions-container"> <div class="actions-container">
<a <a
v-if="!isOnTrash"
id="button-edit" id="button-edit"
:aria-label="$i18n.getFrom('items','edit_item')" :aria-label="$i18n.getFrom('items','edit_item')"
@click.prevent.stop="goToItemEditPage(item)"> @click.prevent.stop="goToItemEditPage(item)">
@ -858,10 +863,19 @@ export default {
}); });
}, },
onClickItem($event, item, index) { onClickItem($event, item, index) {
if ($event.ctrlKey) { if ($event.ctrlKey || $event.shiftKey) {
this.$set(this.selectedItems, index, !this.selectedItems[index]); this.$set(this.selectedItems, index, !this.selectedItems[index]);
} else { } else {
this.$router.push(this.$routerHelper.getItemPath(item.collection_id, item.id)); if(this.isOnTrash){
this.$toast.open({
duration: 3000,
message: this.$i18n.get('info_warning_remove_from_trash_first'),
position: 'is-bottom',
type: 'is-warning'
});
} else {
this.$router.push(this.$routerHelper.getItemPath(item.collection_id, item.id));
}
} }
}, },
goToItemEditPage(item) { goToItemEditPage(item) {

View File

@ -438,8 +438,6 @@
applyFilter() { applyFilter() {
this.$parent.close(); this.$parent.close();
let selectedOptions = [];
if(this.isTaxonomy){ if(this.isTaxonomy){
this.$eventBusSearch.$emit('input', { this.$eventBusSearch.$emit('input', {
filter: 'checkbox', filter: 'checkbox',
@ -449,23 +447,6 @@
collection_id: this.collection_id, collection_id: this.collection_id,
terms: this.selected terms: this.selected
}); });
for (let selected of this.selected) {
for(let i in this.finderColumns){
let valueIndex = this.finderColumns[i].findIndex(option => option.value == selected);
if (valueIndex >= 0) {
selectedOptions.push(this.finderColumns[i][valueIndex]);
}
}
}
this.$eventBusSearch.$emit('sendValuesToTags', {
filterId: this.filter.id,
value: selectedOptions,
});
this.$emit('appliedCheckBoxModal', selectedOptions);
} else { } else {
this.$eventBusSearch.$emit('input', { this.$eventBusSearch.$emit('input', {
filter: 'checkbox', filter: 'checkbox',
@ -474,36 +455,9 @@
collection_id: this.collection_id ? this.collection_id : this.filter.collection_id, collection_id: this.collection_id ? this.collection_id : this.filter.collection_id,
value: this.selected, value: this.selected,
}); });
// if(!isNaN(this.selected[0])){
// for (let option of this.options) {
// let valueIndex = this.selected.findIndex(item => item == option.value);
// if (valueIndex >= 0) {
// selectedOptions.push(this.options[valueIndex].label);
// }
// }
// }
if(Array.isArray(this.selected)){
for (let aSelected of this.selected) {
let valueIndex = this.options.findIndex(option => option.value == aSelected);
if (valueIndex >= 0) {
selectedOptions.push(this.options[valueIndex]);
}
}
} else {
let valueIndex = this.options.findIndex(option => option.value == this.selected);
if (valueIndex >= 0) {
selectedOptions.push(this.options[valueIndex]);
}
}
this.$emit('appliedCheckBoxModal', selectedOptions);
} }
this.$emit('appliedCheckBoxModal');
} }
} }
} }

View File

@ -271,7 +271,7 @@
<!-- View Modes Dropdown --> <!-- View Modes Dropdown -->
<div <div
v-if="isOnTheme && !isRepositoryLevel" v-if="isOnTheme"
class="search-control-item"> class="search-control-item">
<b-field> <b-field>
<b-dropdown <b-dropdown

View File

@ -271,7 +271,7 @@
</div> </div>
<!-- View Modes Dropdown --> <!-- View Modes Dropdown -->
<!-- <div <div
v-if="isOnTheme" v-if="isOnTheme"
class="search-control-item"> class="search-control-item">
<b-field> <b-field>
@ -372,7 +372,7 @@
</b-dropdown-item> </b-dropdown-item>
</b-dropdown> </b-dropdown>
</b-field> </b-field>
</div> --> </div>
<!-- Text simple search (used on mobile, instead of the one from filter list)--> <!-- Text simple search (used on mobile, instead of the one from filter list)-->
<div class="is-hidden-tablet search-control-item"> <div class="is-hidden-tablet search-control-item">

View File

@ -385,6 +385,7 @@ return apply_filters( 'tainacan-admin-i18n', [
'info_showing_taxonomies' => __( 'Showing taxonomies ', 'tainacan' ), 'info_showing_taxonomies' => __( 'Showing taxonomies ', 'tainacan' ),
'info_showing_events' => __( 'Showing events ', 'tainacan' ), 'info_showing_events' => __( 'Showing events ', 'tainacan' ),
'info_showing_processes' => __( 'Showing processes ', 'tainacan' ), 'info_showing_processes' => __( 'Showing processes ', 'tainacan' ),
'info_warning_remove_from_trash_first' => __( 'Remove this item from trash first'),
'info_to' => __( ' to ', 'tainacan' ), 'info_to' => __( ' to ', 'tainacan' ),
'info_of' => __( ' of ', 'tainacan' ), 'info_of' => __( ' of ', 'tainacan' ),
'info_created_by' => __( 'Created by: ', 'tainacan' ), 'info_created_by' => __( 'Created by: ', 'tainacan' ),

View File

@ -90,6 +90,8 @@ class REST_Facets_Controller extends REST_Controller {
$options = $metadatum->get_metadata_type_options(); $options = $metadatum->get_metadata_type_options();
$args = $this->prepare_filters($request); $args = $this->prepare_filters($request);
// handle filter with relationship metadata
if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ){ if( $metadatum_type === 'Tainacan\Metadata_Types\Relationship' ){
$selected = $this->getRelationshipSelectedValues($request, $metadatum->get_id()); $selected = $this->getRelationshipSelectedValues($request, $metadatum->get_id());
@ -99,14 +101,13 @@ class REST_Facets_Controller extends REST_Controller {
$args['posts_per_page'] = $request['number']; $args['posts_per_page'] = $request['number'];
} }
if( $selected ){
//$args['postin'] = $selected;
}
$items = $this->items_repository->fetch($args, $options['collection_id'], 'WP_Query'); $items = $this->items_repository->fetch($args, $options['collection_id'], 'WP_Query');
$ids = [];
if ($items->have_posts()) { if ($items->have_posts()) {
while ( $items->have_posts() ) { while ( $items->have_posts() ) {
$items->the_post(); $items->the_post();
$ids[] = (string) $items->post->ID;
$item = new Entities\Item($items->post); $item = new Entities\Item($items->post);
$prepared_item = $restItemsClass->prepare_item_for_response($item, $request); $prepared_item = $restItemsClass->prepare_item_for_response($item, $request);
@ -117,10 +118,34 @@ class REST_Facets_Controller extends REST_Controller {
wp_reset_postdata(); wp_reset_postdata();
} }
// retrieve selected items
if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){
foreach( $selected as $index => $item_id ){
if( in_array($item_id,$ids) ){
continue;
}
$item = new Entities\Item($item_id);
$prepared_item = $restItemsClass->prepare_item_for_response($item, $request);
$response[$index] = $prepared_item;
if( isset($request['number']) && ($index+1) >= $request['number']){
break;
}
}
}
$this->total_items = $items->found_posts; $this->total_items = $items->found_posts;
$this->total_pages = ceil($this->total_items / (int) $items->query_vars['posts_per_page']); $this->total_pages = ceil($this->total_items / (int) $items->query_vars['posts_per_page']);
} else if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){ }
// handle filter with Taxonomy metadata
else if ( $metadatum_type === 'Tainacan\Metadata_Types\Taxonomy' ){
$this->taxonomy = $this->taxonomy_repository->fetch($options['taxonomy_id']); $this->taxonomy = $this->taxonomy_repository->fetch($options['taxonomy_id']);
$selected = $this->getTaxonomySelectedValues($request, $options['taxonomy_id']); $selected = $this->getTaxonomySelectedValues($request, $options['taxonomy_id']);
@ -131,22 +156,42 @@ class REST_Facets_Controller extends REST_Controller {
} else { } else {
if( $selected ){ $terms = $this->terms_repository->fetch($args, $this->taxonomy);
//$args['include'] = $selected;
// retrieve selected items
if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){
$ids = $this->get_terms_ids( $terms );
foreach( $selected as $index => $term_id ){
if( in_array($term_id,$ids) ){
continue;
}
$term_selected = $this->terms_repository->fetch($term_id, $this->taxonomy);
array_unshift($terms, $term_selected);
if( isset($request['number']) && ($index+1) >= $request['number']){
break;
}
}
} }
$terms = $this->terms_repository->fetch($args, $this->taxonomy);
$restTermClass = new REST_Terms_Controller(); $restTermClass = new REST_Terms_Controller();
} }
foreach ($terms as $term) { foreach ($terms as $term) {
array_push($response, $restTermClass->prepare_item_for_response( $term, $request )); array_push($response, $restTermClass->prepare_item_for_response( $term, $request ));
} }
$this->set_pagination_properties_term_type( $args, $response ); $this->set_pagination_properties_term_type( $args, $response );
} else { }
// handle filter with Text metadata
else {
$metadatum_id = $metadatum->get_id(); $metadatum_id = $metadatum->get_id();
$offset = ''; $offset = '';
@ -174,10 +219,25 @@ class REST_Facets_Controller extends REST_Controller {
} }
} }
if($selected){ // retrieve selected items
//foreach( $selected as $value ){
//$response[] = ['mvalue' => $value]; if( $selected && $request['getSelected'] && $request['getSelected'] === '1'){
//} $rawValues = $this->get_values( $response );
foreach( $selected as $index => $value ){
if( in_array($value,$rawValues) ){
continue;
}
$row = ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
$response[$index] = $row;
if( isset($request['number']) && ($index+1) >= $request['number']){
break;
}
}
} }
$this->set_pagination_properties_text_type( $collection_id, $metadatum_id, ($request['search']) ? $request['search'] : '' , $offset, $number ); $this->set_pagination_properties_text_type( $collection_id, $metadatum_id, ($request['search']) ? $request['search'] : '' , $offset, $number );
@ -343,7 +403,7 @@ class REST_Facets_Controller extends REST_Controller {
if( isset($request['current_query']['metaquery']) ){ if( isset($request['current_query']['metaquery']) ){
foreach( $request['current_query']['metaquery'] as $metaquery ){ foreach( $request['current_query']['metaquery'] as $metaquery ){
if( $metaquery['key'] === $metadatum_id ){ if( $metaquery['key'] == $metadatum_id ){
return $metaquery['value']; return $metaquery['value'];
@ -368,7 +428,7 @@ class REST_Facets_Controller extends REST_Controller {
if( isset($request['current_query']['metaquery']) ){ if( isset($request['current_query']['metaquery']) ){
foreach( $request['current_query']['metaquery'] as $metaquery ){ foreach( $request['current_query']['metaquery'] as $metaquery ){
if( $metaquery['key'] === $metadatum_id ){ if( $metaquery['key'] == $metadatum_id ){
return $metaquery['value']; return $metaquery['value'];
@ -379,6 +439,32 @@ class REST_Facets_Controller extends REST_Controller {
return []; return [];
} }
/**
*
*/
private function get_terms_ids( $terms ){
$ids = [];
foreach( $terms as $term ){
$ids[] = (string) $term->WP_Term->term_id;
}
return $ids;
}
/**
*
*/
private function get_values( $rows ){
$values = [];
foreach( $rows as $row ){
$values[] = $row['mvalue'];
}
return $values;
}
} }
?> ?>

View File

@ -139,9 +139,25 @@ class REST_Items_Controller extends REST_Controller {
public function prepare_item_for_response( $item, $request ) { public function prepare_item_for_response( $item, $request ) {
if(!empty($item)){ if(!empty($item)){
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-item-meta', [], $request);
$extra_metadata_values = [];
foreach ($extra_metadata as $extra_meta) {
$extra_metadata_values[$extra_meta] = get_post_meta($item->get_id(), $extra_meta, true);
}
if(!isset($request['fetch_only'])) { if(!isset($request['fetch_only'])) {
$item_arr = $item->_toArray(); $item_arr = $item->_toArray();
$item_arr = array_merge($extra_metadata_values, $item_arr);
if ( $request['context'] === 'edit' ) { if ( $request['context'] === 'edit' ) {
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
} }
@ -169,6 +185,8 @@ class REST_Items_Controller extends REST_Controller {
$item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter);
$item_arr = array_merge($extra_metadata_values, $item_arr);
if(is_array($attributes_to_filter) && array_key_exists('meta', $attributes_to_filter)){ if(is_array($attributes_to_filter) && array_key_exists('meta', $attributes_to_filter)){
$args = array('post__in' => $attributes_to_filter['meta']); $args = array('post__in' => $attributes_to_filter['meta']);
@ -183,19 +201,6 @@ class REST_Items_Controller extends REST_Controller {
$item_arr['url'] = get_permalink( $item_arr['id'] ); $item_arr['url'] = get_permalink( $item_arr['id'] );
$item_arr['exposer_urls'] = \Tainacan\Exposers\Exposers::get_exposer_urls(get_rest_url(null, "{$this->namespace}/{$this->rest_base}/{$item->get_id()}/")); $item_arr['exposer_urls'] = \Tainacan\Exposers\Exposers::get_exposer_urls(get_rest_url(null, "{$this->namespace}/{$this->rest_base}/{$item->get_id()}/"));
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-item-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr; return $item_arr;
} }

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="block"> <div class="block">
<div <div
v-for="(option, index) in options" v-for="(option, index) in options.slice(0, filter.max_options)"
:key="index" :key="index"
class="metadatum"> class="metadatum">
<b-checkbox <b-checkbox
@ -12,7 +12,7 @@
</b-checkbox> </b-checkbox>
<div <div
class="view-all-button-container" class="view-all-button-container"
v-if="option.seeMoreLink && index == options.length - 1" v-if="option.seeMoreLink && index == options.slice(0, filter.max_options).length - 1"
@click="openCheckboxModal()" @click="openCheckboxModal()"
v-html="option.seeMoreLink"/> v-html="option.seeMoreLink"/>
</div> </div>
@ -112,7 +112,7 @@
let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ? let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ?
this.metadatum_object.metadata_type_options.collection_id : this.collection_id; this.metadatum_object.metadata_type_options.collection_id : this.collection_id;
promise = this.getValuesRelationship( collectionTarget, null, [], 0, this.filter.max_options); promise = this.getValuesRelationship( collectionTarget, null, [], 0, this.filter.max_options, false, '1');
promise promise
.then(() => { .then(() => {
if(this.options.length > this.filter.max_options){ if(this.options.length > this.filter.max_options){
@ -122,7 +122,7 @@
this.$console.error(error); this.$console.error(error);
}) })
} else { } else {
promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel, [], 0, this.filter.max_options ); promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel, [], 0, this.filter.max_options, false, '1' );
promise promise
.then(() => { .then(() => {
if(this.options.length > this.filter.max_options){ if(this.options.length > this.filter.max_options){
@ -200,22 +200,9 @@
query: this.query query: this.query
}, },
events: { events: {
appliedCheckBoxModal: (options) => { this. appliedCheckBoxModal(options) } appliedCheckBoxModal: () => this.loadOptions()
} }
}); });
},
appliedCheckBoxModal(options) {
this.options = this.options.concat(options)
for(let i = 0; i < this.options.length; ++i) {
for(let j = i + 1; j < this.options.length; ++j) {
if(this.options[i].value == this.options[j].value)
this.options.splice(j--, 1);
}
if (i == this.options.length - 1)
this.options[i].seeMoreLink = `<a style="font-size: 0.75rem;"> ${ this.$i18n.get('label_view_all') } </a>`;
}
this.selectedValues();
} }
} }
} }

View File

@ -18,10 +18,10 @@ export const filter_type_mixin = {
query: {} query: {}
}, },
methods: { methods: {
getValuesPlainText(metadatumId, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal) { getValuesPlainText(metadatumId, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') {
let query_items = { 'current_query': this.query }; let query_items = { 'current_query': this.query };
let url = `/collection/${this.collection}/facets/${metadatumId}?`; let url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`;
if(offset != undefined && number != undefined){ if(offset != undefined && number != undefined){
url += `offset=${offset}&number=${number}&`; url += `offset=${offset}&number=${number}&`;
@ -102,9 +102,9 @@ export const filter_type_mixin = {
this.$console.error(error); this.$console.error(error);
}); });
}, },
getValuesRelationship(collectionTarget, search, valuesToIgnore, offset, number, isInCheckboxModal) { getValuesRelationship(collectionTarget, search, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') {
let query_items = { 'current_query': this.query }; let query_items = { 'current_query': this.query };
let url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + '?'; let url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`;
if(offset != undefined && number != undefined){ if(offset != undefined && number != undefined){
url += `offset=${offset}&number=${number}`; url += `offset=${offset}&number=${number}`;

View File

@ -99,7 +99,7 @@
let query_items = { 'current_query': this.query }; let query_items = { 'current_query': this.query };
axios.get('/collection/'+ this.collection +'/facets/' + this.metadatum axios.get('/collection/'+ this.collection +'/facets/' + this.metadatum
+ `?hideempty=0&order=asc&parent=0&number=${this.filter.max_options}&` + qs.stringify(query_items)) + `?getSelected=1&hideempty=0&order=asc&parent=0&number=${this.filter.max_options}&` + qs.stringify(query_items))
.then( res => { .then( res => {
for (let item of res.data) { for (let item of res.data) {
@ -213,23 +213,10 @@
query: this.query query: this.query
}, },
events: { events: {
appliedCheckBoxModal: (options) => { this. appliedCheckBoxModal(options) } appliedCheckBoxModal: () => this.loadOptions()
}, },
width: 'calc(100% - 8.333333333%)', width: 'calc(100% - 8.333333333%)',
}); });
},
appliedCheckBoxModal(options) {
this.options = this.options.concat(options)
for(let i = 0; i < this.options.length; ++i) {
for(let j = i + 1; j < this.options.length; ++j) {
if(this.options[i].value == this.options[j].value)
this.options.splice(j--, 1);
}
if (i == this.options.length - 1)
this.options[i].seeMoreLink = `<a style="font-size: 0.75rem;"> ${ this.$i18n.get('label_view_all') } </a>`;
}
this.selectedValues();
} }
} }
} }

View File

@ -8,6 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
* Class TainacanMetadatumType * Class TainacanMetadatumType
*/ */
class Text extends Metadata_Type { class Text extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){ function __construct(){
// call metadatum type constructor // call metadatum type constructor
@ -26,5 +27,27 @@ class Text extends Metadata_Type {
item_id="'.$itemMetadata->get_item()->get_id().'" item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\' value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-text>'; 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 TainacanMetadatumType
*/ */
class Textarea extends Metadata_Type { class Textarea extends Metadata_Type {
use \Tainacan\Traits\Formatter_Text;
function __construct(){ function __construct(){
// call metadatum type constructor // call metadatum type constructor
@ -21,11 +22,33 @@ class Textarea extends Metadata_Type {
* @return string * @return string
*/ */
public function render( $itemMetadata ){ public function render( $itemMetadata ) {
return '<tainacan-textarea return '<tainacan-textarea
metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'" metadatum_id ="'.$itemMetadata->get_metadatum()->get_id().'"
item_id="'.$itemMetadata->get_item()->get_id().'" item_id="'.$itemMetadata->get_item()->get_id().'"
value=\''.json_encode( $itemMetadata->get_value() ).'\' value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-textarea>'; 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

@ -500,12 +500,17 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertFalse( is_numeric($document_id) ); $this->assertFalse( is_numeric($document_id) );
$attachments = $items[0]->get_attachments(); $attachments = $items[0]->get_attachments();
$this->assertEquals( 1, count( $attachments ) );
$this->assertTrue( count($items[2]->get_attachments()) > 0 ); if(@file_get_contents ( 'https://www.w3schools.com/w3css/img_lights.jpg' ))
$this->assertEquals( 1, count( $attachments ) );
if(@file_get_contents ( 'https://www.youtube.com/watch?v=V8dpmD4HG5s&start_radio=1&list=RDEMZS6OrHEAut8dOA38mVtVpg' ))
$this->assertTrue( count($items[2]->get_attachments()) > 0 );
$document_id = $items[2]->get_document(); $document_id = $items[2]->get_document();
$this->assertTrue( is_numeric($document_id) );
if(@file_get_contents ( 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg' ))
$this->assertTrue( is_numeric($document_id) );
$document = $items[3]->get_document(); $document = $items[3]->get_document();
$this->assertTrue( wp_http_validate_url($document) !== false ); $this->assertTrue( wp_http_validate_url($document) !== false );

View File

@ -243,5 +243,64 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
//first 2 metadata are repository metadata //first 2 metadata are repository metadata
$this->assertTrue( in_array('metadado', $names) ); $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);
}
} }