From 0004ae052eda8e20a87e987e77d0f91dd54c0590 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 26 Sep 2015 22:34:25 -0400 Subject: [PATCH] CRM-16980 - Only show relevant contacts when creating case role --- CRM/Case/Form/CaseView.php | 15 +++++++--- CRM/Contact/Form/Relationship.php | 44 ++++++++++++++++++++-------- templates/CRM/Case/Form/CaseView.js | 32 ++++++++++++++++++-- templates/CRM/Case/Form/CaseView.tpl | 4 +-- 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php index 87414e2fb4..dc8d0fac6f 100644 --- a/CRM/Case/Form/CaseView.php +++ b/CRM/Case/Form/CaseView.php @@ -208,9 +208,12 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { return; } + $allowedRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactID); + CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header') - ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header'); + ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header') + ->addVars('relationshipTypes', CRM_Contact_Form_Relationship::getRelationshipTypeMetadata($allowedRelationshipTypes)); $xmlProcessor = new CRM_Case_XMLProcessor_Process(); $caseRoles = $xmlProcessor->get($this->_caseType, 'CaseRoles'); @@ -369,10 +372,14 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo); $this->assign('globalGroupInfo', $globalGroupInfo); - // List relationship types for adding an arbitrary new role to the case - $roleTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactID); - $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) + $roleTypes, FALSE, array('class' => 'crm-select2 twenty')); + $this->add('select', + 'role_type', + ts('Relationship Type'), + array('' => ts('- select type -')) + $allowedRelationshipTypes, + FALSE, + array('class' => 'crm-select2 twenty', 'data-select-params' => '{"allowClear": false}') + ); $hookCaseSummary = CRM_Utils_Hook::caseSummary($this->_caseID); if (is_array($hookCaseSummary)) { diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 21cb3950bd..be6317214d 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -300,24 +300,14 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); // Metadata needed on clientside - $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); - $jsData = array(); - // Get just what we need to keep the dom small - $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b')); + $this->assign('relationshipData', self::getRelationshipTypeMetadata($relationshipList)); + foreach ($this->_allRelationshipNames as $id => $vals) { if ($vals['name_a_b'] === 'Employee of') { $this->assign('employmentRelationship', $id); - } - if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { - $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames[$id], $whatWeWant)); - // Add user-friendly placeholder - foreach (array('a', 'b') as $x) { - $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); - $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); - } + break; } } - $this->assign('relationshipData', $jsData); $this->addField('relationship_type_id', array('options' => array('' => ts('- select -')) + $relationshipList, 'class' => 'huge', 'placeholder' => '- select -'), TRUE); @@ -572,4 +562,32 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { } } + /** + * @param $relationshipList + * @return array + */ + public static function getRelationshipTypeMetadata($relationshipList) { + $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); + $allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); + $jsData = array(); + // Get just what we need to keep the dom small + $whatWeWant = array_flip(array( + 'contact_type_a', + 'contact_type_b', + 'contact_sub_type_a', + 'contact_sub_type_b', + )); + foreach ($allRelationshipNames as $id => $vals) { + if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { + $jsData[$id] = array_filter(array_intersect_key($allRelationshipNames[$id], $whatWeWant)); + // Add user-friendly placeholder + foreach (array('a', 'b') as $x) { + $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); + $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); + } + } + } + return $jsData; + } + } diff --git a/templates/CRM/Case/Form/CaseView.js b/templates/CRM/Case/Form/CaseView.js index 3ade6a2b90..b09f18bb61 100644 --- a/templates/CRM/Case/Form/CaseView.js +++ b/templates/CRM/Case/Form/CaseView.js @@ -66,8 +66,36 @@ }, '#addCaseRoleDialog': { pre: function() { - $('[name=role_type]', this).val('').change(); - $('[name=add_role_contact_id]', this).val('').crmEntityRef({create: true, api: {params: {contact_type: 'Individual'}}}); + var $contactField = $('[name=add_role_contact_id]', this); + $('[name=role_type]', this) + .off('.miniform') + .on('change.miniform', function() { + var val = $(this).val(); + $contactField.val('').change().prop('disabled', !val); + if (val) { + var + pieces = val.split('_'), + rType = pieces[0], + target = pieces[2], // b or a + contact_type = CRM.vars.relationshipTypes[rType]['contact_type_' + target], + contact_sub_type = CRM.vars.relationshipTypes[rType]['contact_sub_type_' + target], + api = {params: {}}; + if (contact_type) { + api.params.contact_type = contact_type; + } + if (contact_sub_type) { + api.params.contact_sub_type = contact_sub_type; + } + $contactField + .data('api-params', api) + .data('user-filter', {}) + .attr('placeholder', CRM.vars.relationshipTypes[rType]['placeholder_' + target]) + .change(); + } + }) + .val('') + .change(); + $contactField.val('').crmEntityRef({create: true, api: {params: {contact_type: 'Individual'}}}); }, post: function(data) { var contactID = $('[name=add_role_contact_id]', this).val(), diff --git a/templates/CRM/Case/Form/CaseView.tpl b/templates/CRM/Case/Form/CaseView.tpl index 19311e304f..a3eee282f5 100644 --- a/templates/CRM/Case/Form/CaseView.tpl +++ b/templates/CRM/Case/Form/CaseView.tpl @@ -152,7 +152,7 @@
{$form.role_type.label}
{$form.role_type.html}

-
+
{/if} @@ -188,7 +188,7 @@ {/literal}
- {ts}Are you sure you want to delete this case role?{/ts} + {ts}Are you sure you want to end this relationship?{/ts}
-- 2.25.1