Last active
June 30, 2020 08:23
-
-
Save lukecav/4224fbb2a57383e3cc511f136697ef58 to your computer and use it in GitHub Desktop.
gravity-forms-list-field-datepicker-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Date Picker in List Fields for Gravity Forms | |
Description: Gives the option of adding a date picker to a list field column | |
Version: 1.6.2 | |
Author: Adrian Gordon | |
Author URI: http://www.itsupportguides.com | |
License: GPL2 | |
Text Domain: gravity-forms-list-field-date-picker | |
------------------------------------------------------------------------ | |
Copyright 2015 Adrian Gordon | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
die(); | |
} | |
load_plugin_textdomain( 'gravity-forms-list-field-date-picker', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); | |
add_action( 'admin_notices', array( 'ITSG_GF_List_Field_Date_Picker', 'admin_warnings' ), 20 ); | |
if ( !class_exists( 'ITSG_GF_List_Field_Date_Picker' ) ) { | |
class ITSG_GF_List_Field_Date_Picker | |
{ | |
private static $name = 'Date Picker in List Fields for Gravity Forms'; | |
private static $slug = 'gravity-forms-list-field-date-picker'; | |
/** | |
* Construct the plugin object | |
*/ | |
function __construct() { | |
// register plugin functions through 'gform_loaded' - | |
// this delays the registration until Gravity Forms has loaded, ensuring it does not run before Gravity Forms is available. | |
add_action( 'gform_loaded', array( &$this, 'register_actions' ) ); | |
} | |
/* | |
* Register plugin functions | |
*/ | |
function register_actions() { | |
if ( ( self::is_gravityforms_installed() ) ) { | |
// addon framework | |
require_once( plugin_dir_path( __FILE__ ).'gravity-forms-list-field-datepicker-addon.php' ); | |
// start the plugin | |
add_filter( 'gform_column_input_content', array( &$this, 'change_column_content' ), 10, 6 ); | |
//add_action( 'gform_enqueue_scripts', array( &$this, 'datepicker_js' ), 90, 2 ); | |
add_action( 'gform_editor_js', array( &$this, 'editor_js' ) ); | |
add_action( 'gform_field_appearance_settings', array( &$this, 'field_datepicker_settings' ) , 10, 2 ); | |
add_filter( 'gform_tooltips', array( &$this, 'field_datepicker_tooltip' ) ); | |
add_filter( 'gform_validation', array( &$this, 'validate_datepicker_fields' ) ); | |
} | |
} | |
/* | |
* Handles custom validation for datepicker enabled fields | |
*/ | |
function validate_datepicker_fields( $validation_result ) { | |
$form = $validation_result['form']; | |
if ( self::list_has_datepicker_field( $form ) ) { | |
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1; | |
foreach( $form['fields'] as &$field ) { | |
$field_page = $field->pageNumber; | |
$is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() ); | |
if ( $field_page != $current_page || $is_hidden ) { | |
continue; | |
} | |
$has_columns = is_array( $field['choices'] ); | |
if ( $has_columns ) { | |
$number_of_columns = sizeof( $field['choices'] ); | |
$column_number = 0; | |
$value = rgpost( "input_{$field['id']}" ); | |
if ( is_array( $value ) ) { | |
foreach( $value as $key => $column_value ) { | |
if ( true == rgar( $field['choices'][ $column_number ], 'isDatePicker' ) ) { | |
$default_format = apply_filters( 'itsg_list_field_datepicker_default_format', 'mdy' ); | |
$date_format = isset( $field['choices'][ $column_number ]['isDatePickerFormat'] ) ? $field['choices'][ $column_number ]['isDatePickerFormat'] : $default_format; | |
$value = $column_value; | |
if ( ! empty( $value ) ) { | |
$date = GFCommon::parse_date( $value, $date_format ); | |
if ( empty( $date ) || ! $this->checkdate( $date['month'], $date['day'], $date['year'] ) ) { | |
$validation_result['is_valid'] = false; // set the form validation to false | |
$field->failed_validation = true; | |
$format_name = ''; | |
switch ( $date_format ) { | |
case 'mdy' : | |
$format_name = 'mm/dd/yyyy'; | |
break; | |
case 'my' : | |
$format_name = 'mm/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 = sprintf( esc_html__( "The column '%s' requires a valid date in %s format.", 'gravity-forms-list-field-date-picker' ), $field['choices'][ $column_number ]['text'], $format_name ); | |
$field->validation_message = $message; | |
} | |
} | |
} | |
if ( $column_number >= ( $number_of_columns - 1 ) ) { | |
$column_number = 0; // reset column number | |
} else { | |
$column_number = $column_number + 1; // increment column number | |
} | |
} | |
} | |
} elseif ( true == rgar( $field, 'itsg_list_field_datepicker' ) ) { | |
$default_format = apply_filters( 'itsg_list_field_datepicker_default_format', 'mdy' ); | |
$date_format = isset( $field['itsg_list_field_datepicker_format'] ) ? $field['itsg_list_field_datepicker_format'] : $default_format; | |
$value = rgpost( "input_{$field['id']}" ); | |
if ( is_array( $value ) ) { | |
foreach( $value as $key => $column_value ) { | |
$value = $column_value; | |
if ( ! empty( $value ) ) { | |
$date = GFCommon::parse_date( $value, $date_format ); | |
if ( empty( $date ) || ! $this->checkdate( $date['month'], $date['day'], $date['year'] ) ) { | |
$validation_result['is_valid'] = false; // set the form validation to false | |
$field->failed_validation = true; | |
$format_name = ''; | |
switch ( $date_format ) { | |
case 'mdy' : | |
$format_name = 'mm/dd/yyyy'; | |
break; | |
case 'my' : | |
$format_name = 'mm/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 = sprintf( esc_html__( 'Requires a valid date in %s format.', 'gravity-forms-list-field-date-picker' ), $format_name ); | |
$field->validation_message = $message; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
//Assign modified $form object back to the validation result | |
$validation_result['form'] = $form; | |
return $validation_result; | |
} // END validate_datepicker_fields | |
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 ); | |
} | |
/* | |
* Changes column field if 'date field' option is ticked. Adds 'datepicker' class. | |
*/ | |
function change_column_content( $input, $input_info, $field, $text, $value, $form_id ) { | |
if ( is_admin() ) { | |
$has_columns = is_array( $field['choices'] ); | |
if ( $has_columns ) { | |
foreach( $field['choices'] as $choice ) { | |
if ( $text == $choice['text'] && true == rgar( $choice, 'isDatePicker' ) && 'itsg_list_field_datepicker_icon_none' != rgar( $choice, 'isDatePickerIcon' ) ) { | |
$new_input = str_replace( "<input ", "<input style='width:80%' ", $input ); | |
$new_input .= '<img style="display:inline" id="gfield_input_datepicker_icon" src="' . GFCommon::get_base_url() . '/images/calendar.png">'; | |
return $new_input; | |
} else if ( $text == $choice['text'] ) { | |
return $input; | |
} | |
} | |
} else { | |
if ( 'on' == rgar( $field, 'itsg_list_field_datepicker' ) && 'itsg_list_field_datepicker_icon_none' != rgar( $field, 'itsg_list_field_datepicker_icon' ) ) { | |
$new_input = str_replace( "<input ", "<input style='width:80%' ", $input ); | |
$new_input .= '<img style="display:inline" id="gfield_input_datepicker_icon" src="' . GFCommon::get_base_url() . '/images/calendar.png">'; | |
return $new_input; | |
} | |
return $input; | |
} | |
} else { | |
$has_columns = is_array( $field['choices'] ); | |
if ( $has_columns ) { | |
foreach( $field['choices'] as $choice ) { | |
if ( $text == $choice['text'] && true == rgar( $choice, 'isDatePicker' ) ) { | |
$default_format = apply_filters( 'itsg_list_field_datepicker_default_format', 'mdy' ); | |
$date_format = isset( $choice['isDatePickerFormat'] ) ? $choice['isDatePickerFormat'] : esc_html( $default_format ); | |
$datepicker_class = 'itsg_list_field_datepicker_icon_none' == rgar( $choice, 'isDatePickerIcon' ) ? 'datepicker_no_icon datepicker' :'datepicker'; | |
$new_input = str_replace( "<input ", "<input class='{$datepicker_class} {$date_format} ' ", $input ); | |
return $new_input; | |
} else if ( $text == $choice['text'] ) { | |
return $input; | |
} | |
} | |
} else { | |
if ( 'on' == rgar( $field, 'itsg_list_field_datepicker' ) ) { | |
$default_format = apply_filters( 'itsg_list_field_datepicker_default_format', 'mdy' ); | |
$date_format = isset( $field['itsg_list_field_datepicker_format'] ) ? $field['itsg_list_field_datepicker_format'] : esc_html( $default_format ); | |
$datepicker_class = 'itsg_list_field_datepicker_icon_none' == rgar( $field, 'itsg_list_field_datepicker_icon' ) ? 'datepicker_no_icon datepicker' :'datepicker'; | |
$new_input = str_replace( "<input ","<input class='{$datepicker_class} {$date_format} ' ", $input ); | |
return $new_input; | |
} | |
return $input; | |
} | |
} | |
} // change_column_content | |
/* | |
* Enqueue JavaScript to footer | |
*/ | |
function datepicker_js( $form, $is_ajax ) { | |
if ( self::list_has_datepicker_field( $form ) ) { | |
wp_enqueue_script( 'gform_datepicker_init' ); | |
wp_enqueue_style( 'itsg_listdatepicker_css', plugins_url( '/css/listdatepicker-css.css', __FILE__ ) ); | |
wp_register_script( 'itsg_listdatepicker_js', plugins_url( '/js/listdatepicker-script.js', __FILE__ ), array( 'jquery' ) ); | |
$settings_array = array( | |
'default_datepicker_date' => esc_js( apply_filters( 'itsg_default_datepicker_date', '' ) ), | |
'gf_base_url' => esc_js( GFCommon::get_base_url() ) | |
); | |
wp_localize_script( 'itsg_listdatepicker_js', 'datepicker_settings', $settings_array ); | |
// Enqueued script with localized data. | |
wp_enqueue_script( 'itsg_listdatepicker_js' ); | |
$use_gf_css = apply_filters( 'itsg_listdatepicker_usegfcss', true ); | |
// load Gravity Forms datepicker CSS styles | |
if ( $use_gf_css ) { | |
if ( ! wp_style_is( 'gforms_css' ) ) { | |
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min'; | |
wp_enqueue_style( 'gforms_datepicker_css', GFCommon::get_base_url() . "/css/datepicker{$min}.css", null, GFCommon::$version ); | |
} | |
} | |
} | |
} // END datepicker_js | |
/* | |
* JavaScript used by form editor - Functions taken from Gravity Forms source and extended to handle the 'Date field' option | |
*/ | |
function editor_js() { | |
?> | |
<script> | |
// ADD drop down options to list field in form editor - hooks into existing GetFieldChoices function. | |
(function (w){ | |
var GetFieldChoicesOld = w.GetFieldChoices; | |
w.GetFieldChoices = function (){ | |
str = GetFieldChoicesOld.apply(this, [field]); | |
if ( typeof field.choices == 'undefined' ) { | |
return ""; | |
} | |
for( var i = 0; i < field.choices.length; i++ ) { | |
var inputType = GetInputType( field ); | |
var isDatePicker = ( typeof field.choices[i].isDatePicker !== 'undefined' && field.choices[i].isDatePicker ) ? 'checked' : ''; | |
var value = ( typeof field.choices[i].value !== 'undefined' ) ? String( field.choices[i].value ) : field.choices[i].text; | |
if ( inputType == 'list' ) { | |
if (i == 0 ){ | |
str += "<p><strong><?php _e( 'Date Picker fields', 'gravity-forms-list-field-date-picker' ); ?></strong><br><?php _e( "Place a tick next to the column name to make it a date picker field. Select the date format from the 'Date Format' options.", 'gravity-forms-list-field-date-picker' ); ?></p>"; | |
} | |
str += "<div>"; | |
str += "<input type='checkbox' name='choice_datepicker' id='" + inputType + "_choice_datepicker_" + i + "' " + isDatePicker + " onclick=\"SetFieldChoiceDP( '" + inputType + "', " + i + ");itsg_gf_list_datepicker_function();\" /> "; | |
str += " <label class='inline' for='"+ inputType + "_choice_datepicker_" + i + "'>"+value+" - <?php _e( 'Make Date Picker', 'gravity-forms-list-field-date-picker' ); ?></label>"; | |
str += "<div style='display:none; background: rgb(244, 244, 244) none repeat scroll 0px 0px; padding: 10px; border-bottom: 1px solid grey; margin: 10px 0;' class='itsg_datepicker'>"; | |
str += "<label for='" + inputType + "_choice_datepickerformat_" + i + "'>"; | |
str += "<?php _e( 'Date Format', 'gravity-forms-list-field-date-picker' ); ?></label>"; | |
str += "<select class='choice_datepickerformat' id='" + inputType + "_choice_datepickerformat_" + i + "' onchange=\"SetFieldChoiceDP( '" + inputType + "', " + i + ");\" style='margin-bottom: 10px;' >"; | |
str += "<option value='mdy'>mm/dd/yyyy</option>"; | |
str += "<option value='dmy'>dd/mm/yyyy</option>"; | |
str += "<option value='dmy_dash'>dd-mm-yyyy</option>"; | |
str += "<option value='dmy_dot'>dd.mm.yyyy</option>"; | |
str += "<option value='ymd_slash'>yyyy/mm/dd</option>"; | |
str += "<option value='ymd_dash'>yyyy-mm-dd</option>"; | |
str += "<option value='ymd_dot'>yyyy.mm.dd</option>"; | |
str += "</select>"; | |
str += "<div class='datepickericon'>"; | |
str += "<input style='margin: 8px;' id='" + inputType + "_choice_datepickericonnone_" + i + "' type='radio' onclick=\"SetFieldChoiceDP( '" + inputType + "', " + i + ");\" value='itsg_list_field_datepicker_icon_none' name='" + inputType + "_field_datepicker_icon_" + i + "'>"; | |
str += "<label class='inline' for='" + inputType + "_choice_datepickericonnone_" + i + "'> <?php _e( 'No Icon', 'gravity-forms-list-field-date-picker' ); ?> </label>"; | |
str += "<input style='margin: 8px;' id='" + inputType + "_choice_datepickericoncalendar_" + i + "' type='radio' onclick=\"SetFieldChoiceDP( '" + inputType + "', " + i + ");\" value='itsg_list_field_datepicker_icon_calendar' name='" + inputType + "_field_datepicker_icon_" + i + "'>"; | |
str += "<label class='inline' for='" + inputType + "_choice_datepickericoncalendar_" + i + "'> <?php _e( 'Calendar Icon', 'gravity-forms-list-field-date-picker' ); ?> </label>"; | |
str += "</div>"; | |
str += "</div>"; | |
str += "</div>"; | |
} | |
} | |
jQuery( '.ginput_container_list img#gfield_input_datepicker_icon' ).css( 'display', 'inline'); // work around to ensure icon is displayed - GF is in the habit of hiding the icon | |
return str; | |
} | |
})(window || {}); | |
function SetFieldChoiceDP( inputType, index ) { | |
var element = jQuery("#" + inputType + "_choice_selected_" + index); | |
if ( 'list' == inputType ) { | |
isDatePicker = jQuery( '#' + inputType + '_choice_datepicker_' + index ).is( ':checked' ); | |
isDatePickerFormat = jQuery( '#' + inputType + '_choice_datepickerformat_' + index ).val(); | |
isDatePickerIcon = jQuery( 'input:radio[name=' + inputType + '_field_datepicker_icon_' + index + ']:checked' ).val(); | |
field = GetSelectedField(); | |
field.choices[index].isDatePicker = isDatePicker; | |
field.choices[index].isDatePickerFormat = isDatePickerFormat; | |
field.choices[index].isDatePickerIcon = isDatePickerIcon; | |
} | |
LoadBulkChoices( field ); | |
UpdateFieldChoices( GetInputType( field ) ); | |
for( var i=0; i < field.choices.length; i++ ) { | |
isDatePicker = jQuery( '#' + inputType + '_choice_datepicker_' + i ).is( ':checked' ); | |
isDatePickerIcon = jQuery( 'input:radio[name=' + inputType + '_field_datepicker_icon_' + i + ']:checked' ).val(); | |
column = i + 1; | |
if ( true == isDatePicker && 'itsg_list_field_datepicker_icon_none' != isDatePickerIcon ) { | |
calendar_input = '<input type="text" disabled="disabled" style="width:80%"><img id="gfield_input_datepicker_icon" src="<?php echo GFCommon::get_base_url() ?>/images/calendar.png" style="display:inline">'; | |
jQuery( 'li#field_' + field.id + ' table.gfield_list_container tbody tr td:nth-child(' + column + ')' ).html( calendar_input ); | |
} | |
} | |
jQuery( '.ginput_container_list img#gfield_input_datepicker_icon' ).css( 'display', 'inline'); // work around to ensure icon is displayed - GF is in the habit of hiding the icon | |
} | |
function itsg_gf_list_datepicker_function() { | |
// handles displaying the date format option for multi column lists | |
jQuery( '#field_columns input[name="choice_datepicker"]' ).each( function() { | |
if ( jQuery(this).is( ':checked' ) ) { | |
jQuery(this).parent( 'div' ).find( '.itsg_datepicker' ).show(); | |
} else { | |
jQuery(this).parent( 'div' ).find( '.itsg_datepicker' ).hide(); | |
} | |
}); | |
// handles displaying the date format option for single column lists | |
jQuery( '.ui-tabs-panel input#itsg_list_field_datepicker' ).each( function() { | |
if (jQuery( this ).is( ':checked' ) ) { | |
jQuery( this ).parent( 'li' ).find( '#itsg_list_field_datepicker_options_div' ).show(); | |
} else { | |
jQuery( this ).parent( 'li' ).find( '#itsg_list_field_datepicker_options_div' ).hide(); | |
} | |
}); | |
// only display this option if a single column list field | |
jQuery( '#field_settings input[id=field_columns_enabled]:visible' ).each(function() { | |
if (jQuery( this ).is( ':checked' ) ) { | |
jQuery( this ).closest( '#field_settings' ).find( '.itsg_list_field_datepicker' ).hide(); | |
} else { | |
jQuery( this ).closest( '#field_settings' ).find( '.itsg_list_field_datepicker' ).show(); | |
} | |
}); | |
// set setting values | |
jQuery( '#field_settings input[id=field_columns_enabled]:visible' ).each(function() { | |
if ( jQuery(this).is( ':checked' ) ) { | |
// for multi-column list field | |
jQuery(this).closest( '#field_settings' ).find( '.itsg_list_field_datepicker' ).hide(); | |
jQuery( '#field_columns:visible select.choice_datepickerformat' ).each( function( index ){ | |
<?php $default_format = apply_filters( 'itsg_list_field_datepicker_default_format', 'mdy' ); ?> | |
var format_value = ( typeof field.choices[index].isDatePickerFormat !== 'undefined' ) ? field.choices[index].isDatePickerFormat : '<?php echo esc_js( $default_format ) ?>'; | |
jQuery(this).val( format_value ); | |
var icon_value = ( typeof field.choices[index].isDatePickerIcon !== 'undefined' ) ? field.choices[index].isDatePickerIcon : 'itsg_list_field_datepicker_icon_calendar'; | |
jQuery( 'input:radio[name="list_field_datepicker_icon_' + index + '"]' ).filter('[value=' + icon_value + ']' ).prop( 'checked', true ); | |
}); | |
} else { | |
// for single-column list field | |
jQuery(this).closest( '#field_settings' ).find( '.itsg_list_field_datepicker' ).show(); | |
} | |
}); | |
} | |
// trigger for when field is opened | |
jQuery( document ).on( 'click', 'ul.gform_fields', function() { | |
itsg_gf_list_datepicker_function(); | |
}); | |
// trigger when 'Enable multiple columns' is ticked | |
jQuery( document ).on( 'change', '#field_settings input[id=field_columns_enabled], .ui-tabs-panel input#itsg_list_field_datepicker', function(){ | |
itsg_gf_list_datepicker_function(); | |
}); | |
// trigger for when column titles are updated | |
jQuery( document ).on( 'change', '#gfield_settings_columns_container #field_columns li', function() { | |
InsertFieldChoice(0); | |
DeleteFieldChoice(0); | |
itsg_gf_list_datepicker_function(); | |
}); | |
// handle 'Enable datepicker' option in the Gravity forms editor | |
jQuery( document ).ready( function($) { | |
//adding setting to fields of type "list" | |
fieldSettings['list'] += ', .itsg_list_field_datepicker'; | |
//set field values when field loads | |
jQuery( document ).bind( 'gform_load_field_settings', function( event, field, form ){ | |
jQuery( '#itsg_list_field_datepicker' ).prop( 'checked', field['itsg_list_field_datepicker'] ); | |
jQuery( '#itsg_list_field_datepicker_format' ).val( field['itsg_list_field_datepicker_format'] ); | |
var icon_value = ( typeof field['itsg_list_field_datepicker_icon'] !== 'undefined' ) ? field['itsg_list_field_datepicker_icon'] : 'itsg_list_field_datepicker_icon_calendar'; | |
if ( '' != icon_value ) { | |
jQuery( 'input:radio[name="itsg_list_field_datepicker_icon"]' ).filter('[value=' + icon_value + ']' ).prop('checked', true); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} // END editor_js | |
/* | |
* Adds custom sortable setting for field | |
*/ | |
function field_datepicker_settings( $position, $form_id ) { | |
// Create settings on position 50 (top position) | |
if ( 50 == $position ) { | |
?> | |
<li class="itsg_list_field_datepicker field_setting"> | |
<input type="checkbox" id="itsg_list_field_datepicker" onclick="SetFieldProperty( 'itsg_list_field_datepicker', this.checked);"> | |
<label class="inline" for="itsg_list_field_datepicker"> | |
<?php _e( 'Enable datepicker', 'gravity-forms-list-field-date-picker' ); ?> | |
<?php gform_tooltip( 'itsg_list_field_datepicker' ); ?> | |
</label> | |
<div id="itsg_list_field_datepicker_options_div" style="background: rgb(244, 244, 244) none repeat scroll 0px 0px; padding: 10px; border-bottom: 1px solid grey; margin-top: 10px;" > | |
<p><strong><?php esc_attr_e( 'Configure Date Picker Field', 'gravity-forms-list-field-date-picker' ); ?></strong> | |
<br> | |
<?php esc_attr_e( "Place a tick next to the column name to make it a date picker field. Select the date format from the 'Date Format' options.", 'gravity-forms-list-field-date-picker' ); ?> | |
</p> | |
<label for="itsg_list_field_datepicker_format" ><? _e( 'Date Format', 'gravity-forms-list-field-date-picker' ) ?></label> | |
<select onchange="SetFieldProperty( 'itsg_list_field_datepicker_format', this.value);" id="itsg_list_field_datepicker_format" class="itsg_list_field_datepicker_format" style="margin-bottom: 10px;"> | |
<option value="mdy">mm/dd/yyyy</option> | |
<option value="dmy">dd/mm/yyyy</option> | |
<option value="dmy_dash">dd-mm-yyyy</option> | |
<option value="dmy_dot">dd.mm.yyyy</option> | |
<option value="ymd_slash">yyyy/mm/dd</option> | |
<option value="ymd_dash">yyyy-mm-dd</option> | |
<option value="ymd_dot">yyyy.mm.dd</option> | |
</select> | |
<br> | |
<input style='margin: 8px 0;' id="itsg_list_field_datepicker_icon_none" type="radio" onclick="SetFieldProperty( 'itsg_list_field_datepicker_icon', this.value);" value="itsg_list_field_datepicker_icon_none" name="itsg_list_field_datepicker_icon"> | |
<label class="inline" for="itsg_list_field_datepicker_icon_none"> <?php _e( 'No Icon', 'gravity-forms-list-field-date-picker' ); ?> </label> | |
<input id="itsg_list_field_datepicker_icon_calendar" type="radio" onclick="SetFieldProperty( 'itsg_list_field_datepicker_icon', this.value);" value="itsg_list_field_datepicker_icon_calendar" name="itsg_list_field_datepicker_icon"> | |
<label class="inline" for="itsg_list_field_datepicker_icon_calendar"> <?php _e( 'Calendar Icon', 'gravity-forms-list-field-date-picker' ); ?> </label> | |
</div> | |
</li> | |
<?php | |
} | |
} // END field_datepicker_settings | |
/* | |
* Tooltip for for datepicker option | |
*/ | |
function field_datepicker_tooltip( $tooltips ) { | |
$tooltips['itsg_list_field_datepicker'] = "<h6>". __( 'Datepicker', 'gravity-forms-list-field-date-picker' )."</h6>". __( 'Makes list field column a datepicker. Only applies to single column list fields.', 'gravity-forms-list-field-date-picker' ); | |
return $tooltips; | |
} // END field_datepicker_tooltip | |
/* | |
* Warning message if Gravity Forms is installed and enabled | |
*/ | |
public static function admin_warnings() { | |
if ( !self::is_gravityforms_installed() ) { | |
printf( | |
'<div class="error"><h3>%s</h3><p>%s</p><p>%s</p></div>', | |
__( 'Warning', 'gravity-forms-list-field-date-picker' ), | |
sprintf ( __( 'The plugin %s requires Gravity Forms to be installed.', 'gravity-forms-list-field-date-picker' ), '<strong>'.self::$name.'</strong>' ), | |
sprintf ( esc_html__( 'Please %sdownload the latest version of Gravity Forms%s and try again.', 'gravity-forms-list-field-date-picker' ), '<a href="https://www.e-junkie.com/ecom/gb.php?cl=54585&c=ib&aff=299380" target="_blank">', '</a>' ) | |
); | |
} | |
} // END admin_warnings | |
/* | |
* Check if GF is installed | |
*/ | |
private static function is_gravityforms_installed() { | |
if ( !function_exists( 'is_plugin_active' ) || !function_exists( 'is_plugin_active_for_network' ) ) { | |
require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | |
} | |
if ( is_multisite() ) { | |
return ( is_plugin_active_for_network( 'gravityforms/gravityforms.php' ) || is_plugin_active( 'gravityforms/gravityforms.php' ) ); | |
} else { | |
return is_plugin_active( 'gravityforms/gravityforms.php' ); | |
} | |
} // END is_gravityforms_installed | |
/* | |
* Check if list field has a date picker in the current form | |
*/ | |
public static function list_has_datepicker_field( $form ) { | |
if ( is_array( $form['fields'] ) ) { | |
foreach ( $form['fields'] as $field ) { | |
if ( 'list' == $field->get_input_type() ) { | |
$has_columns = is_array( $field->choices ); | |
if ( $has_columns ) { | |
foreach( $field['choices'] as $choice ) { | |
if ( true == rgar( $choice, 'isDatePicker' ) ) { | |
return true; | |
} | |
} | |
} else if ( true == rgar( $field, 'itsg_list_field_datepicker' ) ) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} // END list_has_datepicker_field | |
} | |
$ITSG_GF_List_Field_Date_Picker = new ITSG_GF_List_Field_Date_Picker(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment