Adds first and second comparator options to intersection metadata. #887.

This commit is contained in:
mateuswetah 2024-06-03 15:11:07 -03:00
parent 685b61b0b1
commit a9df08c15f
4 changed files with 119 additions and 13 deletions

View File

@ -420,7 +420,7 @@ export default {
-webkit-column-gap: 0;
-webkit-column-rule: none;
column-count: 2;
column-gap: 4em;
column-gap: 3em;
column-rule: none;
padding-bottom: 0.5em;

View File

@ -29,6 +29,46 @@
</option>
</b-select>
</b-field>
<div style="column-count: 2;">
<b-field :addons="false">
<label
style="line-height: normal;"
class="label is-inline">
{{ $i18n.getHelperTitle('tainacan-filter-dates-intersection', 'first_comparator') }}<span>&nbsp;*&nbsp;</span>
<help-button
:title="$i18n.getHelperTitle('tainacan-filter-dates-intersection', 'first_comparator')"
:message="$i18n.getHelperMessage('tainacan-filter-dates-intersection', 'first_comparator')" />
</label>
<b-select
v-model="firstComparator"
@update:model-value="emitValues()">
<option
v-for="(comparatorObject, comparatorKey) in comparatorsObject"
:key="comparatorKey"
:value="comparatorKey"
v-html="comparatorObject.symbol + '&nbsp;' + comparatorObject.label" />
</b-select>
</b-field>
<b-field :addons="false">
<label
style="line-height: normal;"
class="label is-inline">
{{ $i18n.getHelperTitle('tainacan-filter-dates-intersection', 'second_comparator') }}<span>&nbsp;*&nbsp;</span>
<help-button
:title="$i18n.getHelperTitle('tainacan-filter-dates-intersection', 'second_comparator')"
:message="$i18n.getHelperMessage('tainacan-filter-dates-intersection', 'second_comparator')" />
</label>
<b-select
v-model="secondComparator"
@update:model-value="emitValues()">
<option
v-for="(comparatorObject, comparatorKey) in comparatorsObject"
:key="comparatorKey"
:value="comparatorKey"
v-html="comparatorObject.symbol + '&nbsp;' + comparatorObject.label" />
</b-select>
</b-field>
</div>
</div>
</template>
@ -50,7 +90,11 @@
loading: true,
metadataType: '',
metadataMessage: '',
secondDateMetadatumId: [Number, String]
secondDateMetadatumId: [Number, String],
secondDateMetadatumName: String,
firstComparator: String,
secondComparator: String,
comparatorsObject: {}
}
},
watch: {
@ -63,13 +107,46 @@
},
created() {
this.secondDateMetadatumId = this.modelValue && this.modelValue.secondary_filter_metadatum_id ? this.modelValue.secondary_filter_metadatum_id : '';
this.secondDateMetadatumName = this.modelValue && this.modelValue.secondary_filter_metadatum_name ? this.modelValue.secondary_filter_metadatum_name : '';
this.firstComparator = this.modelValue && this.modelValue.first_comparator ? this.modelValue.first_comparator : '>=';
this.secondComparator = this.modelValue && this.modelValue.second_comparator ? this.modelValue.second_comparator : '<=';
this.loading = true;
this.fetchMetadata();
console.log(this.filter)
this.comparatorsObject = {
'=': {
symbol: '&#61;',
label: this.$i18n.get('is_equal_to')
},
'!=': {
symbol: '&#8800;',
label: this.$i18n.get('is_not_equal_to')
},
'>': {
symbol: '&#62;',
label: this.$i18n.get('after')
},
'>=': {
symbol: '&#8805;',
label: this.$i18n.get('after_or_on_day')
},
'<': {
symbol: '&#60;',
label: this.$i18n.get('before')
},
'<=': {
symbol: '&#8804;',
label: this.$i18n.get('before_or_on_day')
}
};
},
methods: {
async fetchMetadata() {
let endpoint = this.filter.collectionId && this.filter.collectionId !== 'default' ? ( '/collections/' + this.filter.collectionId + '/metadata' ) : '/metadata';
let endpoint = this.filter.collection_id && this.filter.collection_id !== 'default' ? ( '/collection/' + this.filter.collection_id + '/metadata' ) : '/metadata';
endpoint += '?metaquery[0][key]=metadata_type&metaquery[0][value]=Tainacan\\Metadata_Types\\Date&nopaging=1&exclude=' + this.filter.metadatum_id;
return await tainacanApi.get(endpoint)
.then(res => {
this.loading = false;
@ -82,8 +159,17 @@
},
onUpdateSecondDateMetadatumId() {
const selectedMetadatum = this.metadata.find( aMetadatum => aMetadatum.id == this.secondDateMetadatumId );
const selectedMetadatumName = selectedMetadatum ? selectedMetadatum.name : '';
this.$emit('update:model-value', { secondary_filter_metadatum_id: this.secondDateMetadatumId, secondary_filter_metadatum_name: selectedMetadatumName});
this.selectedMetadatumName = selectedMetadatum ? selectedMetadatum.name : '';
this.selectedMetadatumId = selectedMetadatum ? selectedMetadatum.id : '';
this.emitValues();
},
emitValues() {
this.$emit('update:model-value', {
first_comparator: this.firstComparator,
second_comparator: this.secondComparator,
secondary_filter_metadatum_id: this.secondDateMetadatumId,
secondary_filter_metadatum_name: this.secondDateMetadatumName
});
},
setErrorsAttributes( type, message ) {
this.metadataType = type;

View File

@ -159,7 +159,7 @@
this.$emit('input', {
filter: 'intersection',
type: 'DATE',
compare: '>=',
compare: this.filterTypeOptions.first_comparator,
metadatum_id: this.metadatumId,
collection_id: this.collectionId,
value: values
@ -167,7 +167,7 @@
this.$emit('input', {
filter: 'intersection',
type: 'DATE',
compare: '<=',
compare: this.filterTypeOptions.second_comparator,
metadatum_id: this.filterTypeOptions.secondary_filter_metadatum_id,
collection_id: this.collectionId,
value: values

View File

@ -17,6 +17,8 @@ class Dates_Intersection extends Filter_Type {
$this->set_default_options([
'secondary_filter_metadatum_id' => '',
'secondary_filter_metadatum_name' => '',
'first_comparator' => '>=',
'second_comparator' => '<='
]);
$this->set_use_max_options(false);
$this->set_preview_template('
@ -59,6 +61,14 @@ class Dates_Intersection extends Filter_Type {
'secondary_filter_metadatum_name' => [
'title' => __( 'Second date metadatum', 'tainacan' ),
'description' => __( 'Label of the other metadatum to which this filter will compare values to find if there is an intersection of dates.', 'tainacan' ),
],
'first_comparator' => [
'title' => __( 'First comparator', 'tainacan' ),
'description' => __( 'Comparator to be used for checking the first metadata value.', 'tainacan' ),
],
'second_comparator' => [
'title' => __( 'Second comparator', 'tainacan' ),
'description' => __( 'Comparator to be used for checking the second metadata value.', 'tainacan' ),
]
];
}
@ -68,18 +78,28 @@ class Dates_Intersection extends Filter_Type {
* @return array|bool true if is validate or array if has error
*/
public function validate_options(\Tainacan\Entities\Filter $filter) {
if ( !in_array($filter->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( empty($this->get_option('secondary_filter_metadatum_id')) ) {
return [
$errors = [];
if ( empty($this->get_option('secondary_filter_metadatum_id')) )
$errors[] = [
'secondary_filter_metadatum_id' => __('The secondary date metadatum is required.','tainacan')
];
}
// Validate if the second date metadatum is a date metadatum
if ( empty($this->get_option('first_comparator')) )
$errors[] = [
'first_comparator' => __('The first comparator is required.','tainacan')
];
if ( empty($this->get_option('second_comparator')) )
$errors[] = [
'second_comparator' => __('The second comparator is required.','tainacan')
];
return true;
return count($errors) > 0 ? $errors : true;
}
}