Skip to content
Merged
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
31 changes: 23 additions & 8 deletions gravity-forms/gw-choice-counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function __construct( $args ) {
'count_field_id' => false,
'choice_field_id' => null,
'choice_field_ids' => false,
'values' => false,
) );

if ( isset( $this->_args['choice_field_id'] ) ) {
Expand Down Expand Up @@ -86,7 +87,7 @@ function output_script() {
// Event handler for all listeners to avoid DRY and to maintain a pointer reference to the function
// which we can use to explicity unbind event handlers
self.updateChoiceEventHandler = function() {
self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId );
self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId, self.values );
};

self.init = function() {
Expand All @@ -107,18 +108,30 @@ function output_script() {
return Boolean( $field.find( 'input[type="checkbox"]' ).length );
}

self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId ) {
self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId, values ) {

var countField = $( '#input_' + formId + '_' + countFieldId ),
count = 0;

for ( var i = 0; i < choiceFieldIds.length; i++ ) {

var $choiceField = $( '#input_' + formId + '_' + choiceFieldIds[ i ] );
if ( self.isCheckboxField( $choiceField ) ) {
count += $choiceField.find( 'input[type="checkbox"]:checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length;
if ( ! values ) {
// If no values provided in the config, just get the number of checkboxes checked.
if ( self.isCheckboxField( $choiceField ) ) {
count += $choiceField.find( 'input[type="checkbox"]:checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length;
} else {
count += $choiceField.find( 'option:selected' ).length;
}
} else {
count += $choiceField.find( 'option:selected' ).length;
// When values are provided, match the values before adding them to count.
var selectedValues = [];
$choiceField.find( 'input[type="checkbox"]:checked' ).each( function( k, $selectedChoice ) {
selectedValues.push( $selectedChoice.value );
});
values.forEach( function( val ) {
count += selectedValues.indexOf( val ) >= 0;
});
}

}
Expand Down Expand Up @@ -151,6 +164,7 @@ function add_init_script( $form ) {
'formId' => $this->_args['form_id'],
'countFieldId' => $this->_args['count_field_id'],
'choiceFieldIds' => $this->_args['choice_field_ids'],
'values' => $this->_args['values'],
);

$script = 'new GWChoiceCount( ' . json_encode( $args ) . ' );';
Expand Down Expand Up @@ -182,7 +196,8 @@ public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
# Configuration

new GW_Choice_Count( array(
'form_id' => 123, // The ID of your form.
'count_field_id' => 4, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.
'choice_field_ids' => array( 5, 6 ), // Any array of Checkbox or Multi-select field IDs which should be counted.
'form_id' => 123, // The ID of your form.
'count_field_id' => 4, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.
'choice_field_ids' => array( 5, 6 ), // Any array of Checkbox or Multi-select field IDs which should be counted.
'values' => array( 'None of the above' ), // Array of values, all of which should match.
) );