1 /*********************************************************************************
2 * SugarCRM is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Affero General Public License version 3 as published by the
7 * Free Software Foundation with the addition of the following permission added
8 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * You should have received a copy of the GNU Affero General Public License along with
18 * this program; if not, see http://www.gnu.org/licenses or write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25 * The interactive user interfaces in modified source and object code versions
26 * of this program must display Appropriate Legal Notices, as required under
27 * Section 5 of the GNU Affero General Public License version 3.
29 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30 * these Appropriate Legal Notices must retain the display of the "Powered by
31 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32 * technical reasons, the Appropriate Legal Notices must display the words
33 * "Powered by SugarCRM".
34 ********************************************************************************/
37 if(typeof(SugarRest) == 'undefined'){
40 * Constructor for SugarRest Service
41 * @param {Object} proxy_url - Relative URL to a proxy file or to the Rest Service
42 * @param {Object} server_url - Full URL to the Web Server
44 SugarRest = function(proxy_url, server_url, application_name){
45 this.proxy_url = proxy_url;
46 this.server_url = server_url;
47 this.session_id = false;
49 this.application_name = application_name;
54 * executes an AJAX request to the Server
55 * @param {Object} method - name of the REST method you wish to call on
56 * @param {Object} args - arguments for the method
57 * @param {Object} callback - a function to call on once the request has returned
58 * @param {Object} params - arguments that are passed along with the callback
60 SugarRest.prototype.call = function(method, args, callback, params, response_type){
61 if(!callback)callback = this.log;
62 console.log(callback);
63 query = this.getQuery(method, args, response_type);
64 YAHOO.util.Connect.asyncRequest('POST', this.proxy_url , {success:callback, failure:callback,scope:this, argument:params}, query);
68 * generates the query string for the AJAX request
69 * @param {Object} method - name of the REST method you wish to call on
70 * @param {Object} args - arguments for the method
72 SugarRest.prototype.getQuery = function(method, args, response_type){
73 if(!response_type)response_type = 'JSON';
74 query = 'method=' + method + '&input_type=JSON&response_type=' + response_type;
76 m = YAHOO.lang.JSON.stringify(args);
77 query += '&rest_data=' + m;
83 * returns the sugar servers information including version , GMT time, and server time
84 * @param {Object} callback - a function your would like to have called on when the AJAX call completes
86 SugarRest.prototype.get_server_info = function(callback){
87 this.call('get_server_info', '', callback);
91 * logs in a user to the SugarCRM instance with a the given user_name and password
92 * @param {Object} user_name
93 * @param {Object} password
95 SugarRest.prototype.login = function(user_name, password){
97 if (typeof(hex_md5) != 'undefined') {
98 password = hex_md5(password);
103 user_name: user_name,
105 encryption: encryption,
106 },this.application_name];
107 console.log('Encryption:' + encryption);
108 this.call('login', loginData, this._login);
113 * takes the result of a login attempt and stores the session id and user id for the logged in user
116 SugarRest.prototype._login = function(o){
118 data = YAHOO.lang.JSON.parse(o.responseText);
120 this.session_id = data['id'];
121 this.user_id = data['name_value_list']['user_id'];
125 * logs a user out of a session
126 * @param {Object} callback
128 SugarRest.prototype.logout = function(callback){
129 this.call('logout', this.session_id, callback);
134 * @param {Object} module_name - name of the module the record belongs to
135 * @param {Object} id - the id of the record
136 * @param {Object} select_fields - a list of fields your would like to retrieve from the record
137 * @param {Object} link_name_to_fields_array -
139 SugarRest.prototype.get_entry = function( module_name, id, select_fields,link_names_to_related_fields, callback){
140 this.call('get_entry', [this.session_id,module_name, id, select_fields,link_names_to_related_fields], callback);
145 * @param {Object} module_name - name of the module the record belongs to
146 * @param {Object} id - the id of the record
147 * @param {Object} select_fields - a list of fields your would like to retrieve from the record
148 * @param {Object} link_name_to_fields_array -
150 SugarRest.prototype.get_entries = function( module_name, ids, select_fields,link_names_to_related_fields, callback){
151 this.call('get_entries', [this.session_id,module_name, ids, select_fields,link_names_to_related_fields], callback);
154 SugarRest.prototype.seamless_login = function(){
155 this.call('seamless_login', this.session_id, this._seamless_login);
158 SugarRest.prototype._seamless_login = function(o){
159 window.open('GET', this.server_url);
170 * @param {Object} module_name
171 * @param {Object} query
172 * @param {Object} order_by
173 * @param {Object} select_fields
174 * @param {Object} offset
175 * @param {Object} max_results
176 * @param {Object} link_name_to_fields
177 * @param {Object} callback
179 SugarRest.prototype.get_entry_list = function(module_name, query, order_by, select_fields,offset,max_results, link_names_to_related_fields, callback){
180 var data = [this.session_id, module_name, query, order_by,offset,select_fields, link_names_to_related_fields, max_results , 0];
181 this.call('get_entry_list', data, callback);
184 SugarRest.prototype.rss_get_entry_list = function(module_name, query, order_by, select_fields, offset, max_results, link_names_to_related_fields, callback){
185 var data = [this.session_id, module_name, query, order_by,offset,select_fields, link_names_to_related_fields, max_results , 0];
186 this.call('get_entry_list', data, callback, null, 'RSS');
188 SugarRest.prototype.rss_popup_get_entry_list = function(module_name, query, order_by, select_fields, offset, max_results, link_names_to_related_fields, callback){
189 var data = [this.session_id, module_name, query, order_by,offset,select_fields, link_names_to_related_fields, max_results , 0];
190 query = this.getQuery('get_entry_list', data, 'RSS');
191 window.open(this.proxy_url+ '?' + query);
197 SugarRest.prototype.set_relationship = function(module_name, module_id, link_name, related_ids){
198 var data = [this.session_id, module_name, module_id, link_name, related_ids];
199 this.call('set_relationship', data, callback);
202 SugarRest.prototype.set_relationships = function (module_name, module_ids, link_names, related_ids){
203 var data = [this.session_id, module_name, module_ids, link_names, related_ids];
204 this.call('set_relationship', data, callback);
207 SugarRest.prototype.get_relationships = function(module_name, module_id, link_name, link_query, link_fields, link_related_fields){
208 var data = [this.session_id, module_name, module_id, link_name, link_query, link_fields, link_related_fields];
209 this.call('get_relationships', data, callback);
212 SugarRest.prototype.set_note_attachment = function(note_id, filename, base64file, related_module, related_module_id, callback){
213 var data = [this.session_id, [note_id, filename, base64file, related_module, related_module_id]];
214 this.call('set_note_attachment',data, callback );
217 SugarRest.prototype.get_note_attachment = function(note_id){
218 this.call('get_note_attachment', [this.session_id, note_id]);
221 SugarRest.prototype.set_document_revision = function(document_id,revision, filename, base64file, callback){
222 var data = [this.session_id, [document_id, revision, filename, base64file]];
223 this.call('set_document_revision',data, callback );
226 SugarRest.prototype.set_document_revision = function(revision_id){
227 this.call('get_document_revision', [this.session_id, revision_id]);
232 * logs an object to the console if it is available
235 SugarRest.prototype.log = function(o){
236 data = YAHOO.lang.JSON.parse(o.responseText);
237 if(typeof(console) != 'undefined')console.log(data);