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 ********************************************************************************/
36 ///////////////////////////////////////////////
38 // superclass for all Sugar* sub-classes
40 ///////////////////////////////////////////////
42 function SugarClass() {
46 SugarClass.prototype.init = function() {}
48 // create inheritance for a class
49 SugarClass.inherit = function(className,parentClassName) {
50 var str = className+".prototype = new "+parentClassName+"();";
51 str += className+".prototype.constructor = "+className+";";
52 str += className+".superclass = "+parentClassName+".prototype;";
59 //Root class of Sugar JS Application:
61 SugarClass.inherit("SugarContainer", "SugarClass");
63 function SugarContainer(root_div) {
64 GLOBAL_REGISTRY.container = this;
68 SugarContainer.prototype.init = function(root_div) {
69 this.root_div = root_div;
70 SugarContainer.superclass.init.call(this);
73 SugarContainer.prototype.start = function(root_widget) {
74 this.root_widget = new root_widget();
75 this.root_widget.load(this.root_div);
78 var global_request_registry = new Object();
81 //////////////////////////////////////////////////
82 // class: SugarDateTime
83 // date and time utilities
85 //////////////////////////////////////////////////
87 SugarClass.inherit("SugarDateTime","SugarClass");
89 function SugarDateTime() {
93 SugarDateTime.prototype.init = function(root_div) {
94 this.root_div = root_div;
97 // return the javascript Date object
98 // given the Sugar Meetings date_start/time_start or date_end/time_end
99 SugarDateTime.mysql2jsDateTime = function(mysql_date, mysql_time) {
100 var match = new RegExp(date_reg_format);
101 if (((result = match.exec(mysql_date))) == null) {
105 var match2 = new RegExp(time_reg_format);
107 if ((result2 = match2.exec(mysql_time)) == null) {
111 var match3 = /^0(\d)/;
113 if ((result3 = match3.exec(result2[1])) != null) {
114 result2[1] = result3[1];
117 if (typeof (result2[3]) != 'undefined') {
118 if (result2[3] == 'pm' || result2[3] == 'PM') {
119 if (parseInt(result2[1]) != 12) {
120 result2[1] = parseInt(result2[1]) + 12;
123 else if (result2[1] == 12) {
128 return new Date(result[date_reg_positions['Y']], result[date_reg_positions['m']] - 1, result[date_reg_positions['d']], result2[1], result2[2], 0, 0);
130 // make it a static func
132 // return the formatted day of the week of the date given a date object
133 SugarDateTime.prototype.getFormattedDate = function(date_obj) {
135 var userDateFormat = GLOBAL_REGISTRY['current_user']['fields']['date_time_format']['date'];
136 var dow = GLOBAL_REGISTRY['calendar_strings']['dom_cal_weekdays_long'][date_obj.getDay()];
137 var month = date_obj.getMonth() + 1;
138 month = GLOBAL_REGISTRY['calendar_strings']['dom_cal_month_long'][month];
142 for (i = 0; i < 5; i++) {
143 switch (userDateFormat.charAt(i)) {
145 returnDate += " " + date_obj.getFullYear();
148 returnDate += " " + month;
151 returnDate += " " + date_obj.getDate();
154 // cn: use locale's date separator? probably not.
155 //returnDate += " " + userDateFormat.charAt(i);
162 SugarDateTime.getFormattedDate = SugarDateTime.prototype.getFormattedDate;
164 // return the formatted day of the week of the date given a date object
165 SugarDateTime.prototype.getFormattedDOW = function(date_obj) {
166 var hour = config.strings.mod_strings.Calendar.dow[date_obj.getDay()];
168 SugarDateTime.getFormattedDOW = SugarDateTime.prototype.getFormattedDOW;
170 // return the formatted hour of the date given a date object
171 SugarDateTime.getAMPM = function(date_obj) {
172 var hour = date_obj.getHour();
178 else if (hour == 12) {
181 else if (hour == 0) {
186 SugarDateTime.getFormattedHour = SugarDateTime.prototype.getFormattedHour;
188 //mod.SugarDateTime.getFormattedDate = publ.getFormattedDate;
190 // return the javascript Date object given a vCal UTC string
191 SugarDateTime.prototype.parseUTCDate = function(date_string) {
192 var match = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/;
193 if (((result = match.exec(date_string))) != null) {
194 var new_date = new Date(Date.UTC(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]) + time_offset));
200 SugarDateTime.parseUTCDate = SugarDateTime.prototype.parseUTCDate;
202 SugarDateTime.prototype.parseAdjustedDate = function(date_string, dst_start, dst_end, gmt_offset_secs) {
204 var match = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/;
205 dst_start_parse = match.exec(dst_start);
206 dst_end_parse = match.exec(dst_end);
208 if (dst_start_parse == null || dst_end_parse == null) {
209 var new_date = new Date(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]));
210 new_date = new Date(new_date.getTime() + gmt_offset_secs * 1000);
212 dst_start_obj = new Date(dst_start_parse[1], dst_start_parse[2] - 1, dst_start_parse[3], dst_start_parse[4], dst_start_parse[5], parseInt(dst_start_parse[6]));
213 dst_end_obj = new Date(dst_end_parse[1], dst_end_parse[2] - 1, dst_end_parse[3], dst_end_parse[4], dst_end_parse[5], parseInt(dst_end_parse[6]));
215 if (((result = match.exec(date_string))) != null) {
216 var new_date = new Date(result[1], result[2] - 1, result[3], result[4], result[5], parseInt(result[6]));
217 var event_ts = new_date.getTime();
218 var dst_start_ts = dst_start_obj.getTime();
219 var dst_end_ts = dst_end_obj.getTime();
221 if (((event_ts >= dst_start_ts || event_ts < dst_end_ts) && dst_start_ts > dst_end_ts)
222 || (event_ts >= dst_start_ts && event_ts < dst_end_ts)) {
223 new_date = new Date(new_date.getTime() + 60 * 60 * 1000);
226 new_date = new Date(new_date.getTime() + gmt_offset_secs * 1000);
233 SugarDateTime.parseAdjustedDate = SugarDateTime.prototype.parseAdjustedDate;
235 // create a hash based on a date
236 SugarDateTime.prototype.getUTCHash = function(startdate) {
237 var month = (startdate.getUTCMonth() < 10) ? "0" + startdate.getUTCMonth() : "" + startdate.getUTCMonth();
238 var day = (startdate.getUTCDate() < 10) ? "0" + startdate.getUTCDate() : "" + startdate.getUTCDate();
239 var hours = (startdate.getUTCHours() < 10) ? "0" + startdate.getUTCHours() : "" + startdate.getUTCHours();
240 var minutes = (startdate.getUTCMinutes() < 10) ? "0" + startdate.getUTCMinutes() : "" + startdate.getUTCMinutes();
241 return startdate.getUTCFullYear() + month + day + hours + minutes;
244 SugarDateTime.getUTCHash = SugarDateTime.prototype.getUTCHash;