3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Case_XMLProcessor_Process
extends CRM_Case_XMLProcessor
{
18 protected $defaultAssigneeOptionsValues = [];
23 * @param string $caseType
24 * @param array $params
26 * @throws CRM_Core_Exception
28 public function run($caseType, &$params) {
29 $xml = $this->retrieve($caseType);
32 $docLink = CRM_Utils_System
::docURL2("user/case-management/set-up");
33 throw new CRM_Core_Exception(ts("Configuration file could not be retrieved for case type = '%1' %2.",
34 [1 => $caseType, 2 => $docLink]
38 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
39 $this->_isMultiClient
= $xmlProcessorProcess->getAllowMultipleCaseClients();
41 $this->process($xml, $params);
47 * @param bool $isLabel
48 * @param bool $maskAction
50 * @return array|bool|mixed
53 public function get($caseType, $fieldSet, $isLabel = FALSE, $maskAction = FALSE) {
54 $xml = $this->retrieve($caseType);
56 $docLink = CRM_Utils_System
::docURL2("user/case-management/set-up");
57 throw new CRM_Core_Exception(ts("Unable to load configuration file for the referenced case type: '%1' %2.",
58 [1 => $caseType, 2 => $docLink]
64 return $this->caseRoles($xml->CaseRoles
);
67 return $this->activitySets($xml->ActivitySets
);
70 return $this->activityTypes($xml->ActivityTypes
, FALSE, $isLabel, $maskAction);
76 * @param array $params
80 public function process($xml, &$params) {
81 $standardTimeline = $params['standardTimeline'] ??
NULL;
82 $activitySetName = $params['activitySetName'] ??
NULL;
84 if ('Open Case' == CRM_Utils_Array
::value('activityTypeName', $params)) {
85 // create relationships for the ones that are required
86 foreach ($xml->CaseRoles
as $caseRoleXML) {
87 foreach ($caseRoleXML->RelationshipType
as $relationshipTypeXML) {
88 if ($relationshipTypeXML->creator
) {
89 if (!$this->createRelationships($relationshipTypeXML,
93 throw new CRM_Core_Exception('Unable to create case relationships');
100 if ('Change Case Start Date' == CRM_Utils_Array
::value('activityTypeName', $params)) {
101 // delete all existing activities which are non-empty
102 $this->deleteEmptyActivity($params);
105 foreach ($xml->ActivitySets
as $activitySetsXML) {
106 foreach ($activitySetsXML->ActivitySet
as $activitySetXML) {
107 if ($standardTimeline) {
108 if ($activitySetXML->timeline
) {
109 return $this->processStandardTimeline($activitySetXML, $params);
112 elseif ($activitySetName) {
113 $name = (string) $activitySetXML->name
;
114 if ($name == $activitySetName) {
115 return $this->processActivitySet($activitySetXML, $params);
123 * @param $activitySetXML
124 * @param array $params
126 public function processStandardTimeline($activitySetXML, &$params) {
127 if ('Change Case Type' == CRM_Utils_Array
::value('activityTypeName', $params)
128 && CRM_Utils_Array
::value('resetTimeline', $params, TRUE)
130 // delete all existing activities which are non-empty
131 $this->deleteEmptyActivity($params);
134 foreach ($activitySetXML->ActivityTypes
as $activityTypesXML) {
135 foreach ($activityTypesXML as $activityTypeXML) {
136 $this->createActivity($activityTypeXML, $params);
142 * @param $activitySetXML
143 * @param array $params
145 public function processActivitySet($activitySetXML, &$params) {
146 foreach ($activitySetXML->ActivityTypes
as $activityTypesXML) {
147 foreach ($activityTypesXML as $activityTypeXML) {
148 $this->createActivity($activityTypeXML, $params);
154 * @param $caseRolesXML
155 * @param bool $isCaseManager
157 * @return array|mixed
159 public function &caseRoles($caseRolesXML, $isCaseManager = FALSE) {
160 // Look up relationship types according to the XML convention (described
161 // from perspective of non-client) but return the labels according to the UI
162 // convention (described from perspective of client)
163 $relationshipTypesToReturn = &$this->allRelationshipTypes(FALSE);
166 foreach ($caseRolesXML as $caseRoleXML) {
167 foreach ($caseRoleXML->RelationshipType
as $relationshipTypeXML) {
168 list($relationshipTypeID,) = $this->locateNameOrLabel($relationshipTypeXML);
169 if ($relationshipTypeID === FALSE) {
173 if (!$isCaseManager) {
174 $result[$relationshipTypeID] = $relationshipTypesToReturn[$relationshipTypeID];
176 elseif ($relationshipTypeXML->manager
== 1) {
177 return $relationshipTypeID;
185 * @param SimpleXMLElement $relationshipTypeXML
186 * @param array $params
189 * @throws CRM_Core_Exception
191 public function createRelationships($relationshipTypeXML, &$params) {
193 // get the relationship
194 list($relationshipType, $relationshipTypeName) = $this->locateNameOrLabel($relationshipTypeXML);
195 if ($relationshipType === FALSE) {
196 $docLink = CRM_Utils_System
::docURL2("user/case-management/set-up");
197 throw new CRM_Core_Exception(ts('Relationship type %1, found in case configuration file, is not present in the database %2',
198 [1 => $relationshipTypeName, 2 => $docLink]
202 $client = $params['clientID'];
203 if (!is_array($client)) {
207 foreach ($client as $key => $clientId) {
208 $relationshipParams = [
209 'relationship_type_id' => substr($relationshipType, 0, -4),
211 'case_id' => $params['caseID'],
212 'start_date' => date("Ymd"),
213 'end_date' => $params['relationship_end_date'] ??
NULL,
216 if (substr($relationshipType, -4) == '_b_a') {
217 $relationshipParams['contact_id_b'] = $clientId;
218 $relationshipParams['contact_id_a'] = $params['creatorID'];
220 if (substr($relationshipType, -4) == '_a_b') {
221 $relationshipParams['contact_id_a'] = $clientId;
222 $relationshipParams['contact_id_b'] = $params['creatorID'];
225 if (!$this->createRelationship($relationshipParams)) {
226 throw new CRM_Core_Exception('Unable to create case relationship');
233 * @param array $params
237 public function createRelationship(&$params) {
238 $dao = new CRM_Contact_DAO_Relationship();
239 $dao->copyValues($params);
240 // only create a relationship if it does not exist
241 if (!$dao->find(TRUE)) {
248 * @param $activityTypesXML
249 * @param bool $maxInst
250 * @param bool $isLabel
251 * @param bool $maskAction
255 public function activityTypes($activityTypesXML, $maxInst = FALSE, $isLabel = FALSE, $maskAction = FALSE) {
256 $activityTypes = CRM_Case_PseudoConstant
::caseActivityType(TRUE, TRUE);
258 foreach ($activityTypesXML as $activityTypeXML) {
259 foreach ($activityTypeXML as $recordXML) {
260 $activityTypeName = (string) $recordXML->name
;
261 $maxInstances = (string) $recordXML->max_instances
;
262 $activityTypeInfo = $activityTypes[$activityTypeName] ??
NULL;
264 if ($activityTypeInfo['id']) {
266 if ($maskAction == 'edit' && '0' === (string) $recordXML->editable
) {
267 $result[$maskAction][] = $activityTypeInfo['id'];
272 //if we want,labels of activities should be returned.
274 $result[$activityTypeInfo['id']] = $activityTypeInfo['label'];
277 $result[$activityTypeInfo['id']] = $activityTypeName;
282 $result[$activityTypeName] = $maxInstances;
290 // call option value hook
291 CRM_Utils_Hook
::optionValues($result, 'case_activity_type');
297 * @param SimpleXMLElement $caseTypeXML
299 * @return array<string> symbolic activity-type names
301 public function getDeclaredActivityTypes($caseTypeXML) {
304 if (!empty($caseTypeXML->ActivityTypes
) && $caseTypeXML->ActivityTypes
->ActivityType
) {
305 foreach ($caseTypeXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
306 $result[] = (string) $activityTypeXML->name
;
310 if (!empty($caseTypeXML->ActivitySets
) && $caseTypeXML->ActivitySets
->ActivitySet
) {
311 foreach ($caseTypeXML->ActivitySets
->ActivitySet
as $activitySetXML) {
312 if ($activitySetXML->ActivityTypes
&& $activitySetXML->ActivityTypes
->ActivityType
) {
313 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
314 $result[] = (string) $activityTypeXML->name
;
320 $result = array_unique($result);
326 * Relationships are straight from XML, described from perspective of non-client
328 * @param SimpleXMLElement $caseTypeXML
330 * @return array<string> symbolic relationship-type names
332 public function getDeclaredRelationshipTypes($caseTypeXML) {
335 if (!empty($caseTypeXML->CaseRoles
) && $caseTypeXML->CaseRoles
->RelationshipType
) {
336 foreach ($caseTypeXML->CaseRoles
->RelationshipType
as $relTypeXML) {
337 list(, $relationshipTypeMachineName) = $this->locateNameOrLabel($relTypeXML);
338 $result[] = $relationshipTypeMachineName;
342 $result = array_unique($result);
348 * @param array $params
350 public function deleteEmptyActivity(&$params) {
351 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
352 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
356 FROM civicrm_activity a
357 INNER JOIN civicrm_activity_contact t ON t.activity_id = a.id
358 INNER JOIN civicrm_case_activity ca on ca.activity_id = a.id
359 WHERE t.contact_id = %1
360 AND t.record_type_id = $targetID
362 AND a.is_current_revision = 1
365 $sqlParams = [1 => [$params['clientID'], 'Integer'], 2 => [$params['caseID'], 'Integer']];
366 CRM_Core_DAO
::executeQuery($query, $sqlParams);
370 * @param array $params
374 public function isActivityPresent(&$params) {
377 FROM civicrm_activity a
378 INNER JOIN civicrm_case_activity ca on ca.activity_id = a.id
379 WHERE a.activity_type_id = %1
385 1 => [$params['activityTypeID'], 'Integer'],
386 2 => [$params['caseID'], 'Integer'],
388 $count = CRM_Core_DAO
::singleValueQuery($query, $sqlParams);
390 // check for max instance
391 $caseType = CRM_Case_BAO_Case
::getCaseType($params['caseID'], 'name');
392 $maxInstance = self
::getMaxInstance($caseType, $params['activityTypeName']);
394 return $maxInstance ?
($count < $maxInstance ?
FALSE : TRUE) : FALSE;
398 * @param $activityTypeXML
399 * @param array $params
402 * @throws CRM_Core_Exception
405 public function createActivity($activityTypeXML, &$params) {
406 $activityTypeName = (string) $activityTypeXML->name
;
407 $activityTypes = CRM_Case_PseudoConstant
::caseActivityType(TRUE, TRUE);
408 $activityTypeInfo = $activityTypes[$activityTypeName] ??
NULL;
410 if (!$activityTypeInfo) {
411 $docLink = CRM_Utils_System
::docURL2("user/case-management/set-up");
412 throw new CRM_Core_Exception(ts('Activity type %1, found in case configuration file, is not present in the database %2',
413 [1 => $activityTypeName, 2 => $docLink]
417 $activityTypeID = $activityTypeInfo['id'];
419 if (isset($activityTypeXML->status
)) {
420 $statusName = (string) $activityTypeXML->status
;
423 $statusName = 'Scheduled';
426 $client = (array) $params['clientID'];
430 if (isset($activityTypeXML->order
)) {
431 $orderVal = (string) $activityTypeXML->order
;
434 if ($activityTypeName == 'Open Case') {
436 'activity_type_id' => $activityTypeID,
437 'source_contact_id' => $params['creatorID'],
439 'is_current_revision' => 1,
440 'subject' => !empty($params['subject']) ?
$params['subject'] : $activityTypeName,
441 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', $statusName),
442 'target_contact_id' => $client,
443 'medium_id' => $params['medium_id'] ??
NULL,
444 'location' => $params['location'] ??
NULL,
445 'details' => $params['details'] ??
NULL,
446 'duration' => $params['duration'] ??
NULL,
447 'weight' => $orderVal,
452 'activity_type_id' => $activityTypeID,
453 'source_contact_id' => $params['creatorID'],
455 'is_current_revision' => 1,
456 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', $statusName),
457 'target_contact_id' => $client,
458 'weight' => $orderVal,
462 $activityParams['assignee_contact_id'] = $this->getDefaultAssigneeForActivity($activityParams, $activityTypeXML, $params['caseID']);
464 //parsing date to default preference format
465 $params['activity_date_time'] = CRM_Utils_Date
::processDate($params['activity_date_time']);
467 if ($activityTypeName == 'Open Case') {
468 // we don't set activity_date_time for auto generated
469 // activities, but we want it to be set for open case.
470 $activityParams['activity_date_time'] = $params['activity_date_time'];
471 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
472 $activityParams['custom'] = $params['custom'];
475 // Add parameters for attachments
477 $numAttachments = Civi
::settings()->get('max_attachments');
478 for ($i = 1; $i <= $numAttachments; $i++
) {
479 $attachName = "attachFile_$i";
480 if (isset($params[$attachName]) && !empty($params[$attachName])) {
481 $activityParams[$attachName] = $params[$attachName];
486 $activityDate = NULL;
487 //get date of reference activity if set.
488 if ($referenceActivityName = (string) $activityTypeXML->reference_activity
) {
490 //we skip open case as reference activity.CRM-4374.
491 if (!empty($params['resetTimeline']) && $referenceActivityName == 'Open Case') {
492 $activityDate = $params['activity_date_time'];
495 $referenceActivityInfo = $activityTypes[$referenceActivityName] ??
NULL;
496 if ($referenceActivityInfo['id']) {
497 $caseActivityParams = ['activity_type_id' => $referenceActivityInfo['id']];
499 //if reference_select is set take according activity.
500 if ($referenceSelect = (string) $activityTypeXML->reference_select
) {
501 $caseActivityParams[$referenceSelect] = 1;
504 $referenceActivity = CRM_Case_BAO_Case
::getCaseActivityDates($params['caseID'], $caseActivityParams, TRUE);
506 if (is_array($referenceActivity)) {
507 foreach ($referenceActivity as $aId => $details) {
508 $activityDate = $details['activity_date'] ??
NULL;
515 if (!$activityDate) {
516 $activityDate = $params['activity_date_time'];
518 list($activity_date, $activity_time) = CRM_Utils_Date
::setDateDefaults($activityDate);
519 $activityDateTime = CRM_Utils_Date
::processDate($activity_date, $activity_time);
520 //add reference offset to date.
521 if ((int) $activityTypeXML->reference_offset
) {
522 $activityDateTime = CRM_Utils_Date
::intervalAdd('day', (int) $activityTypeXML->reference_offset
,
527 $activityParams['activity_date_time'] = CRM_Utils_Date
::format($activityDateTime);
530 // if same activity is already there, skip and dont touch
531 $params['activityTypeID'] = $activityTypeID;
532 $params['activityTypeName'] = $activityTypeName;
533 if ($this->isActivityPresent($params)) {
536 $activityParams['case_id'] = $params['caseID'];
537 if (!empty($activityParams['is_auto'])) {
538 $activityParams['skipRecentView'] = TRUE;
541 // @todo - switch to using api & remove the parameter pre-wrangling above.
542 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
545 throw new CRM_Core_Exception('Unable to create Activity');
548 // create case activity record
550 'activity_id' => $activity->id
,
551 'case_id' => $params['caseID'],
553 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
558 * Return the default assignee contact for the activity.
560 * @param array $activityParams
561 * @param object $activityTypeXML
564 * @return int|null the ID of the default assignee contact or null if none.
566 protected function getDefaultAssigneeForActivity($activityParams, $activityTypeXML, $caseId) {
567 if (!isset($activityTypeXML->default_assignee_type
)) {
571 $defaultAssigneeOptionsValues = $this->getDefaultAssigneeOptionValues();
573 switch ($activityTypeXML->default_assignee_type
) {
574 case $defaultAssigneeOptionsValues['BY_RELATIONSHIP']:
575 return $this->getDefaultAssigneeByRelationship($activityParams, $activityTypeXML, $caseId);
578 case $defaultAssigneeOptionsValues['SPECIFIC_CONTACT']:
579 return $this->getDefaultAssigneeBySpecificContact($activityTypeXML);
582 case $defaultAssigneeOptionsValues['USER_CREATING_THE_CASE']:
583 return $activityParams['source_contact_id'];
586 case $defaultAssigneeOptionsValues['NONE']:
593 * Fetches and caches the activity's default assignee options.
597 protected function getDefaultAssigneeOptionValues() {
598 if (!empty($this->defaultAssigneeOptionsValues
)) {
599 return $this->defaultAssigneeOptionsValues
;
602 $defaultAssigneeOptions = civicrm_api3('OptionValue', 'get', [
603 'option_group_id' => 'activity_default_assignee',
604 'options' => ['limit' => 0],
607 foreach ($defaultAssigneeOptions['values'] as $option) {
608 $this->defaultAssigneeOptionsValues
[$option['name']] = $option['value'];
611 return $this->defaultAssigneeOptionsValues
;
615 * Returns the default assignee for the activity by searching for the target's
616 * contact relationship type defined in the activity's details.
618 * @param array $activityParams
619 * @param object $activityTypeXML
622 * @return int|null the ID of the default assignee contact or null if none.
624 protected function getDefaultAssigneeByRelationship($activityParams, $activityTypeXML, $caseId) {
625 $isDefaultRelationshipDefined = isset($activityTypeXML->default_assignee_relationship
)
626 && preg_match('/\d+_[ab]_[ab]/', $activityTypeXML->default_assignee_relationship
);
628 if (!$isDefaultRelationshipDefined) {
632 $targetContactId = is_array($activityParams['target_contact_id'])
633 ? CRM_Utils_Array
::first($activityParams['target_contact_id'])
634 : $activityParams['target_contact_id'];
635 list($relTypeId, $a, $b) = explode('_', $activityTypeXML->default_assignee_relationship
);
638 'relationship_type_id' => $relTypeId,
639 "contact_id_$b" => $targetContactId,
641 'case_id' => $caseId,
642 'options' => ['limit' => 1],
645 if ($this->isBidirectionalRelationshipType($relTypeId)) {
646 $params["contact_id_$a"] = $targetContactId;
647 $params['options']['or'] = [['contact_id_a', 'contact_id_b']];
650 $relationships = civicrm_api3('Relationship', 'get', $params);
651 if (empty($relationships['count'])) {
652 $params['case_id'] = ['IS NULL' => 1];
653 $relationships = civicrm_api3('Relationship', 'get', $params);
656 if ($relationships['count']) {
657 $relationship = CRM_Utils_Array
::first($relationships['values']);
659 // returns the contact id on the other side of the relationship:
660 return (int) $relationship['contact_id_a'] === (int) $targetContactId
661 ?
$relationship['contact_id_b']
662 : $relationship['contact_id_a'];
670 * Determines if the given relationship type is bidirectional or not by
671 * comparing their labels.
675 protected function isBidirectionalRelationshipType($relationshipTypeId) {
676 $relationshipTypeResult = civicrm_api3('RelationshipType', 'get', [
677 'id' => $relationshipTypeId,
678 'options' => ['limit' => 1],
681 if ($relationshipTypeResult['count'] === 0) {
685 $relationshipType = CRM_Utils_Array
::first($relationshipTypeResult['values']);
687 return $relationshipType['label_b_a'] === $relationshipType['label_a_b'];
691 * Returns the activity's default assignee for a specific contact if the contact exists,
692 * otherwise returns null.
694 * @param object $activityTypeXML
698 protected function getDefaultAssigneeBySpecificContact($activityTypeXML) {
699 if (!$activityTypeXML->default_assignee_contact
) {
703 $contact = civicrm_api3('Contact', 'get', [
704 'id' => $activityTypeXML->default_assignee_contact
,
707 if ($contact['count'] == 1) {
708 return $activityTypeXML->default_assignee_contact
;
715 * @param $activitySetsXML
719 public static function activitySets($activitySetsXML) {
721 foreach ($activitySetsXML as $activitySetXML) {
722 foreach ($activitySetXML as $recordXML) {
723 $activitySetName = (string) $recordXML->name
;
724 $activitySetLabel = (string) $recordXML->label
;
725 $result[$activitySetName] = $activitySetLabel;
734 * @param string|null $activityTypeName
736 * @return array|bool|mixed
737 * @throws CRM_Core_Exception
739 public function getMaxInstance($caseType, $activityTypeName = NULL) {
740 $xml = $this->retrieve($caseType);
742 if ($xml === FALSE) {
743 throw new CRM_Core_Exception('Unable to locate xml definition for case type ' . $caseType);
746 $activityInstances = $this->activityTypes($xml->ActivityTypes
, TRUE);
747 return $activityTypeName ? CRM_Utils_Array
::value($activityTypeName, $activityInstances) : $activityInstances;
753 * @return array|mixed
755 public function getCaseManagerRoleId($caseType) {
756 $xml = $this->retrieve($caseType);
757 return $this->caseRoles($xml->CaseRoles
, TRUE);
761 * @param string $caseType
763 * @return array<\Civi\CCase\CaseChangeListener>
765 public function getListeners($caseType) {
766 $xml = $this->retrieve($caseType);
768 if ($xml->Listeners
&& $xml->Listeners
->Listener
) {
769 foreach ($xml->Listeners
->Listener
as $listenerXML) {
770 $class = (string) $listenerXML;
771 $listeners[] = new $class();
780 public function getRedactActivityEmail() {
781 return $this->getBoolSetting('civicaseRedactActivityEmail', 'RedactActivityEmail');
785 * Retrieves AllowMultipleCaseClients setting.
788 * 1 if allowed, 0 if not
790 public function getAllowMultipleCaseClients() {
791 return $this->getBoolSetting('civicaseAllowMultipleClients', 'AllowMultipleCaseClients');
795 * Retrieves NaturalActivityTypeSort setting.
798 * 1 if natural, 0 if alphabetic
800 public function getNaturalActivityTypeSort() {
801 return $this->getBoolSetting('civicaseNaturalActivityTypeSort', 'NaturalActivityTypeSort');
805 * @param string $settingKey
806 * @param string $xmlTag
807 * @param mixed $default
811 private function getBoolSetting($settingKey, $xmlTag, $default = 0) {
812 $setting = Civi
::settings()->get($settingKey);
813 if ($setting !== 'default') {
814 return (int) $setting;
816 if ($xml = $this->retrieve("Settings")) {
817 return (string) $xml->{$xmlTag} ?
1 : 0;
823 * At some point name and label got mixed up for case roles.
824 * Check against known machine name values, and then if no match check
826 * This is subject to some edge cases, but we catch those with a system
828 * We do this to avoid requiring people to update their xml files which can
829 * be stored in external files we can't/don't want to edit.
831 * @param SimpleXMLElement $xml
833 * @return array[bool|string,string]
835 public function locateNameOrLabel($xml) {
836 $lookupString = (string) $xml->name
;
838 // Don't use pseudoconstant because we need everything both name and
839 // label and disabled types.
840 $relationshipTypes = civicrm_api3('RelationshipType', 'get', [
841 'options' => ['limit' => 0],
844 // First look and see if it matches a machine name in the system.
845 // There are some edge cases here where we've actually been passed in a
846 // display label and it happens to match the machine name for a different
847 // db entry, but we have a system status check.
848 // But, we do want to check against the a_b version first, because of the
849 // way direction matters and that for bidirectional only one is present in
850 // the list where this eventually gets used, so return that first.
851 $relationshipTypeMachineNames = array_column($relationshipTypes, 'id', 'name_a_b');
852 if (isset($relationshipTypeMachineNames[$lookupString])) {
853 return ["{$relationshipTypeMachineNames[$lookupString]}_b_a", $lookupString];
855 $relationshipTypeMachineNames = array_column($relationshipTypes, 'id', 'name_b_a');
856 if (isset($relationshipTypeMachineNames[$lookupString])) {
857 return ["{$relationshipTypeMachineNames[$lookupString]}_a_b", $lookupString];
860 // Now at this point assume we've been passed a display label, so find
861 // what it matches and return the associated machine name. This is a bit
862 // trickier because suppose somebody has changed the display labels so
863 // that they are now the same, but the machine names are different. We
864 // don't know which to return and so while it's the right relationship type
865 // it might be the backwards direction. We have to pick one to try first.
867 $relationshipTypeDisplayLabels = array_column($relationshipTypes, 'id', 'label_a_b');
868 if (isset($relationshipTypeDisplayLabels[$lookupString])) {
870 "{$relationshipTypeDisplayLabels[$lookupString]}_b_a",
871 $relationshipTypes[$relationshipTypeDisplayLabels[$lookupString]]['name_a_b'],
874 $relationshipTypeDisplayLabels = array_column($relationshipTypes, 'id', 'label_b_a');
875 if (isset($relationshipTypeDisplayLabels[$lookupString])) {
877 "{$relationshipTypeDisplayLabels[$lookupString]}_a_b",
878 $relationshipTypes[$relationshipTypeDisplayLabels[$lookupString]]['name_b_a'],
882 // Just go with what we were passed in, even though it doesn't seem
883 // to match *anything*. This was what it did before.
884 return [FALSE, $lookupString];