Merge pull request #427 from tainacan/feature/423

Feature/423
This commit is contained in:
Mateus Machado Luna 2020-09-01 17:04:02 -03:00 committed by GitHub
commit bcd428f923
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 165 additions and 43 deletions

View File

@ -69,6 +69,7 @@
v-if="taxonomy_id && taxonomies.length && (input_type == 'tainacan-taxonomy-checkbox' || input_type == 'tainacan-taxonomy-radio')"
:addons="false"
:label="$i18n.getHelperTitle('tainacan-taxonomy', 'visible_options_list')">
 
<b-switch
size="is-small"
v-model="visible_options_list"
@ -81,6 +82,7 @@
v-if="taxonomy_id && taxonomies.length && taxonomies.find((taxonomy) => taxonomy.id == taxonomy_id).allow_insert == 'yes'"
:addons="false"
:label="$i18n.get('label_taxonomy_allow_new_terms')">
&nbsp;
<b-switch
size="is-small"
v-model="allow_new_terms"
@ -91,6 +93,48 @@
:title="$i18n.getHelperTitle('tainacan-taxonomy', 'allow_new_terms')"
:message="$i18n.getHelperMessage('tainacan-taxonomy', 'allow_new_terms')"/>
</b-field>
<b-field
v-if="collections.length"
:addons="false">
<label class="label">
{{ $i18n.getHelperTitle('tainacan-taxonomy', 'link_filtered_by_collections') }}
<help-button
:title="$i18n.getHelperTitle('tainacan-taxonomy', 'link_filtered_by_collections')"
:message="$i18n.getHelperMessage('tainacan-taxonomy', 'link_filtered_by_collections')"/>
</label>
<b-taginput
:value="getSelectedTaxonomyCollections()"
autocomplete
:open-on-focus="true"
:data="collections.filter((collection) => !link_filtered_by_collections.includes(collection.id))"
field="name"
@input="updateSelectedCollections"
@focus="clear()"
attached
:aria-close-label="$i18n.get('remove_value')"
:class="{'has-selected': link_filtered_by_collections != undefined && link_filtered_by_collections != []}"
:placeholder="$i18n.get('instruction_select_one_or_more_collections')"
@typing="filterCollections">
<template slot-scope="props">
<div class="media">
<div
v-if="props.option.thumbnail && props.option.thumbnail.thumbnail && props.option.thumbnail.thumbnail[0]"
class="media-left">
<img
width="24"
:alt="$i18n.get('label_thumbnail')"
:src="props.option.thumbnail.thumbnail[0]" >
</div>
<div class="media-content">
{{ props.option.name }}
</div>
</div>
</template>
<template slot="empty">
{{ $i18n.get('info_no_options_found') }}
</template>
</b-taginput>
</b-field>
</section>
</template>
@ -111,12 +155,15 @@
taxonomy_id: '',
loading: true,
allow_new_terms: 'yes',
link_filtered_by_collections: [],
visible_options_list: false,
input_type: 'tainacan-taxonomy-radio',
multiple_types: {},
single_types: {},
taxonomyType:'',
taxonomyMessage: ''
taxonomyMessage: '',
collections: [],
loadedCollections: []
}
},
computed: {
@ -164,6 +211,7 @@
},
created(){
this.fetchTaxonomies();
this.fetchCollections();
this.single_types['tainacan-taxonomy-radio'] = 'Radio';
this.multiple_types['tainacan-taxonomy-tag-input'] = 'Tag Input';
@ -185,6 +233,7 @@
}
this.visible_options_list = ( this.value.visible_options_list ) ? this.value.visible_options_list : false;
this.link_filtered_by_collections = ( this.value.link_filtered_by_collections ) ? this.value.link_filtered_by_collections : [];
}
this.isReady = true;
@ -197,8 +246,24 @@
this.taxonomyType = type;
this.taxonomyMessage = message;
},
fetchTaxonomies(){
fetchCollections(){
return axios.get('/collections?nopaging=1')
.then(res => {
let collections = res.data;
this.loading = false;
if (collections)
this.collections = collections;
else
this.collections = [];
this.loadedCollections = this.collections;
})
.catch(error => {
this.$console.log(error);
});
},
fetchTaxonomies(){
return axios.get('/taxonomies?nopaging=1&order=asc&orderby=title')
.then(res => {
let taxonomies = res.data;
@ -226,7 +291,25 @@
taxonomy_id: this.taxonomy_id,
input_type: this.input_type,
allow_new_terms: this.allow_new_terms,
visible_options_list: this.visible_options_list
visible_options_list: this.visible_options_list,
link_filtered_by_collections: this.link_filtered_by_collections
})
},
updateSelectedCollections(selectedCollections) {
this.link_filtered_by_collections = selectedCollections.map(collection => collection.id);
this.emitValues();
},
getSelectedTaxonomyCollections() {
if (this.link_filtered_by_collections && this.link_filtered_by_collections.length)
return this.collections.filter((collection) => this.link_filtered_by_collections.includes(collection.id));
return [];
},
filterCollections(searchString) {
this.collections.filter((option) => {
return option.name
.toString()
.toLowerCase()
.indexOf(searchString.toLowerCase()) >= 0
})
}
}

View File

@ -13,20 +13,21 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Taxonomy extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('term');
$this->set_repository( \Tainacan\Repositories\Terms::get_instance() );
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('term');
$this->set_repository( \Tainacan\Repositories\Terms::get_instance() );
$this->set_default_options([
'allow_new_terms' => 'no',
'link_filtered_by_collections' => []
]);
$this->set_default_options([
'allow_new_terms' => 'no'
]);
$this->set_form_component('tainacan-form-taxonomy');
$this->set_form_component('tainacan-form-taxonomy');
$this->set_component('tainacan-taxonomy');
$this->set_name( __('Taxonomy', 'tainacan') );
$this->set_description( __('A metadatum to use a taxonomy in this collection', 'tainacan') );
$this->set_description( __('A metadatum to use a taxonomy in this collection', 'tainacan') );
$this->set_preview_template('
<div>
<div>
@ -71,36 +72,42 @@ class Taxonomy extends Metadata_Type {
<a class="add-new-term">'. __('View all') . '</a>
</div>
');
add_filter( 'tainacan-term-to-html', [$this, 'term_to_html'], 10, 2 );
}
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'taxonomy_id' => [
'title' => __( 'Related Collection', 'tainacan' ),
'description' => __( 'Select the collection to fetch items', 'tainacan' ),
],
'input_type' => [
'title' => __( 'Input type', 'tainacan' ),
'description' => __( 'The html type of the terms list ', 'tainacan' ),
/**
* @inheritdoc
*/
public function get_form_labels(){
return [
'taxonomy_id' => [
'title' => __( 'Related Collection', 'tainacan' ),
'description' => __( 'Select the collection to fetch items', 'tainacan' ),
],
'input_type' => [
'title' => __( 'Input type', 'tainacan' ),
'description' => __( 'The html type of the terms list ', 'tainacan' ),
],
'visible_options_list' => [
'title' => __( 'Always visible options list', 'tainacan' ),
'description' => __( 'Check this option if you are displaying a checkbox or radio input type and wish the options list to always be visible.', 'tainacan' ),
],
'allow_new_terms' => [
'title' => __( 'Allow new terms', 'tainacan' ),
'description' => __( 'Allows to create new terms directly on the item form.', 'tainacan' ),
]
];
}
'title' => __( 'Always visible options list', 'tainacan' ),
'description' => __( 'Check this option if you are displaying a checkbox or radio input type and wish the options list to always be visible.', 'tainacan' ),
],
'allow_new_terms' => [
'title' => __( 'Allow new terms', 'tainacan' ),
'description' => __( 'Allows to create new terms directly on the item form.', 'tainacan' ),
],
'link_filtered_by_collections' => [
'title' => __( 'Link filtered by collections', 'tainacan' ),
'description' => __( 'Links to term items list filtered by certain collections instead of repository level term items page.', 'tainacan' ),
]
];
}
public function validate_options( Metadatum $metadatum) {
if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
return true;
if (empty($this->get_option('taxonomy_id')))
return ['taxonomy_id' => __('Please select a taxonomy', 'tainacan')];
@ -169,7 +176,6 @@ class Taxonomy extends Metadata_Type {
}
}
}
return true;
@ -182,16 +188,16 @@ class Taxonomy extends Metadata_Type {
*
* @return bool Valid or not
*/
public function validate( Item_Metadata_Entity $item_metadata) {
public function validate( Item_Metadata_Entity $item_metadata) {
$item = $item_metadata->get_item();
$item = $item_metadata->get_item();
if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
$valid = true;
if ('no' === $this->get_option('allow_new_terms') || false === $this->get_option('allow_new_terms')) { //support legacy bug when it was saved as false
if ('no' === $this->get_option('allow_new_terms') || false === $this->get_option('allow_new_terms')) { //support legacy bug when it was saved as false
$terms = $item_metadata->get_value();
if (false === $terms)
@ -217,7 +223,38 @@ class Taxonomy extends Metadata_Type {
return $valid;
}
}
public function term_to_html($return, $term) {
$collections = $this->get_option( 'link_filtered_by_collections' );
if ( !empty( $collections ) ) {
$return = '';
$id = $term->get_id();
if ( $id ) {
$link = get_term_link( (int) $id );
if (is_string($link)) {
$meta_query = [
'metaquery' => [
[
'key' => 'collection_id',
'compare' => 'IN',
'value' => $collections
]
]
];
$link = $link . '?' . http_build_query( $meta_query );
$return = "<a data-linkto='term' data-id='$id' href='$link'>";
$return.= $term->get_name();
$return .= "</a>";
}
}
return $return;
}
return $return;
}
/**
* Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string

View File

@ -119,6 +119,7 @@
box-shadow: none;
}
.taginput-container {
border-radius: 1px !important;
padding: 0px !important;
background-color: transparent !important;

View File

@ -534,6 +534,7 @@ return apply_filters( 'tainacan-admin-i18n', [
'instruction_select_title_mapping' => __( 'Before runnning import, consider selecting the title source metadata', 'tainacan'),
'instruction_click_error_to_go_to_metadata' => __( 'Click on the error to go to the metadata:', 'tainacan'),
'instruction_click_to_see_or_search' => __( 'Click to see options or type to search...', 'tainacan'),
'instruction_select_one_or_more_collections' => __( 'Select one or more collections', 'tainacan'),
// Info. Other feedback to user.
'info_items_tab_all' => __( 'Every published item, including those visible only to editors.', 'tainacan' ),