Skip to content

Commit

Permalink
Merge pull request #1 from Joeventures/support-multi-key-fields
Browse files Browse the repository at this point in the history
Support multiple key fields
  • Loading branch information
Joeventures authored Jun 5, 2017
2 parents acc4a4f + f72e165 commit ace888b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Once you have your API key and secret, log in to your WordPress admin interface.

For any form you build, you will see a "Fieldbook" option listed under each form's settings. Select whether you want the Fieldbook feed to only create new records, or update existing records. Then, choose your fieldbook sheet, and map the fields from your Fieldbook sheet to your form fields.

If you chose the Update option, you will also need to specify a "key" field for both Fieldbook and your form. This helps Fieldbook for Gravity Forms find the record that needs to be updated. For example, if you have a "Name" column in your Fieldbook sheet, where each name is unique, Fieldbook for Gravity Forms will search for a record with a matching name and update that record upon form submission. To make that match happen, you would specify the name field in both your form and your Fieldbook sheet. If a matching record is not found, a new record will be created.
If you chose the Update option, you will also need to map which fields in Fieldbook should match the data fed from your form. This helps Fieldbook for Gravity Forms find the record that needs to be updated. For example, if you have a "Name" column in your Fieldbook sheet, where each name is unique, Fieldbook for Gravity Forms will search for a record with a matching name and update that record upon form submission. To make that match happen, you would specify the name field in both your form and your Fieldbook sheet. If a matching record is not found, a new record will be created.

You may need to create more than one Fieldbook feed if form submissions use fields that can be found in more than one sheet. For example, a donation form may need to create new records in your Donations sheet, but update existing records in your Donors sheet. Be careful in these types of situations and understand that the order that you create your feeds matters. To use the donation form example, make sure to create the "donors" feed first, then the "donations" feed after that. The reason: each donation must include a link to the donor. Fieldbook for Gravity Forms executes its feeds in the order they are created. If a donor doesn't exist when the donation record is created, then the donation record will not be linked to the proper donor.

Expand All @@ -27,6 +27,10 @@ For anything else related to Fieldbook for Gravity Forms, including the function
* [Gravity Forms Add-On Framework](https://www.gravityhelp.com/documentation/category/add-on-framework/)
* [Fieldbook](https://github.com/fieldbook/api-docs)

## What's new in v1.1.0

* **Breaking Change** Support for using multiple key fields through a key field mapper.

## Credits

Thanks to the people at Fieldbook, @jasoncrawford in particular; and to @carlhancock at Gravity Forms for your feedback and advice along the way.
43 changes: 13 additions & 30 deletions class-fieldbookforgravityforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class FieldbookForGForms extends GFFeedAddOn {
public $_async_feed_processing = true;
protected $_version = '1.0';
protected $_version = '1.1.0';
protected $_min_gravityforms_version = '2.2';
protected $_slug = 'gravityfield';
protected $_path = 'fieldbookforgravityforms/fieldbookforgravityforms.php';
Expand Down Expand Up @@ -252,8 +252,7 @@ public function feed_settings_fields() {
'type' => 'field_map',
'field_map' => $this->map_fields()
),
$this->do_field_key_field(),
$this->do_field_fb_key_field(),
$this->do_field_map_key_fields(),
array(
'type' => 'feed_condition',
'name' => 'feed_condition',
Expand Down Expand Up @@ -315,36 +314,19 @@ public function do_field_feed_type() {
);
}

public function do_field_key_field() {

public function do_field_map_key_fields() {
return array(
'label' => 'Form Key Field',
'name' => 'key_field',
'type' => 'field_select',
'label' => 'Map Key Fields',
'name' => 'map_key_fields',
'type' => 'field_map',
'field_map' => $this->map_fields(),
'dependency' => array(
'field' => 'feed_type',
'values' => array('update')
)
);
}

public function do_field_fb_key_field() {

$choices = $this->map_fb_fields_for_match();

return array(
'label' => 'Fieldbook Key Field',
'name' => 'fb_key_field',
'type' => 'select',
'dependency' => array(
'field' => 'feed_type',
'values' => array('update')
),
'choices' => $choices
);

}

public function do_field_sheet_name($label = 'Sheet Name') {

$choices = array();
Expand Down Expand Up @@ -396,12 +378,13 @@ public function process_feed($feed, $entry, $form) {
if($feed_type == 'create') {
$fb->create($params);
} elseif($feed_type == 'update') {
$search_fields = $this->get_field_map_fields($feed, 'map_key_fields');
$find_param = array();
foreach($search_fields as $fieldbook_field => $form_field) {
if($form_field == '') continue;
$find_param[$fieldbook_field] = $this->get_field_value($form, $entry, $form_field);
}

$key_field_id = strval($feed['meta']['key_field']);
$key_field_name = $feed['meta']['fb_key_field'];
$key_field_value = $this->get_field_value($form,$entry,$key_field_id);

$find_param = array($key_field_name => $key_field_value);
$fieldbook_record = $fb->search($find_param);
if(count($fieldbook_record) > 0) {
$update_id = $fieldbook_record[0]['id'];
Expand Down
2 changes: 1 addition & 1 deletion fieldbookforgravityforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Fieldbook for Gravity Forms
Description: Send Gravity Form submissions to Fieldbook.
Version: 1.0.0
Version: 1.1.0
Author: Joe Winter
Author URI: https://joe.ventures
License: GPL2
Expand Down

0 comments on commit ace888b

Please sign in to comment.