]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/vCals/vCal.php
Release 6.5.6
[Github/sugarcrm.git] / modules / vCals / vCal.php
1 <?php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 require_once('modules/Calendar/Calendar.php');
40
41 class vCal extends SugarBean {
42         // Stored fields
43         var $id;
44         var $date_modified;
45         var $user_id;
46         var $content;
47         var $deleted;
48         var $type;
49         var $source;
50         var $module_dir = "vCals";
51         var $table_name = "vcals";
52
53         var $object_name = "vCal";
54
55         var $new_schema = true;
56
57         var $field_defs = array(
58         );
59
60         // This is used to retrieve related fields from form posts.
61         var $additional_column_fields = Array();
62
63         const UTC_FORMAT = 'Ymd\THi00\Z';
64
65         function vCal()
66         {
67
68                 parent::SugarBean();
69                 $this->disable_row_level_security = true;
70         }
71
72         function get_summary_text()
73         {
74                 return "";
75         }
76
77
78         function fill_in_additional_list_fields()
79         {
80         }
81
82         function fill_in_additional_detail_fields()
83         {
84         }
85
86         function get_list_view_data()
87         {
88         }
89
90         // combines all freebusy vcals and returns just the FREEBUSY lines as a string
91         function get_freebusy_lines_cache(&$user_bean)
92         {
93                 $str = '';
94                 // First, get the list of IDs.
95                 $query = "SELECT id from vcals where user_id='{$user_bean->id}' AND type='vfb' AND deleted=0";
96                 $vcal_arr = $this->build_related_list($query, new vCal());
97
98                 foreach ($vcal_arr as $focus)
99                 {
100                         if (empty($focus->content))
101                         {
102                                 return '';
103                         }
104
105                         $lines = explode("\n",$focus->content);
106
107                         foreach ($lines as $line)
108                         {
109                                 if ( preg_match('/^FREEBUSY[;:]/i',$line))
110                                 {
111                                         $str .= "$line\n";
112                                 }
113                         }
114                 }
115
116                 return $str;
117         }
118
119         // query and create the FREEBUSY lines for SugarCRM Meetings and Calls and
120         // return the string
121         function create_sugar_freebusy($user_bean, $start_date_time, $end_date_time)
122         {
123                 $str = '';
124                 global $DO_USER_TIME_OFFSET,$timedate;
125
126                 $DO_USER_TIME_OFFSET = true;
127                 if(empty($GLOBALS['current_user']) || empty($GLOBALS['current_user']->id)) {
128                     $GLOBALS['current_user'] = $user_bean;
129                 }
130                 // get activities.. queries Meetings and Calls
131                 $acts_arr =
132                 CalendarActivity::get_activities($user_bean->id,
133                         array("show_calls" => true),
134                         $start_date_time,
135                         $end_date_time,
136                         'freebusy');
137
138                 // loop thru each activity, get start/end time in UTC, and return FREEBUSY strings
139                 foreach($acts_arr as $act)
140                 {
141                         $startTimeUTC = $act->start_time->format(self::UTC_FORMAT);
142                         $endTimeUTC = $act->end_time->format(self::UTC_FORMAT);
143
144                         $str .= "FREEBUSY:". $startTimeUTC ."/". $endTimeUTC."\n";
145                 }
146                 return $str;
147
148         }
149
150         // return a freebusy vcal string
151         function get_vcal_freebusy($user_focus,$cached=true)
152         {
153            global $locale, $timedate;
154            $str = "BEGIN:VCALENDAR\n";
155            $str .= "VERSION:2.0\n";
156            $str .= "PRODID:-//SugarCRM//SugarCRM Calendar//EN\n";
157            $str .= "BEGIN:VFREEBUSY\n";
158
159            $name = $locale->getLocaleFormattedName($user_focus->first_name, $user_focus->last_name);
160            $email = $user_focus->email1;
161
162            // get current date for the user
163            $now_date_time = $timedate->getNow(true);
164
165            // get start date ( 1 day ago )
166            $start_date_time = $now_date_time->get("yesterday");
167
168            // get date 2 months from start date
169                         global $sugar_config;
170                         $timeOffset = 2;
171             if (isset($sugar_config['vcal_time']) && $sugar_config['vcal_time'] != 0 && $sugar_config['vcal_time'] < 13)
172                         {
173                                 $timeOffset = $sugar_config['vcal_time'];
174                         }
175            $end_date_time = $start_date_time->get("+$timeOffset months");
176
177            // get UTC time format
178            $utc_start_time = $start_date_time->asDb();
179            $utc_end_time = $end_date_time->asDb();
180            $utc_now_time = $now_date_time->asDb();
181
182            $str .= "ORGANIZER;CN=$name:$email\n";
183            $str .= "DTSTART:$utc_start_time\n";
184            $str .= "DTEND:$utc_end_time\n";
185
186            // now insert the freebusy lines
187            // retrieve cached freebusy lines from vcals
188                    if ($timeOffset != 0)
189                    {
190            if ($cached == true)
191            {
192              $str .= $this->get_freebusy_lines_cache($user_focus);
193            }
194            // generate freebusy from Meetings and Calls
195            else
196            {
197                $str .= $this->create_sugar_freebusy($user_focus,$start_date_time,$end_date_time);
198                         }
199            }
200
201            // UID:20030724T213406Z-10358-1000-1-12@phoenix
202            $str .= "DTSTAMP:$utc_now_time\n";
203            $str .= "END:VFREEBUSY\n";
204            $str .= "END:VCALENDAR\n";
205            return $str;
206
207         }
208
209         // static function:
210         // cache vcals
211         function cache_sugar_vcal(&$user_focus)
212         {
213             vCal::cache_sugar_vcal_freebusy($user_focus);
214         }
215
216         // static function:
217         // caches vcal for Activities in Sugar database
218         function cache_sugar_vcal_freebusy(&$user_focus)
219         {
220             $focus = new vCal();
221             // set freebusy members and save
222             $arr = array('user_id'=>$user_focus->id,'type'=>'vfb','source'=>'sugar');
223             $focus->retrieve_by_string_fields($arr);
224
225
226             $focus->content = $focus->get_vcal_freebusy($user_focus,false);
227             $focus->type = 'vfb';
228             $focus->date_modified = null;
229             $focus->source = 'sugar';
230             $focus->user_id = $user_focus->id;
231             $focus->save();
232         }
233
234         /**
235          * get ics file content for meeting invite email
236          */
237         public static function get_ical_event(SugarBean $bean, User $user){
238                 $str = "";
239
240                 $str .= "BEGIN:VCALENDAR\n";
241                 $str .= "VERSION:2.0\n";
242                 $str .= "PRODID:-//SugarCRM//SugarCRM Calendar//EN\n";
243                 $str .= "BEGIN:VEVENT\n";
244                 $str .= "UID:".$bean->id."\n";
245                 $str .= "ORGANIZER;CN=".$user->full_name.":".$user->email1."\n";
246                 $str .= "DTSTART:".SugarDateTime::createFromFormat($GLOBALS['timedate']->get_db_date_time_format(),$bean->date_start)->format(self::UTC_FORMAT)."\n";
247                 $str .= "DTEND:".SugarDateTime::createFromFormat($GLOBALS['timedate']->get_db_date_time_format(),$bean->date_end)->format(self::UTC_FORMAT)."\n";
248                 $str .= "DTSTAMP:". $GLOBALS['timedate']->getNow(false)->format(self::UTC_FORMAT) ."\n";
249                 $str .= "SUMMARY:" . $bean->name . "\n";
250                 $str .= "DESCRIPTION:" . $bean->description . "\n";
251                 $str .= "END:VEVENT\n";
252                 $str .= "END:VCALENDAR\n";
253
254                 return $str;
255         }
256
257 }
258
259 ?>