From 161fc5cc9f1267dbea4078602a902f6d25eb26e0 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Fri, 31 May 2024 09:40:38 -0300 Subject: [PATCH 1/3] Begins implementation of enabled comparators for date filter. #886. --- .../edition/filter-edition-form.vue | 2 + .../components/filter-types/date/FormDate.vue | 80 +++++++++++++ .../filter-types/date/TainacanFilterDate.vue | 113 ++++++++---------- .../filter-types/date/class-tainacan-date.php | 33 +++++ 4 files changed, 163 insertions(+), 65 deletions(-) create mode 100644 src/views/admin/components/filter-types/date/FormDate.vue diff --git a/src/views/admin/components/edition/filter-edition-form.vue b/src/views/admin/components/edition/filter-edition-form.vue index e3c609502..b13491c9f 100644 --- a/src/views/admin/components/edition/filter-edition-form.vue +++ b/src/views/admin/components/edition/filter-edition-form.vue @@ -249,6 +249,7 @@ import { nextTick } from 'vue'; import { mapActions } from 'vuex'; import { formHooks } from "../../js/mixins"; +import FormFilterDate from '../filter-types/date/FormDate.vue'; import FormFilterNumeric from '../filter-types/numeric/FormNumeric.vue'; import FormFilterNumericInterval from '../filter-types/numeric-interval/FormNumericInterval.vue'; import FormFilterNumericListInterval from '../filter-types/numeric-list-interval/FormNumericListInterval.vue'; @@ -256,6 +257,7 @@ import FormFilterNumericListInterval from '../filter-types/numeric-list-interval export default { name: 'FilterEditionForm', components: { + 'tainacan-filter-form-date': FormFilterDate, 'tainacan-filter-form-numeric': FormFilterNumeric, 'tainacan-filter-form-numeric-interval': FormFilterNumericInterval, 'tainacan-filter-form-numeric-list-interval': FormFilterNumericListInterval diff --git a/src/views/admin/components/filter-types/date/FormDate.vue b/src/views/admin/components/filter-types/date/FormDate.vue new file mode 100644 index 000000000..feab75549 --- /dev/null +++ b/src/views/admin/components/filter-types/date/FormDate.vue @@ -0,0 +1,80 @@ + + + \ No newline at end of file diff --git a/src/views/admin/components/filter-types/date/TainacanFilterDate.vue b/src/views/admin/components/filter-types/date/TainacanFilterDate.vue index 5c6e30649..749dc0fe9 100644 --- a/src/views/admin/components/filter-types/date/TainacanFilterDate.vue +++ b/src/views/admin/components/filter-types/date/TainacanFilterDate.vue @@ -10,66 +10,25 @@ :aria-label="$i18n.get('label_comparator')" class="button is-white"> - + - - =  {{ $i18n.get('is_equal_to') }} - - - ≠  {{ $i18n.get('is_not_equal_to') }} - - - >  {{ $i18n.get('after') }} - - - ≥  {{ $i18n.get('after_or_on_day') }} - - - <  {{ $i18n.get('before') }} - - - ≤  {{ $i18n.get('before_or_on_day') }} - + - ': return '>'; - case '>=': return '≥'; - case '<': return '<'; - case '<=': return '≤'; - default: return ''; - } } }, watch: { @@ -156,6 +105,40 @@ deep: true, }, }, + created() { + this.comparatorsObject = { + '=': { + symbol: '=', + label: this.$i18n.get('is_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('=') < 0 ? 'no' : 'yes' + }, + '!=': { + symbol: '≠', + label: this.$i18n.get('is_not_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('!=') < 0 ? 'no' : 'yes' + }, + '>': { + symbol: '>', + label: this.$i18n.get('after'), + enabled: this.filterTypeOptions.comparators.indexOf('>') < 0 ? 'no' : 'yes' + }, + '>=': { + symbol: '≥', + label: this.$i18n.get('after_or_on_day'), + enabled: this.filterTypeOptions.comparators.indexOf('>=') < 0 ? 'no' : 'yes' + }, + '<': { + symbol: '<', + label: this.$i18n.get('before'), + enabled: this.filterTypeOptions.comparators.indexOf('<') < 0 ? 'no' : 'yes' + }, + '<=': { + symbol: '≤', + label: this.$i18n.get('before_or_on_day'), + enabled: this.filterTypeOptions.comparators.indexOf('<=') < 0 ? 'no' : 'yes' + }, + }; + }, mounted() { this.updateSelectedValues(); }, diff --git a/src/views/admin/components/filter-types/date/class-tainacan-date.php b/src/views/admin/components/filter-types/date/class-tainacan-date.php index 7c76022d6..255fc6988 100644 --- a/src/views/admin/components/filter-types/date/class-tainacan-date.php +++ b/src/views/admin/components/filter-types/date/class-tainacan-date.php @@ -13,7 +13,11 @@ class Date extends Filter_Type { $this->set_name( __('Date', 'tainacan') ); $this->set_supported_types(['date']); $this->set_component('tainacan-filter-date'); + $this->set_form_component('tainacan-filter-form-date'); $this->set_use_max_options(false); + $this->set_default_options([ + 'comparators' => [ '=', '!=', '>', '>=', '<', '<=' ] + ]); $this->set_preview_template('
@@ -57,6 +61,35 @@ class Date extends Filter_Type { '); } + + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'comparators' => [ + 'title' => __( 'Enabled comparators', 'tainacan' ), + 'description' => __( 'A list of comparators to be available in the filter, such as equal, greater than, smaller than, etc.', 'tainacan' ), + ] + ]; + } + + /** + * @param \Tainacan\Entities\Filter $filter + * @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('comparators') ) ) + return [ + 'comparators' => __('"Comparators" array is required', 'tainacan') + ]; + + return true; + } + } class Date_Helper { From 658789e2bb5867d857d074d81e372b5befca01ce Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Mon, 3 Jun 2024 16:32:48 -0300 Subject: [PATCH 2/3] Disables comparator dropdown if only one option is available. #886. --- .../admin/components/filter-types/date/FormDate.vue | 11 +++++++---- .../filter-types/date/TainacanFilterDate.vue | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/views/admin/components/filter-types/date/FormDate.vue b/src/views/admin/components/filter-types/date/FormDate.vue index feab75549..d23c3a073 100644 --- a/src/views/admin/components/filter-types/date/FormDate.vue +++ b/src/views/admin/components/filter-types/date/FormDate.vue @@ -12,7 +12,8 @@ v-for="(comparatorObject, comparatorKey) in comparatorsObject" :key="comparatorKey" v-model="comparators" - :native-value="comparatorObject.key" + :native-value="comparatorKey" + :disabled="comparators.indexOf(comparatorKey) >= 0 && comparators.length <= 1" name="metadata_type_relationship[display_related_item_metadata]" @update:model-value="emitValues()"> @@ -33,7 +34,8 @@ ], data() { return { - comparatorsObject: Object + comparatorsObject: Object, + comparators: Array } }, created() { @@ -72,8 +74,9 @@ }; }, methods: { - onUpdateComparators(value) { - this.$emit('update:model-value', { comparators: value }); + emitValues() { + console.log(this.comparators) + this.$emit('update:model-value', { comparators: this.comparators }); } } } diff --git a/src/views/admin/components/filter-types/date/TainacanFilterDate.vue b/src/views/admin/components/filter-types/date/TainacanFilterDate.vue index 749dc0fe9..22e552e80 100644 --- a/src/views/admin/components/filter-types/date/TainacanFilterDate.vue +++ b/src/views/admin/components/filter-types/date/TainacanFilterDate.vue @@ -1,6 +1,7 @@ @@ -102,15 +122,50 @@ data() { return { step: [Number, String], - showEditStepOptions: false + showEditStepOptions: false, + comparatorsObject: Object, + comparators: Array } }, created() { this.step = this.modelValue && this.modelValue.step ? this.modelValue.step : 1; + this.comparators = ( this.modelValue && this.modelValue.comparators ) ? this.modelValue.comparators : [ '=', '!=', '>', '>=', '<', '<=' ]; + this.comparatorsObject = { + '=': { + symbol: '=', + label: this.$i18n.get('is_equal_to'), + enabled: this.comparators.indexOf('=') < 0 ? 'no' : 'yes' + }, + '!=': { + symbol: '≠', + label: this.$i18n.get('is_not_equal_to'), + enabled: this.comparators.indexOf('!=') < 0 ? 'no' : 'yes' + }, + '>': { + symbol: '>', + label: this.$i18n.get('after'), + enabled: this.comparators.indexOf('>') < 0 ? 'no' : 'yes' + }, + '>=': { + symbol: '≥', + label: this.$i18n.get('after_or_on_day'), + enabled: this.comparators.indexOf('>=') < 0 ? 'no' : 'yes' + }, + '<': { + symbol: '<', + label: this.$i18n.get('before'), + enabled: this.comparators.indexOf('<') < 0 ? 'no' : 'yes' + }, + '<=': { + symbol: '≤', + label: this.$i18n.get('before_or_on_day'), + enabled: this.comparators.indexOf('<=') < 0 ? 'no' : 'yes' + } + }; }, methods: { - onUpdateStep(modelValue) { - this.$emit('update:model-value', { step: modelValue }); + emitValues() { + this.$emit('update:model-value', { step: this.step, comparators: this.comparators }); } } } diff --git a/src/views/admin/components/filter-types/numeric/TainacanFilterNumeric.vue b/src/views/admin/components/filter-types/numeric/TainacanFilterNumeric.vue index 4d6a77ea2..25ca61d2a 100644 --- a/src/views/admin/components/filter-types/numeric/TainacanFilterNumeric.vue +++ b/src/views/admin/components/filter-types/numeric/TainacanFilterNumeric.vue @@ -1,6 +1,7 @@ - - =  {{ $i18n.get('is_equal_to') }} - - - ≠  {{ $i18n.get('is_not_equal_to') }} - - - >  {{ $i18n.get('greater_than') }} - - - ≥  {{ $i18n.get('greater_than_or_equal_to') }} - - - <  {{ $i18n.get('less_than') }} - - - ≤  {{ $i18n.get('less_than_or_equal_to') }} - + - ': return '>'; - case '>=': return '≥'; - case '<': return '<'; - case '<=': return '≤'; - default: return ''; - } - } - }, watch: { 'query': { handler() { @@ -110,6 +66,41 @@ deep: true } }, + created() { + this.comparatorsObject = { + '=': { + symbol: '=', + label: this.$i18n.get('is_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('=') < 0 ? 'no' : 'yes' + }, + '!=': { + symbol: '≠', + label: this.$i18n.get('is_not_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('!=') < 0 ? 'no' : 'yes' + }, + '>': { + symbol: '>', + label: this.$i18n.get('greater_than'), + enabled: this.filterTypeOptions.comparators.indexOf('>') < 0 ? 'no' : 'yes' + }, + '>=': { + symbol: '≥', + label: this.$i18n.get('greater_than_or_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('>=') < 0 ? 'no' : 'yes' + }, + '<': { + symbol: '<', + label: this.$i18n.get('less_than'), + enabled: this.filterTypeOptions.comparators.indexOf('<') < 0 ? 'no' : 'yes' + }, + '<=': { + symbol: '≤', + label: this.$i18n.get('less_than_or_equal_to'), + enabled: this.filterTypeOptions.comparators.indexOf('<=') < 0 ? 'no' : 'yes' + }, + }; + this.comparator = this.filterTypeOptions.comparators[0]; + }, mounted() { this.updateSelectedValues(); }, diff --git a/src/views/admin/components/filter-types/numeric/class-tainacan-numeric.php b/src/views/admin/components/filter-types/numeric/class-tainacan-numeric.php index b973d8460..e5bb818c7 100644 --- a/src/views/admin/components/filter-types/numeric/class-tainacan-numeric.php +++ b/src/views/admin/components/filter-types/numeric/class-tainacan-numeric.php @@ -16,7 +16,8 @@ class Numeric extends Filter_Type { $this->set_form_component('tainacan-filter-form-numeric'); $this->set_use_max_options(false); $this->set_default_options([ - 'step' => 1 + 'step' => 1, + 'comparators' => [ '=', '!=', '>', '>=', '<', '<=' ] ]); $this->set_preview_template('
@@ -77,6 +78,10 @@ class Numeric extends Filter_Type { 'step' => [ 'title' => __( 'Step', 'tainacan' ), 'description' => __( 'The amount to be increased or decreased when clicking on the filter control buttons. This also defines whether the input accepts decimal numbers.', 'tainacan' ), + ], + 'comparators' => [ + 'title' => __( 'Enabled comparators', 'tainacan' ), + 'description' => __( 'A list of comparators to be available in the filter, such as equal, greater than, smaller than, etc.', 'tainacan' ), ] ]; } @@ -89,13 +94,24 @@ class Numeric extends Filter_Type { if ( !in_array($filter->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) return true; - if ( empty($this->get_option('step')) ) { - return [ + $errors = []; + + if ( empty($this->get_option('step')) ) + $errors[] = [ 'step' => __('"Step" value is required','tainacan') ]; - } - return true; + if ( empty($this->get_option('comparators')) ) + $errors[] = [ + 'comparators' => __('"Comparators" array is required', 'tainacan') + ]; + + if ( count( $this->get_option('comparators') ) < 1 ) + $errors[] = [ + 'comparators' => __('At least one comparator should be provided', 'tainacan') + ]; + + return count($errors) ? $errors : true; } } @@ -134,6 +150,8 @@ class Numeric_Helper { case '<=': $filter_arguments['label'] = '≤ ' . $filter_arguments['label'][0]; break; + default: + $filter_arguments['label'] = $filter_arguments['label'][0]; } }