Skip to content
Snippets Groups Projects
Commit 9c7ffe36 authored by colemanw's avatar colemanw
Browse files

Option to limit case roles to certain groups

parent 094d53fc
Branches
Tags
No related merge requests found
......@@ -167,7 +167,7 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
foreach ($definition['caseRoles'] as $values) {
$xmlFile .= "<RelationshipType>\n";
foreach ($values as $key => $value) {
$xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n";
$xmlFile .= "<{$key}>" . (is_array($value) ? implode(',', array_map(['\CRM_Case_BAO_CaseType', 'encodeXmlString'], $value)) : self::encodeXmlString($value)) . "</{$key}>\n";
}
$xmlFile .= "</RelationshipType>\n";
}
......@@ -284,7 +284,11 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
if (isset($xml->CaseRoles)) {
$definition['caseRoles'] = [];
foreach ($xml->CaseRoles->RelationshipType as $caseRoleXml) {
$definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
$caseRole = json_decode(json_encode($caseRoleXml), TRUE);
if (!empty($caseRole['groups'])) {
$caseRole['groups'] = explode(',', $caseRole['groups']);
}
$definition['caseRoles'][] = $caseRole;
}
}
......
......@@ -168,11 +168,31 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
}
$allowedRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactID);
$relationshipTypeMetadata = CRM_Contact_Form_Relationship::getRelationshipTypeMetadata($allowedRelationshipTypes);
$caseTypeDefinition = civicrm_api3('CaseType', 'getsingle', ['name' => $this->_caseType])['definition'];
foreach ($caseTypeDefinition['caseRoles'] as $role) {
if (!empty($role['groups'])) {
$relationshipType = civicrm_api3('RelationshipType', 'get', [
'sequential' => 1,
'name_a_b' => $role['name'],
'name_b_a' => $role['name'],
'options' => ['limit' => 1, 'or' => [["name_a_b", "name_b_a"]]],
]);
if (($relationshipType['values'][0]['name_a_b'] ?? NULL) === $role['name']) {
$relationshipTypeMetadata[$relationshipType['id']]['group_a'] = $role['groups'];
}
if (($relationshipType['values'][0]['name_b_a'] ?? NULL) === $role['name']) {
$relationshipTypeMetadata[$relationshipType['id']]['group_b'] = $role['groups'];
}
}
}
CRM_Core_Resources::singleton()
->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header')
->addVars('relationshipTypes', CRM_Contact_Form_Relationship::getRelationshipTypeMetadata($allowedRelationshipTypes));
->addVars('relationshipTypes', $relationshipTypeMetadata);
$xmlProcessor = new CRM_Case_XMLProcessor_Process();
$caseRoles = $xmlProcessor->get($this->_caseType, 'CaseRoles');
......
......@@ -8,6 +8,7 @@ Required vars: caseType
<th>{{ts('Display Label')}}</th>
<th>{{ts('Assign to Creator')}}</th>
<th>{{ts('Is Manager')}}</th>
<th>{{ts('Restrict to Groups')}}</th>
<th></th>
</tr>
</thead>
......@@ -17,6 +18,10 @@ Required vars: caseType
<td>{{relType.displayLabel}}</td>
<td><input type="checkbox" ng-model="relType.creator" ng-true-value="'1'" ng-false-value="'0'"></td>
<td><input type="radio" ng-model="relType.manager" value="1" ng-change="onManagerChange(relType)"></td>
<td><input ng-list class="big"
crm-entityref="{entity: 'Group', api: {id_field: 'name', params: {is_hidden: 0, is_active: 1}}, select: {allowClear: true, multiple: true, placeholder: ts('Select Group')}}"
ng-model="relType.groups"
/></td>
<td>
<a crm-icon="fa-trash" class="crm-hover-button" ng-click="removeItem(caseType.definition.caseRoles,relType)" title="{{ts('Remove')}}"></a>
</td>
......
......@@ -73,29 +73,12 @@
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();
prepareRelationshipField(val, $contactField);
}
})
.val('')
.change();
$contactField.val('').crmEntityRef({create: true, api: {params: {contact_type: 'Individual'}}});
$contactField.val('').crmEntityRef();
},
post: function(data) {
var contactID = $('[name=add_role_contact_id]', this).val(),
......@@ -114,11 +97,7 @@
},
'#editCaseRoleDialog': {
pre: function(data) {
var params = {create: true};
if (data.contact_type) {
params.api = {params: {contact_type: data.contact_type}};
}
$('[name=edit_role_contact_id]', this).val('').crmEntityRef(params);
prepareRelationshipField(data.rel_type, $('[name=edit_role_contact_id]', this));
},
post: function(data) {
data.rel_contact = $('[name=edit_role_contact_id]', this).val();
......@@ -164,6 +143,31 @@
},
detached = {};
function prepareRelationshipField(relType, $contactField) {
var
pieces = relType.split('_'),
rType = pieces[0],
target = pieces[2], // b or a
relationshipType = CRM.vars.relationshipTypes[rType],
api = {params: {}};
if (relationshipType['contact_type_' + target]) {
api.params.contact_type = relationshipType['contact_type_' + target];
}
if (relationshipType['contact_sub_type_' + target]) {
api.params.contact_sub_type = relationshipType['contact_sub_type_' + target];
}
if (relationshipType['group_' + target]) {
api.params.group = {IN: relationshipType['group_' + target]};
}
$contactField
.data('create-links', !relationshipType['group_' + target])
.data('api-params', api)
.data('user-filter', {})
.attr('placeholder', relationshipType['placeholder_' + target])
.change()
.crmEntityRef();
}
function detachMiniForms() {
detached = {};
$.each(miniForms, function(selector) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment