From 47a78c93171b267e920ce1b4cdd11fa6ee9e6a54 Mon Sep 17 00:00:00 2001 From: Kirk Jackson Date: Wed, 29 Jul 2020 16:14:09 +0100 Subject: [PATCH] Add relationship start and end dates to the relationship processor --- .../class-relationship-processor.php | 36 ++++++++++--------- .../relationship/relationship_config.php | 18 ++++++++++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/processors/relationship/class-relationship-processor.php b/processors/relationship/class-relationship-processor.php index 5c2aa81..438f026 100644 --- a/processors/relationship/class-relationship-processor.php +++ b/processors/relationship/class-relationship-processor.php @@ -72,27 +72,31 @@ public function pre_processor( $config, $form ) { // cfc transient object $transient = $this->plugin->transient->get(); - $relationship = civicrm_api3( 'Relationship', 'get', [ - 'sequential' => 1, + $identifying_params = [ 'contact_id_a' => $transient->contacts->{'cid_'.$config['contact_a']}, 'contact_id_b' => $transient->contacts->{'cid_'.$config['contact_b']}, 'relationship_type_id' => $config['relationship_type'], - ] ); + ]; - if ( $relationship['count'] ) { - return; + $relationship = civicrm_api3( 'Relationship', 'get', [ 'sequential' => 1 ] + $identifying_params ); + + // Get form values + $form_values = []; // Must always be an array, or adding another array will fail. + $form_values = $this->plugin->helper->map_fields_to_processor( $config, $form, $form_values ); + + if ( $relationship['count'] === 0 ) { + $form_values += $identifying_params; + } elseif ( $relationship['count'] === 1 ) { + $form_values['id'] = $relationship['values'][0]['id']; // Relationship ID } else { - try { - $create_relationship = civicrm_api3( 'Relationship', 'create', [ - 'sequential' => 1, - 'contact_id_a' => $transient->contacts->{'cid_'.$config['contact_a']}, - 'contact_id_b' => $transient->contacts->{'cid_'.$config['contact_b']}, - 'relationship_type_id' => $config['relationship_type'], - ] ); - } catch ( CiviCRM_API3_Exception $e ) { - $error = $e->getMessage() . '

' . $e->getTraceAsString() . '
'; - return [ 'note' => $error, 'type' => 'error' ]; - } + return; + } + + try { + $create_relationship = civicrm_api3( 'Relationship', 'create', $form_values ); + } catch ( CiviCRM_API3_Exception $e ) { + $error = $e->getMessage() . '

' . $e->getTraceAsString() . '
'; + return [ 'note' => $error, 'type' => 'error' ]; } } diff --git a/processors/relationship/relationship_config.php b/processors/relationship/relationship_config.php index 24ae631..3f384ea 100644 --- a/processors/relationship/relationship_config.php +++ b/processors/relationship/relationship_config.php @@ -6,6 +6,10 @@ 'options' => [ 'limit' => 0 ], ] ); +$relationshipFields = civicrm_api3( 'Relationship', 'getfields', [] ); + +$fields = [ 'start_date', 'end_date' ]; + ?>
@@ -54,3 +58,17 @@
+ + +
+ +
+ {{{_field slug=""}}} +
+
+