Commit 8cbc39a7 authored by bgm's avatar bgm Committed by Aegir user
Browse files

Make it possible to select a contact from a drop-drop (Contact ID field,...

Make it possible to select a contact from a drop-drop (Contact ID field, combined with logged-in user)
parent 62da1711
......@@ -65,44 +65,55 @@ class CRM_Activityprofile_APIWrappers_Profile {
// This is a profile with activity fields, but without an activity_id
// Let's create a new contact and a new activity.
// We only do the minimum and let profile.submit do the rest.
$contact_params = [];
$contact_id = NULL;
$ctype = Civi::settings()->get('activityprofile_' . $profile_id . '_ctype');
$csubtype = Civi::settings()->get('activityprofile_' . $profile_id . '_csubtype');
if ($ctype) {
$contact_params['contact_type'] = 'Organization';
$contact_params['organization_name'] = $apiRequest['params']['organization_name'] ?? 'ActivityProfile TempContact';
}
elseif (!empty($apiRequest['params']['organization_name'])) {
$contact_params['contact_type'] = 'Organization';
$contact_params['organization_name'] = $apiRequest['params']['organization_name'];
}
else {
$contact_params['contact_type'] = 'Individual';
$contact_params['first_name'] = $apiRequest['params']['first_name'] ?? 'ActivityProfile';
$contact_params['last_name'] = $apiRequest['params']['last_name'] ?? 'TempContact';
// Check if the contact ID was provided and if we have access
if (!empty($apiRequest['params']['id'])) {
if (CRM_Contact_BAO_Contact_Permission::allow($apiRequest['params']['id'], CRM_Core_Permission::VIEW)) {
$contact_id = $apiRequest['params']['id'];
}
}
if ($csubtype) {
$contact_params['contact_sub_type'] = $csubtype;
// Otherwise check if use is logged-in
if (!$contact_id) {
$contact_id = CRM_Core_Session::singleton()->get('userID');
}
$contact = civicrm_api3('Contact', 'create', $contact_params);
if (!$contact_id) {
$contact_params = [];
$ctype = Civi::settings()->get('activityprofile_' . $profile_id . '_ctype');
$csubtype = Civi::settings()->get('activityprofile_' . $profile_id . '_csubtype');
if ($ctype) {
$contact_params['contact_type'] = 'Organization';
$contact_params['organization_name'] = $apiRequest['params']['organization_name'] ?? 'ActivityProfile TempContact';
}
elseif (!empty($apiRequest['params']['organization_name'])) {
$contact_params['contact_type'] = 'Organization';
$contact_params['organization_name'] = $apiRequest['params']['organization_name'];
}
else {
$contact_params['contact_type'] = 'Individual';
$contact_params['first_name'] = $apiRequest['params']['first_name'] ?? 'ActivityProfile';
$contact_params['last_name'] = $apiRequest['params']['last_name'] ?? 'TempContact';
}
if ($csubtype) {
$contact_params['contact_sub_type'] = $csubtype;
}
$contact = civicrm_api3('Contact', 'create', $contact_params);
$contact_id = $contact['id'];
}
// Setting the contact_id is necessary so that we can pass through
// CRM_Profile_Form::validateContactActivityProfile()
// which is called by civicrm_api3_profile_submit().
$apiRequest['params']['contact_id'] = $contact['id'];
$apiRequest['params']['contact_id'] = $contact_id;
// We default to setting the subject to the profile title,
// if no subject field was included in the profile.
// For new Cases, the case_subject must be copied into the subject field.
if (empty($apiRequest['params']['subject']) && !empty($params['case_subject'])) {
$params['subject'] = $params['case_subject'];
$apiRequest['params']['subject'] = $params['case_subject'];
}
$title = $params['subject'] ?? '';
if (empty($title)) {
......@@ -113,10 +124,14 @@ class CRM_Activityprofile_APIWrappers_Profile {
$atype = Civi::settings()->get('activityprofile_' . $profile_id . '_atype') ?? 1;
// We will link the activity with the logged-in user (source_contact_id), if logged-in
// Otherwise, we have probably created a new contact above.
$source_contact_id = CRM_Core_Session::singleton()->get('userID');
$activity = civicrm_api3('Activity', 'create', [
'activity_type_id' => $atype,
'source_contact_id' => $contact['id'],
'target_contact_id' => $contact['id'],
'source_contact_id' => $source_contact_id ?? $contact_id,
'target_contact_id' => $contact_id,
'subject' => $title,
]);
......@@ -284,6 +299,28 @@ class CRM_Activityprofile_APIWrappers_Profile {
}
}
}
// Contact ID field
// Implicitly lets the user select a contact of a given type, if allowed
if ($val['name'] == 'id' && $val['entity'] == 'contact') {
$ctype = Civi::settings()->get('activityprofile_' . $profile_id . '_ctype');
$csubtype = Civi::settings()->get('activityprofile_' . $profile_id . '_csubtype');
if ($ctype) {
$api = \Civi\Api4\Contact::get()
->addSelect('display_name')
->addWhere('contact_type', '=', $ctype);
if ($csubtype) {
$api->addWhere('contact_sub_type', '=', $csubtype);
}
$contacts = $api->execute()->indexBy('id')->column('display_name');
$val['html_type'] = 'Select';
$val['options'] = $contacts;
}
}
}
// Re-sort on weight. Make sure to preserve keys.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment