CRM-13965-qa-fixes
[civicrm-core.git] / js / rest.js
index 0cdb94f71c931a6f3c959f480264ffc0ef7da7af..4f4e1e7147854db450178966715eb152d0432227 100644 (file)
@@ -1,6 +1,6 @@
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -84,10 +84,35 @@ var CRM = CRM || {};
   /**
    * AJAX api
    */
+  CRM.api3 = function(entity, action, params) {
+    if (typeof(entity) === 'string') {
+      params = {
+        entity: entity,
+        action: action.toLowerCase(),
+        json: JSON.stringify(params || {})
+      };
+    } else {
+      params = {
+        entity: 'api3',
+        action: 'call',
+        json: JSON.stringify(entity)
+      }
+    }
+    return $.ajax({
+      url: CRM.url('civicrm/ajax/rest'),
+      dataType: 'json',
+      data: params,
+      type: params.action.indexOf('get') < 0 ? 'POST' : 'GET'
+    });
+  };
+
+  /**
+   * @deprecated
+   * AJAX api
+   */
   CRM.api = function(entity, action, params, options) {
     // Default settings
-    var json = false,
-    settings = {
+    var settings = {
       context: null,
       success: function(result, settings) {
         return true;
@@ -102,7 +127,7 @@ var CRM = CRM || {};
         }
         return settings.success.call(this, result, settings);
       },
-      ajaxURL: 'civicrm/ajax/rest',
+      ajaxURL: 'civicrm/ajax/rest'
     };
     action = action.toLowerCase();
     // Default success handler
@@ -122,24 +147,11 @@ var CRM = CRM || {};
           return true;
         };
     }
-    for (var i in params) {
-      if (i.slice(0, 4) == 'api.' || typeof(params[i]) == 'Object') {
-        json = true;
-        break;
-      }
-    }
-    if (json) {
-      params = {
-        entity: entity,
-        action: action,
-        json: JSON.stringify(params)
-      };
-    }
-    else {
-      params.entity = entity;
-      params.action = action;
-      params.json = 1;
-    }
+    params = {
+      entity: entity,
+      action: action,
+      json: JSON.stringify(params)
+    };
     // Pass copy of settings into closure to preserve its value during multiple requests
     (function(stg) {
       $.ajax({
@@ -160,69 +172,4 @@ var CRM = CRM || {};
     return CRM.api.call(this, entity, action, params, options);
   };
 
-  $.fn.crmAutocomplete = function (params, options) {
-    if (typeof params == 'undefined') params = {};
-    if (typeof options == 'undefined') options = {};
-    params = $().extend({
-      rowCount:35,
-      json:1,
-      entity:'Contact',
-      action:'getquick',
-      sequential:1
-    }, params);
-
-    options = $().extend({}, {
-        field :'name',
-        skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'],
-        result: function(data){
-        return false;
-      },
-      formatItem: function(data,i,max,value,term){
-        var tmp = [];
-        for (attr in data) {
-          if ($.inArray (attr, options.skip) == -1 && data[attr]) {
-            tmp.push(data[attr]);
-          }
-        }
-        return  tmp.join(' :: ');
-      },
-      parse: function (data){
-             var acd = new Array();
-             for(cid in data.values){
-               delete data.values[cid]["data"];// to be removed once quicksearch doesn't return data
-               acd.push({ data:data.values[cid], value:data.values[cid].sort_name, result:data.values[cid].sort_name });
-             }
-             return acd;
-      },
-      delay:100,
-        width:250,
-      minChars:1
-      }, options
-    );
-    var contactUrl = CRM.url('civicrm/ajax/rest', params);
-
-    return this.each(function() {
-      var selector = this;
-      if (typeof $.fn.autocomplete != 'function')
-        $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj
-        var extraP = {};
-        extraP [options.field] = function () {return $(selector).val();};
-        $(this).autocomplete( contactUrl, {
-          extraParams:extraP,
-          formatItem: function(data,i,max,value,term){
-            return options.formatItem(data,i,max,value,term);
-          },
-          parse: function(data){ return options.parse(data);},
-          width: options.width,
-          delay:options.delay,
-          max:25,
-          dataType:'json',
-          minChars:options.minChars,
-          selectFirst: true
-       }).result(function(event, data, formatted) {
-            options.result(data);
-        });
-     });
-   }
-
 })(jQuery, CRM);