Skip to content

Commit

Permalink
Merge pull request #101 from mecachisenros/dev
Browse files Browse the repository at this point in the history
Merge dev into master
  • Loading branch information
mecachisenros committed Mar 6, 2019
2 parents 6f0492b + 9261a0a commit d40dc4b
Show file tree
Hide file tree
Showing 36 changed files with 4,249 additions and 368 deletions.
47 changes: 44 additions & 3 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function cfc_select2_defaults( selector, value ) {
}
}

jQuery( selector ).cfcSelect2({
jQuery( selector ).cfcSelect2({
ajax: {
url: ajaxurl,
dataType: 'json',
Expand Down Expand Up @@ -102,5 +102,46 @@ function cfc_select2_defaults( selector, value ) {
.append( new Option( e.params.data.text, e.params.data.id, false, false ) )
.trigger( 'select2:close' );
} )
});
}
});
}

// decorate formJSON to add price field options for autopopulate field conditionals
jQuery( document ).ready( function( $ ) {

$.fn.originalFormJSON = $.fn.formJSON;

$.fn.formJSON = function() {

var form = $( this ).originalFormJSON();

if ( ! form.config || ! form.config.fields ) return form;

for ( var field_id in form.config.fields ) {

var config = form.config.fields[field_id].config;

if ( config.auto && ( config.auto_type.indexOf( 'price_field_' ) !== -1 || config.auto_type.indexOf( 'custom_' ) !== -1 ) ) {

form.config.fields[field_id].config.option = {};
// cfc_price_field_<id> or custom_<id>
var preset_name = config.auto_type.replace( 'cfc_', '' ),
options = preset_options[preset_name].data;

if ( options.constructor !== Array ) return form;
options.map( function( option ) {

var parts = option.split( '|' );

form.config.fields[field_id].config.option[parts[0]] = {
value: parts[0],
label: parts[1],
calc_value: parts[2]
};

} );
}
}

return form;
}
} );
67 changes: 67 additions & 0 deletions assets/js/autopop_conditionals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// processor conditionals, add price field options for autopopulate
jQuery( document ).ready( function( $ ) {
$( '.caldera-editor-body' ).on( 'change', '.caldera-conditional-field-set', function( e ) {

var field = $( this ),
field_compare = field.parent().find( '.compare-type' ),
type = field.data( 'condition' ),
pid = field.data( 'id' ),
name = "config[" + type + "][" + pid + "][conditions][group][" + field.data('row') + "][" + field.data('line') + "]",
lineid = field.data( 'line' ),
target = $( '#' + lineid + "_value" ),
curval = target.find( '.caldera-conditional-value-field' ).first();

var field_id = this.value,
form = core_form.formJSON();

if ( field_id.indexOf( '{' ) >= 0 ) return;

if ( ! field_id || ! form || ( ! form.config && ! form.config[field_id] ) ) return;

var config = form.config.fields[field_id].config;

if ( curval.length ) {
if ( curval.val().length )
target.data( 'value', curval.val() );
} else if ( 0 === target.val() ) {
target.data( 'value', 0 );
} else if ( '0' === target.val() ) {
target.data( 'value', '0' );
}

field_compare.show();

if ( config.auto && ( config.auto_type.indexOf( 'price_field_' ) !== -1 || config.auto_type.indexOf( 'custom_' ) !== -1 ) ) {
// 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;

options_rows.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>';

target.html( out );

}

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

Expand Down Expand Up @@ -137,6 +137,15 @@ class CiviCRM_Caldera_Forms {
*/
public $html;

/**
* CiviDiscount helper object.
*
* @since 1.0
* @access public
* @var object $cividiscount The CiviDiscount helper object
*/
public $cividiscount;

/**
* Returns a single instance of this object when called.
*
Expand Down Expand Up @@ -183,7 +192,7 @@ public static function instance() {
private function check_dependencies() {

// Bail if Caldera Forms is not available
if ( ! defined( 'CFCORE_VER' ) || ! version_compare( CFCORE_VER, '1.7', '>=' ) ) {
if ( ! defined( 'CFCORE_VER' ) || ! version_compare( CFCORE_VER, '1.8.1', '>=' ) ) {
add_action( 'admin_notices', [$this, 'caldera_forms_version_notice'] );
return false;
}
Expand Down Expand Up @@ -228,6 +237,8 @@ private function include_files() {
include CF_CIVICRM_INTEGRATION_PATH . 'includes/class-civicrm-caldera-forms-transient.php';
// Include html class
include CF_CIVICRM_INTEGRATION_PATH . 'includes/class-civicrm-caldera-forms-html.php';
// include CiviDiscount helper class
include CF_CIVICRM_INTEGRATION_PATH . 'includes/class-civicrm-caldera-forms-cividiscount.php';

}

Expand Down Expand Up @@ -260,6 +271,9 @@ private function setup_objects() {
$this->assets = new CiviCRM_Caldera_Forms_Assets( $this );
// init html class
$this->html = new CiviCRM_Caldera_Forms_HTML( $this );
// init cividiscount class
if ( $this->processors->enabled_extensions && in_array( 'org.civicrm.module.cividiscount', $this->processors->enabled_extensions ) )
$this->cividiscount = new CiviCRM_Caldera_Forms_CiviDiscount( $this );

}

Expand All @@ -283,7 +297,7 @@ private function register_hooks() {
public function caldera_forms_version_notice() {
?>
<div class="notice notice-error">
<p><?php _e( 'Caldera Forms CiviCRM requires Caldera Forms v1.7 or higher.', 'caldera-forms-civicrm' ); ?></p>
<p><?php _e( 'Caldera Forms CiviCRM requires Caldera Forms v1.8.1 or higher.', 'caldera-forms-civicrm' ); ?></p>
</div>
<?php
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,28 @@ public function register_field_type( $field_types ) {
*/
public function handle_current_employer_field( $mapped_field, $civi_field, $field, $config, $form ) {

if ( $civi_field == 'current_employer' && $field['type'] == 'civicrm_contact_reference' ) {
if ( ! is_numeric( $mapped_field ) && isset( $field['config']['new_organization'] ) ) {
$employer = civicrm_api3( 'Contact', 'create', [
'contact_type' => 'Organization',
'organization_name' => $mapped_field,
] );
} else {
$employer = civicrm_api3( 'Contact', 'get', [
'contact_id' => $mapped_field,
'return' => 'organization_name'
] );
}
return $employer['values'][$employer['id']]['organization_name'];
if ( $field['type'] != 'civicrm_contact_reference' ) return $mapped_field;

if ( $civi_field != 'current_employer' ) return $mapped_field;

if ( ! is_numeric( $mapped_field ) && isset( $field['config']['new_organization'] ) ) {
$employer = civicrm_api3( 'Contact', 'create', [
'contact_type' => 'Organization',
'organization_name' => $mapped_field,
] );
} else {
$employer = civicrm_api3( 'Contact', 'get', [
'contact_id' => $mapped_field,
'return' => 'organization_name'
] );
}

if ( isset( $employer['count'] ) && $employer['count'] )
return [
'organization_name' => $employer['values'][$employer['id']]['organization_name'],
'employer_id' => $employer['id']
];

return $mapped_field;
}

Expand All @@ -131,10 +138,20 @@ public function handle_current_employer_field( $mapped_field, $civi_field, $fiel
* @param array $config processor config
*/
public function pre_render_current_employer_value( $value, $civi_field, $field, $entity, $config ) {
if ( $civi_field == 'current_employer' && $field['type'] == 'civicrm_contact_reference' ) {
$employer = civicrm_api3( 'Contact', 'get', [ 'contact_type' => 'Organization', 'organization_name' => $entity[$civi_field] ] );
return $employer['id'];
}

if ( $field['type'] != 'civicrm_contact_reference' ) return $value;

if ( $civi_field != 'current_employer' ) return $value;

$employer = civicrm_api3( 'Contact', 'get', [
'contact_type' => 'Organization',
'organization_name' => $entity[$civi_field],
'return' => 'organization_name',
'options' => [ 'limit' => 1 ]
] );

if ( isset( $employer['count'] ) && $employer['count'] ) return $employer['id'];

return $value;
}

Expand Down
Loading

0 comments on commit d40dc4b

Please sign in to comment.