Individual.tpl code cleanup
authorColeman Watts <coleman@civicrm.org>
Tue, 10 Jun 2014 13:30:40 +0000 (14:30 +0100)
committerColeman Watts <coleman@civicrm.org>
Tue, 10 Jun 2014 13:42:06 +0000 (14:42 +0100)
templates/CRM/Contact/Form/Edit/Individual.tpl

index 5998aaedc02b16c9eaa98eb84f35ac2158f7edba..9b93a0e7c6270701f38c9223b5320c93692bc4de 100644 (file)
 <script type="text/javascript">
 {literal}
 CRM.$(function($) {
-{/literal}
-  var cid=parseFloat("{$contactId}");//parseInt is octal by default
-  var contactIndividual = "{crmURL p='civicrm/ajax/rest' q='entity=contact&action=get&json=1&contact_type=Individual&return=display_name,sort_name,email&rowCount=50' h=0}";
-  var viewIndividual = "{crmURL p='civicrm/contact/view' q='reset=1&cid=' h=0}";
-  var editIndividual = "{crmURL p='civicrm/contact/add' q='reset=1&action=update&cid=' h=0}";
-  var checkSimilar = {$checkSimilar};
-  var lastnameMsg;
-{literal}
-  $(document).ready(function() {
-    if (cj('#contact_sub_type *').length == 0) {//if they aren't any subtype we don't offer the option
-      cj('#contact_sub_type').parent().hide();
-    }
-    if (!isNaN(cid) || ! checkSimilar) {
-     return;//no dupe check if this is a modif or if checkSimilar is disabled (contact_ajax_check_similar in civicrm_setting table)
-    }
-    cj('#last_name').blur(function () {
-      // Close msg if it exists
-      lastnameMsg && lastnameMsg.close && lastnameMsg.close();
-      if (this.value == '') return;
-      cj.getJSON(contactIndividual,{sort_name:cj('#last_name').val()},
-        function(data){
-          if (data.is_error == 1 || data.count == 0) {
-            return;
+  {/literal}
+    var cid = "{$contactId}",
+      viewIndividual = "{crmURL p='civicrm/contact/view' q='reset=1&cid=' h=0}",
+      checkSimilar = {$checkSimilar},
+      lastnameMsg;
+  {literal}
+  if ($('#contact_sub_type *').length == 0) {//if they aren't any subtype we don't offer the option
+    $('#contact_sub_type').parent().hide();
+  }
+  if (cid.length || !checkSimilar) {
+   return;//no dupe check if this is a modif or if checkSimilar is disabled (contact_ajax_check_similar in civicrm_setting table)
+  }
+  $('#last_name').change(function() {
+    // Close msg if it exists
+    lastnameMsg && lastnameMsg.close && lastnameMsg.close();
+    if (this.value == '') return;
+    CRM.api3('contact', 'get', {
+      sort_name: $('#last_name').val(),
+      contact_type: 'Individual',
+      'return': 'display_name,sort_name,email'
+    })
+      .done(function(data) {
+        if (data.is_error == 1 || data.count == 0) {
+          return;
+        }
+        var msg = "<em>{/literal}{ts escape='js'}If the person you were trying to add is listed below, click their name to view or edit their record{/ts}{literal}:</em>";
+        if ( data.count == 1 ) {
+          var title = "{/literal}{ts escape='js'}Similar Contact Found{/ts}{literal}";
+        } else {
+          var title = "{/literal}{ts escape='js'}Similar Contacts Found{/ts}{literal}";
+        }
+        msg += '<ul class="matching-contacts-actions">';
+        $.each(data.values, function(i, contact){
+          if ( !(contact.email) ) {
+            contact.email = '';
           }
-          var msg = "<em>{/literal}{ts escape='js'}If the person you were trying to add is listed below, click their name to view or edit their record{/ts}{literal}:</em>";
-          if ( data.count == 1 ) {
-            var title = "{/literal}{ts escape='js'}Similar Contact Found{/ts}{literal}";
-          } else {
-            var title = "{/literal}{ts escape='js'}Similar Contacts Found{/ts}{literal}";
-          }
-          msg += '<ul class="matching-contacts-actions">';
-          cj.each(data.values, function(i,contact){
-            if ( !(contact.email) ) {
-              contact.email = '';
-            }
-          msg += '<li><a href="'+viewIndividual+contact.id+'">'+ contact.display_name +'</a> '+contact.email+'</li>';
-        });
-        msg += '</ul>';
-        lastnameMsg = CRM.alert(msg, title);
-        cj('.matching-contacts-actions a').click(function(){
-          // No confirmation dialog on click
-          global_formNavigate = true;
-          return true;
-        });
+        msg += '<li><a href="'+viewIndividual+contact.id+'">'+ contact.display_name +'</a> '+contact.email+'</li>';
+      });
+      msg += '</ul>';
+      lastnameMsg = CRM.alert(msg, title);
+      $('.matching-contacts-actions a').click(function(){
+        // No confirmation dialog on click
+        global_formNavigate = true;
+        return true;
       });
     });
   });