-- Indentation fixes
[civicrm-core.git] / js / rest.js
1 /*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | This file is a part of CiviCRM. |
6 | |
7 | CiviCRM is free software; you can copy, modify, and distribute it |
8 | under the terms of the GNU Affero General Public License |
9 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
10 | |
11 | CiviCRM is distributed in the hope that it will be useful, but |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14 | See the GNU Affero General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Affero General Public |
17 | License and the CiviCRM Licensing Exception along |
18 | with this program; if not, contact CiviCRM LLC |
19 | at info[AT]civicrm[DOT]org. If you have questions about the |
20 | GNU Affero General Public License or the licensing of CiviCRM, |
21 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
22 +--------------------------------------------------------------------+
23 */
24 /*
25 * Copyright (C) 2009-2010 Xavier Dutoit
26 * Licensed to CiviCRM under the Academic Free License version 3.0.
27 */
28
29 var CRM = CRM || {};
30
31 (function($, CRM) {
32 /**
33 * Almost like {crmURL} but on the client side
34 * eg: var url = CRM.url('civicrm/contact/view', {reset:1,cid:42});
35 * or: $('a.my-link').crmURL();
36 */
37 var tplURL = '/civicrm/example?placeholder';
38 var urlInitted = false;
39 CRM.url = function (p, params) {
40 if (p == "init") {
41 tplURL = params;
42 urlInitted = true;
43 return;
44 }
45 if (!urlInitted) {
46 console && console.log && console.log('Warning: CRM.url called before initialization');
47 }
48 params = params || '';
49 var frag = p.split ('?');
50 var url = tplURL.replace("civicrm/example", frag[0]);
51
52 if (typeof(params) == 'string') {
53 url = url.replace("placeholder", params);
54 }
55 else {
56 url = url.replace("placeholder", $.param(params));
57 }
58 if (frag[1]) {
59 url += (url.indexOf('?') === (url.length - 1) ? '' : '&') + frag[1];
60 }
61 // remove trailing "?"
62 if (url.indexOf('?') === (url.length - 1)) {
63 url = url.slice(0, (url.length - 1));
64 }
65 return url;
66 };
67
68 // Backwards compatible with jQuery fn
69 $.extend ({'crmURL':
70 function (p, params) {
71 console && console.log && console.log('Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.');
72 return CRM.url(p, params);
73 }
74 });
75
76 $.fn.crmURL = function () {
77 return this.each(function() {
78 if (this.href) {
79 this.href = CRM.url(this.href);
80 }
81 });
82 };
83
84 /**
85 * AJAX api
86 */
87 CRM.api = function(entity, action, params, options) {
88 // Default settings
89 var settings = {
90 context: null,
91 success: function(result, settings) {
92 return true;
93 },
94 error: function(result, settings) {
95 $().crmError(result.error_message, ts('Error'));
96 return false;
97 },
98 callBack: function(result, settings) {
99 if (result.is_error == 1) {
100 return settings.error.call(this, result, settings);
101 }
102 return settings.success.call(this, result, settings);
103 },
104 ajaxURL: 'civicrm/ajax/rest'
105 };
106 action = action.toLowerCase();
107 // Default success handler
108 switch (action) {
109 case "update":
110 case "create":
111 case "setvalue":
112 case "replace":
113 settings.success = function() {
114 CRM.alert('', ts('Saved'), 'success');
115 return true;
116 };
117 break;
118 case "delete":
119 settings.success = function() {
120 CRM.alert('', ts('Removed'), 'success');
121 return true;
122 };
123 }
124 params = {
125 entity: entity,
126 action: action,
127 json: JSON.stringify(params)
128 };
129 // Pass copy of settings into closure to preserve its value during multiple requests
130 (function(stg) {
131 $.ajax({
132 url: stg.ajaxURL.indexOf('http') === 0 ? stg.ajaxURL : CRM.url(stg.ajaxURL),
133 dataType: 'json',
134 data: params,
135 type: action.indexOf('get') < 0 ? 'POST' : 'GET',
136 success: function(result) {
137 stg.callBack.call(stg.context, result, stg);
138 }
139 });
140 })($.extend({}, settings, options));
141 };
142
143 // Backwards compatible with jQuery fn
144 $.fn.crmAPI = function(entity, action, params, options) {
145 console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.');
146 return CRM.api.call(this, entity, action, params, options);
147 };
148
149 /**
150 * FIXME: This function is not documented, is not used anywhere in the codebase, and doesn't
151 * clearly differentiate field elements which store the "contact id" versus the "contact label".
152 * My guess is that it's designed more for "quick-search" and less for "CRUD forms".
153 *
154 * @param params
155 * @param options
156 * @return {*}
157 */
158 $.fn.crmAutocomplete = function (params, options) {
159 if (typeof params == 'undefined') params = {};
160 if (typeof options == 'undefined') options = {};
161 params = $().extend({
162 rowCount:35,
163 json:1,
164 entity:'Contact',
165 action:'getquick',
166 sequential:1
167 }, params);
168
169 options = $().extend({}, {
170 field :'name',
171 skip : ['id','contact_id','contact_type','contact_is_deleted',"email_id",'address_id', 'country_id'],
172 result: function(data){
173 return false;
174 },
175 formatItem: function(data,i,max,value,term){
176 var tmp = [];
177 for (attr in data) {
178 if ($.inArray (attr, options.skip) == -1 && data[attr]) {
179 tmp.push(data[attr]);
180 }
181 }
182 return tmp.join(' :: ');
183 },
184 parse: function (data){
185 var acd = new Array();
186 for(cid in data.values){
187 delete data.values[cid]["data"];// to be removed once quicksearch doesn't return data
188 acd.push({ data:data.values[cid], value:data.values[cid].sort_name, result:data.values[cid].sort_name });
189 }
190 return acd;
191 },
192 delay:100,
193 width:250,
194 minChars:1
195 }, options
196 );
197 var contactUrl = CRM.url('civicrm/ajax/rest', params);
198
199 return this.each(function() {
200 var selector = this;
201 if (typeof $.fn.autocomplete != 'function')
202 $.fn.autocomplete = cj.fn.autocomplete;//to work around the fubar cj
203 var extraP = {};
204 extraP [options.field] = function () {return $(selector).val();};
205 $(this).autocomplete( contactUrl, {
206 extraParams:extraP,
207 formatItem: function(data,i,max,value,term){
208 return options.formatItem(data,i,max,value,term);
209 },
210 parse: function(data){ return options.parse(data);},
211 width: options.width,
212 delay:options.delay,
213 max:25,
214 dataType:'json',
215 minChars:options.minChars,
216 selectFirst: true
217 }).result(function(event, data, formatted) {
218 options.result(data);
219 });
220 });
221 }
222
223 })(jQuery, CRM);