From 685b61b0b110f7601b73757782c6683d22054aa1 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Mon, 3 Jun 2024 11:50:50 -0300 Subject: [PATCH] Creates first version of dates intersection filter. #887. --- .../edition/filter-edition-form.vue | 4 +- .../FormDatesIntersection.vue | 98 +++++++++ .../TainacanFilterDatesIntersection.vue | 187 ++++++++++++++++++ .../class-tainacan-dates-intersection.php | 119 +++++++++++ .../class-tainacan-filter-type-helper.php | 1 + .../filter-types/tainacan-filter-item.vue | 1 + src/views/tainacan-i18n.php | 1 + 7 files changed, 410 insertions(+), 1 deletion(-) create mode 100644 src/views/admin/components/filter-types/dates-intersection/FormDatesIntersection.vue create mode 100644 src/views/admin/components/filter-types/dates-intersection/TainacanFilterDatesIntersection.vue create mode 100644 src/views/admin/components/filter-types/dates-intersection/class-tainacan-dates-intersection.php diff --git a/src/views/admin/components/edition/filter-edition-form.vue b/src/views/admin/components/edition/filter-edition-form.vue index e3c609502..3f0e845ab 100644 --- a/src/views/admin/components/edition/filter-edition-form.vue +++ b/src/views/admin/components/edition/filter-edition-form.vue @@ -252,13 +252,15 @@ import { formHooks } from "../../js/mixins"; 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'; +import FormFilterDatesIntersection from '../filter-types/dates-intersection/FormDatesIntersection.vue'; export default { name: 'FilterEditionForm', components: { 'tainacan-filter-form-numeric': FormFilterNumeric, 'tainacan-filter-form-numeric-interval': FormFilterNumericInterval, - 'tainacan-filter-form-numeric-list-interval': FormFilterNumericListInterval + 'tainacan-filter-form-numeric-list-interval': FormFilterNumericListInterval, + 'tainacan-filter-form-dates-intersection': FormFilterDatesIntersection }, mixins: [ formHooks ], props: { diff --git a/src/views/admin/components/filter-types/dates-intersection/FormDatesIntersection.vue b/src/views/admin/components/filter-types/dates-intersection/FormDatesIntersection.vue new file mode 100644 index 000000000..83ddf0de3 --- /dev/null +++ b/src/views/admin/components/filter-types/dates-intersection/FormDatesIntersection.vue @@ -0,0 +1,98 @@ + + + \ No newline at end of file diff --git a/src/views/admin/components/filter-types/dates-intersection/TainacanFilterDatesIntersection.vue b/src/views/admin/components/filter-types/dates-intersection/TainacanFilterDatesIntersection.vue new file mode 100644 index 000000000..c2f16c362 --- /dev/null +++ b/src/views/admin/components/filter-types/dates-intersection/TainacanFilterDatesIntersection.vue @@ -0,0 +1,187 @@ + + + + + diff --git a/src/views/admin/components/filter-types/dates-intersection/class-tainacan-dates-intersection.php b/src/views/admin/components/filter-types/dates-intersection/class-tainacan-dates-intersection.php new file mode 100644 index 000000000..cf3162042 --- /dev/null +++ b/src/views/admin/components/filter-types/dates-intersection/class-tainacan-dates-intersection.php @@ -0,0 +1,119 @@ +set_name( __('Dates Intersection', 'tainacan') ); + $this->set_supported_types(['date']); + $this->set_component('tainacan-filter-dates-intersection'); + $this->set_form_component('tainacan-filter-form-dates-intersection'); + $this->set_default_options([ + 'secondary_filter_metadatum_id' => '', + 'secondary_filter_metadatum_name' => '', + ]); + $this->set_use_max_options(false); + $this->set_preview_template(' +
+
+ +
+

until

+
+ +
+
+ '); + + } + + /** + * @inheritdoc + */ + public function get_form_labels(){ + return [ + 'secondary_filter_metadatum_id' => [ + 'title' => __( 'Second date metadatum', 'tainacan' ), + 'description' => __( 'The other metadatum to which this filter will compare values to find if there is an intersection of dates.', 'tainacan' ), + ], + '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' ), + ] + ]; + } + + /** + * @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('secondary_filter_metadatum_id')) ) { + return [ + 'secondary_filter_metadatum_id' => __('The secondary date metadatum is required.','tainacan') + ]; + } + + // Validate if the second date metadatum is a date metadatum + + return true; + } + +} + +class Dates_Intersection_Interval_Helper { + use \Tainacan\Traits\Singleton_Instance; + + protected function init() { + add_filter( 'tainacan-api-items-tainacan-filter-dates-intersection-filter-arguments', [$this, 'format_filter_arguments']); + } + + function format_filter_arguments( $filter_arguments ) { + if ( + !isset($filter_arguments['compare']) || + !isset($filter_arguments['label']) + ) { + return $filter_arguments; + } + + if ( + is_array($filter_arguments['label']) && + count($filter_arguments['label']) === 2 + ) { + $filter_arguments['label'] = $filter_arguments['label'][0] . ' - ' . $filter_arguments['label'][1]; + } + if ( + isset( $filter_arguments['filter'] ) && + isset( $filter_arguments['filter']['filter_type_options'] ) && + isset( $filter_arguments['filter']['filter_type_options']['secondary_filter_metadatum_name'] ) && + !empty( $filter_arguments['filter']['filter_type_options']['secondary_filter_metadatum_name'] ) + ) { + $filter_arguments['filter']['name'] = $filter_arguments['filter']['name'] . ' - ' . $filter_arguments['filter']['filter_type_options']['secondary_filter_metadatum_name']; + } + return $filter_arguments; + } +} +Dates_Intersection_Interval_Helper::get_instance(); \ No newline at end of file diff --git a/src/views/admin/components/filter-types/filter-type-helper/class-tainacan-filter-type-helper.php b/src/views/admin/components/filter-types/filter-type-helper/class-tainacan-filter-type-helper.php index 594589ec6..cb4ffa3b5 100644 --- a/src/views/admin/components/filter-types/filter-type-helper/class-tainacan-filter-type-helper.php +++ b/src/views/admin/components/filter-types/filter-type-helper/class-tainacan-filter-type-helper.php @@ -38,6 +38,7 @@ class Filter_Type_Helper { $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Selectbox'); $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Autocomplete'); $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Date_Interval'); + $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Dates_Intersection'); $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Numeric_Interval'); $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\TaxonomyTaginput'); $this->Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\TaxonomyCheckbox'); diff --git a/src/views/admin/components/filter-types/tainacan-filter-item.vue b/src/views/admin/components/filter-types/tainacan-filter-item.vue index 73479b805..bd3f4609f 100644 --- a/src/views/admin/components/filter-types/tainacan-filter-item.vue +++ b/src/views/admin/components/filter-types/tainacan-filter-item.vue @@ -100,6 +100,7 @@ TainacanFilterTaxonomyCheckbox: defineAsyncComponent(() => import('./taxonomy/TainacanFilterCheckbox.vue')), TainacanFilterTaxonomyTaginput: defineAsyncComponent(() => import('./taxonomy/TainacanFilterTaginput.vue')), TainacanFilterDateInterval: defineAsyncComponent(() => import('./date-interval/TainacanFilterDateInterval.vue')), + TainacanFilterDatesIntersection: defineAsyncComponent(() => import('./dates-intersection/TainacanFilterDatesIntersection.vue')), TainacanFilterNumericInterval: defineAsyncComponent(() => import('./numeric-interval/TainacanFilterNumericInterval.vue')), TainacanFilterNumericListInterval: defineAsyncComponent(() => import('./numeric-list-interval/TainacanFilterNumericListInterval.vue')) }, diff --git a/src/views/tainacan-i18n.php b/src/views/tainacan-i18n.php index c63e44a7b..4bb2101c4 100644 --- a/src/views/tainacan-i18n.php +++ b/src/views/tainacan-i18n.php @@ -788,6 +788,7 @@ return apply_filters( 'tainacan-i18n', [ 'instruction_click_to_add_a_point' => __( 'Drag to reposition or click to insert a marker', 'tainacan' ), 'instruction_select_geocoordinate_metadatum' => __( 'Select a geocoordinate metadatum', 'tainacan' ), 'instruction_multiple_terms_insertion' => __( 'Type or paste here a list of names using a separator to create multiple terms at once.', 'tainacan' ), + 'instruction_select_second_date_to_compare' => __( 'Select the second date metadatum', 'tainacan' ), // Info. Other feedback to user. 'info_items_tab_all' => __( 'Every item, except by those sent to trash.', 'tainacan' ),