diff --git a/CRM/I3val/ActivityHandler.php b/CRM/I3val/ActivityHandler.php index b966275..12f41b4 100644 --- a/CRM/I3val/ActivityHandler.php +++ b/CRM/I3val/ActivityHandler.php @@ -370,4 +370,44 @@ protected function getOptionValues($option_group, $indexed_by = 'value') { } return self::$_option_values[$option_group]; } + + /** + * Propegate Contact Reference data based on a Contact Reference Display Name + */ + protected function resolveContactReferenceField(&$data, $fieldname, $id_fieldname) { + if (!empty($data[$fieldname])) { + $contact = $this->getContactReference($data[$fieldname]); + + if (!empty($contact)) { + $data[$id_fieldname] = $contact['id']; + $data[$fieldname] = $contact['display_name']; + } + } + } + + /** + * Retrieve contact details for a passed Contact ID or Display Name + */ + protected function getContactReference($contact_value) { + $contact = []; + + try { + $contact = \Civi\Api4\Contact::get(FALSE) + ->addSelect('id', 'display_name'); + if (is_numeric($contact_value)) { + $contact->addWhere('id', '=', $contact_value); + } + else { + $contact->addWhere('display_name', '=', $contact_value); + } + $contact = $contact->execute() + ->first(); + + } catch (Exception $e) { + Civi::log()->warning('I3val: Contact retrieve contact information for '.var_export($data[$fieldname],true).' with '.$e->getMessage()); + } + + return $contact; + } + } diff --git a/CRM/I3val/Converter.php b/CRM/I3val/Converter.php index 7a7a418..77c4739 100644 --- a/CRM/I3val/Converter.php +++ b/CRM/I3val/Converter.php @@ -36,6 +36,7 @@ protected function getParameterMapping() { ts('Last Name') => 'i3val_contact_updates.last_name', 'organization_name' => 'i3val_contact_updates.organization_name', 'gender_id' => 'i3val_contact_updates.gender', + 'employer_id' => 'i3val_contact_updates.employer', ts('City') => 'i3val_address_updates.city', ts('Street Address') => 'i3val_address_updates.street_address', ts('Postal Code') => 'i3val_address_updates.postal_code', diff --git a/CRM/I3val/Handler/ContactUpdate.php b/CRM/I3val/Handler/ContactUpdate.php index e72981b..75eb799 100644 --- a/CRM/I3val/Handler/ContactUpdate.php +++ b/CRM/I3val/Handler/ContactUpdate.php @@ -35,6 +35,7 @@ public function getField2Label() { 'household_name' => E::ts('Household Name'), 'preferred_language' => E::ts('Preferred Language'), 'job_title' => E::ts('Job Title'), + 'employer' => E::ts('Current Employer'), 'prefix' => E::ts('Prefix'), 'suffix' => E::ts('Suffix'), 'gender' => E::ts('Gender'), @@ -136,6 +137,7 @@ public function applyChanges($activity, $values, $objects = array()) { $contact_update['id'] = $contact['id']; $this->resolveFields($contact_update); $this->resolvePreferredLanguageToLabel($contact_update, FALSE); + CRM_I3val_Session::log("UPDATE contact " . json_encode($contact_update)); civicrm_api3('Contact', 'create', $contact_update); } @@ -161,6 +163,9 @@ public function renderActivityData($activity, $form) { if (isset($values['suffix']) && !empty($form->contact['individual_suffix'])) { $values['suffix']['current'] = $form->contact['individual_suffix']; } + if (isset($values['employer']) && !empty($form->contact['current_employer'])) { + $values['employer']['current'] = $form->contact['current_employer']; + } // create input fields and apply checkboxes $active_fields = array(); @@ -183,7 +188,7 @@ public function renderActivityData($activity, $form) { // generate input field if (in_array($fieldname, array('prefix', 'suffix', 'gender'))) { - // add the text input + // add the select input $form->add( 'select', "{$fieldname}_applied", @@ -191,6 +196,19 @@ public function renderActivityData($activity, $form) { $this->getOptionList($fieldname) ); + } elseif ($fieldname == 'employer') { + $submitted_employer = $values[$fieldname]['submitted'] ?? ''; + + // add the select input + $form->add( + 'select', + "{$fieldname}_applied", + $fieldlabel, + $this->getOrganizationsList($submitted_employer), + FALSE, + ['class' => 'crm-select2'] + ); + } elseif ($fieldname == 'birth_date' || $fieldname == 'deceased_date') { $form->addDate( "{$fieldname}_applied", @@ -234,7 +252,14 @@ public function renderActivityData($activity, $form) { ); } - if (!empty($values[$fieldname]['applied'])) { + if ($fieldname == 'employer' && !empty($values[$fieldname]['submitted'])) { + $chosen_employer = $values[$fieldname]['applied'] ?? $values[$fieldname]['submitted']; + $contact = $this->getContactReference($chosen_employer); + + $default_employer = $contact['id'] ?? ''; + $form->setDefaults(array("{$fieldname}_applied" => $default_employer)); + } + elseif (!empty($values[$fieldname]['applied'])) { $form->setDefaults(array("{$fieldname}_applied" => $values[$fieldname]['applied'])); } else { $form->setDefaults(array("{$fieldname}_applied" => isset($values[$fieldname]['submitted']) ? $values[$fieldname]['submitted'] : '')); @@ -312,6 +337,7 @@ protected function resolveFields(&$data, $add_default = FALSE) { $this->resolveOptionValueField($data, 'gender', 'gender', 'gender_id'); $this->resolveOptionValueField($data, 'individual_prefix', 'prefix', 'prefix_id'); $this->resolveOptionValueField($data, 'individual_suffix', 'suffix', 'suffix_id'); + $this->resolveContactReferenceField($data, 'employer', 'employer_id'); } @@ -336,6 +362,41 @@ protected function getOptionList($fieldname) { } } + protected function getOrganizationsList($chosen_organization) { + // Launch a hook for custom parameters to be added + $filter_params = []; + + // Invoke a hook to allow other extensions to modify the parameters / filters + CRM_Utils_Hook::singleton()->invoke( + ['filter_params'], + $filter_params, $null, $null, $null, $null, $null, + 'I3val_alter_organizationfilter'); + + // Grab organizations + $select_list = []; + try { + $contacts = \Civi\Api4\Contact::get(FALSE) + ->addSelect('id','display_name') + ->addWhere('contact_type', '=', 'Organization'); + foreach ($filter_params as $id => $field) { + if (!empty($field['field']) && !empty($field['condition']) && !empty($field['value'])) { + $contacts->addWhere($field['field'], $field['condition'], $field['value']); + } + } + $contacts = $contacts->execute(); + + // Build select + foreach ($contacts as $id => $contact) { + $select_list[$contact['id']] = $contact['display_name']; + } + } catch (Exception $e) { + Civi::log()->warning('I3Val: Error occurred in retrieving Organizations with custom params : '.var_export($custom_params,true). ' error : '.$e->getMessage()); + CRM_Core_Session::setStatus(E::ts("There was an issue obtaining the full list of Organizations")); + } + + return $select_list; + } + /** * Since the brilliant preferred_language field has no *_id * counterpart, we are forced to decide whether we want to diff --git a/CRM/I3val/Handler/EmailUpdate.php b/CRM/I3val/Handler/EmailUpdate.php index 8c97a07..087745e 100644 --- a/CRM/I3val/Handler/EmailUpdate.php +++ b/CRM/I3val/Handler/EmailUpdate.php @@ -322,9 +322,9 @@ protected function getExistingEmail($values, &$default_action = NULL) { } // second: find by location type - if (isset($values['location_type_id'])) { + if (isset($email_submitted['location_type_id'])) { foreach ($emails as $email) { - if ($values['location_type_id'] == $email['location_type_id']) { + if ($email_submitted['location_type_id'] == $email['location_type_id']) { $this->resolveFields($email); $default_action = 'update'; return $email; diff --git a/CRM/I3val/Handler/PhoneUpdate.php b/CRM/I3val/Handler/PhoneUpdate.php index 718f098..d9d60d0 100644 --- a/CRM/I3val/Handler/PhoneUpdate.php +++ b/CRM/I3val/Handler/PhoneUpdate.php @@ -357,7 +357,7 @@ protected function getExistingPhone($values, &$default_action = NULL) { // second: find by location type if (isset($values['location_type_id'])) { foreach ($phones as $phone) { - if ($values['location_type_id'] == $phone['location_type_id']) { + if ($phone_submitted['location_type_id'] == $phone['location_type_id']) { $this->resolveFields($phone); $default_action = 'update'; return $phone; diff --git a/resources/contact_updates_custom_group.json b/resources/contact_updates_custom_group.json index c0c9edf..d385d87 100644 --- a/resources/contact_updates_custom_group.json +++ b/resources/contact_updates_custom_group.json @@ -450,6 +450,54 @@ "is_active": "1", "is_view": "1" }, + { + "_lookup": ["column_name", "custom_group_id"], + "_translate": ["label"], + "name": "employer_original", + "column_name": "employer_original", + "label": "Current Employer (original)", + "data_type": "ContactReference", + "html_type": "Autocomplete-Select", + "filter": "action=get&contact_type=Organization", + "text_length": "255", + "is_required": "0", + "is_searchable": "1", + "is_search_range": "1", + "is_active": "1", + "is_view": "1" + }, + { + "_lookup": ["column_name", "custom_group_id"], + "_translate": ["label"], + "name": "employer_submitted", + "column_name": "employer_submitted", + "label": "Current Employer (submitted)", + "data_type": "ContactReference", + "html_type": "Autocomplete-Select", + "filter": "action=get&contact_type=Organization", + "text_length": "255", + "is_required": "0", + "is_searchable": "1", + "is_search_range": "1", + "is_active": "1", + "is_view": "1" + }, + { + "_lookup": ["column_name", "custom_group_id"], + "_translate": ["label"], + "name": "employer_applied", + "column_name": "employer_applied", + "label": "Current Employer (applied)", + "data_type": "ContactReference", + "html_type": "Autocomplete-Select", + "filter": "action=get&contact_type=Organization", + "text_length": "255", + "is_required": "0", + "is_searchable": "1", + "is_search_range": "1", + "is_active": "1", + "is_view": "1" + }, { "_lookup": ["column_name", "custom_group_id"], "_translate": ["label"], @@ -832,4 +880,4 @@ "is_view": "1" } ] -} \ No newline at end of file +}