Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing requested update for Email / Phone based on Location Type not providing the proper action #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CRM/I3val/ActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
1 change: 1 addition & 0 deletions CRM/I3val/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
65 changes: 63 additions & 2 deletions CRM/I3val/Handler/ContactUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -183,14 +188,27 @@ 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",
$fieldlabel,
$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",
Expand Down Expand Up @@ -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'] : ''));
Expand Down Expand Up @@ -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');
}


Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions CRM/I3val/Handler/EmailUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion CRM/I3val/Handler/PhoneUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 49 additions & 1 deletion resources/contact_updates_custom_group.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -832,4 +880,4 @@
"is_view": "1"
}
]
}
}