Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 52 additions & 67 deletions gravity-forms/gw-populate-date.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
*
* Provides the ability to populate a Date field with a modified date based on the current date or a user-submitted date.
*
* @version 2.8
* @version 2.6
* @author David Smith <[email protected]>
* @license GPL-2.0+
* @link http://gravitywiz.com/populate-dates-gravity-form-fields/
*/
class GW_Populate_Date {

protected static $is_script_output = false;
protected $_args;

private $_args = array();
private $_field_values = array();
protected static $is_script_output = false;

public function __construct( $args = array() ) {

Expand All @@ -28,7 +27,6 @@ public function __construct( $args = array() ) {
'min_date' => false,
'enable_i18n' => false,
'override_on_submission' => false,
'utc_offset' => get_option( 'gmt_offset' ), // Used only for time calculations on the current date.
) );

$this->_field_values = array();
Expand All @@ -49,8 +47,7 @@ public function init() {
return;
}

// Always load the script if a modifier is provided, even if no source_field_id is set
if ( $this->_args['modifier'] || $this->_args['source_field_id'] ) {
if ( $this->_args['source_field_id'] ) {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ) );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );
add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) );
Expand Down Expand Up @@ -245,9 +242,7 @@ public function get_modified_date( $field, $timestamp = false ) {
$hour = 12;
}
}
// Ensure the time value is retained as a String.
// If saved in array format, it will not reload the value after conditional viewing/hiding.
$date = "{$hour}:{$minute} {$ampm}";
$date = array( $hour, $minute, $ampm );
} elseif ( $this->_args['enable_i18n'] ) {
$date = strftime( $format, $timestamp );
} else {
Expand Down Expand Up @@ -287,6 +282,16 @@ public static function output_script() {
<script type="text/javascript">

( function( $ ) {
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== 'undefined'
? args[number]
: match;
});
};
}

window.GWPopulateDate = function( args ) {

Expand All @@ -301,22 +306,34 @@ public static function output_script() {

self.init = function() {

if( self.sourceFieldId ) {
self.$sourceInputs = GWDates.getFieldInputs( self.sourceFieldId, self.formId );
self.$sourceInputs.change( function() {
self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
} );
self.$sourceInputs = GWDates.getFieldInputs( self.sourceFieldId, self.formId );

// Listen for GPPA's new `gppa_updated_batch_fields`
$( document ).on( 'gppa_updated_batch_fields', function ( e, formId, updatedFieldIDs ) {
for ( var i = 0, max = updatedFieldIDs.length; i < max; i ++ ) {
if ( self.sourceFieldId === parseInt( updatedFieldIDs[i] ) ) {
self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
}
}
} );
self.$sourceInputs.change( function() {
self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
} );

// Listen for any dynamic content loaded on modifier field (if existing) to refresh values on Target.
if ( self.modifier && self instanceof GWPopulateDate ) {
var modifier = self.modifier.inputId;

if ( modifier ) {
var modifierParent = '#field_' + self.formId + '_' + modifier;
var modifierTarget = '#input_' + self.formId + '_' + modifier;
$( modifierParent ).on( 'change', modifierTarget, function() {
self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
} );
}
}

// Listen for GPPA's new `gppa_updated_batch_fields`
$( document ).on( 'gppa_updated_batch_fields', function ( e, formId, updatedFieldIDs ) {
for ( var i = 0, max = updatedFieldIDs.length; i < max; i ++ ) {
if ( self.sourceFieldId === parseInt( updatedFieldIDs[i] ) ) {
self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
}
}
} );

if( typeof self.modifier == 'object' ) {
self.$modifierInputs = self.getInputs( self.modifier.inputId );
self.$modifierInputs.change( function() {
Expand All @@ -336,7 +353,7 @@ public static function output_script() {

self.populateDate = function( sourceFieldId, targetFieldId, modifier, format ) {

var timestamp = sourceFieldId ? GWDates.getFieldTimestamp( sourceFieldId, self.formId, undefined, self.utcOffset ) : new Date().getTime();
var timestamp = GWDates.getFieldTimestamp( sourceFieldId, self.formId );
if( timestamp === 0 ) {
return;
}
Expand All @@ -358,7 +375,7 @@ public static function output_script() {
case 'field':
var inputId = self.modifier.inputId,
value = self.getFieldValue( inputId ),
modifier = value !== '' ? self.modifier.modifier.gformFormat( value ) : false;
modifier = value !== '' ? self.modifier.modifier.format( value ) : false;
break;
}

Expand All @@ -370,11 +387,6 @@ public static function output_script() {
var $input = self.getInputs( inputId ),
value = self.getCleanNumber( $input.val(), gformExtractFieldId( inputId ), self.formId );

// Cannot retrieve value from `gfield_radio` directly on `$input`.
if ( ! $input.val() && $input.hasClass( 'gfield_radio' ) ) {
value = $input.find('input[type="radio"]:checked').val();
}

value = gform.applyFilters( 'gwpd_get_field_value', value, $input, inputId );

if ( ! value || isNaN( value ) ) {
Expand Down Expand Up @@ -408,7 +420,7 @@ public static function output_script() {
var fieldId = gformExtractFieldId( inputId ),
inputIndex = gformExtractInputIndex( inputId ),
id = inputIndex !== fieldId ? '#input_{0}_{1}' : '#input_{0}_{1}_{2}',
$input = $( id.gformFormat( self.formId, fieldId, inputIndex ) );
$input = $( id.format( self.formId, fieldId, inputIndex ) );

return $input;
};
Expand Down Expand Up @@ -1548,7 +1560,7 @@ function processTzCorrection(tzOffset, oldValue) {

window.GWDates = {

getFieldTimestamp: function( dateTimeFieldId, formId, $inputs, utcOffset ) {
getFieldTimestamp: function( dateTimeFieldId, formId, $inputs ) {

var field;

Expand Down Expand Up @@ -1609,45 +1621,23 @@ function processTzCorrection(tzOffset, oldValue) {
break;
}

/*
* If the date matches the current date, attach the time to it as well so time calculations
* work as expected rather than off of midnight.
*/
var now = new Date();

// Convert now to use the UTC offset from the server.
now = new Date( now.getTime() + ( now.getTimezoneOffset() * 60000 ) + ( utcOffset * 60 * 60000 ) );

if (datetime.getDate() == now.getDate() && datetime.getMonth() == now.getMonth() && datetime.getFullYear() == now.getFullYear()) {
datetime.setHours(now.getHours());
datetime.setMinutes(now.getMinutes());
datetime.setSeconds(now.getSeconds());
}

var timestamp = datetime === false ? false : datetime.getTime();

break;

case 'time':

var hour = parseInt( $inputs.eq( 0 ).val() ),
min = parseInt( $inputs.eq( 1 ).val() ),
var hour = $inputs.eq( 0 ).val(),
min = $inputs.eq( 1 ).val(),
ampm = $inputs.eq( 2 ).val(),
missingData = isNaN( hour ) || isNaN( min ),
missingData = ! hour || ! min,
datetime = missingData ? false : new Date();

if ( $inputs.eq( 2 ).length ) {
hours = hour;

if (ampm.toLowerCase() === 'am') {
hours += hour === 12 ? -12 : 0;
} else {
hours += hour !== 12 ? 12 : 0;
}

datetime.setHours( hours );
if ( $inputs.eq( 2 ).length ) {
datetime.setHours( parseInt( hour ) + ( ampm.toLowerCase() === 'pm' ? 12 : 0 ) );
} else {
datetime.setHours( hour );
datetime.setHours( parseInt( hour ) );
}

datetime.setMinutes( min );
Expand Down Expand Up @@ -1742,7 +1732,7 @@ classes = $field.attr( 'class' ),
case 'time':
var hours = isNaN( date.getHours() ) ? '' : date.getHours(),
minutes = isNaN( date.getMinutes() ) ? '' : date.getMinutes(),
hasAMPM = $inputs.filter( 'select' ).length === 1,
hasAMPM = $inputs.length === 3,
isPM = false;

if ( hasAMPM ) {
Expand All @@ -1751,9 +1741,6 @@ classes = $field.attr( 'class' ),
} else if ( hours > 12 ) {
hours -= 12;
isPM = true;
} else if ( hours == 12 ) {
// for 12 PM, the PM display should update
isPM = true;
}
}

Expand Down Expand Up @@ -1894,13 +1881,12 @@ public function add_init_script( $form ) {
'targetFieldId' => $this->_args['target_field_id'],
'sourceFieldId' => $this->_args['source_field_id'],
'modifier' => $this->_args['modifier'],
'utcOffset' => $this->_args['utc_offset'],
// Keep the format in the `date()` format for JS.
'format' => $this->get_format( false ),
);

$script = 'new GWPopulateDate( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array_filter( array( 'gw_populate_date', $this->_args['form_id'], $this->_args['source_field_id'], $this->_args['target_field_id'], rgar( $this->_args['modifier'], 'inputId' ) ) ) );
$slug = implode( '_', array( 'gw_populate_date', $this->_args['form_id'], $this->_args['source_field_id'], $this->_args['target_field_id'] ) );

GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );

Expand Down Expand Up @@ -2068,4 +2054,3 @@ function ( $s ) {
'target_field_id' => 5,
'modifier' => '+7 days',
) );

Loading