update to pr 16559
authorJaap Jansma <jaap.jansma@civicoop.org>
Mon, 6 Jul 2020 14:33:54 +0000 (16:33 +0200)
committerJaap Jansma <jaap.jansma@civicoop.org>
Mon, 6 Jul 2020 14:33:54 +0000 (16:33 +0200)
CRM/Case/XMLProcessor/Process.php

index 6bffb852dd88434b1218146bb3f405b7c8d3a932..d010a47c68351cb3131a83785749e9602eadb726 100644 (file)
@@ -459,7 +459,7 @@ AND        a.is_deleted = 0
       ];
     }
 
-    $activityParams['assignee_contact_id'] = $this->getDefaultAssigneeForActivity($activityParams, $activityTypeXML);
+    $activityParams['assignee_contact_id'] = $this->getDefaultAssigneeForActivity($activityParams, $activityTypeXML, $params['caseID']);
 
     //parsing date to default preference format
     $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time']);
@@ -559,10 +559,11 @@ AND        a.is_deleted = 0
    *
    * @param array $activityParams
    * @param object $activityTypeXML
+   * @param int $caseId
    *
    * @return int|null the ID of the default assignee contact or null if none.
    */
-  protected function getDefaultAssigneeForActivity($activityParams, $activityTypeXML) {
+  protected function getDefaultAssigneeForActivity($activityParams, $activityTypeXML, $caseId) {
     if (!isset($activityTypeXML->default_assignee_type)) {
       return NULL;
     }
@@ -571,7 +572,7 @@ AND        a.is_deleted = 0
 
     switch ($activityTypeXML->default_assignee_type) {
       case $defaultAssigneeOptionsValues['BY_RELATIONSHIP']:
-        return $this->getDefaultAssigneeByRelationship($activityParams, $activityTypeXML);
+        return $this->getDefaultAssigneeByRelationship($activityParams, $activityTypeXML, $caseId);
 
       break;
       case $defaultAssigneeOptionsValues['SPECIFIC_CONTACT']:
@@ -616,10 +617,11 @@ AND        a.is_deleted = 0
    *
    * @param array $activityParams
    * @param object $activityTypeXML
+   * @param int $caseId
    *
    * @return int|null the ID of the default assignee contact or null if none.
    */
-  protected function getDefaultAssigneeByRelationship($activityParams, $activityTypeXML) {
+  protected function getDefaultAssigneeByRelationship($activityParams, $activityTypeXML, $caseId) {
     $isDefaultRelationshipDefined = isset($activityTypeXML->default_assignee_relationship)
       && preg_match('/\d+_[ab]_[ab]/', $activityTypeXML->default_assignee_relationship);
 
@@ -636,6 +638,8 @@ AND        a.is_deleted = 0
       'relationship_type_id' => $relTypeId,
       "contact_id_$b" => $targetContactId,
       'is_active' => 1,
+      'case_id' => $caseId,
+      'options' => ['limit' => 1],
     ];
 
     if ($this->isBidirectionalRelationshipType($relTypeId)) {
@@ -644,6 +648,10 @@ AND        a.is_deleted = 0
     }
 
     $relationships = civicrm_api3('Relationship', 'get', $params);
+    if (empty($relationships['count'])) {
+      $params['case_id'] = ['IS NULL' => 1];
+      $relationships = civicrm_api3('Relationship', 'get', $params);
+    }
 
     if ($relationships['count']) {
       $relationship = CRM_Utils_Array::first($relationships['values']);