CRM-16980 - Only show relevant contacts when creating case role
authorColeman Watts <coleman@civicrm.org>
Sun, 27 Sep 2015 02:34:25 +0000 (22:34 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 29 Sep 2015 02:38:50 +0000 (22:38 -0400)
CRM/Case/Form/CaseView.php
CRM/Contact/Form/Relationship.php
templates/CRM/Case/Form/CaseView.js
templates/CRM/Case/Form/CaseView.tpl

index 87414e2fb40c4b66213e440a0badd0bef9bfb782..dc8d0fac6f640305c9b1e55864930d6360e889cc 100644 (file)
@@ -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)) {
index 21cb3950bd38f30c3ac248e443a60e56ba46f371..be6317214d4990cc0044a3457111b89fd33f18b5 100644 (file)
@@ -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;
+  }
+
 }
index 3ade6a2b90c6d8f1e80b951b47b085710365acb0..b09f18bb619ce96685eb28488e52a7f7270caabc 100644 (file)
     },
     '#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(),
index 19311e304f0a77d16a8706c00727c2868fa65538..a3eee282f5987a2b16b7b124bda522a389b9e6d3 100644 (file)
           <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}
   
       {/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 -->