CMB2-Date-Range-Field icon indicating copy to clipboard operation
CMB2-Date-Range-Field copied to clipboard

Added the possibility to split date range values

Open RubenMartins opened this issue 4 years ago • 7 comments

This will allow to query more effectively between dates.

Props to @rasmustaarnby #9

Can you review this @jtsternberg please?

RubenMartins avatar Apr 22 '21 16:04 RubenMartins

Some notes:

  1. Should the split_date_range arg be set to true as default?
  2. Should be removed the default meta_key and meta_value in DB which is in json when the split_date_range is set to true?

Let me know your thoughts.

RubenMartins avatar Apr 22 '21 16:04 RubenMartins

I can merge when @jtsternberg is approving with what he sees for this.

tw2113 avatar Apr 22 '21 17:04 tw2113

Thank you @jtsternberg for the quick review and comments. When possible can you give it another review? Should be ready to ship with this last commits @tw2113

Again thank you both for the quick reply

RubenMartins avatar Apr 22 '21 17:04 RubenMartins

This isn't a repo I have access to, but here's a quick review.

  1. Should the split_date_range arg be set to true as default?
  2. Should be removed the default meta_key and meta_value in DB which is in json when the split_date_range is set to true?
  1. no, because backwards-compatibility should be maintained
  2. Probably not a bad idea

Unfortunately I can't remove the 2. as the daterangepicker js is using it to populate the field value on load.

RubenMartins avatar Apr 22 '21 17:04 RubenMartins

@jtsternberg this is not correct, can you please let me know how to accomplish the class call to CMB2_Fiel

Any updates on this please?

RubenMartins avatar Apr 28 '21 20:04 RubenMartins

@RubenMartins try this:

	function save_split_date_range( $field_id, $updated, $action, $cmb2_field ) {
		if ( ! $updated || 'repeatable' === $action || ! $cmb2_field->args( 'split_date_range' ) ) {
			return;
		}

		$start_field = $cmb2_field->get_field_clone( array(
			'id' => $field_id . '_start',
		) );

		$end_field = $cmb2_field->get_field_clone( array(
			'id' => $field_id . '_end',
		) );

		if ( $action === 'removed' ) {
			$start_field->remove_data();
			$end_field->remove_data();

			return;
		}

		$value = json_decode( $cmb2_field->value, true );

		if ( is_array( $value ) ) {
			$value = array_map( 'sanitize_text_field', $value );

			$start_field->update_data( $value['start'] );
			$end_field->update_data( $value['end'] );
		}
	}

jtsternberg avatar Apr 28 '21 22:04 jtsternberg

@RubenMartins try this:

	function save_split_date_range( $field_id, $updated, $action, $cmb2_field ) {
		if ( ! $updated || 'repeatable' === $action || ! $cmb2_field->args( 'split_date_range' ) ) {
			return;
		}

		$start_field = $cmb2_field->get_field_clone( array(
			'id' => $field_id . '_start',
		) );

		$end_field = $cmb2_field->get_field_clone( array(
			'id' => $field_id . '_end',
		) );

		if ( $action === 'removed' ) {
			$start_field->remove_data();
			$end_field->remove_data();

			return;
		}

		$value = json_decode( $cmb2_field->value, true );

		if ( is_array( $value ) ) {
			$value = array_map( 'sanitize_text_field', $value );

			$start_field->update_data( $value['start'] );
			$end_field->update_data( $value['end'] );
		}
	}

Thank you @jtsternberg this is way better. When possible please review the pr again.

RubenMartins avatar Apr 29 '21 08:04 RubenMartins