]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/vCals/vCal.php
Release 6.4.0
[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-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         const UTC_FORMAT = 'Ymd\THi00\Z';
73
74         function vCal()
75         {
76
77                 parent::SugarBean();
78                 $this->disable_row_level_security = true;
79         }
80
81         function get_summary_text()
82         {
83                 return "";
84         }
85
86
87         function fill_in_additional_list_fields()
88         {
89         }
90
91         function fill_in_additional_detail_fields()
92         {
93         }
94
95         function get_list_view_data()
96         {
97         }
98
99         // combines all freebusy vcals and returns just the FREEBUSY lines as a string
100         function get_freebusy_lines_cache(&$user_bean)
101         {
102                 $str = '';
103                 // First, get the list of IDs.
104                 $query = "SELECT id from vcals where user_id='{$user_bean->id}' AND type='vfb' AND deleted=0";
105                 $vcal_arr = $this->build_related_list($query, new vCal());
106
107                 foreach ($vcal_arr as $focus)
108                 {
109                         if (empty($focus->content))
110                         {
111                                 return '';
112                         }
113
114                         $lines = explode("\n",$focus->content);
115
116                         foreach ($lines as $line)
117                         {
118                                 if ( preg_match('/^FREEBUSY[;:]/i',$line))
119                                 {
120                                         $str .= "$line\n";
121                                 }
122                         }
123                 }
124
125                 return $str;
126         }
127
128         // query and create the FREEBUSY lines for SugarCRM Meetings and Calls and
129         // return the string
130         function create_sugar_freebusy($user_bean, $start_date_time, $end_date_time)
131         {
132                 $str = '';
133                 global $DO_USER_TIME_OFFSET,$timedate;
134
135                 $DO_USER_TIME_OFFSET = true;
136                 // get activities.. queries Meetings and Calls
137                 $acts_arr =
138                 CalendarActivity::get_activities($user_bean->id,
139                         array("show_calls" => true),
140                         $start_date_time,
141                         $end_date_time,
142                         'freebusy');
143
144                 // loop thru each activity, get start/end time in UTC, and return FREEBUSY strings
145                 foreach($acts_arr as $act)
146                 {
147                         $startTimeUTC = $act->start_time->format(self::UTC_FORMAT);
148                         $endTimeUTC = $act->end_time->format(self::UTC_FORMAT);
149
150                         $str .= "FREEBUSY:". $startTimeUTC ."/". $endTimeUTC."\n";
151                 }
152                 return $str;
153
154         }
155
156         // return a freebusy vcal string
157         function get_vcal_freebusy($user_focus,$cached=true)
158         {
159            global $locale, $timedate;
160            $str = "BEGIN:VCALENDAR\n";
161            $str .= "VERSION:2.0\n";
162            $str .= "PRODID:-//SugarCRM//SugarCRM Calendar//EN\n";
163            $str .= "BEGIN:VFREEBUSY\n";
164
165            $name = $locale->getLocaleFormattedName($user_focus->first_name, $user_focus->last_name);
166            $email = $user_focus->email1;
167
168            // get current date for the user
169            $now_date_time = $timedate->getNow(true);
170
171            // get start date ( 1 day ago )
172            $start_date_time = $now_date_time->get("yesterday");
173
174            // get date 2 months from start date
175                         global $sugar_config;
176                         $timeOffset = 2;
177             if (isset($sugar_config['vcal_time']) && $sugar_config['vcal_time'] != 0 && $sugar_config['vcal_time'] < 13)
178                         {
179                                 $timeOffset = $sugar_config['vcal_time'];
180                         }
181            $end_date_time = $start_date_time->get("+$timeOffset months");
182
183            // get UTC time format
184            $utc_start_time = $start_date_time->asDb();
185            $utc_end_time = $end_date_time->asDb();
186            $utc_now_time = $now_date_time->asDb();
187
188            $str .= "ORGANIZER;CN=$name:$email\n";
189            $str .= "DTSTART:$utc_start_time\n";
190            $str .= "DTEND:$utc_end_time\n";
191
192            // now insert the freebusy lines
193            // retrieve cached freebusy lines from vcals
194                    if ($timeOffset != 0)
195                    {
196            if ($cached == true)
197            {
198              $str .= $this->get_freebusy_lines_cache($user_focus);
199            }
200            // generate freebusy from Meetings and Calls
201            else
202            {
203                $str .= $this->create_sugar_freebusy($user_focus,$start_date_time,$end_date_time);
204                         }
205            }
206
207            // UID:20030724T213406Z-10358-1000-1-12@phoenix
208            $str .= "DTSTAMP:$utc_now_time\n";
209            $str .= "END:VFREEBUSY\n";
210            $str .= "END:VCALENDAR\n";
211            return $str;
212
213         }
214
215         // static function:
216         // cache vcals
217         function cache_sugar_vcal(&$user_focus)
218         {
219             vCal::cache_sugar_vcal_freebusy($user_focus);
220         }
221
222         // static function:
223         // caches vcal for Activities in Sugar database
224         function cache_sugar_vcal_freebusy(&$user_focus)
225         {
226             $focus = new vCal();
227             // set freebusy members and save
228             $arr = array('user_id'=>$user_focus->id,'type'=>'vfb','source'=>'sugar');
229             $focus->retrieve_by_string_fields($arr);
230
231
232             $focus->content = $focus->get_vcal_freebusy($user_focus,false);
233             $focus->type = 'vfb';
234             $focus->date_modified = null;
235             $focus->source = 'sugar';
236             $focus->user_id = $user_focus->id;
237             $focus->save();
238         }
239
240
241 }
242
243 ?>