dateFormat ) ? 'mdy' : $this->dateFormat; $date = GFCommon::parse_date( $value, $format ); if ( empty( $date ) || ! $this->checkdate( $date['month'], $date['day'], $date['year'] ) ) { $this->failed_validation = true; $format_name = ''; switch ( $format ) { case 'mdy' : $format_name = 'mm/dd/yyyy'; break; case 'dmy' : $format_name = 'dd/mm/yyyy'; break; case 'dmy_dash' : $format_name = 'dd-mm-yyyy'; break; case 'dmy_dot' : $format_name = 'dd.mm.yyyy'; break; case 'ymd_slash' : $format_name = 'yyyy/mm/dd'; break; case 'ymd_dash' : $format_name = 'yyyy-mm-dd'; break; case 'ymd_dot' : $format_name = 'yyyy.mm.dd'; break; } $message = $this->dateType == 'datepicker' ? sprintf( esc_html__( 'Please enter a valid date in the format (%s).', 'gravityforms' ), $format_name ) : esc_html__( 'Please enter a valid date.', 'gravityforms' ); $this->validation_message = empty( $this->errorMessage ) ? $message : $this->errorMessage; } } } public function is_value_submission_empty( $form_id ) { $value = rgpost( 'input_' . $this->id ); if ( is_array( $value ) ) { // Date field and date drop-downs foreach ( $value as $input ) { if ( strlen( trim( $input ) ) <= 0 ) { return true; } } return false; } else { // Date picker return strlen( trim( $value ) ) <= 0; } } public function get_field_input( $form, $value = '', $entry = null ) { $picker_value = ''; if ( is_array( $value ) ) { // GFCommon::parse_date() takes a numeric array. $value = array_values( $value ); } else { $picker_value = $value; } $format = empty( $this->dateFormat ) ? 'mdy' : esc_attr( $this->dateFormat ); $date_info = GFCommon::parse_date( $value, $format ); $day_value = esc_attr( rgget( 'day', $date_info ) ); $month_value = esc_attr( rgget( 'month', $date_info ) ); $year_value = esc_attr( rgget( 'year', $date_info ) ); $is_entry_detail = $this->is_entry_detail(); $is_form_editor = $this->is_form_editor(); $form_id = $form['id']; $id = intval( $this->id ); $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; $size = $this->size; $disabled_text = $is_form_editor ? "disabled='disabled'" : ''; $class_suffix = $is_entry_detail ? '_admin' : ''; $class = $size . $class_suffix; $form_sub_label_placement = rgar( $form, 'subLabelPlacement' ); $field_sub_label_placement = $this->subLabelPlacement; $is_sub_label_above = $field_sub_label_placement == 'above' || ( empty( $field_sub_label_placement ) && $form_sub_label_placement == 'above' ); $sub_label_class_attribute = $field_sub_label_placement == 'hidden_label' ? "class='hidden_sub_label screen-reader-text'" : ''; $month_input = GFFormsModel::get_input( $this, $this->id . '.1' ); $day_input = GFFormsModel::get_input( $this, $this->id . '.2' ); $year_input = GFFormsModel::get_input( $this, $this->id . '.3' ); $month_sub_label = rgar( $month_input, 'customLabel' ) != '' ? $month_input['customLabel'] : esc_html( _x( 'MM', 'Abbreviation: Month', 'gravityforms' ) ); $day_sub_label = rgar( $day_input, 'customLabel' ) != '' ? $day_input['customLabel'] : esc_html__( 'DD', 'gravityforms' ); $year_sub_label = rgar( $year_input, 'customLabel' ) != '' ? $year_input['customLabel'] : esc_html__( 'YYYY', 'gravityforms' ); $month_placeholder_attribute = GFCommon::get_input_placeholder_attribute( $month_input ); $day_placeholder_attribute = GFCommon::get_input_placeholder_attribute( $day_input ); $year_placeholder_attribute = GFCommon::get_input_placeholder_attribute( $year_input ); $month_placeholder_value = GFCommon::get_input_placeholder_value( $month_input ); $day_placeholder_value = GFCommon::get_input_placeholder_value( $day_input ); $year_placeholder_value = GFCommon::get_input_placeholder_value( $year_input ); $date_picker_placeholder = $this->get_field_placeholder_attribute(); $is_html5 = RGFormsModel::is_html5_enabled(); $date_input_type = $is_html5 ? 'number' : 'text'; $month_html5_attributes = $is_html5 ? "min='1' max='12' step='1'" : ''; $day_html5_attributes = $is_html5 ? "min='1' max='31' step='1'" : ''; $year_min = apply_filters( 'gform_date_min_year', '1920', $form, $this ); $year_max = apply_filters( 'gform_date_max_year', date( 'Y' ) + 1, $form, $this ); $year_min_attribute = $is_html5 && is_numeric( $year_min ) ? "min='{$year_min}'" : ''; $year_max_attribute = $is_html5 && is_numeric( $year_max ) ? "max='{$year_max}'" : ''; $year_step_attribute = $is_html5 ? "step='1'" : ''; $field_position = substr( $format, 0, 3 ); if ( $is_form_editor ) { $datepicker_display = in_array( $this->dateType, array( 'datefield', 'datedropdown' ) ) ? 'none' : 'inline'; $datefield_display = $this->dateType == 'datefield' ? 'inline' : 'none'; $dropdown_display = $this->dateType == 'datedropdown' ? 'inline' : 'none'; $icon_display = $this->calendarIconType == 'calendar' ? 'inline' : 'none'; if ( $is_sub_label_above ) { $month_field = "
"; $day_field = "
"; $year_field = "
"; } else { $month_field = "
"; $day_field = "
"; $year_field = "
"; } $month_dropdown = "
" . $this->get_month_dropdown( '', "{$field_id}_1", rgar( $date_info, 'month' ), '', $disabled_text, $month_placeholder_value ) . '
'; $day_dropdown = "
" . $this->get_day_dropdown( '', "{$field_id}_2", rgar( $date_info, 'day' ), '', $disabled_text, $day_placeholder_value ) . '
'; $year_dropdown = "
" . $this->get_year_dropdown( '', "{$field_id}_3", rgar( $date_info, 'year' ), '', $disabled_text, $year_placeholder_value, $form ) . '
'; $field_string = "
"; switch ( $field_position ) { case 'dmy' : $date_inputs = $day_field . $month_field . $year_field . $day_dropdown . $month_dropdown . $year_dropdown; break; case 'ymd' : $date_inputs = $year_field . $month_field . $day_field . $year_dropdown . $month_dropdown . $day_dropdown; break; default : $date_inputs = $month_field . $day_field . $year_field . $month_dropdown . $day_dropdown . $year_dropdown; break; } $field_string .= "
{$date_inputs}
"; return $field_string; } else { $date_type = $this->dateType; if ( in_array( $date_type, array( 'datefield', 'datedropdown' ) ) ) { switch ( $field_position ) { case 'dmy' : $tabindex = $this->get_tabindex(); if ( $date_type == 'datedropdown' ) { $field_str = "
" . $this->get_day_dropdown( "input_{$id}[]", "{$field_id}_2", rgar( $date_info, 'day' ), $tabindex, $disabled_text, $day_placeholder_value ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_month_dropdown( "input_{$id}[]", "{$field_id}_1", rgar( $date_info, 'month' ), $tabindex, $disabled_text, $month_placeholder_value ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_year_dropdown( "input_{$id}[]", "{$field_id}_3", rgar( $date_info, 'year' ), $tabindex, $disabled_text, $year_placeholder_value, $form ) . '
'; } else { $field_str = $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; } break; case 'ymd' : $tabindex = $this->get_tabindex(); if ( $date_type == 'datedropdown' ) { $field_str = "
" . $this->get_year_dropdown( "input_{$id}[]", "{$field_id}_3", rgar( $date_info, 'year' ), $tabindex, $disabled_text, $year_placeholder_value, $form ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_month_dropdown( "input_{$id}[]", "{$field_id}_1", rgar( $date_info, 'month' ), $tabindex, $disabled_text, $month_placeholder_value ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_day_dropdown( "input_{$id}[]", "{$field_id}_2", rgar( $date_info, 'day' ), $tabindex, $disabled_text, $day_placeholder_value ) . '
'; } else { $field_str = $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; } break; default : $tabindex = $this->get_tabindex(); if ( $date_type == 'datedropdown' ) { $field_str = "
" . $this->get_month_dropdown( "input_{$id}[]", "{$field_id}_1", rgar( $date_info, 'month' ), $tabindex, $disabled_text, $month_placeholder_value ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_day_dropdown( "input_{$id}[]", "{$field_id}_2", rgar( $date_info, 'day' ), $tabindex, $disabled_text, $day_placeholder_value ) . '
'; $tabindex = $this->get_tabindex(); $field_str .= "
" . $this->get_year_dropdown( "input_{$id}[]", "{$field_id}_3", rgar( $date_info, 'year' ), $tabindex, $disabled_text, $year_placeholder_value, $form ) . '
'; } else { $field_str = $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; $tabindex = $this->get_tabindex(); $field_str .= $is_sub_label_above ? "
" : "
"; } break; } return "
$field_str
"; } else { $picker_value = esc_attr( GFCommon::date_display( $picker_value, $format ) ); $icon_class = $this->calendarIconType == 'none' ? 'datepicker_no_icon' : 'datepicker_with_icon'; $icon_url = empty( $this->calendarIconUrl ) ? GFCommon::get_base_url() . '/images/calendar.png' : $this->calendarIconUrl; $icon_url = esc_url( $icon_url ); $tabindex = $this->get_tabindex(); $class = esc_attr( $class ); return "
"; } } } public function get_field_label_class() { return $this->dateType == 'datefield' ? 'gfield_label gfield_label_before_complex' : 'gfield_label'; } public function get_value_default() { $value = parent::get_value_default(); if ( is_array( $this->inputs ) ) { $value = $this->get_date_array_by_format( $value ); } return $value; } /** * The default value for mulit-input date fields will always be an array in mdy order * this code will alter the order of the values to the date format of the field */ public function get_date_array_by_format( $value ) { $format = empty( $this->dateFormat ) ? 'mdy' : esc_attr( $this->dateFormat ); $position = substr( $format, 0, 3 ); $date = array_combine( array( 'm', 'd', 'y' ), $value ); // takes our numerical array and converts it to an associative array $value = array_merge( array_flip( str_split( $position ) ), $date ); // uses the mdy position as the array keys and creates a new array in the desired order return $value; } public function checkdate( $month, $day, $year ) { if ( empty( $month ) || ! is_numeric( $month ) || empty( $day ) || ! is_numeric( $day ) || empty( $year ) || ! is_numeric( $year ) || strlen( $year ) != 4 ) { return false; } return checkdate( $month, $day, $year ); } public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) { return GFCommon::date_display( $value, $this->dateFormat ); } public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) { return GFCommon::date_display( $value, $this->dateFormat ); } /** * Gets merge tag values. * * @since Unknown * @access public * * @uses GFCommon::date_display() * @uses GF_Field_Date::$dateFormat * * @param array|string $value The value of the input. * @param string $input_id The input ID to use. * @param array $entry The Entry Object. * @param array $form The Form Object * @param string $modifier The modifier passed. * @param array|string $raw_value The raw value of the input. * @param bool $url_encode If the result should be URL encoded. * @param bool $esc_html If the HTML should be escaped. * @param string $format The format that the value should be. * @param bool $nl2br If the nl2br function should be used. * * @return string The processed merge tag. */ public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) { $format_modifier = empty( $modifier ) ? $this->dateFormat : $modifier; return GFCommon::date_display( $value, $format_modifier ); } private function get_month_dropdown( $name = '', $id = '', $selected_value = '', $tabindex = '', $disabled_text = '', $placeholder = '' ) { if ( $placeholder == '' ) { $placeholder = esc_html__( 'Month', 'gravityforms' ); } return $this->get_number_dropdown( $name, $id, $selected_value, $tabindex, $disabled_text, $placeholder, 1, 12 ); } private function get_day_dropdown( $name = '', $id = '', $selected_value = '', $tabindex = '', $disabled_text = '', $placeholder = '' ) { if ( $placeholder == '' ) { $placeholder = esc_html__( 'Day', 'gravityforms' ); } return $this->get_number_dropdown( $name, $id, $selected_value, $tabindex, $disabled_text, $placeholder, 1, 31 ); } private function get_year_dropdown( $name = '', $id = '', $selected_value = '', $tabindex = '', $disabled_text = '', $placeholder = '', $form ) { if ( $placeholder == '' ) { $placeholder = esc_html__( 'Year', 'gravityforms' ); } $year_min = apply_filters( 'gform_date_min_year', '1920', $form, $this ); $year_max = apply_filters( 'gform_date_max_year', date( 'Y' ) + 1, $form, $this ); return $this->get_number_dropdown( $name, $id, $selected_value, $tabindex, $disabled_text, $placeholder, $year_max, $year_min ); } private function get_number_dropdown( $name, $id, $selected_value, $tabindex, $disabled_text, $placeholder, $start_number, $end_number ) { $str = "'; return $str; } /** * Returns the value to save in the entry. * * @param string $value * @param array $form * @param string $input_name * @param int $lead_id * @param array $lead * * @return string */ public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) { // if $value is a default value and also an array, it will be an associative array; to be safe, let's convert all array $value to numeric if ( is_array( $value ) ) { $value = array_values( $value ); } $value = GFFormsModel::prepare_date( $this->dateFormat, $value ); $value = $this->sanitize_entry_value( $value, $form['id'] ); return $value; } public function get_entry_inputs() { return null; } public function sanitize_settings() { parent::sanitize_settings(); $this->calendarIconType = wp_strip_all_tags( $this->calendarIconType ); $this->calendarIconUrl = wp_strip_all_tags( $this->calendarIconUrl ); if ( $this->dateFormat && ! in_array( $this->dateFormat, array( 'mdy', 'dmy', 'dmy_dash', 'dmy_dot', 'ymd_slash', 'ymd_dash', 'ymd_dot' ) ) ) { $this->dateFormat = 'mdy'; } } } GF_Fields::register( new GF_Field_Date() );