Skip to content

Commit

Permalink
CRM-16980 - Only show relevant contacts when creating case role
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Sep 29, 2015
1 parent 3b1c37f commit 0004ae0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
15 changes: 11 additions & 4 deletions CRM/Case/Form/CaseView.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ public function buildQuickForm() {
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');
Expand Down Expand Up @@ -369,10 +372,14 @@ public function buildQuickForm() {
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)) {
Expand Down
44 changes: 31 additions & 13 deletions CRM/Contact/Form/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,14 @@ public function buildQuickForm() {
$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);

Expand Down Expand Up @@ -572,4 +562,32 @@ protected function setMessage($outcome) {
}
}

/**
* @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;
}

}
32 changes: 30 additions & 2 deletions templates/CRM/Case/Form/CaseView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Case/Form/CaseView.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<div>{$form.role_type.label}</div>
<div>{$form.role_type.html}</div><br />
<div><label for="add_role_contact_id">{ts}Assign To{/ts}:</label></div>
<div><input name="add_role_contact_id" placeholder="{ts}- select contact -{/ts}" class="huge" /></div>
<div><input name="add_role_contact_id" placeholder="{ts}- first select relationship type -{/ts}" class="huge" /></div>
</div>
{/if}

Expand Down Expand Up @@ -188,7 +188,7 @@
{/literal}

<div id="deleteCaseRoleDialog" class="hiddenElement">
{ts}Are you sure you want to delete this case role?{/ts}
{ts}Are you sure you want to end this relationship?{/ts}
</div>

</div><!-- /.crm-accordion-body -->
Expand Down

0 comments on commit 0004ae0

Please sign in to comment.