]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/jsclass_async.js
Release 6.2.0
[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-2011 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 function method_callback (request_id,rslt,e) {
48         if(rslt == null) {
49             return;
50         }
51
52         if(typeof (global_request_registry[request_id]) != 'undefined') {       
53             widget = global_request_registry[request_id][0];
54             method_name = global_request_registry[request_id][1];
55             widget[method_name](rslt);
56         }
57 }
58                                                                                    
59
60 //////////////////////////////////////////////////
61 // class: SugarVCalClient
62 // async retrieval/parsing of vCal freebusy info 
63 // 
64 //////////////////////////////////////////////////
65
66 SugarClass.inherit("SugarVCalClient","SugarClass");
67
68 function SugarVCalClient() {
69         this.init();
70 }
71   
72     SugarVCalClient.prototype.init = function(){
73       //this.urllib = importModule("urllib");
74     }
75
76     SugarVCalClient.prototype.load = function(user_id,request_id){
77
78       this.user_id = user_id;
79
80       // get content at url and declare the callback using anon function:
81       urllib.getURL('./vcal_server.php?type=vfb&source=outlook&user_id='+user_id,[["Content-Type", "text/plain"]], function (result) { 
82                   if (typeof GLOBAL_REGISTRY.freebusy == 'undefined')
83                   {
84                      GLOBAL_REGISTRY.freebusy = new Object();
85                   }
86                           if (typeof GLOBAL_REGISTRY.freebusy_adjusted == 'undefined')
87                   {
88                      GLOBAL_REGISTRY.freebusy_adjusted = new Object();
89                   }
90                   // parse vCal and put it in the registry using the user_id as a key:
91                   GLOBAL_REGISTRY.freebusy[user_id] = SugarVCalClient.parseResults(result.responseText, false);
92                   // parse for current user adjusted vCal
93                   GLOBAL_REGISTRY.freebusy_adjusted[user_id] = SugarVCalClient.parseResults(result.responseText, true);
94                   // now call the display() on the widget registered at request_id:
95                   global_request_registry[request_id][0].display();
96                   })
97     }
98
99     // parse vCal freebusy info and return object
100     SugarVCalClient.prototype.parseResults = function(textResult, adjusted){
101       var match = /FREEBUSY.*?\:([\w]+)\/([\w]+)/g;
102     //  datetime = new SugarDateTime();
103       var result;
104       var timehash = new Object();
105       var dst_start;
106       var dst_end;
107
108           if(GLOBAL_REGISTRY.current_user.fields.dst_start == null) 
109                 dst_start = '19700101T000000Z';
110           else 
111                 dst_start = GLOBAL_REGISTRY.current_user.fields.dst_start.replace(/ /gi, 'T').replace(/:/gi,'').replace(/-/gi,'') + 'Z';
112                 
113           if(GLOBAL_REGISTRY.current_user.fields.dst_end == null)               
114                 dst_end = '19700101T000000Z';
115       else 
116                 dst_end = GLOBAL_REGISTRY.current_user.fields.dst_end.replace(/ /gi, 'T').replace(/:/gi,'').replace(/-/gi,'') + 'Z';
117                 
118       gmt_offset_secs = GLOBAL_REGISTRY.current_user.fields.gmt_offset * 60;
119       // loop thru all FREEBUSY matches
120       while(((result= match.exec(textResult))) != null)
121       {
122         var startdate;
123         var enddate;
124         if(adjusted) {// send back adjusted for current_user
125                   startdate = SugarDateTime.parseAdjustedDate(result[1], dst_start, dst_end, gmt_offset_secs);
126           enddate = SugarDateTime.parseAdjustedDate(result[2], dst_start, dst_end, gmt_offset_secs);
127         }
128         else { // GMT
129               startdate = SugarDateTime.parseUTCDate(result[1]);
130           enddate = SugarDateTime.parseUTCDate(result[2]);
131             }
132
133         var startmins = startdate.getUTCMinutes();
134
135         // pick the start slot based on the minutes
136         if ( startmins >= 0 && startmins < 15) {
137           startdate.setUTCMinutes(0);
138         }
139         else if ( startmins >= 15 && startmins < 30) {
140           startdate.setUTCMinutes(15);
141         }
142         else if ( startmins >= 30 && startmins < 45) {
143           startdate.setUTCMinutes(30);
144         }
145         else {
146           startdate.setUTCMinutes(45);
147         }
148  
149         // starting at startdate, create hash of each busy 15 min 
150         // timeslot and store as a key
151         for(var i=0;i<100;i++)
152         {
153           if (startdate.valueOf() < enddate.valueOf())
154           {
155             var hash = SugarDateTime.getUTCHash(startdate);
156             if (typeof (timehash[hash]) == 'undefined')
157             {
158               timehash[hash] = 0;            
159             }
160             timehash[hash] += 1;            
161             startdate = new Date(startdate.valueOf()+(15*60*1000));
162
163           }
164           else
165           {
166             break;
167           }
168         }
169       }
170       return timehash;  
171     }
172     SugarVCalClient.parseResults = SugarVCalClient.prototype.parseResults;
173
174 //////////////////////////////////////////////////
175 // class: SugarRPCClient
176 // wrapper around async JSON-RPC client class
177 // 
178 //////////////////////////////////////////////////
179 SugarRPCClient.allowed_methods = ['retrieve','query','save','set_accept_status','get_objects_from_module', 'email', 'get_user_array', 'get_full_list'];
180
181 SugarClass.inherit("SugarRPCClient","SugarClass");
182
183 function SugarRPCClient() {
184         this.init();
185 }
186
187 /*
188  * PUT NEW METHODS IN THIS ARRAY:
189  */
190 SugarRPCClient.prototype.allowed_methods = ['retrieve','query','save','set_accept_status', 'get_objects_from_module', 'email', 'get_user_array', 'get_full_list'];
191
192 SugarRPCClient.prototype.init = function() {
193         this._serviceProxy;
194         this._showError= function (e){ 
195                 alert("ERROR CONNECTING to: ./index.php?entryPoint=json_server, ERROR:"+e); 
196         }
197         this.serviceURL = './index.php?entryPoint=json_server';
198         this._serviceProxy = new jsonrpc.ServiceProxy(this.serviceURL,this.allowed_methods);
199 }
200
201 // send a 3rd argument of value 'true' to make the call synchronous.
202 // in synchronous mode, the return will be the result.
203 // in asynchronous mode, the return will be the request_id to map the call-back function to.
204 SugarRPCClient.prototype.call_method = function(method,args) {
205         var self=this;
206         try {
207                 var the_result;
208         
209                 if(arguments.length == 3 && arguments[2] == true) {
210                         // aha! fooled you! this function can be called synchronous!
211                         the_result = this._serviceProxy[method](args);
212                 } else {
213                         // make the call asynchronous
214                         this._serviceProxy[method](args, method_callback);
215                         the_result = this._serviceProxy.httpConn.request_id;
216                 }
217                 return the_result;
218         } catch(e) {//error before calling server
219                 this._showError(e);
220         }
221 }
222
223
224 var global_rpcClient =  new SugarRPCClient();