]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/service/utils/SugarRest.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / service / utils / SugarRest.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
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.
11  * 
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
15  * details.
16  * 
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
20  * 02110-1301 USA.
21  * 
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.
24  * 
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.
28  * 
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  ********************************************************************************/
35
36
37 if(typeof(SugarRest) == 'undefined'){
38
39 /**
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 
43  */     
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;
48         this.user_id = false;
49         this.application_name = application_name;
50         
51 }
52
53 /**
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
59  */
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);
65 }
66
67 /**
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 
71  */
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;
75         if(args != null){
76                 m = YAHOO.lang.JSON.stringify(args);
77                 query += '&rest_data=' + m;
78         }
79         return query;
80 }       
81
82 /**
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
85  */
86 SugarRest.prototype.get_server_info =  function(callback){
87         this.call('get_server_info', '', callback);
88 }
89
90 /**
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
94  */
95 SugarRest.prototype.login = function(user_name, password){
96         encryption = 'PLAIN';
97         if (typeof(hex_md5) != 'undefined') {
98                 password = hex_md5(password);
99                 encryption = 'MD5';
100         }
101         
102         var loginData = [{
103                 user_name: user_name,
104                 password:password,
105                 encryption: encryption,
106         },this.application_name];
107         console.log('Encryption:' + encryption);
108         this.call('login', loginData, this._login);
109         
110 }
111
112 /**
113  * takes the result of a login attempt and stores the session id and user id for the logged in user
114  * @param {Object} o
115  */
116 SugarRest.prototype._login = function(o){
117                 
118         data = YAHOO.lang.JSON.parse(o.responseText);
119         console.log(data);
120         this.session_id = data['id'];
121         this.user_id = data['name_value_list']['user_id'];
122 }
123
124 /**
125  * logs a user out of a session
126  * @param {Object} callback
127  */
128 SugarRest.prototype.logout = function(callback){
129         this.call('logout', this.session_id, callback);
130 }
131
132 /**
133  * 
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 - 
138  */
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);
141 }
142
143 /**
144  * 
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 - 
149  */
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);
152 }
153
154 SugarRest.prototype.seamless_login = function(){
155         this.call('seamless_login', this.session_id, this._seamless_login);
156 }
157
158 SugarRest.prototype._seamless_login = function(o){
159         window.open('GET', this.server_url);
160 }
161
162
163
164
165
166
167
168 /**
169  * 
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
178  */
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);
182 }
183
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');
187 }
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);
192         
193 }
194
195
196
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);
200 }
201
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);
205 }
206
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);
210 }
211
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 );
215 }
216
217 SugarRest.prototype.get_note_attachment = function(note_id){
218         this.call('get_note_attachment', [this.session_id, note_id]);
219 }
220
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 );
224 }
225
226 SugarRest.prototype.set_document_revision = function(revision_id){
227         this.call('get_document_revision', [this.session_id, revision_id]);
228 }
229
230
231 /**
232  * logs an object to the console if it is available
233  * @param {Object} o
234  */
235 SugarRest.prototype.log = function(o){
236         data = YAHOO.lang.JSON.parse(o.responseText);
237         if(typeof(console) != 'undefined')console.log(data);
238         return data;
239 }
240
241
242         
243         
244 }