]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/jsclass_async.js
Release 6.5.1
[Github/sugarcrm.git] / jssource / src_files / include / javascript / jsclass_async.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
38
39 //////////////////////////////////////////////////////////////////
40 // called on the return of a JSON-RPC async request,
41 // and calls the display() method on the widget registered
42 // in the registry at the request_id key returned by the server
43
44
45 //////////////////////////////////////////////////////////////////
46
47 /**
48  * This is the callback function called from SugarRPCClient.prototype.call_method
49  * found below.
50  * @param o The response object returned by YUI2's ajax request.
51  */
52 function method_callback (o) {
53     var resp = YAHOO.lang.JSON.parse(o.responseText),
54         request_id = o.tId,
55         result = resp.result;
56
57         if(result == null) {
58             return;
59         }
60     reqid = global_request_registry[request_id];
61         if(typeof (reqid)  != 'undefined') {
62             widget = global_request_registry[request_id][0];
63             method_name = global_request_registry[request_id][1];
64             widget[method_name](result);
65         }
66 }
67
68 //////////////////////////////////////////////////
69 // class: SugarVCalClient
70 // async retrieval/parsing of vCal freebusy info
71 //
72 //////////////////////////////////////////////////
73
74 SugarClass.inherit("SugarVCalClient","SugarClass");
75
76 function SugarVCalClient() {
77         this.init();
78 }
79
80 SugarVCalClient.prototype.init = function() {}
81
82 SugarVCalClient.prototype.load = function(user_id, request_id) {
83     this.user_id = user_id;
84
85     var vcal_server = './vcal_server.php?type=vfb&source=outlook&user_id=' + user_id;
86     if (typeof(window.combo_date_start) != 'undefined') {
87         var date_start = window.combo_date_start.datetime;
88         vcal_server += '&datestart='+date_start;
89     }
90
91     // Bug 44239: Removed reliance on jsolait
92     YAHOO.util.Connect.asyncRequest('GET', vcal_server, {
93         success: function (result) {
94             if (typeof GLOBAL_REGISTRY.freebusy == 'undefined') {
95                 GLOBAL_REGISTRY.freebusy = new Object();
96             }
97             if (typeof GLOBAL_REGISTRY.freebusy_adjusted == 'undefined') {
98                 GLOBAL_REGISTRY.freebusy_adjusted = new Object();
99             }
100             // parse vCal and put it in the registry using the user_id as a key:
101             GLOBAL_REGISTRY.freebusy[user_id] = SugarVCalClient.prototype.parseResults(result.responseText, false);
102             // parse for current user adjusted vCal
103             GLOBAL_REGISTRY.freebusy_adjusted[user_id] = SugarVCalClient.prototype.parseResults(result.responseText, true);
104             // now call the display() on the widget registered at request_id:
105             global_request_registry[request_id][0].display();
106         },
107         failure: function(result) { this.success(result); },
108         argument: { result: result }
109     });
110 }
111
112 // parse vCal freebusy info and return object
113 SugarVCalClient.prototype.parseResults = function(textResult, adjusted) {
114     var match = /FREEBUSY.*?\:([\w]+)\/([\w]+)/g;
115     //  datetime = new SugarDateTime();
116     var result;
117     var timehash = new Object();
118     var dst_start;
119     var dst_end;
120
121     if (GLOBAL_REGISTRY.current_user.fields.dst_start == null)
122         dst_start = '19700101T000000Z';
123     else
124         dst_start = GLOBAL_REGISTRY.current_user.fields.dst_start.replace(/ /gi, 'T').replace(/:/gi, '').replace(/-/gi, '') + 'Z';
125
126     if (GLOBAL_REGISTRY.current_user.fields.dst_end == null)
127         dst_end = '19700101T000000Z';
128     else
129         dst_end = GLOBAL_REGISTRY.current_user.fields.dst_end.replace(/ /gi, 'T').replace(/:/gi, '').replace(/-/gi, '') + 'Z';
130
131     gmt_offset_secs = GLOBAL_REGISTRY.current_user.fields.gmt_offset * 60;
132     // loop thru all FREEBUSY matches
133     while (((result = match.exec(textResult))) != null) {
134         var startdate;
135         var enddate;
136         if (adjusted) {// send back adjusted for current_user
137             startdate = SugarDateTime.parseAdjustedDate(result[1], dst_start, dst_end, gmt_offset_secs);
138             enddate = SugarDateTime.parseAdjustedDate(result[2], dst_start, dst_end, gmt_offset_secs);
139         }
140         else { // GMT
141             startdate = SugarDateTime.parseUTCDate(result[1]);
142             enddate = SugarDateTime.parseUTCDate(result[2]);
143         }
144
145         var startmins = startdate.getUTCMinutes();
146
147         // pick the start slot based on the minutes
148         if (startmins >= 0 && startmins < 15) {
149             startdate.setUTCMinutes(0);
150         }
151         else if (startmins >= 15 && startmins < 30) {
152             startdate.setUTCMinutes(15);
153         }
154         else if (startmins >= 30 && startmins < 45) {
155             startdate.setUTCMinutes(30);
156         }
157         else {
158             startdate.setUTCMinutes(45);
159         }
160
161         // starting at startdate, create hash of each busy 15 min
162         // timeslot and store as a key
163                 while (startdate.valueOf() < enddate.valueOf()) {
164                         var hash = SugarDateTime.getUTCHash(startdate);
165                         if (typeof (timehash[hash]) == 'undefined') {
166                                 timehash[hash] = 0;
167                         }
168                         timehash[hash] += 1;
169                         startdate = new Date(startdate.valueOf() + (15 * 60 * 1000));
170
171                 }
172     }
173
174     return timehash;
175 }
176
177 SugarVCalClient.parseResults = SugarVCalClient.prototype.parseResults;
178 //////////////////////////////////////////////////
179 // class: SugarRPCClient
180 // wrapper around async JSON-RPC client class
181 //
182 //////////////////////////////////////////////////
183 SugarRPCClient.allowed_methods = ['retrieve','query','save','set_accept_status','get_objects_from_module', 'email', 'get_user_array', 'get_full_list'];
184
185 SugarClass.inherit("SugarRPCClient","SugarClass");
186
187 function SugarRPCClient() {
188         this.init();
189 }
190
191 /*
192  * PUT NEW METHODS IN THIS ARRAY:
193  */
194 SugarRPCClient.prototype.allowed_methods = ['retrieve','query','get_objects_from_module'];
195
196 SugarRPCClient.prototype.init = function() {
197         this._showError= function (e){
198                 alert("ERROR CONNECTING to: ./index.php?entryPoint=json_server, ERROR:"+e);
199         }
200         this.serviceURL = './index.php?entryPoint=json_server';
201 }
202
203 /**
204  * Note: This method used to depend on JSOlait which is now removed. It has been reworked to use YUI for the aynchronous call
205  * and the synchronous call in sugar_3.js.
206  * @param method
207  * @param args
208  * @param synchronous Pass in true if synchronous call is desired
209  */
210 SugarRPCClient.prototype.call_method = function(method, args, synchronous) {
211     var result,
212         transaction,
213         post_data = YAHOO.lang.JSON.stringify({method: method, id: 1, params: [args]});
214
215     synchronous = synchronous || false;
216
217     try {
218         if (synchronous) {
219             result = http_fetch_sync(this.serviceURL, post_data);
220             result = YAHOO.lang.JSON.parse(result.responseText).result;
221             return result;
222         } else { // asynchronous call
223             // note: Unfortunately we don't have a separate error handler and it is built into the method_callback. Maybe a future todo.
224             transaction = YAHOO.util.Connect.asyncRequest('POST', this.serviceURL, {success: method_callback, failure: method_callback}, post_data);
225             return transaction.tId;
226         }
227     } catch(e) { // error before calling server
228         this._showError(e);
229     }
230 }
231
232 var global_rpcClient =  new SugarRPCClient();