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