2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 require_once('modules/Calendar/Calendar.php');
41 class vCal extends SugarBean {
50 var $module_dir = "vCals";
51 var $table_name = "vcals";
53 var $object_name = "vCal";
55 var $new_schema = true;
57 var $field_defs = array(
60 // This is used to retrieve related fields from form posts.
61 var $additional_column_fields = Array();
63 const UTC_FORMAT = 'Ymd\THi00\Z';
66 const CHARSPERLINE = 75;
72 $this->disable_row_level_security = true;
75 function get_summary_text()
81 function fill_in_additional_list_fields()
85 function fill_in_additional_detail_fields()
89 function get_list_view_data()
93 // combines all freebusy vcals and returns just the FREEBUSY lines as a string
94 function get_freebusy_lines_cache(&$user_bean)
96 $ical_array = array();
97 // First, get the list of IDs.
98 $query = "SELECT id from vcals where user_id='{$user_bean->id}' AND type='vfb' AND deleted=0";
99 $vcal_arr = $this->build_related_list($query, new vCal());
101 foreach ($vcal_arr as $focus)
103 if (empty($focus->content))
108 $ical_arr = self::create_ical_array_from_string($focus->content);
109 $ical_array = array_merge($ical_array, $ical_arr);
112 return self::create_ical_string_from_array($ical_array);
115 // query and create the FREEBUSY lines for SugarCRM Meetings and Calls and
117 function create_sugar_freebusy($user_bean, $start_date_time, $end_date_time)
119 $ical_array = array();
120 global $DO_USER_TIME_OFFSET,$timedate;
122 $DO_USER_TIME_OFFSET = true;
123 if(empty($GLOBALS['current_user']) || empty($GLOBALS['current_user']->id)) {
124 $GLOBALS['current_user'] = $user_bean;
126 // get activities.. queries Meetings and Calls
128 CalendarActivity::get_activities($user_bean->id,
129 array("show_calls" => true),
134 // loop thru each activity, get start/end time in UTC, and return FREEBUSY strings
135 foreach($acts_arr as $act)
137 $startTimeUTC = $act->start_time->format(self::UTC_FORMAT);
138 $endTimeUTC = $act->end_time->format(self::UTC_FORMAT);
140 $ical_array[] = array("FREEBUSY", $startTimeUTC ."/". $endTimeUTC);
142 return self::create_ical_string_from_array($ical_array);
146 // return a freebusy vcal string
147 function get_vcal_freebusy($user_focus,$cached=true)
149 global $locale, $timedate;
150 $ical_array = array();
151 $ical_array[] = array("BEGIN", "VCALENDAR");
152 $ical_array[] = array("VERSION", "2.0");
153 $ical_array[] = array("PRODID", "-//SugarCRM//SugarCRM Calendar//EN");
154 $ical_array[] = array("BEGIN", "VFREEBUSY");
156 $name = $locale->getLocaleFormattedName($user_focus->first_name, $user_focus->last_name);
157 $email = $user_focus->email1;
159 // get current date for the user
160 $now_date_time = $timedate->getNow(true);
162 // get start date ( 1 day ago )
163 $start_date_time = $now_date_time->get("yesterday");
165 // get date 2 months from start date
166 global $sugar_config;
168 if (isset($sugar_config['vcal_time']) && $sugar_config['vcal_time'] != 0 && $sugar_config['vcal_time'] < 13)
170 $timeOffset = $sugar_config['vcal_time'];
172 $end_date_time = $start_date_time->get("+$timeOffset months");
174 // get UTC time format
175 $utc_start_time = $start_date_time->asDb();
176 $utc_end_time = $end_date_time->asDb();
177 $utc_now_time = $now_date_time->asDb();
179 $ical_array[] = array("ORGANIZER;CN=$name", "VFREEBUSY");
180 $ical_array[] = array("DTSTART", $utc_start_time);
181 $ical_array[] = array("DTEND", $utc_end_time);
183 $str = self::create_ical_string_from_array($ical_array);
185 // now insert the freebusy lines
186 // retrieve cached freebusy lines from vcals
187 if ($timeOffset != 0)
191 $str .= $this->get_freebusy_lines_cache($user_focus);
193 // generate freebusy from Meetings and Calls
196 $str .= $this->create_sugar_freebusy($user_focus,$start_date_time,$end_date_time);
200 // UID:20030724T213406Z-10358-1000-1-12@phoenix
201 $str .= self::fold_ical_lines("DTSTAMP", $utc_now_time) . self::EOL;
202 $str .= "END:VFREEBUSY".self::EOL;
203 $str .= "END:VCALENDAR".self::EOL;
210 function cache_sugar_vcal(&$user_focus)
212 self::cache_sugar_vcal_freebusy($user_focus);
216 // caches vcal for Activities in Sugar database
217 function cache_sugar_vcal_freebusy(&$user_focus)
220 // set freebusy members and save
221 $arr = array('user_id'=>$user_focus->id,'type'=>'vfb','source'=>'sugar');
222 $focus->retrieve_by_string_fields($arr);
225 $focus->content = $focus->get_vcal_freebusy($user_focus,false);
226 $focus->type = 'vfb';
227 $focus->date_modified = null;
228 $focus->source = 'sugar';
229 $focus->user_id = $user_focus->id;
234 * Lines of text SHOULD NOT be longer than 75 octets, excluding the line break.
235 * Long content lines SHOULD be split into a multiple line representations using a line "folding" technique
237 public static function fold_ical_lines($key, $value)
239 $iCalValue = $key . ":" . $value;
241 if (strlen($iCalValue) <= self::CHARSPERLINE) {
245 $firstchars = substr($iCalValue, 0, self::CHARSPERLINE);
246 $remainingchars = substr($iCalValue, self::CHARSPERLINE);
247 $end = self::EOL . self::TAB;
249 $remainingchars = substr(
251 $end . $remainingchars,
252 self::CHARSPERLINE + strlen(self::EOL),
256 -strlen($end) // exclude last EOL and TAB chars
259 return $firstchars . $remainingchars;
263 * this function takes an iCal string and converts it to iCal array while following RFC rules
265 public static function create_ical_array_from_string($ical_string)
267 $ical_string = preg_replace("/\r\n\s+/", "", $ical_string);
268 $lines = preg_split("/\r?\n/", $ical_string);
269 $ical_array = array();
271 foreach ($lines as $line) {
272 $line = self::unescape_ical_chars($line);
273 $line = explode(":", $line, 2);
274 if (count($line) != 2) {
277 $ical_array[] = array($line[0], $line[1]);
284 * this function takes an iCal array and converts it to iCal string while following RFC rules
286 public static function create_ical_string_from_array($ical_array)
289 foreach ($ical_array as $ical) {
290 $str .= self::fold_ical_lines($ical[0], self::escape_ical_chars($ical[1])) . self::EOL;
296 * escape iCal chars as per RFC 5545: http://tools.ietf.org/html/rfc5545#section-3.3.11
298 * @param string $string string to escape chars
299 * @return escaped string
301 public static function escape_ical_chars($string)
303 $string = str_replace(array("\\", "\r", "\n", ";", ","), array("\\\\", "\\r", "\\n", "\\;", "\\,"), $string);
308 * unescape iCal chars as per RFC 5545: http://tools.ietf.org/html/rfc5545#section-3.3.11
310 * @param string $string string to escape chars
311 * @return unescaped string
313 public static function unescape_ical_chars($string)
315 $string = str_replace(array("\\r", "\\n", "\\;", "\\,", "\\\\"), array("\r", "\n", ";", ",", "\\"), $string);
320 * get ics file content for meeting invite email
322 public static function get_ical_event(SugarBean $bean, User $user){
324 $ical_array = array();
326 $ical_array[] = array("BEGIN", "VCALENDAR");
327 $ical_array[] = array("VERSION", "2.0");
328 $ical_array[] = array("PRODID", "-//SugarCRM//SugarCRM Calendar//EN");
329 $ical_array[] = array("BEGIN", "VEVENT");
330 $ical_array[] = array("UID", $bean->id);
331 $ical_array[] = array("ORGANIZED;CN=" . $user->full_name, $user->email1);
332 $ical_array[] = array("DTSTART", $timedate->fromDb($bean->date_start)->format(self::UTC_FORMAT));
333 $ical_array[] = array("DTEND", $timedate->fromDb($bean->date_end)->format(self::UTC_FORMAT));
335 $ical_array[] = array(
337 $GLOBALS['timedate']->getNow(false)->format(self::UTC_FORMAT)
339 $ical_array[] = array("SUMMARY", $bean->name);
340 $ical_array[] = array("LOCATION", $bean->location);
342 $descPrepend = empty($bean->join_url) ? "" : $bean->join_url . self::EOL . self::EOL;
343 $ical_array[] = array("DESCRIPTION", $descPrepend . $bean->description);
345 $ical_array[] = array("END", "VEVENT");
346 $ical_array[] = array("END", "VCALENDAR");
348 return self::create_ical_string_from_array($ical_array);