1 /*********************************************************************************
2 * SugarCRM Community Edition 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 ********************************************************************************/
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
45 //////////////////////////////////////////////////////////////////
48 * This is the callback function called from SugarRPCClient.prototype.call_method
50 * @param o The response object returned by YUI2's ajax request.
52 function method_callback (o) {
53 var resp = YAHOO.lang.JSON.parse(o.responseText),
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);
68 //////////////////////////////////////////////////
69 // class: SugarVCalClient
70 // async retrieval/parsing of vCal freebusy info
72 //////////////////////////////////////////////////
74 SugarClass.inherit("SugarVCalClient","SugarClass");
76 function SugarVCalClient() {
80 SugarVCalClient.prototype.init = function() {}
82 SugarVCalClient.prototype.load = function(user_id, request_id) {
83 this.user_id = user_id;
85 // Bug 44239: Removed reliance on jsolait
86 YAHOO.util.Connect.asyncRequest('GET', './vcal_server.php?type=vfb&source=outlook&user_id=' + user_id, {
87 success: function (result) {
88 if (typeof GLOBAL_REGISTRY.freebusy == 'undefined') {
89 GLOBAL_REGISTRY.freebusy = new Object();
91 if (typeof GLOBAL_REGISTRY.freebusy_adjusted == 'undefined') {
92 GLOBAL_REGISTRY.freebusy_adjusted = new Object();
94 // parse vCal and put it in the registry using the user_id as a key:
95 GLOBAL_REGISTRY.freebusy[user_id] = SugarVCalClient.parseResults(result.responseText, false);
96 // parse for current user adjusted vCal
97 GLOBAL_REGISTRY.freebusy_adjusted[user_id] = SugarVCalClient.parseResults(result.responseText, true);
98 // now call the display() on the widget registered at request_id:
99 global_request_registry[request_id][0].display();
101 failure: function(result) { this.success(result); },
102 argument: { result: result }
106 // parse vCal freebusy info and return object
107 SugarVCalClient.prototype.parseResults = function(textResult, adjusted) {
108 var match = /FREEBUSY.*?\:([\w]+)\/([\w]+)/g;
109 // datetime = new SugarDateTime();
111 var timehash = new Object();
115 if (GLOBAL_REGISTRY.current_user.fields.dst_start == null)
116 dst_start = '19700101T000000Z';
118 dst_start = GLOBAL_REGISTRY.current_user.fields.dst_start.replace(/ /gi, 'T').replace(/:/gi, '').replace(/-/gi, '') + 'Z';
120 if (GLOBAL_REGISTRY.current_user.fields.dst_end == null)
121 dst_end = '19700101T000000Z';
123 dst_end = GLOBAL_REGISTRY.current_user.fields.dst_end.replace(/ /gi, 'T').replace(/:/gi, '').replace(/-/gi, '') + 'Z';
125 gmt_offset_secs = GLOBAL_REGISTRY.current_user.fields.gmt_offset * 60;
126 // loop thru all FREEBUSY matches
127 while (((result = match.exec(textResult))) != null) {
130 if (adjusted) {// send back adjusted for current_user
131 startdate = SugarDateTime.parseAdjustedDate(result[1], dst_start, dst_end, gmt_offset_secs);
132 enddate = SugarDateTime.parseAdjustedDate(result[2], dst_start, dst_end, gmt_offset_secs);
135 startdate = SugarDateTime.parseUTCDate(result[1]);
136 enddate = SugarDateTime.parseUTCDate(result[2]);
139 var startmins = startdate.getUTCMinutes();
141 // pick the start slot based on the minutes
142 if (startmins >= 0 && startmins < 15) {
143 startdate.setUTCMinutes(0);
145 else if (startmins >= 15 && startmins < 30) {
146 startdate.setUTCMinutes(15);
148 else if (startmins >= 30 && startmins < 45) {
149 startdate.setUTCMinutes(30);
152 startdate.setUTCMinutes(45);
155 // starting at startdate, create hash of each busy 15 min
156 // timeslot and store as a key
157 while (startdate.valueOf() < enddate.valueOf()) {
158 var hash = SugarDateTime.getUTCHash(startdate);
159 if (typeof (timehash[hash]) == 'undefined') {
163 startdate = new Date(startdate.valueOf() + (15 * 60 * 1000));
172 SugarVCalClient.parseResults = SugarVCalClient.prototype.parseResults;
173 //////////////////////////////////////////////////
174 // class: SugarRPCClient
175 // wrapper around async JSON-RPC client class
177 //////////////////////////////////////////////////
178 SugarRPCClient.allowed_methods = ['retrieve','query','save','set_accept_status','get_objects_from_module', 'email', 'get_user_array', 'get_full_list'];
180 SugarClass.inherit("SugarRPCClient","SugarClass");
182 function SugarRPCClient() {
187 * PUT NEW METHODS IN THIS ARRAY:
189 SugarRPCClient.prototype.allowed_methods = ['retrieve','query','save','set_accept_status', 'get_objects_from_module', 'email', 'get_user_array', 'get_full_list'];
191 SugarRPCClient.prototype.init = function() {
192 this._showError= function (e){
193 alert("ERROR CONNECTING to: ./index.php?entryPoint=json_server, ERROR:"+e);
195 this.serviceURL = './index.php?entryPoint=json_server';
199 * Note: This method used to depend on JSOlait which is now removed. It has been reworked to use YUI for the aynchronous call
200 * and the synchronous call in sugar_3.js.
203 * @param synchronous Pass in true if synchronous call is desired
205 SugarRPCClient.prototype.call_method = function(method, args, synchronous) {
208 post_data = YAHOO.lang.JSON.stringify({method: method, id: 1, params: [args]});
210 synchronous = synchronous || false;
214 result = http_fetch_sync(this.serviceURL, post_data);
215 result = YAHOO.lang.JSON.parse(result.responseText).result;
217 } else { // asynchronous call
218 // note: Unfortunately we don't have a separate error handler and it is built into the method_callback. Maybe a future todo.
219 transaction = YAHOO.util.Connect.asyncRequest('POST', this.serviceURL, {success: method_callback, failure: method_callback}, post_data);
220 return transaction.tId;
222 } catch(e) { // error before calling server
227 var global_rpcClient = new SugarRPCClient();