feat: add geo coordinate metadata component

This commit is contained in:
vnmedeiros 2022-12-02 15:59:53 -03:00
parent 3e5a893231
commit 0b0bb4be51
5 changed files with 168 additions and 0 deletions

View File

@ -0,0 +1,49 @@
<template>
<section>
<b-field
:addons="false"
:label="$i18n.getHelperTitle('tainacan-text', 'display_suggestions')">
&nbsp;
<b-switch
size="is-small"
:value="displaySuggestions"
@input="onUpdateDisplaySuggestions"
:true-value="'yes'"
:false-value="'no'" />
<help-button
:title="$i18n.getHelperTitle('tainacan-text', 'display_suggestions')"
:message="$i18n.getHelperMessage('tainacan-text', 'display_suggestions')"/>
</b-field>
</section>
</template>
<script>
export default {
props: {
value: [ String, Object, Array ]
},
data() {
return {
displaySuggestions: String
}
},
created() {
this.displaySuggestions = this.value && this.value.display_suggestions ? this.value.display_suggestions : 'no';
},
methods: {
onUpdateDisplaySuggestions(value) {
this.displaySuggestions = value;
this.$emit('input', { display_suggestions: value });
}
}
}
</script>
<style scoped>
section{
margin-bottom: 10px;
}
.tainacan-help-tooltip-trigger {
font-size: 1em;
}
</style>

View File

@ -0,0 +1,47 @@
<template>
<div>
<b-input
:id="'tainacan-item-metadatum_id-' + itemMetadatum.metadatum.id + (itemMetadatum.parent_meta_id ? ('_parent_meta_id-' + itemMetadatum.parent_meta_id) : '')"
:disabled="disabled"
type="text"
@input.native="onInput"
@blur="onBlur"
:placeholder="itemMetadatum.metadatum.placeholder || ''" />
<p
style="font-size: 0.75em;"
class="has-text-danger is-italic">{{ $i18n.get('info_error_invalid_geocoordinate') }}</p>
</div>
</template>
<script>
export default {
mixins: [ ],
props: {
itemMetadatum: Object,
value: [String, Array],
disabled: false,
},
data() {
return {
coordinateValue: '-14.4086569,-51.31668',
}
},
created(){
if (this.value)
this.coordinateValue= this.value;
},
methods: {
onInput: _.debounce(function ($event) {
if ($event.target.value != '') {
this.$emit('input', this.coordinateValue);
} else {
this.$emit('input', '');
}
}, 300),
onBlur() {
this.$emit('blur');
}
}
}
</script>

View File

@ -0,0 +1,64 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Item_Metadata_Entity;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class GeoCoordinate
*/
class GeoCoordinate extends Metadata_Type {
function __construct() {
// call metadatum type constructor
parent::__construct();
$this->set_primitive_type('geo');
$this->set_component('tainacan-geocoordinate');
$this->set_form_component('tainacan-form-geocoordinate');
$this->set_name( __('Geographical Location', 'tainacan') );
$this->set_description( __('Represents a geographical location that is determined by latitude and longitude coordinates.', 'tainacan') );
$this->set_preview_template('
<div>
<div class="control">
!!POINT IN MAP!!
</div>
</div>
');
}
/**
* Validates a given coordinate
*
* @param float|int|string $lat Latitude
* @param float|int|string $long Longitude
* @return bool `true` if the coordinate is valid, `false` if not
*/
private function validateLatLong($lat, $long) {
return preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?),[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $lat.','.$long);
}
public function validate( Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$value = is_array($value) ? $value : [$value];
foreach ($value as $coordinate) {
$arr_coordinate = explode(",", $coordinate);
if( count($arr_coordinate) != 2 || !$this->validateLatLong($arr_coordinate[0], $arr_coordinate[1])) {
$this->add_error( sprintf(__('The value (%s) is not a valid geo coordinate', 'tainacan'), $coordinate ) );
return false;
}
}
return true;
}
/**
* Get the value as a HTML string with proper date format set in admin
* @return string
*/
public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = is_array($value) ? implode(" - ", $value) : $value;
return $return;
}
}

View File

@ -42,6 +42,7 @@ class Metadata_Type_Helper {
$this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\Compound');
$this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\User');
$this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\Control');
$this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\GeoCoordinate');
// the priority should see less than on function
// `load_admin_page()` of class `Admin` in file /src/views/class-tainacan-admin.php

View File

@ -45,6 +45,7 @@ import Relationship from '../components/metadata-types/relationship/Relationship
import Taxonomy from '../components/metadata-types/taxonomy/Taxonomy.vue';
import Compound from '../components/metadata-types/compound/Compound.vue';
import User from '../components/metadata-types/user/User.vue';
import GeoCoordinate from '../components/metadata-types/geocoordinate/GeoCoordinate.vue'
import FormText from '../components/metadata-types/text/FormText.vue';
import FormRelationship from '../components/metadata-types/relationship/FormRelationship.vue';
@ -52,6 +53,8 @@ import FormTaxonomy from '../components/metadata-types/taxonomy/FormTaxonomy.vue
import FormSelectbox from '../components/metadata-types/selectbox/FormSelectbox.vue';
import FormNumeric from '../components/metadata-types/numeric/FormNumeric.vue';
import FormUser from '../components/metadata-types/user/FormUser.vue';
import FormGeoCoordinate from '../components/metadata-types/geocoordinate/FormGeoCoordinate.vue';
import FilterNumeric from '../components/filter-types/numeric/Numeric.vue';
import FilterDate from '../components/filter-types/date/Date.vue';
@ -212,6 +215,8 @@ export default (element) => {
Vue.component('tainacan-taxonomy', Taxonomy);
Vue.component('tainacan-compound', Compound);
Vue.component('tainacan-user', User);
Vue.component('tainacan-geocoordinate', GeoCoordinate);
/* Metadata Option forms */
Vue.component('tainacan-form-text', FormText);
@ -221,6 +226,8 @@ export default (element) => {
Vue.component('tainacan-form-numeric', FormNumeric);
Vue.component('tainacan-form-user', FormUser);
Vue.component('term-edition-form', TermEditionForm);
Vue.component('tainacan-form-geocoordinate', FormGeoCoordinate);
/* Filters */
Vue.component('tainacan-filter-numeric', FilterNumeric);