Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mecachisenros committed Aug 8, 2019
2 parents bf7f07b + ecdd330 commit ebad8f4
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 131 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,40 @@ A WordPress plugin that integrates the [Caldera Forms](https://wordpress.org/plu

The Caldera Forms CiviCRM plugin contains a set of form processors that interact with CiviCRM's API to retrieve, create and update data in CiviCRM. With this plugin, you can create responsive forms that expose CiviCRM fields and entities like Activities, Relationships, Tags, Groups and more.

**Note**: _this is the development repository for Caldera Forms CiviCRM integration. The plugin can be found in the [WordPress plugin directory](https://wordpress.org/plugins/cf-civicrm/), unless you are developing or testing (`dev` branch) it's encouraged to install it from the WordPress plugin directory._

This plugin is in active development and is currently being tested with the latest CiviCRM version (5.x).

### Features

* Add up to **10 Contacts** on the same form
* Auto-populate form if the user is logged in
* Define Contact Type: Organization, Individual, Household, and Custom Contact Subtypes
* Map Custom Fields data
* Add Relationships to each contact
* Create Activities on form submission
* Select Email Template for notification (requires [Email API Extension](https://civicrm.org/extensions/e-mail-api))
* CiviDiscount integration for Participant Registration and special field (requires [CiviDiscount Extension](https://civicrm.org/extensions/cividiscount))
* Open a Case on form submission
* Checksum support to auto-populate form with URLs like **example.com/some-page?cid={contact.contact_id}&{contact.checksum}**
* Add Memberships (CiviMember)
* Add Participants (CiviEvent)
* Add Contributions with Line Items (for live transactions a [Caldera Forms Payment add-on](https://calderaforms.com/caldera-forms-add-ons/#/payment) is needed)
- Add up to **10 Contacts** on the same form
- Auto-populate form if the user is logged in
- Define Contact Type: Organization, Individual, Household, and Custom Contact Subtypes
- Map Custom Fields data
- Add Relationships to each contact
- Create Activities on form submission
- Select Email Template for notification (requires [Email API Extension](https://civicrm.org/extensions/e-mail-api))
- CiviDiscount integration for Participant Registration and special field (requires [CiviDiscount Extension](https://civicrm.org/extensions/cividiscount))
- Open a Case on form submission
- Checksum support to auto-populate form with URLs like **example.com/some-page?cid={contact.contact_id}&{contact.checksum}**
- Add Memberships (CiviMember)
- Add Participants (CiviEvent)
- Add Contributions with Line Items (for live transactions a [Caldera Forms Payment add-on](https://calderaforms.com/caldera-forms-add-ons/#/payment) is needed)

### Requirements

To use this plugin, the following is needed:

* WordPress
* CiviCRM 5.x
* [Caldera Forms](https://wordpress.org/plugins/caldera-forms/ "Caldera Forms WordPress plugin") v1.8.1 or greater to be installed


*WARNING* This plugin is in active development (stable beta) and is currently being tested with CiviCRM version 5.x.
- WordPress
- CiviCRM 5.x
- [Caldera Forms](https://wordpress.org/plugins/caldera-forms/ "Caldera Forms WordPress plugin") v1.8.1 or greater to be installed

### Using Caldera Forms CiviCRM

* Get an [overview of Caldera Forms](/docs/overview.md)
* Setup [CiviCRM Processors](/docs/processors.md) on your form
* Use [Custom Fields](/docs/custom-fields.md)
* [Examples](/docs/examples.md)
- Get an [overview of Caldera Forms](/docs/overview.md)
- Setup [CiviCRM Processors](/docs/processors.md) on your form
- Use [Custom Fields](/docs/custom-fields.md)
- [Examples](/docs/examples.md)

### Contribute

Expand Down
4 changes: 2 additions & 2 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.3
* Version: 1.0.4
* 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.3' );
define( 'CF_CIVICRM_INTEGRATION_VER', '1.0.4' );
define( 'CF_CIVICRM_INTEGRATION_URL', plugin_dir_url( __FILE__ ) );
define( 'CF_CIVICRM_INTEGRATION_PATH', plugin_dir_path( __FILE__ ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public function pre_render_current_employer_value( $value, $civi_field, $field,

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

if ( $civi_field != 'current_employer' ) return $value;
if ( ! in_array(
$civi_field,
['current_employer', 'organization_name']
)
) return $value;

$employer = civicrm_api3( 'Contact', 'get', [
'contact_type' => 'Organization',
Expand Down
64 changes: 40 additions & 24 deletions fields/presets/class-civicrm-custom-fields-presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ public function custom_fields_options_presets( $presets ) {
// get all custom fields
$custom_fields = $this->custom_fields_get();

if ( ! $custom_fields ) return $presets;
if ( empty( $custom_fields ) ) return $presets;

$extends = $this->entities_extend_get();

array_map( function( $field ) use ( &$presets, $extends ) {

if ( ! in_array( $field['html_type'], $this->allowed_html_types ) ) return;
if ( empty( $field['option_group_id'] ) ) return;

if ( ! in_array( $field['custom_group_id.extends'], $extends ) ) return;

if ( ! $field['option_group_id'] ) return;
if ( ! in_array( $field['html_type'], $this->allowed_html_types ) ) return;

$custom_options = $this->option_values_get( $field['option_group_id'] );

Expand Down Expand Up @@ -133,12 +133,12 @@ public function autopopulate_custom_fields_types() {

array_map( function( $field ) use ( $extends ) {

if ( empty( $field['option_group_id'] ) ) return;

if ( ! in_array( $field['html_type'], $this->allowed_html_types ) ) return;

if ( ! in_array( $field['custom_group_id.extends'], $extends ) ) return;

if ( ! $field['option_group_id'] ) return;

echo "<option value=\"custom_{$field['id']}\"{{#is auto_type value=\"custom_{$field['id']}\"}} selected=\"selected\"{{/is}}>" . sprintf( __( 'CiviCRM - %1$s - %2$s', 'cf-civicrm' ), $field['custom_group_id.title'], $field['label'] ) . "</option>";

}, $custom_fields );
Expand Down Expand Up @@ -167,25 +167,33 @@ public function autopopulate_custom_fields_values( $field, $form ) {

if ( ! $custom_fields ) return $field;

foreach ( $custom_fields as $key => $custom_field ) {
// custom field id
$custom_field_id = (int) str_replace( 'custom_', '', $field['config']['auto_type'] );

if ( ! in_array( $custom_field['html_type'], $this->allowed_html_types ) && ! isset( $custom_field['option_group_id'] ) ) continue;
if ( empty( $custom_fields[$custom_field_id] ) ) return $field;

if ( $field['config']['auto_type'] !== 'custom_' . $custom_field['id'] ) continue;
// custom field settings
$custom_field = $custom_fields[$custom_field_id];

$custom_options = $this->option_values_get( $custom_field['option_group_id'] );
if ( empty( $custom_field['option_group_id'] ) ) return $field;

if ( ! $custom_options ) continue;
if ( ! in_array( $custom_field['html_type'], $this->allowed_html_types ) ) return $field;

$field['config']['option'] = array_reduce( $custom_options, function( $options, $option ) {
$options[$option['value']] = [
'value' => $option['value'],
'label' => $option['label']
];
return $options;
}, [] );
if ( $field['config']['auto_type'] !== 'custom_' . $custom_field['id'] ) return $field;

}
// get options
$custom_options = $this->option_values_get( $custom_field['option_group_id'] );

if ( ! $custom_options ) return $field;

// populate field options
$field['config']['option'] = array_reduce( $custom_options, function( $options, $option ) {
$options[$option['value']] = [
'value' => $option['value'],
'label' => $option['label']
];
return $options;
}, [] );

return $field;

Expand All @@ -205,7 +213,6 @@ public function custom_fields_get() {

// get all custom fields
$custom_fields = civicrm_api3( 'CustomField', 'get', [
'sequential' => 1,
'is_active' => 1,
'return' => [ 'name', 'label', 'custom_group_id', 'option_group_id', 'html_type', 'custom_group_id.extends', 'custom_group_id.title' ],
'options' => [ 'limit' => 0 ],
Expand All @@ -232,11 +239,20 @@ public function custom_fields_get() {
*/
public function option_values_get( $option_group_id ) {

$option_values = civicrm_api3( 'OptionValue', 'get', [
'sequential' => 1,
'option_group_id' => $option_group_id,
'options' => ['limit' => 0, 'sort' => "weight ASC"],
] );
try {

$option_values = civicrm_api3( 'OptionValue', 'get', [
'sequential' => 1,
'option_group_id' => $option_group_id,
'options' => ['limit' => 0, 'sort' => 'weight ASC'],
] );

} catch ( CiviCRM_API3_Exception $e ) {

// return false if there's an error
return false;

}

if ( $option_values['count'] && ! $option_values['is_error'] ) return $option_values['values'];

Expand Down
65 changes: 65 additions & 0 deletions includes/class-civicrm-caldera-forms-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,4 +1166,69 @@ public function get_case_custom_fields() {

}

/**
* Retrieves the related contacts for a given contact.
*
* @since 1.0.4
* @param array|int $contact The contact data array or the contact_id
* @param array $form The form config
* @return array|bool $related_contacts The related contacts or false
*/
public function get_contact_related_contacts( $contact, $form ) {

// get relationship processors
$relationship_configs = $this->get_processor_by_type( 'civicrm_relationship', $form );

if ( empty( $relationship_configs ) ) return false;

$contact_configs = $this->get_processor_by_type( 'civicrm_contact', $form );

// processor id and its contact link
$contact_link_relations = array_reduce( $contact_configs, function( $relations, $processor ) {
$relations[$processor['ID']] = $processor['config']['contact_link'];
return $relations;
}, [] );

$contact_id = is_array( $contact ) ? $contact['id'] : $contact;

return array_reduce( $relationship_configs, function( $contacts, $processor ) use ( $contact_id, $contact_link_relations ) {

if ( empty( $processor['runtimes'] ) ) return $contacts;

$relationship = civicrm_api3( 'Relationship', 'get', [
'contact_id_a' => $contact_id,
'contact_id_b' => $contact_id,
'relationship_type_id' => $processor['config']['relationship_type'],
'is_active' => 1,
'options' => [
'or' => [['contact_id_a', 'contact_id_b']],
'limit' => 1,
'sort' => 'id desc'
]
] );

// bail if no relationship or we have more than one relationship
if ( ! $relationship['count'] || $relationship['count'] > 1 ) return $contacts;

$result = $relationship = $relationship['values'][$relationship['id']];
// unset relationship possible collisioning ids
unset( $result['id'], $result['relationship_type_id'], $result['case_id'] );
// get 'opposite' realtion, contact_id_a <=> contact_id_b
$relation = array_search( $contact_id , $result ) == 'contact_id_a'
? 'contact_id_b'
: 'contact_id_a';

$contact_processor_id = array_search(
$processor['config'][ str_replace( '_id', '', $relation ) ], // relation is stored as contact_a|b in the processor, stupid me
$contact_link_relations
);

$contacts[$contact_processor_id] = $this->get_civi_contact( $relationship[$relation] );

return $contacts;

}, [] );

}

}
12 changes: 11 additions & 1 deletion processors/case/case_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</div>
<?php } } ?>

<!-- Case Manager -->
<!-- Case Creator -->
<div class="caldera-config-group">
<label><?php _e( 'Case Created By', 'cf-civicrm' ); ?></label>
<div class="caldera-config-field">
Expand All @@ -93,6 +93,15 @@
</div>
</div>

<!-- Case Manager -->
<div class="caldera-config-group">
<label><?php _e( 'Case Manager', 'cf-civicrm' ); ?></label>
<div class="caldera-config-field">
<select id="{{_id}}_manager_id" class="block-input field-config" style="width: 100%;" nonce="<?php echo wp_create_nonce('admin_get_civi_contact'); ?>" name="{{_name}}[manager_id]">
</select>
</div>
</div>

<!-- Custom fields -->
<h2><?php _e( 'Custom Fields', 'cf-civicrm' ); ?></h2>
<?php foreach ( caldera_forms_civicrm()->helper->get_case_custom_fields() as $key => $custom_field ) { ?>
Expand Down Expand Up @@ -128,5 +137,6 @@ class="caldera-config-group"
} ).trigger( 'change' );

cfc_select2_defaults( '#{{_id}}_creator_id', '{{creator_id}}' );
cfc_select2_defaults( '#{{_id}}_manager_id', '{{manager_id}}' );
} );
</script>
Loading

0 comments on commit ebad8f4

Please sign in to comment.