diff --git a/README.md b/README.md index 2514289..7d4f0ea 100644 --- a/README.md +++ b/README.md @@ -4,28 +4,34 @@ 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. +### 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 +* Checksum support to auto-populate form with URLs like **example.com/support?cid={contact.contact_id}&{contact.checksum}** + +### Requirements + +To use this plugin, the following is needed: + +* WordPress +* CiviCRM 4.6.x or 4.7.x +* [Caldera Forms](https://wordpress.org/plugins/caldera-forms/ "Caldera Forms WordPress plugin") to be installed + *WARNING* This plugin is at a very early stage of development (bleeding edge alpha) and is currently being tested with CiviCRM versions 4.6.x and 4.7.x. -Currently supports: - -### + Contact entity -* Up to **10 Contact** prcessors on the same form -* **Auto-populate form** if the user is logged in -* **Checksum support** to auto-populate form with URLs like **example.com/support?cid={contact.contact_id}&{contact.checksum}** -* **Contact Type**: Organization, Individual, Household, Custom Type -* **Contact Subtypes** -* **Custom Fields** - -### + Activity entity -### + Relationship entity -### + Group entity -### + Tag entity -### + Address entity -### + Email entity -### + Phone entity -### + Note entity -### + Website entity -### + IM (Instant Messenger) entity - -### Coming soon -+ Request a feature +### Using Caldera Forms CiviCRM + +* Get an [overview of Caldera Forms](/docs/overview.md) +* Setup [CiviCRM Processors](/docs/processors.md) on you form +* Use [Custom Fields](/docs/custom-fields.md) + +### Contribute + +Please help improve this plugin by using the extension issue queue to report any troubles and to make requests for feature improvements. The issue queue is here: https://github.com/mecachisenros/caldera-forms-civicrm/issues + +Issues submitted to the issue queue will be addressed based on time and interest. If you are a [developer contributions](/docs/dev/contribute.md) are welcome. diff --git a/caldera-forms-civicrm.php b/caldera-forms-civicrm.php index f8ab14e..b821008 100644 --- a/caldera-forms-civicrm.php +++ b/caldera-forms-civicrm.php @@ -2,7 +2,7 @@ /** * Plugin Name: Caldera Forms CiviCRM * Description: CiviCRM integration for Caldera Forms. - * Version: 0.3 + * Version: 0.4 * Author: Andrei Mondoc * Author URI: https://github.com/mecachisenros * Plugin URI: https://github.com/mecachisenros/caldera-forms-civicrm @@ -15,7 +15,7 @@ * * @since 0.1 */ -define( 'CF_CIVICRM_INTEGRATION_VER', '0.3' ); +define( 'CF_CIVICRM_INTEGRATION_VER', '0.4' ); define( 'CF_CIVICRM_INTEGRATION_URL', plugin_dir_url( __FILE__ ) ); define( 'CF_CIVICRM_INTEGRATION_PATH', plugin_dir_path( __FILE__ ) ); @@ -64,6 +64,15 @@ class CiviCRM_Caldera_Forms { */ public static $templates; + /** + * The form management object. + * + * @since 0.4 + * @access public + * @var object $templates The form management object + */ + public static $forms; + /** * Returns a single instance of this object when called. * @@ -110,7 +119,7 @@ public static function instance() { private function check_dependencies() { // Bail if Caldera Forms is not available - if ( ! defined( 'CFCORE_VER' ) ) return false; + if ( ! defined( 'CFCORE_VER' ) && version_compare( CFCORE_VER, '1.5', '>=' ) ) return false; // Bail if CiviCRM is not available if ( ! function_exists( 'civi_wp' ) ) return false; @@ -143,6 +152,9 @@ private function include_files() { // Include template management class include CF_CIVICRM_INTEGRATION_PATH . 'includes/class-civicrm-caldera-forms-templates.php'; + // Include forms management class + include CF_CIVICRM_INTEGRATION_PATH . 'includes/class-civicrm-caldera-forms-forms.php'; + } /** @@ -161,6 +173,9 @@ private function setup_objects() { // init templates manager self::$templates = new CiviCRM_Caldera_Forms_Templates; + // init forms manager + self::$forms = new CiviCRM_Caldera_Forms_Forms; + } /** diff --git a/docs/custom-fields.md b/docs/custom-fields.md new file mode 100644 index 0000000..6878663 --- /dev/null +++ b/docs/custom-fields.md @@ -0,0 +1,33 @@ +# Data fields + +There are a couple of options to be aware of when syncing data to CiviCRM. This plugin adds two Special Field Types and integrates the Select Bulk/Insert to Custom Fields created in CiviCRM + +## Special fields + +When adding new fields to your Caldera form there are two new options available in the Special field type: + +* CiviCRM Country +* CiviCRM State/Provice + +![Caldera CiviCRM Special Fields Types](./images/caldera-civicrm-special-fields.jpg) + +Similar to using a Profile form in CiviCRM, the Country field should be place prior to the State/Provide field so the correct options are displayed. + +The default Country and State/Province used in the field will be taken from the settings in **Administer > Localization > Languages, Currency, Locations** + +## Custom Data with Select fields + +You can create custom fields in CiviCRM that are: + +* Dropdown Select +* Checkbox +* Radio +* Date Picker + +And have then select those options when you setup your Caldera Form. Start by adding a new field to the form and giving it a name. Then scroll down to the Bulk Insert/Preset option and select the field creating in CiviCRM you want to use and click Insert Options. + +![Bulk Insert/Preset](./images/caldera-select-bulk-insert.png) + +When using the Date Picker you must use the same settings that are in **Administer > Localization > Date Formats** in the **Date Input Fields** section. And adjusted by customized in **Administer > Customize Data and Screens > Date Preferences** + +![Date Picker Field Format](./images/caldera-civicrm-date-format.jpg) diff --git a/docs/dev/contribute.md b/docs/dev/contribute.md new file mode 100644 index 0000000..1333ed7 --- /dev/null +++ b/docs/dev/contribute.md @@ -0,0 +1 @@ +TODO diff --git a/docs/images/caldera-civicrm-date-format.jpg b/docs/images/caldera-civicrm-date-format.jpg new file mode 100644 index 0000000..2aae315 Binary files /dev/null and b/docs/images/caldera-civicrm-date-format.jpg differ diff --git a/docs/images/caldera-civicrm-special-fields.jpg b/docs/images/caldera-civicrm-special-fields.jpg new file mode 100644 index 0000000..2138e49 Binary files /dev/null and b/docs/images/caldera-civicrm-special-fields.jpg differ diff --git a/docs/images/caldera-contact-checked-selections.jpg b/docs/images/caldera-contact-checked-selections.jpg new file mode 100644 index 0000000..8bab247 Binary files /dev/null and b/docs/images/caldera-contact-checked-selections.jpg differ diff --git a/docs/images/caldera-contact-processor.jpg b/docs/images/caldera-contact-processor.jpg new file mode 100644 index 0000000..0a8eede Binary files /dev/null and b/docs/images/caldera-contact-processor.jpg differ diff --git a/docs/images/caldera-field-display-condition.jpg b/docs/images/caldera-field-display-condition.jpg new file mode 100644 index 0000000..c5e932a Binary files /dev/null and b/docs/images/caldera-field-display-condition.jpg differ diff --git a/docs/images/caldera-forms-civicrm-processors.jpg b/docs/images/caldera-forms-civicrm-processors.jpg new file mode 100644 index 0000000..378420b Binary files /dev/null and b/docs/images/caldera-forms-civicrm-processors.jpg differ diff --git a/docs/images/caldera-select-bulk-insert.png b/docs/images/caldera-select-bulk-insert.png new file mode 100644 index 0000000..fc60879 Binary files /dev/null and b/docs/images/caldera-select-bulk-insert.png differ diff --git a/docs/images/caldera-use-processor-condition.jpg b/docs/images/caldera-use-processor-condition.jpg new file mode 100644 index 0000000..e218864 Binary files /dev/null and b/docs/images/caldera-use-processor-condition.jpg differ diff --git a/docs/images/caldera-variables-receive.jpg b/docs/images/caldera-variables-receive.jpg new file mode 100644 index 0000000..9fd3c22 Binary files /dev/null and b/docs/images/caldera-variables-receive.jpg differ diff --git a/docs/images/caldera-variables-set.jpg b/docs/images/caldera-variables-set.jpg new file mode 100644 index 0000000..bf5dba1 Binary files /dev/null and b/docs/images/caldera-variables-set.jpg differ diff --git a/docs/images/civicrm-activity-processor.jpg b/docs/images/civicrm-activity-processor.jpg new file mode 100644 index 0000000..5fd0c1b Binary files /dev/null and b/docs/images/civicrm-activity-processor.jpg differ diff --git a/docs/images/civicrm-address-processor.jpg b/docs/images/civicrm-address-processor.jpg new file mode 100644 index 0000000..aa8daea Binary files /dev/null and b/docs/images/civicrm-address-processor.jpg differ diff --git a/docs/images/civicrm-email-processor.jpg b/docs/images/civicrm-email-processor.jpg new file mode 100644 index 0000000..d0e9734 Binary files /dev/null and b/docs/images/civicrm-email-processor.jpg differ diff --git a/docs/images/civicrm-group-processor.jpg b/docs/images/civicrm-group-processor.jpg new file mode 100644 index 0000000..8cdea26 Binary files /dev/null and b/docs/images/civicrm-group-processor.jpg differ diff --git a/docs/images/civicrm-im-processor.jpg b/docs/images/civicrm-im-processor.jpg new file mode 100644 index 0000000..0d3b2f1 Binary files /dev/null and b/docs/images/civicrm-im-processor.jpg differ diff --git a/docs/images/civicrm-note-processor.jpg b/docs/images/civicrm-note-processor.jpg new file mode 100644 index 0000000..1213cbc Binary files /dev/null and b/docs/images/civicrm-note-processor.jpg differ diff --git a/docs/images/civicrm-phone-processor.jpg b/docs/images/civicrm-phone-processor.jpg new file mode 100644 index 0000000..3dc2b7a Binary files /dev/null and b/docs/images/civicrm-phone-processor.jpg differ diff --git a/docs/images/civicrm-relationship-processor.jpg b/docs/images/civicrm-relationship-processor.jpg new file mode 100644 index 0000000..763264b Binary files /dev/null and b/docs/images/civicrm-relationship-processor.jpg differ diff --git a/docs/images/civicrm-tag-processor.jpg b/docs/images/civicrm-tag-processor.jpg new file mode 100644 index 0000000..8e2b49c Binary files /dev/null and b/docs/images/civicrm-tag-processor.jpg differ diff --git a/docs/images/civicrm-website-processor.jpg b/docs/images/civicrm-website-processor.jpg new file mode 100644 index 0000000..dc7cf7e Binary files /dev/null and b/docs/images/civicrm-website-processor.jpg differ diff --git a/docs/images/contact-processor-magic-tags.jpg b/docs/images/contact-processor-magic-tags.jpg new file mode 100644 index 0000000..fb0d35b Binary files /dev/null and b/docs/images/contact-processor-magic-tags.jpg differ diff --git a/docs/index.md b/docs/index.md new file mode 120000 index 0000000..32d46ee --- /dev/null +++ b/docs/index.md @@ -0,0 +1 @@ +../README.md \ No newline at end of file diff --git a/docs/overview.md b/docs/overview.md new file mode 100644 index 0000000..fbb0311 --- /dev/null +++ b/docs/overview.md @@ -0,0 +1,47 @@ +### Caldera Forms Overview + +Caldera Forms uses Processors to extend what can be done with forms. By installing and activating the Caldera Forms CiviCRM plugin new processors are added to allow information from forms to be added or updated in CiviCRM. + +![Caldera Forms CiviCRM Processors](./images/caldera-forms-civicrm-processors.jpg) + +Select and configure the processors needed for each form to send the wanted information to their CiviCRM contact record. + +_To get acquainted with Caldera Forms check out their [getting started guide](https://calderaforms.com/getting-started/ "Caldera Forms Getting Started Guide")._ + +#### Settings + +Settings are outlined in more detail in each processor see [CiviCRM Processors](./processors.md) + +It is important to also note that processors initiate in order from top to bottom as listed underneath the “Add Processor” button and that you can drag-and-drop the order of the processors in the list. + +#### Magic Tags + +Magic Tags are what are used to map or sync the data fields between Caldera Forms and CiviCRM. Every field in a form has a magic tag formed with the field’s slug, which is similar the field name all lower case and with _ for the spaces. That is used with % on each side to use the magic tag. For example, if you have a Email Address field in your form with the slug `email_address` and magic tag `%email_address%`. + +![Caldera Forms Magic Tags](./images/contact-processor-magic-tags.jpg) + +_For more information about this read [Using Magic Tags In Caldera Forms](https://calderaforms.com/doc/using-magic-tags-caldera-forms/ "Using Magic Tags In Caldera Forms") documentation._ + +#### Conditions + +Fields in a form can be displayed based on conditions set. This is managed in the Conditions menu tab of that form. + +![Caldera Forms Field Display Conditions](./images/caldera-field-display-condition.jpg) + +Conditions are used to manage the logic used for each processor. Without adding any settings, it runs the processor each time the form is submitted. If all fields are required, no Conditions need to be set. If some fields are not required, use the Conditions to set when that processor should be used. Not doing this will cause the form to not submit. For example if you require first name, last name and email, but not phone number you would use a separate processor with a condition. + +![Caldera Forms Use Processor Conditions](./images/caldera-use-processor-condition.jpg) + +#### Variables + +Variables are dynamic form values that have use in either other areas of your form or in different parts of your website. Use a static variable to hold data that you might need to use as a magic tag in some other part of the Caldera Forms Builder. Use a passback variable to send data to the next HTTP request as query variables when using the redirect proccesor. For example you can send fields from one form to another. + +The variables are set in the first form simiarl to this: + +![Caldera Variables Set](./images/caldera-variables-set.jpg) + +And received in the second form using hidden fields like this: + +![Caldera Variables Get](./images/caldera-variables-receive.jpg) + +_For more information about this read [Getting Started: Using Form Variables](https://calderaforms.com/doc/using-form-variables/ "Getting Started: Using Form Variables") documentation._ diff --git a/docs/processors.md b/docs/processors.md new file mode 100644 index 0000000..34739e1 --- /dev/null +++ b/docs/processors.md @@ -0,0 +1,79 @@ +# CiviCRM Processors + +The following Processors are added when the CiviCRM Caldera Forms plugin is activated. Multiple processors of each type can be used to accomplish for each Caldera form. + +### Contact entity + +This processor is the one that needs to be used before others can be used, as it defines that contact the information is being applied to in CiviCRM. Rquired fields for using this processor are Contact Type and Dedupe Rule. + +![CiviCRM Contact Processor](./images/caldera-contact-processor.jpg) + +Magic Tags are used to map the fields created in the Caldera form with fields in CiviCRM, see [overview of Caldera Forms](./overview.md) for more detail on Magic Tags. + +![Caldera Forms Magic Tags](./images/contact-processor-magic-tags.jpg) + +The contact processor has some of the processors outlined below built in, so that more information about the contact can be mapped in single processor. By checking more options, need selections appear. + +![CiviCRM Contact additional Checked Processors](./images/caldera-contact-checked-selections.jpg) + +However, keep in mind that if some of the fields are not required separate processors may need to be used with condition set, see [overview of Caldera Forms](./overview.md). + +### Activity entity + +The Activity Processor provides the ability to record an activity for one more contacts on form submission. The activities available are all the activities available in CiviCRM and custom fields for those Activity Types can also be processed. + +![CiviCRM Activity Processor](./images/civicrm-activity-processor.jpg) + +### Relationship entity + +The Relationship processor provides a way to assign a relationship to two contacts on form submission. The Relationship Types are selected from the ones avaialbe in your CiviCRM instance. + +![CiviCRM Relationships Processor](./images/civicrm-relationship-processor.jpg) + +### Group entity + +The Group processor allows for a contact to be added to a group on form submission. + +![CiviCRM Group Processor](./images/civicrm-group-processor.jpg) + +### Tag entity + +The Tag processor allows for a tag to be added to a contact on on form submission. + +![CiviCRM Tag Processor](./images/civicrm-tag-processor.jpg) + +### Address entity + +The Address processor provides the ability for a specific address type to be added or update for that contact in CiviCRM. + +![CiviCRM Address Processor](./images/civicrm-address-processor.jpg) + +### Email entity + +The Email processor provides the ability for a specific email address type to be added or update for that contact in CiviCRM. + +![CiviCRM Email Processor](./images/civicrm-email-processor.jpg) + +### Phone entity + +The Phone processor provides the ability for a specific phone number and type to be added or update for that contact in CiviCRM. + +![CiviCRM Phone Processor](./images/civicrm-phone-processor.jpg) + +### Note entity + +The Note processor allows for a subject and note to be added to a contact on on form submission. + +![CiviCRM Note Processor](./images/civicrm-note-processor.jpg) + +### Website entity + +The Website processor provides the ability for a specific URL and type to be added or update for that contact in CiviCRM. + +![CiviCRM Website Processor](./images/civicrm-website-processor.jpg) + +### IM (Instant Messenger) entity + +The IM processor provides the ability for a specific IM and type to be added or update for that contact in CiviCRM. + +![CiviCRM IM (Instant Messenger) Processor](./images/civicrm-im-processor.jpg) diff --git a/fields/civicrm_state/field.php b/fields/civicrm_state/field.php index 84cd62f..0a262ae 100644 --- a/fields/civicrm_state/field.php +++ b/fields/civicrm_state/field.php @@ -32,7 +32,7 @@ var id = jQuery(this).val(); var options = jQuery(this).data( 'options' ).filter( '[data-crm-country-id="' + id + '"]' ); cfStates.html( options ); - }); + }).trigger('change'); } }); diff --git a/includes/class-civicrm-caldera-forms-forms.php b/includes/class-civicrm-caldera-forms-forms.php new file mode 100644 index 0000000..31f0111 --- /dev/null +++ b/includes/class-civicrm-caldera-forms-forms.php @@ -0,0 +1,61 @@ +register_hooks(); + } + + /** + * Register hooks + * + * @since 0.4 + */ + public function register_hooks() { + + // reorder processors on save form + add_filter( 'caldera_forms_presave_form', array( $this, 'reorder_contact_processors' ), 20 ); + + } + + /** + * Reorder Contact processors, fires when a form is saved + * + * @uses 'caldera_forms_presave_form' filter + * @since 0.4 + * + * @param array $form The form + * @return array $form The modified form + */ + public function reorder_contact_processors( $form ) { + + $contact_processors = $rest_processors = array(); + foreach ( $form['processors'] as $pId => $processor ) { + if( $processor['type'] == 'civicrm_contact' ){ + $contact_processors[$pId] = $processor; + } + if( $processor['type'] != 'civicrm_contact' ){ + $rest_processors[$pId] = $processor; + } + } + + // Sort Contact processors based on Contact Link + uasort( $contact_processors, function( $a, $b ){ + return $a['config']['contact_link'] - $b['config']['contact_link']; + }); + + $form['processors'] = array_merge( $contact_processors, $rest_processors); + + return $form; + } +} diff --git a/includes/class-civicrm-caldera-forms-helper.php b/includes/class-civicrm-caldera-forms-helper.php index d35c3e3..93d81be 100644 --- a/includes/class-civicrm-caldera-forms-helper.php +++ b/includes/class-civicrm-caldera-forms-helper.php @@ -142,10 +142,13 @@ public static function get_contact_custom_fields() { */ public static function get_wp_civi_contact( $id ) { - $wp_civicrm_contact = civicrm_api3( 'UFMatch', 'getsingle', array( + $params = array( 'sequential' => 1, 'uf_id' => $id, - )); + 'domain_id' => CRM_Core_BAO_Domain::getDomain()->id, + ); + + $wp_civicrm_contact = civicrm_api3( 'UFMatch', 'getsingle', $params ); return $wp_civicrm_contact['contact_id']; } @@ -329,4 +332,79 @@ public static function custom_field_extends( $custom_id ) { } + /** + * Helper method to map fields values to processor + * + * @since 0.4 + * + * @param array $config The processor settings + * @param array $form The form settings + * @param array $form_values The submitted form values + * @param string $processor The processor key, only necessary for the Contact processor class + * @return array $form_values + */ + public static function map_fields_to_processor( $config, $form, &$form_values, $processor = null ){ + + foreach ( ( $processor ? $config[$processor] : $config ) as $key => $field_id ) { + if ( ! empty( $field_id ) ) { + + // If we have a bracket magic tag, do bracket magic tag + if ( strpos( $field_id, '{' ) !== false ) { + $mapped_field = Caldera_Forms_Magic_Doer::do_bracket_magic( $field_id, $form, NULL, NULL, NULL ); + } else { + + // Get field by ID or slug + $mapped_field = + Caldera_Forms_Field_Util::get_field( $field_id, $form ) ? + Caldera_Forms_Field_Util::get_field( $field_id, $form ) : + Caldera_Forms::get_field_by_slug(str_replace( '%', '', $field_id ), $form ); + + // Get field data + $mapped_field = Caldera_Forms::get_field_data( $mapped_field['ID'], $form ); + } + + if( ! empty( $mapped_field ) ){ + + if ( $processor ) { + $form_values[$processor][$key] = $mapped_field; + } else { + $form_values[$key] = $mapped_field; + } + } + } + } + + return $form_values; + } + + /** + * Helper method to map CiviCRM data to form fields (autopopulate/prerender) + * + * @since 0.4 + * + * @param array $config The processor settings + * @param array $form The form settings + * @param array $ignore_fields The fields to be ignored during data mapping + * @param string $processor The processor key, only necessary for the Contact processor class + * @return array $form The form settings + */ + public static function map_fields_to_prerender( $config, &$form, $ignore_fields, $entity, $processor = null ){ + + foreach ( ( $processor ? $config[$processor] : $config ) as $field => $value ) { + if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { + + // Get field by ID or slug + $mapped_field = + Caldera_Forms_Field_Util::get_field( $value, $form ) ? + Caldera_Forms_Field_Util::get_field( $value, $form ) : + Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); + + // Set default value + $form['fields'][$mapped_field['ID']]['config']['default'] = $entity[$field]; + } + } + + return $form; + } + } diff --git a/mkdocs.yaml b/mkdocs.yaml new file mode 100644 index 0000000..a9a97b0 --- /dev/null +++ b/mkdocs.yaml @@ -0,0 +1,11 @@ +site_name: Caldera Forms CiviCRM +repo_url: https://github.com/mecachisenros/caldera-forms-civicrm +theme: material +markdown_extensions: +pages: +- Introduction: index.md +- Overview: overview.md +- Procesors: processors.md +- Custom Fields: custom-fields.md +- Developer docs: + - Contribute: dev/contribute.md diff --git a/processors/activity/class-activity-processor.php b/processors/activity/class-activity-processor.php index de7d132..19d2b42 100644 --- a/processors/activity/class-activity-processor.php +++ b/processors/activity/class-activity-processor.php @@ -65,15 +65,8 @@ public function processor( $config, $form ) { // globalised transient object global $transdata; - // Get form values for each processor field - // $value is the field id - $form_values = array(); - foreach( $config as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values[$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values ); if( ! empty( $form_values ) ) { $form_values['source_contact_id'] = $transdata['civicrm']['contact_id_'.$config['contact_link']]; // Contact ID set in Contact Processor diff --git a/processors/address/class-address-processor.php b/processors/address/class-address-processor.php index 203520b..e57cd0d 100644 --- a/processors/address/class-address-processor.php +++ b/processors/address/class-address-processor.php @@ -16,6 +16,15 @@ class CiviCRM_Caldera_Forms_Address_Processor { */ public $key_name = 'civicrm_address'; + /** + * Fields to ignore while prepopulating + * + * @since 0.4 + * @access public + * @var array $fields_to_ignore Fields to ignore + */ + public $fields_to_ignore = array( 'contact_link', 'location_type_id' ); + /** * Initialises this object. * @@ -79,15 +88,8 @@ public function processor( $config, $form ) { // Ignore if none found } - // Get form values for each processor field - // $value is the field id - $form_values = array(); - foreach( $config as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values[$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values ); if( ! empty( $form_values ) ) { $form_values['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -140,14 +142,13 @@ public function pre_render( $form ){ } } - unset( $pr_id['config']['contact_link'], $pr_id['config']['location_type_id'] ); - if ( isset( $civi_contact_address ) && ! isset( $civi_contact_address['count'] ) ) { - foreach ( $pr_id['config'] as $field => $value ) { - if ( ! empty( $value ) ) { - $form['fields'][$value]['config']['default'] = $civi_contact_address[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $this->fields_to_ignore, + $civi_contact_address + ); } // Clear Address data diff --git a/processors/contact/class-contact-processor.php b/processors/contact/class-contact-processor.php index 8f66215..8b64be4 100644 --- a/processors/contact/class-contact-processor.php +++ b/processors/contact/class-contact-processor.php @@ -25,6 +25,15 @@ class CiviCRM_Caldera_Forms_Contact_Processor { */ private $entities_to_prerender = array( 'process_address', 'process_phone', 'process_email', 'process_website', 'process_im' ); + /** + * Fields to ignore while prepopulating + * + * @since 0.4 + * @access public + * @var array $fields_to_ignore Fields to ignore + */ + public $fields_to_ignore = array( 'auto_pop', 'contact_type', 'contact_sub_type', 'contact_link', 'dedupe_rule', 'location_type_id', 'website_type_id' ); + /** * Initialises this object. * @@ -76,22 +85,8 @@ public function processor( $config, $form ) { // globalised transient object global $transdata; - // Get form values for each processor field - // $value is the field id - $form_values = array(); - foreach ( $config['civicrm_contact'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - - // If field is checkbox convert submitted string to array - $field_conf = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $field_id ), $form ); - if( $field_conf['type'] == 'checkbox' ){ - $mapped_field = explode( ', ', $mapped_field ); - } - - $form_values['civicrm_contact'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_contact' ); if ( ! empty( $form_values['civicrm_contact'] ) ) { @@ -138,12 +133,34 @@ public function processor( $config, $form ) { // Pass contact id if found $form_values['civicrm_contact']['contact_id'] = $ids ? $ids[0] : 0; + // Prevent API overriding exisiting contact_sub_type + // If we have a contact_id, get the contact and push the sub-type set in Contact config + if( $form_values['civicrm_contact']['contact_id'] ){ + $existing_contact = CiviCRM_Caldera_Forms_Helper::get_civi_contact( $form_values['civicrm_contact']['contact_id'] ); + if ( is_array( $existing_contact['contact_sub_type'] ) ) { + if ( ! empty( $config['civicrm_contact']['contact_sub_type'] ) ) { + array_push( $existing_contact['contact_sub_type'], $config['civicrm_contact']['contact_sub_type'] ); + } + $form_values['civicrm_contact']['contact_sub_type'] = $existing_contact['contact_sub_type']; + } + } + $create_contact = civicrm_api3( 'Contact', 'create', $form_values['civicrm_contact'] ); // Store $cid CiviCRM_Caldera_Forms_Helper::set_civi_transdata( $config['contact_link'], $create_contact['id'] ); $transdata['civicrm'] = CiviCRM_Caldera_Forms_Helper::get_civi_transdata(); + // Add contact to Domain group if set, if not set 'domain_group_id' should be 0 + $domain_group_id = CiviCRM_Caldera_Forms_Helper::get_civicrm_settings( 'domain_group_id' ); + if( $domain_group_id ){ + $group_contact = civicrm_api3( 'GroupContact', 'create', array( + 'sequential' => 1, + 'group_id' => $domain_group_id, + 'contact_id' => $create_contact['id'], + )); + } + /** * Process enabled entities. * @since 0.3 @@ -169,7 +186,7 @@ public function processor( $config, $form ) { * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_address( $config, $form, $transdata, $form_values ){ + public function process_address( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { @@ -183,14 +200,8 @@ public function process_address( $config, $form, $transdata, $form_values ){ // Ignore if none found } - // Get form values for each processor field - // $value is the field id - foreach ( $config['civicrm_address'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values['civicrm_address'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_address' ); if( ! empty( $form_values['civicrm_address'] ) ) { $form_values['civicrm_address']['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -220,7 +231,7 @@ public function process_address( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_phone( $config, $form, $transdata, $form_values ){ + public function process_phone( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { @@ -236,14 +247,8 @@ public function process_phone( $config, $form, $transdata, $form_values ){ // Ignore if none found } - // Get form values for each processor field - // $value is the field id - foreach ( $config['civicrm_phone'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values['civicrm_phone'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_phone' ); if( ! empty( $form_values['civicrm_phone'] ) ) { $form_values['civicrm_phone']['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -269,16 +274,12 @@ public function process_phone( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_note( $config, $form, $transdata, $form_values ){ + public function process_note( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { - foreach ( $config['civicrm_note'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values['civicrm_note'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_note' ); if( ! empty( $form_values['civicrm_note'] ) ) { $form_values['civicrm_note']['entity_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -298,7 +299,7 @@ public function process_note( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_email( $config, $form, $transdata, $form_values ){ + public function process_email( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { @@ -314,14 +315,8 @@ public function process_email( $config, $form, $transdata, $form_values ){ // Ignore if none found } - // Get form values for each processor field - // $value is the field id - foreach ( $config['civicrm_email'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values['civicrm_email'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_email' ); if ( ! empty( $form_values['civicrm_email'] ) ) { $form_values['civicrm_email']['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -347,7 +342,7 @@ public function process_email( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_website( $config, $form, $transdata, $form_values ){ + public function process_website( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { @@ -363,14 +358,8 @@ public function process_website( $config, $form, $transdata, $form_values ){ // Ignore if none found } - // Get form values for each processor field - // $value is the field id - foreach ( $config['civicrm_website'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if( ! empty( $mapped_field ) ){ - $form_values['civicrm_website'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_website' ); if( ! empty( $form_values['civicrm_website'] ) ) { $form_values['civicrm_website']['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -396,7 +385,7 @@ public function process_website( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_im( $config, $form, $transdata, $form_values ){ + public function process_im( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { @@ -412,14 +401,9 @@ public function process_im( $config, $form, $transdata, $form_values ){ // Ignore if none found } - // Get form values for each processor field - // $value is the field id - foreach ( $config['civicrm_im'] as $key => $field_id ) { - $mapped_field = Caldera_Forms::get_field_data( $field_id, $form ); - if ( ! empty( $mapped_field ) ){ - $form_values['civicrm_im'][$key] = $mapped_field; - } - } + // Get form values + $form_values = CiviCRM_Caldera_Forms_Helper::map_fields_to_processor( $config, $form, $form_values, 'civicrm_im' ); + if( ! empty( $form_values['civicrm_im'] ) ){ $form_values['civicrm_im']['contact_id'] = $transdata['civicrm']['contact_id_' . $config['contact_link']]; // Contact ID set in Contact Processor @@ -444,7 +428,7 @@ public function process_im( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_group( $config, $form, $transdata, $form_values ){ + public function process_group( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { $result = civicrm_api3( 'GroupContact', 'create', array( @@ -465,7 +449,7 @@ public function process_group( $config, $form, $transdata, $form_values ){ * @param array $transdata The globalised transient object * @param array $form_values The field values beeing submitted */ - public function process_tag( $config, $form, $transdata, $form_values ){ + public function process_tag( $config, $form, $transdata, &$form_values ){ if ( ! empty( $transdata['civicrm']['contact_id_' . $config['contact_link']] ) ) { foreach ( $config['civicrm_tag'] as $key => $value ) { @@ -556,20 +540,18 @@ public function pre_render( $form ){ } - // Fields to ignore when populating/mapping Civi data to form fields - $ignore_fields = array( 'auto_pop', 'contact_type', 'contact_sub_type', 'contact_link', 'dedupe_rule', 'location_type_id', 'website_type_id' ); - // Map CiviCRM contact data to form defaults if ( isset( $civi_contact ) && $civi_contact != 0 ) { CiviCRM_Caldera_Forms_Helper::set_civi_transdata( $pr_id['config']['contact_link'], $civi_contact['contact_id'] ); $transdata['civicrm'] = CiviCRM_Caldera_Forms_Helper::get_civi_transdata(); - foreach ( $pr_id['config']['civicrm_contact'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $this->fields_to_ignore, + $civi_contact, + 'civicrm_contact' + ); } // Clear Contact data @@ -580,7 +562,7 @@ public function pre_render( $form ){ foreach ( $pr_id['config']['enabled_entities'] as $entity => $value) { if( isset( $entity ) && in_array( $entity, $this->entities_to_prerender ) ){ $pre_render_entity = str_replace( 'process_', 'pre_render_', $entity ); - $this->$pre_render_entity( $pr_id, $transdata, $form, $ignore_fields ); + $this->$pre_render_entity( $pr_id, $transdata, $form, $this->fields_to_ignore ); } } } @@ -619,12 +601,13 @@ public function pre_render_address( $pr_id, $transdata, &$form, $ignore_fields ) } if ( isset( $civi_contact_address ) && ! isset( $civi_contact_address['count'] ) ) { - foreach ( $pr_id['config']['civicrm_address'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact_address[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $ignore_fields, + $civi_contact_address, + 'civicrm_address' + ); } // Clear Address data @@ -661,12 +644,13 @@ public function pre_render_phone( $pr_id, $transdata, &$form, $ignore_fields ){ } if ( isset( $civi_contact_phone ) && ! isset( $civi_contact_phone['count'] ) ) { - foreach ( $pr_id['config']['civicrm_phone'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact_phone[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $ignore_fields, + $civi_contact_phone, + 'civicrm_phone' + ); } // Clear Phone data @@ -702,12 +686,13 @@ public function pre_render_email( $pr_id, $transdata, &$form, $ignore_fields ){ } if ( isset( $civi_contact_email ) && ! isset( $civi_contact_email['count'] ) ) { - foreach ( $pr_id['config']['civicrm_email'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact_email[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $ignore_fields, + $civi_contact_email, + 'civicrm_email' + ); } // Clear Email data @@ -743,12 +728,13 @@ public function pre_render_website( $pr_id, $transdata, &$form, $ignore_fields ) } if ( isset( $civi_contact_website ) && ! isset( $civi_contact_website['count'] ) ) { - foreach ( $pr_id['config']['civicrm_website'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact_website[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $ignore_fields, + $civi_contact_website, + 'civicrm_website' + ); } // Clear Website data @@ -784,12 +770,13 @@ public function pre_render_im( $pr_id, $transdata, &$form, $ignore_fields ){ } if ( isset( $civi_contact_im ) && ! isset( $civi_contact_im['count'] ) ) { - foreach ( $pr_id['config']['civicrm_im'] as $field => $value ) { - if ( ! empty( $value ) && ! in_array( $field, $ignore_fields ) ) { - $mapped_field = Caldera_Forms::get_field_by_slug(str_replace( '%', '', $value ), $form ); - $form['fields'][$mapped_field['ID']]['config']['default'] = $civi_contact_im[$field]; - } - } + $form = CiviCRM_Caldera_Forms_Helper::map_fields_to_prerender( + $pr_id['config'], + $form, + $ignore_fields, + $civi_contact_im, + 'civicrm_im' + ); } // Clear Im data diff --git a/processors/contact/contact_config.php b/processors/contact/contact_config.php index 9ab4889..9718d4f 100644 --- a/processors/contact/contact_config.php +++ b/processors/contact/contact_config.php @@ -109,7 +109,7 @@