Skip to content

Commit e808de3

Browse files
scytspivurno
andauthored
gw-day-count.php: Migrated from GitHub Gists. (#564)
* `gw-day-count.php`: Migrated from GitHub Gists. * `gw-day-count.php`: Update plugin header. Fix PHPCS errors. * Formatting: Ignore phpcs extract rule. * gw-day-count.php: Updated header to add deprecation notice. Co-authored-by: David Smith <[email protected]>
1 parent 27621f2 commit e808de3

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

gravity-forms/gw-day-count.php

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
/**
3+
* --- STOP! ---
4+
* Get the latest version:
5+
* https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/
6+
* -------------
7+
*
8+
* Gravity Wiz // Gravity Forms // Calculate Number of Days Between Two Gravity Form Date Fields
9+
* http://gravitywiz.com/calculate-number-of-days-between-two-dates/
10+
*
11+
* Allows you to calculate the number of days between two Gravity Form date fields and populate that number into a
12+
* field on your Gravity Form.
13+
*/
14+
class GWDayCount {
15+
16+
private static $script_output;
17+
18+
function __construct( $args ) {
19+
20+
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
21+
extract( wp_parse_args( $args, array(
22+
'form_id' => false,
23+
'start_field_id' => false,
24+
'end_field_id' => false,
25+
'count_field_id' => false,
26+
'include_end_date' => true,
27+
) ) );
28+
29+
$this->form_id = $form_id;
30+
$this->start_field_id = $start_field_id;
31+
$this->end_field_id = $end_field_id;
32+
$this->count_field_id = $count_field_id;
33+
$this->count_adjust = $include_end_date ? 1 : 0;
34+
35+
add_filter( "gform_pre_render_{$form_id}", array( &$this, 'load_form_script' ) );
36+
add_action( "gform_pre_submission_{$form_id}", array( &$this, 'override_submitted_value' ) );
37+
38+
}
39+
40+
function load_form_script( $form ) {
41+
42+
// workaround to make this work for < 1.7
43+
$this->form = $form;
44+
add_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
45+
46+
if ( self::$script_output ) {
47+
return $form;
48+
}
49+
50+
?>
51+
52+
<script type="text/javascript">
53+
54+
(function($){
55+
56+
window.gwdc = function( options ) {
57+
58+
this.options = options;
59+
this.startDateInput = $( '#input_' + this.options.formId + '_' + this.options.startFieldId );
60+
this.endDateInput = $( '#input_' + this.options.formId + '_' + this.options.endFieldId );
61+
this.countInput = $( '#input_' + this.options.formId + '_' + this.options.countFieldId );
62+
63+
this.init = function() {
64+
65+
var gwdc = this;
66+
67+
// add data for "format" for parsing date
68+
gwdc.startDateInput.data( 'format', this.options.startDateFormat );
69+
gwdc.endDateInput.data( 'format', this.options.endDateFormat );
70+
71+
gwdc.populateDayCount();
72+
73+
gwdc.startDateInput.change( function() {
74+
gwdc.populateDayCount();
75+
} );
76+
77+
gwdc.endDateInput.change( function() {
78+
gwdc.populateDayCount();
79+
} );
80+
81+
$( '#ui-datepicker-div' ).hide();
82+
83+
}
84+
85+
this.getDayCount = function() {
86+
87+
var startDate = this.parseDate( this.startDateInput.val(), this.startDateInput.data('format') )
88+
var endDate = this.parseDate( this.endDateInput.val(), this.endDateInput.data('format') );
89+
var dayCount = 0;
90+
91+
if( !this.isValidDate( startDate ) || !this.isValidDate( endDate ) )
92+
return '';
93+
94+
if( startDate > endDate ) {
95+
return 0;
96+
} else {
97+
98+
var diff = endDate - startDate;
99+
dayCount = diff / ( 60 * 60 * 24 * 1000 ); // secs * mins * hours * milliseconds
100+
dayCount = Math.round( dayCount ) + this.options.countAdjust;
101+
102+
return dayCount;
103+
}
104+
105+
}
106+
107+
this.parseDate = function( value, format ) {
108+
109+
if( !value )
110+
return false;
111+
112+
format = format.split('_');
113+
var dateFormat = format[0];
114+
var separators = { slash: '/', dash: '-', dot: '.' };
115+
var separator = format.length > 1 ? separators[format[1]] : separators.slash;
116+
var dateArr = value.split(separator);
117+
118+
switch( dateFormat ) {
119+
case 'mdy':
120+
return new Date( dateArr[2], dateArr[0] - 1, dateArr[1] );
121+
case 'dmy':
122+
return new Date( dateArr[2], dateArr[1] - 1, dateArr[0] );
123+
case 'ymd':
124+
return new Date( dateArr[0], dateArr[1] - 1, dateArr[2] );
125+
}
126+
127+
return false;
128+
}
129+
130+
this.populateDayCount = function() {
131+
this.countInput.val( this.getDayCount() ).change();
132+
}
133+
134+
this.isValidDate = function( date ) {
135+
return !isNaN( Date.parse( date ) );
136+
}
137+
138+
this.init();
139+
140+
}
141+
142+
})(jQuery);
143+
144+
</script>
145+
146+
<?php
147+
self::$script_output = true;
148+
return $form;
149+
}
150+
151+
function add_init_script( $return ) {
152+
153+
$start_field_format = false;
154+
$end_field_format = false;
155+
156+
foreach ( $this->form['fields'] as &$field ) {
157+
158+
if ( $field['id'] == $this->start_field_id ) {
159+
$start_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
160+
}
161+
162+
if ( $field['id'] == $this->end_field_id ) {
163+
$end_field_format = $field['dateFormat'] ? $field['dateFormat'] : 'mdy';
164+
}
165+
}
166+
167+
$script = "new gwdc({
168+
formId: {$this->form['id']},
169+
startFieldId: {$this->start_field_id},
170+
startDateFormat: '$start_field_format',
171+
endFieldId: {$this->end_field_id},
172+
endDateFormat: '$end_field_format',
173+
countFieldId: {$this->count_field_id},
174+
countAdjust: {$this->count_adjust}
175+
});";
176+
177+
$slug = implode( '_', array( 'gw_display_count', $this->start_field_id, $this->end_field_id, $this->count_field_id ) );
178+
GFFormDisplay::add_init_script( $this->form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
179+
180+
// remove filter so init script is not output on subsequent forms
181+
remove_filter( 'gform_init_scripts_footer', array( &$this, 'add_init_script' ) );
182+
183+
return $return;
184+
}
185+
186+
function override_submitted_value( $form ) {
187+
188+
$start_date = false;
189+
$end_date = false;
190+
191+
foreach ( $form['fields'] as &$field ) {
192+
193+
if ( $field['id'] == $this->start_field_id ) {
194+
$start_date = self::parse_field_date( $field );
195+
}
196+
197+
if ( $field['id'] == $this->end_field_id ) {
198+
$end_date = self::parse_field_date( $field );
199+
}
200+
}
201+
202+
if ( $start_date > $end_date ) {
203+
204+
$day_count = 0;
205+
206+
} else {
207+
208+
$diff = $end_date - $start_date;
209+
$day_count = $diff / ( 60 * 60 * 24 ); // secs * mins * hours
210+
$day_count = round( $day_count ) + $this->count_adjust;
211+
212+
}
213+
214+
$_POST[ "input_{$this->count_field_id}" ] = $day_count;
215+
216+
}
217+
218+
static function parse_field_date( $field ) {
219+
220+
$date_value = rgpost( "input_{$field['id']}" );
221+
$date_format = empty( $field['dateFormat'] ) ? 'mdy' : esc_attr( $field['dateFormat'] );
222+
$date_info = GFCommon::parse_date( $date_value, $date_format );
223+
if ( empty( $date_info ) ) {
224+
return false;
225+
}
226+
227+
return strtotime( "{$date_info['year']}-{$date_info['month']}-{$date_info['day']}" );
228+
}
229+
230+
}
231+
232+
# Configuration
233+
234+
new GWDayCount( array(
235+
'form_id' => 16,
236+
'start_field_id' => 1,
237+
'end_field_id' => 2,
238+
'count_field_id' => 4,
239+
) );

0 commit comments

Comments
 (0)