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

dev/core#3077 - CiviCase - Call hooks when creating relationships #22814

Merged
merged 2 commits into from
Feb 26, 2022
Merged
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
58 changes: 24 additions & 34 deletions CRM/Case/XMLProcessor/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,59 +194,49 @@ public function &caseRoles($caseRolesXML, $isCaseManager = FALSE) {
* @return bool
* @throws CRM_Core_Exception
*/
public function createRelationships($relationshipTypeXML, &$params) {

public function createRelationships($relationshipTypeXML, $params) {
// get the relationship
list($relationshipType, $relationshipTypeName) = $this->locateNameOrLabel($relationshipTypeXML);
[$relationshipType, $relationshipTypeName] = $this->locateNameOrLabel($relationshipTypeXML);
if ($relationshipType === FALSE) {
$docLink = CRM_Utils_System::docURL2("user/case-management/set-up");
throw new CRM_Core_Exception(ts('Relationship type %1, found in case configuration file, is not present in the database %2',
[1 => $relationshipTypeName, 2 => $docLink]
));
}

$client = $params['clientID'];
if (!is_array($client)) {
$client = [$client];
}
$clients = (array) $params['clientID'];
$relationshipValues = [];

foreach ($client as $key => $clientId) {
$relationshipParams = [
foreach ($clients as $clientId) {
// $relationshipType string ends in either `_a_b` or `_b_a`
$a = substr($relationshipType, -3, 1);
$b = substr($relationshipType, -1);
$relationshipValues[] = [
'relationship_type_id' => substr($relationshipType, 0, -4),
'is_active' => 1,
'case_id' => $params['caseID'],
'start_date' => date("Ymd"),
'end_date' => $params['relationship_end_date'] ?? NULL,
"contact_id_$a" => $clientId,
"contact_id_$b" => $params['creatorID'],
];
}

if (substr($relationshipType, -4) == '_b_a') {
$relationshipParams['contact_id_b'] = $clientId;
$relationshipParams['contact_id_a'] = $params['creatorID'];
}
if (substr($relationshipType, -4) == '_a_b') {
$relationshipParams['contact_id_a'] = $clientId;
$relationshipParams['contact_id_b'] = $params['creatorID'];
}

if (!$this->createRelationship($relationshipParams)) {
throw new CRM_Core_Exception('Unable to create case relationship');
//\Civi\Api4\Relationship::save(FALSE)
// ->setRecords($relationshipValues)
// ->setMatch(['case_id', 'relationship_type_id', 'contact_id_a', 'contact_id_b'])
// ->execute();
// FIXME: The above api code would be better, but doesn't work
// See discussion in https://github.com/civicrm/civicrm-core/pull/15030
foreach ($relationshipValues as $params) {
$dao = new CRM_Contact_DAO_Relationship();
$dao->copyValues($params);
// only create a relationship if it does not exist
if (!$dao->find(TRUE)) {
CRM_Contact_BAO_Relationship::add($params);
}
}
return TRUE;
}

/**
* @param array $params
*
* @return bool
*/
public function createRelationship(&$params) {
$dao = new CRM_Contact_DAO_Relationship();
$dao->copyValues($params);
// only create a relationship if it does not exist
if (!$dao->find(TRUE)) {
$dao->save();
}
return TRUE;
}

Expand Down