Skip to content

Commit

Permalink
Merge pull request #136 from mecachisenros/dev
Browse files Browse the repository at this point in the history
v1.0.5
  • Loading branch information
mecachisenros authored Nov 8, 2019
2 parents ebad8f4 + 9cd3e52 commit 52c3066
Show file tree
Hide file tree
Showing 17 changed files with 1,238 additions and 653 deletions.
93 changes: 75 additions & 18 deletions assets/js/autopop_conditionals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// processor conditionals, add price field options for autopopulate
jQuery( document ).ready( function( $ ) {

function build_options_html(options, name, field_id, target) {
var out = '<select name="' + name + '[value]" class="caldera-processor-value-bind caldera-conditional-value-field" data-field="' + field_id + '" style="max-width: 220px; width: 220px;">';
out += '<option value=""></option>';

options.map( function( option ) {

var parts = option.split('|'),
label = parts[1],
value = parts[0],
sel = '';

if ( target.data( 'value' ) ) {
if ( target.data( 'value' ).toString() === value )
sel = ' selected="selected"';
}

out += '<option value="' + value + '"' + sel + '>' + label + '</option>';
} )

out += '</select>';

return out;

}
$( '.caldera-editor-body' ).on( 'change', '.caldera-conditional-field-set', function( e ) {

var field = $( this ),
Expand Down Expand Up @@ -35,33 +60,65 @@ jQuery( document ).ready( function( $ ) {
// cfc_price_field_<id> or custom_<id>
var preset_name = config.auto_type;
preset_name = config.auto_type.replace( 'cfc_', '' );

var options_rows = preset_options[preset_name].data,
out = '<select name="' + name + '[value]" class="caldera-processor-value-bind caldera-conditional-value-field" data-field="' + field_id + '" style="max-width: 220px; width: 220px;">';
out += '<option value=""></option>';

if ( ! Array.isArray( options_rows ) ) return;
var options = preset_options[preset_name].data;

options_rows.map( function( option ) {
if ( ! Array.isArray( options ) ) return;

var parts = option.split('|'),
label = parts[1],
value = parts[0],
sel = '';
var out = build_options_html( options, name, field_id, target );

target.html( out );

} else if ( form.config.fields[field_id].type == 'civicrm_state' ) {

$.ajax( {
url: ajaxurl,
type: 'post',
data: {
action: 'civicrm_get_state_province',
},
success: function ( response ) {
var result = JSON.parse( response );

if ( ! Object.keys(result).length ) return;

var formtatted_options = Object.keys(result).map( function( state_id ) {
var state = result[state_id]
return state_id + '|' + state.name;
} )

var out = build_options_html( formtatted_options, name, field_id, target );

target.html( out );

if ( target.data( 'value' ) ) {
if ( target.data( 'value' ).toString() === value )
sel = ' selected="selected"';
}
} );

out += '<option value="' + value + '"' + sel + '>' + label + '</option>';
} )
} else if ( form.config.fields[field_id].type == 'civicrm_country' ) {

out += '</select>';
$.ajax( {
url: ajaxurl,
type: 'post',
data: {
action: 'civicrm_get_countries',
},
success: function ( response ) {
var result = JSON.parse( response );

target.html( out );
if ( ! result.values.length ) return;

var formtatted_options = result.values.map( function( country ) {
return country.id + '|' + country.name;
} )

var out = build_options_html( formtatted_options, name, field_id, target );

target.html( out );

}
} );

}

} );
} );
} );
6 changes: 3 additions & 3 deletions cf-civicrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: CF CiviCRM
* Description: CiviCRM integration for Caldera Forms.
* Version: 1.0.4
* Version: 1.0.5
* Author: Andrei Mondoc
* Author URI: https://github.com/mecachisenros
* Plugin URI: https://github.com/mecachisenros/cf-civicrm
Expand All @@ -16,7 +16,7 @@
*
* @since 0.1
*/
define( 'CF_CIVICRM_INTEGRATION_VER', '1.0.4' );
define( 'CF_CIVICRM_INTEGRATION_VER', '1.0.5' );
define( 'CF_CIVICRM_INTEGRATION_URL', plugin_dir_url( __FILE__ ) );
define( 'CF_CIVICRM_INTEGRATION_PATH', plugin_dir_path( __FILE__ ) );

Expand Down Expand Up @@ -202,7 +202,7 @@ private function check_dependencies() {

// Bail if unable to init CiviCRM
// FIXME This should only be called when needed
if ( ! civi_wp()->initialize() ) return $processors;
if ( ! civi_wp()->initialize() ) return false;

// we're good
return true;
Expand Down
79 changes: 77 additions & 2 deletions fields/civicrm_file/class-civicrm-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function register_hooks() {
// add civicrm file upload config template for file field
add_action( 'caldera_forms_field_settings_template', [ $this, 'civicrm_upload_config_template' ], 20, 2 );

// handle advanced file 2.0 cf2_file
add_filter( 'rest_dispatch_request', [ $this, 'handle_cf2_advanced_file_upload' ], 10, 3 );

}

Expand Down Expand Up @@ -128,6 +130,79 @@ public function handle_civicrm_uploads( $file, $args ) {

}

/**
* Filters the rest resquest for cf2 advanced file uploads.
*
* The adcnaved file upload (2.0) doesn't trigger the
* 'caldera_forms_file_upload_handler' filter hence this workaround,
* we filter the rest request for the file route, grab the necessary
* params, and hook twice in wp_handle_upload() function to create
* and pass on our file id.
*
* @since 1.0.5
* @param mixed $result The result
* @param WP_REST_Request $request The request
* @param string $route The route
* @return mixed $result
*/
public function handle_cf2_advanced_file_upload( $result, WP_REST_Request $request, $route ) {

if ( $route != '/cf-api/v3/file' ) return $result;

$params = $request->get_params();

if ( empty( $params['formId'] ) || empty( $params['fieldId'] ) || empty( $params['hashes'] ) ) return $result;

$form = Caldera_Forms::get_form( $params['formId'] );
$field = Caldera_Forms_Field_Util::get_field( $params['fieldId'], $form );

if ( empty( $field['config']['civicrm_file_upload'] ) ) return $result;

if ( empty( $_FILES ) ) return $result;

if ( empty( $_FILES['file'] ) ) return $result;

if ( ! Caldera_Forms_Render_Nonce::verify_nonce( $params['verify'], $params['formId'] ) ) return $result;

$file_to_move = $_FILES['file'];

if ( ! hash_equals( md5_file( $file_to_move['tmp_name'] ), $params['hashes'] ) ) return $result;

$args = [
'form_id' => $params['formId'],
'field_id' => $params['fieldId'],
];

$file_id = null;

// filter file upload before wp handles it
add_filter( 'pre_move_uploaded_file', function( $abort, $file ) use ( $file_to_move, $args, &$file_id ) {

if ( $file !== $file_to_move ) return $abort;

// its a civicrm file, create it
$upload = $this->handle_civicrm_uploads( $file, $args );
$file_id = $upload['url'];

return true;

}, 10, 2 );

// pass our file id as the url once wp has handled the file
add_filter( 'wp_handle_upload', function( $upload ) use ( &$file_id ) {

if ( $file_id ) {
$upload['url'] = $file_id;
}

return $upload;

} );

return $result;

}

/**
* Adds CiviCRM File Uplad config template.
*
Expand All @@ -140,8 +215,8 @@ public function handle_civicrm_uploads( $file, $args ) {
*/
public function civicrm_upload_config_template( $config, $field_slug ) {

if( $config['field'] == 'File' ) include CF_CIVICRM_INTEGRATION_PATH . 'fields/civicrm_file/file_config.php';
if( $config['field'] == 'Advanced File Uploader' ) include CF_CIVICRM_INTEGRATION_PATH . 'fields/civicrm_file/advanced_file_config.php';
if( $field_slug == 'file' ) include CF_CIVICRM_INTEGRATION_PATH . 'fields/civicrm_file/file_config.php';
if( in_array( $field_slug, [ 'advanced_file', 'cf2_file' ] ) ) include CF_CIVICRM_INTEGRATION_PATH . 'fields/civicrm_file/advanced_file_config.php';

}
}
58 changes: 38 additions & 20 deletions fields/discount/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,46 @@ class="btn btn-block"
success: function( response ) {

var options = JSON.parse( response );
for ( var option_id in options ) {
// option
var option = options[option_id],
element = $( '#' + option_id );
// calc value
element.attr( 'data-calc-value', option.calc_value );
element.data( 'calc-value', option.calc_value );
// label text
element.parent().attr( 'data-label', option.label );
element.parent().contents().map( function( el ) {
// replace text node (type 3), the label
if ( this.nodeType == 3 ) {
// remove white spaces to avoid empty strings replacement
this.textContent = this.textContent.trim();
if ( this.textContent )
this.textContent = ' ' + option.label;
}
} );

}
if ( options ) {
for ( var option_id in options ) {
// option
var option = options[option_id],
element = $( '#' + option_id );
// calc value
element.attr( 'data-calc-value', option.calc_value );
element.data( 'calc-value', option.calc_value );
// label text
element.parent().attr( 'data-label', option.label );
element.parent().contents().map( function( el ) {
// replace text node (type 3), the label
if ( this.nodeType == 3 ) {
// remove white spaces to avoid empty strings replacement
this.textContent = this.textContent.trim();
if ( this.textContent )
this.textContent = ' ' + option.label;
}
} );

}

if ( !$( '.cfc-discount-success-message' ).length ) {
var success = '<div class="cfc-discount-success-message alert alert-success"><span><?php esc_html_e( 'Discount applied successfully.', 'cf-civicrm' ); ?></span></div>';
$( '.cfc-discount-error-message' ).remove();
discount_button.parent().parent().after( $( success ) );
}

$( 'body' ).trigger( 'cfc.discount.apply', { form_id: form_id_attr, instance: form_instance, options: options } );

$( 'body' ).trigger( 'cfc.discount.apply', { form_id: form_id_attr, instance: form_instance, options: options } );
} else {

if ( !$( '.cfc-discount-error-message' ).length ) {
var error = '<div class="cfc-discount-error-message alert alert-error"><span><?php esc_html_e( 'The code supplied is not valid.', 'cf-civicrm' ); ?></span></div>';
$( '.cfc-discount-success-message' ).remove();
discount_button.parent().parent().after( $( error ) );
}

}

}

Expand Down
2 changes: 1 addition & 1 deletion fields/presets/class-civicrm-price-sets-presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function autopopulate_price_field_values( $field, $form ) {
'disabled' => $this->disable_all_fields
];

if ( isset( $price_field_value['tax_amount'] ) && $this->plugin->helper->get_tax_settings()['invoicing'] ) {
if ( isset( $price_field_value['tax_amount'] ) && $this->plugin->helper->get_tax_invoicing() ) {
$option['calc_value'] += $price_field_value['tax_amount'];
$option['label'] = $this->plugin->helper->format_tax_label( $price_field_value['label'], $price_field_value['amount'], $price_field_value['tax_amount'] );
}
Expand Down
15 changes: 15 additions & 0 deletions includes/class-civicrm-caldera-forms-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function register_hooks() {
add_action( 'wp_ajax_nopriv_do_code_cividiscount', [ $this, 'do_code_cividiscount' ] );
// premiums
add_action( 'wp_ajax_civicrm_get_premiums', [ $this, 'civicrm_get_premiums' ] );
// countries and states
add_action( 'wp_ajax_civicrm_get_state_province', [ $this, 'civicrm_get_state_province' ] );
add_action( 'wp_ajax_civicrm_get_countries', [ $this, 'civicrm_get_countries' ] );
}

/**
Expand Down Expand Up @@ -211,4 +214,16 @@ public function civicrm_get_premiums() {
die;
}

public function civicrm_get_countries() {
$countries = $this->plugin->helper->get_countries();
echo json_encode( $countries );
die;
}

public function civicrm_get_state_province() {
$state_provinces = $this->plugin->helper->get_state_province();
echo json_encode( $state_provinces );
die;
}

}
Loading

0 comments on commit 52c3066

Please sign in to comment.