Commit 3d272948 authored by bgm's avatar bgm
Browse files

Fixes weird behaviours when creating cases and missing case_type_id

parent 66c008c4
......@@ -16,28 +16,32 @@ class CRM_Activityprofile_APIWrappers_Profile {
$profile_id = _civicrm_api3_profile_getProfileID($params['profile_id']);
$isContactActivityProfile = $this->isActivityProfile($profile_id);
// If it is not for a Contact or Activity, check if it's for a new Case
if (!$isContactActivityProfile) {
$uf_group_type = civicrm_api3('UFGroup', 'getsingle', [
'id' => $profile_id,
'return' => 'group_type',
])['group_type'];
// It seems like this can be a string or an array
// and can also mix types together, such as:
// [0] => Case,Contact
// [1] => CaseType:9
if (is_array($uf_group_type)) {
foreach ($uf_group_type as $type) {
if (strpos($type, 'Case') !== FALSE) {
$isContactActivityProfile = TRUE;
}
// If we are creating a new Case, check for a Case Type
// A bit redundant with isActivityProfile()
$uf_group_type = civicrm_api3('UFGroup', 'getsingle', [
'id' => $profile_id,
'return' => 'group_type',
])['group_type'];
// It seems like this can be a string or an array
// and can also mix types together, such as:
// [0] => Case,Contact
// [1] => CaseType:9
if (is_array($uf_group_type)) {
foreach ($uf_group_type as $type) {
if (strpos($type, 'Case') !== FALSE) {
$isContactActivityProfile = TRUE;
}
if (strpos($type, 'CaseType') !== FALSE) {
$parts = explode(':', $type);
$apiRequest['params']['case_type_id'] = $parts[1];
$isContactActivityProfile = TRUE;
}
}
elseif (strpos($uf_group_type, 'Case') !== FALSE) {
$isContactActivityProfile = TRUE;
}
}
elseif (strpos($uf_group_type, 'Case') !== FALSE) {
$isContactActivityProfile = TRUE;
}
if (!$isContactActivityProfile || !empty($params['activity_id'])) {
return $apiRequest;
......@@ -79,6 +83,12 @@ class CRM_Activityprofile_APIWrappers_Profile {
$contact_id = CRM_Core_Session::singleton()->get('userID');
}
// Unset the contact ID because a Case profile cannot update a Contact ID
if (!empty($apiRequest['params']['id'])) {
CRM_Core_Session::singleton()->set('orgContactID', $apiRequest['params']['id']);
unset($apiRequest['params']['id']);
}
if (!$contact_id) {
$contact_params = [];
......@@ -140,7 +150,7 @@ class CRM_Activityprofile_APIWrappers_Profile {
* for fields that are not part of the main entity (ex: Activity fields).
*/
public function toApiOutput($apiRequest, $result) {
if ($apiRequest['action'] != 'getfields') {
if (!in_array($apiRequest['action'], ['getfields', 'submit'])) {
return $result;
}
......@@ -328,6 +338,31 @@ class CRM_Activityprofile_APIWrappers_Profile {
return $a['weight'] - $b['weight'];
});
// If we are creating a Case, add the Case Type, if known
// api/v3/Profile.php checks the ProfileFields to see which bits to add to the api.case.create call
$uf_group_type = civicrm_api3('UFGroup', 'getsingle', [
'id' => $profile_id,
'return' => 'group_type',
])['group_type'];
// It seems like this can be a string or an array
// and can also mix types together, such as:
// [0] => Case,Contact
// [1] => CaseType:9
if (is_array($uf_group_type)) {
foreach ($uf_group_type as $type) {
if (strpos($type, 'CaseType') !== FALSE) {
$result['values']['case_type_id'] = [
'title' => ts('Case Type ID'),
'name' => 'case_type_id',
'entity' => 'Case',
'html_type' => 'hidden',
];
break;
}
}
}
return $result;
}
......
......@@ -190,6 +190,25 @@ function activityprofile_civicrm_post($op, $objectName, $objectId, &$objectRef)
}
}
if ($op == 'create' && $objectName == 'CaseContact') {
// Detect if we are being called from remoteform
// and if so, set the correct Activity Type.
// CiviCRM core has other safeguards against this, so it's a bit of a loophole.
$path = CRM_Utils_System::currentPath();
if ($path == 'civicrm/remoteform') {
$org_id = CRM_Core_Session::singleton()->get('orgContactID');
if ($org_id) {
civicrm_api3('CaseContact', 'create', [
'id' => $objectId,
'contact_id' => $org_id,
'case_id' => $objectRef->case_id,
]);
}
}
}
// This is a bit redundant with the APIWrapper code for Profile submission
// but that code only kicks-in when using remoteform and Activity fields.
// This makes it possible to set the contact-type/subtype if using just a plain profile.
......
Supports Markdown
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