44 *
55 * Provides the ability to populate a Date field with a modified date based on the current date or a user-submitted date.
66 *
7- * @version 2.8
7+ * @version 2.6
88 * @author David Smith <[email protected] > 99 * @license GPL-2.0+
1010 * @link http://gravitywiz.com/populate-dates-gravity-form-fields/
1111 */
1212class GW_Populate_Date {
1313
14- protected static $ is_script_output = false ;
14+ protected $ _args ;
1515
16- private $ _args = array ();
17- private $ _field_values = array ();
16+ protected static $ is_script_output = false ;
1817
1918 public function __construct ( $ args = array () ) {
2019
@@ -28,7 +27,6 @@ public function __construct( $args = array() ) {
2827 'min_date ' => false ,
2928 'enable_i18n ' => false ,
3029 'override_on_submission ' => false ,
31- 'utc_offset ' => get_option ( 'gmt_offset ' ), // Used only for time calculations on the current date.
3230 ) );
3331
3432 $ this ->_field_values = array ();
@@ -49,8 +47,7 @@ public function init() {
4947 return ;
5048 }
5149
52- // Always load the script if a modifier is provided, even if no source_field_id is set
53- if ( $ this ->_args ['modifier ' ] || $ this ->_args ['source_field_id ' ] ) {
50+ if ( $ this ->_args ['source_field_id ' ] ) {
5451 add_filter ( 'gform_pre_render ' , array ( $ this , 'load_form_script ' ) );
5552 add_filter ( 'gform_register_init_scripts ' , array ( $ this , 'add_init_script ' ) );
5653 add_filter ( 'gform_enqueue_scripts ' , array ( $ this , 'enqueue_form_scripts ' ) );
@@ -245,9 +242,7 @@ public function get_modified_date( $field, $timestamp = false ) {
245242 $ hour = 12 ;
246243 }
247244 }
248- // Ensure the time value is retained as a String.
249- // If saved in array format, it will not reload the value after conditional viewing/hiding.
250- $ date = "{$ hour }: {$ minute } {$ ampm }" ;
245+ $ date = array ( $ hour , $ minute , $ ampm );
251246 } elseif ( $ this ->_args ['enable_i18n ' ] ) {
252247 $ date = strftime ( $ format , $ timestamp );
253248 } else {
@@ -287,11 +282,10 @@ public static function output_script() {
287282 <script type="text/javascript">
288283
289284 ( function( $ ) {
290-
291- if ( ! String.prototype.format ) {
285+ if (!String.prototype.format) {
292286 String.prototype.format = function() {
293287 var args = arguments;
294- return this.replace(/{(\d+)}/g, function( match, number ) {
288+ return this.replace(/{(\d+)}/g, function(match, number) {
295289 return typeof args[number] !== 'undefined'
296290 ? args[number]
297291 : match;
@@ -312,12 +306,11 @@ public static function output_script() {
312306
313307 self.init = function() {
314308
315- if( self.sourceFieldId ) {
316- self.$sourceInputs = GWDates.getFieldInputs( self.sourceFieldId, self.formId );
317- self.$sourceInputs.change( function() {
318- self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
319- } );
309+ self.$sourceInputs = GWDates.getFieldInputs( self.sourceFieldId, self.formId );
320310
311+ self.$sourceInputs.change( function() {
312+ self.populateDate( self.sourceFieldId, self.targetFieldId, self.getModifier(), self.format );
313+ } );
321314
322315 // Listen for any dynamic content loaded on modifier field (if existing) to refresh values on Target.
323316 if ( self.modifier && self instanceof GWPopulateDate ) {
@@ -360,7 +353,7 @@ public static function output_script() {
360353
361354 self.populateDate = function( sourceFieldId, targetFieldId, modifier, format ) {
362355
363- var timestamp = sourceFieldId ? GWDates.getFieldTimestamp( sourceFieldId, self.formId, undefined, self.utcOffset ) : new Date().getTime( );
356+ var timestamp = GWDates.getFieldTimestamp( sourceFieldId, self.formId );
364357 if( timestamp === 0 ) {
365358 return;
366359 }
@@ -382,7 +375,7 @@ public static function output_script() {
382375 case 'field':
383376 var inputId = self.modifier.inputId,
384377 value = self.getFieldValue( inputId ),
385- modifier = value !== '' ? self.modifier.modifier.gformFormat ( value ) : false;
378+ modifier = value !== '' ? self.modifier.modifier.format ( value ) : false;
386379 break;
387380 }
388381
@@ -394,11 +387,6 @@ public static function output_script() {
394387 var $input = self.getInputs( inputId ),
395388 value = self.getCleanNumber( $input.val(), gformExtractFieldId( inputId ), self.formId );
396389
397- // Cannot retrieve value from `gfield_radio` directly on `$input`.
398- if ( ! $input.val() && $input.hasClass( 'gfield_radio' ) ) {
399- value = $input.find('input[type="radio"]:checked').val();
400- }
401-
402390 value = gform.applyFilters( 'gwpd_get_field_value', value, $input, inputId );
403391
404392 if ( ! value || isNaN( value ) ) {
@@ -432,7 +420,7 @@ public static function output_script() {
432420 var fieldId = gformExtractFieldId( inputId ),
433421 inputIndex = gformExtractInputIndex( inputId ),
434422 id = inputIndex !== fieldId ? '#input_{0}_{1}' : '#input_{0}_{1}_{2}',
435- $input = $( id.gformFormat ( self.formId, fieldId, inputIndex ) );
423+ $input = $( id.format ( self.formId, fieldId, inputIndex ) );
436424
437425 return $input;
438426 };
@@ -1572,7 +1560,7 @@ function processTzCorrection(tzOffset, oldValue) {
15721560
15731561 window.GWDates = {
15741562
1575- getFieldTimestamp: function( dateTimeFieldId, formId, $inputs, utcOffset ) {
1563+ getFieldTimestamp: function( dateTimeFieldId, formId, $inputs ) {
15761564
15771565 var field;
15781566
@@ -1633,45 +1621,23 @@ function processTzCorrection(tzOffset, oldValue) {
16331621 break;
16341622 }
16351623
1636- /*
1637- * If the date matches the current date, attach the time to it as well so time calculations
1638- * work as expected rather than off of midnight.
1639- */
1640- var now = new Date();
1641-
1642- // Convert now to use the UTC offset from the server.
1643- now = new Date( now.getTime() + ( now.getTimezoneOffset() * 60000 ) + ( utcOffset * 60 * 60000 ) );
1644-
1645- if (datetime.getDate() == now.getDate() && datetime.getMonth() == now.getMonth() && datetime.getFullYear() == now.getFullYear()) {
1646- datetime.setHours(now.getHours());
1647- datetime.setMinutes(now.getMinutes());
1648- datetime.setSeconds(now.getSeconds());
1649- }
1650-
16511624 var timestamp = datetime === false ? false : datetime.getTime();
16521625
16531626 break;
16541627
16551628 case 'time':
16561629
1657- var hour = parseInt( $inputs.eq( 0 ).val() ),
1658- min = parseInt( $inputs.eq( 1 ).val() ),
1630+ var hour = $inputs.eq( 0 ).val(),
1631+ min = $inputs.eq( 1 ).val(),
16591632 ampm = $inputs.eq( 2 ).val(),
1660- missingData = isNaN( hour ) || isNaN( min ) ,
1633+ missingData = ! hour || ! min,
16611634 datetime = missingData ? false : new Date();
16621635
1663- if ( $inputs.eq( 2 ).length ) {
1664- hours = hour;
16651636
1666- if (ampm.toLowerCase() === 'am') {
1667- hours += hour === 12 ? -12 : 0;
1668- } else {
1669- hours += hour !== 12 ? 12 : 0;
1670- }
1671-
1672- datetime.setHours( hours );
1637+ if ( $inputs.eq( 2 ).length ) {
1638+ datetime.setHours( parseInt( hour ) + ( ampm.toLowerCase() === 'pm' ? 12 : 0 ) );
16731639 } else {
1674- datetime.setHours( hour );
1640+ datetime.setHours( parseInt( hour ) );
16751641 }
16761642
16771643 datetime.setMinutes( min );
@@ -1766,7 +1732,7 @@ classes = $field.attr( 'class' ),
17661732 case 'time':
17671733 var hours = isNaN( date.getHours() ) ? '' : date.getHours(),
17681734 minutes = isNaN( date.getMinutes() ) ? '' : date.getMinutes(),
1769- hasAMPM = $inputs.filter( 'select' ). length === 1 ,
1735+ hasAMPM = $inputs.length === 3 ,
17701736 isPM = false;
17711737
17721738 if ( hasAMPM ) {
@@ -1775,9 +1741,6 @@ classes = $field.attr( 'class' ),
17751741 } else if ( hours > 12 ) {
17761742 hours -= 12;
17771743 isPM = true;
1778- } else if ( hours == 12 ) {
1779- // for 12 PM, the PM display should update
1780- isPM = true;
17811744 }
17821745 }
17831746
@@ -1918,13 +1881,12 @@ public function add_init_script( $form ) {
19181881 'targetFieldId ' => $ this ->_args ['target_field_id ' ],
19191882 'sourceFieldId ' => $ this ->_args ['source_field_id ' ],
19201883 'modifier ' => $ this ->_args ['modifier ' ],
1921- 'utcOffset ' => $ this ->_args ['utc_offset ' ],
19221884 // Keep the format in the `date()` format for JS.
19231885 'format ' => $ this ->get_format ( false ),
19241886 );
19251887
19261888 $ script = 'new GWPopulateDate( ' . json_encode ( $ args ) . ' ); ' ;
1927- $ 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 ' ) ) ) );
1889+ $ slug = implode ( '_ ' , array ( 'gw_populate_date ' , $ this ->_args ['form_id ' ], $ this ->_args ['source_field_id ' ], $ this ->_args ['target_field_id ' ] ) );
19281890
19291891 GFFormDisplay::add_init_script ( $ this ->_args ['form_id ' ], $ slug , GFFormDisplay::ON_PAGE_RENDER , $ script );
19301892
@@ -2092,4 +2054,3 @@ function ( $s ) {
20922054 'target_field_id ' => 5 ,
20932055 'modifier ' => '+7 days ' ,
20942056) );
2095-
0 commit comments