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.
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 ********************************************************************************/
42 * Find first day of week according to user's settings
43 * @param SugarDateTime $date
44 * @return SugarDateTime $date
46 static function get_first_day_of_week(SugarDateTime $date){
47 $fdow = $GLOBALS['current_user']->get_first_day_of_week();
48 if($date->day_of_week < $fdow)
49 $date = $date->get('-7 days');
50 return $date->get_day_by_index_this_week($fdow);
55 * Get list of needed fields for modules
58 static function get_fields(){
80 * Get array of needed time data
81 * @param SugarBean $bean
84 static function get_time_data(SugarBean $bean){
87 $start_field = "date_start";
88 $end_field = "date_end";
90 if($bean->object_name == 'Task')
91 $start_field = $end_field = "date_due";
92 if(empty($bean->$start_field))
94 if(empty($bean->$end_field))
95 $bean->$end_field = $bean->$start_field;
97 $timestamp = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$bean->$start_field,new DateTimeZone('UTC'))->format('U');
98 $arr['timestamp'] = $timestamp;
99 $arr['time_start'] = $GLOBALS['timedate']->fromTimestamp($arr['timestamp'])->format($GLOBALS['timedate']->get_time_format());
100 $date_start = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$bean->$start_field,new DateTimeZone('UTC'));
101 $arr['ts_start'] = $date_start->get("-".$date_start->format("H")." hours -".$date_start->format("i")." minutes -".$date_start->format("s")." seconds")->format('U');
102 $arr['offset'] = $date_start->format('H') * 3600 + $date_start->format('i') * 60;
103 $date_end = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$bean->$end_field,new DateTimeZone('UTC'));
104 if($bean->object_name != 'Task')
105 $date_end->modify("-1 minute");
106 $arr['ts_end'] = $date_end->get("+1 day")->get("-".$date_end->format("H")." hours -".$date_end->format("i")." minutes -".$date_end->format("s")." seconds")->format('U');
107 $arr['days'] = ($arr['ts_end'] - $arr['ts_start']) / (3600*24);
114 * Get array that will be sent back to ajax frontend
115 * @param SugarBean $bean
118 static function get_sendback_array(SugarBean $bean){
120 if(isset($bean->parent_name) && isset($_REQUEST['parent_name']))
121 $bean->parent_name = $_REQUEST['parent_name'];
124 if($bean->object_name == 'Call')
125 $users = $bean->get_call_users();
126 else if($bean->object_name == 'Meeting')
127 $users = $bean->get_meeting_users();
129 foreach($users as $u)
130 $user_ids[] = $u->id;
132 $field_list = CalendarUtils::get_fields();
133 $field_arr = array();
134 foreach($field_list[$bean->module_dir] as $field){
135 $field_arr[$field] = $bean->$field;
138 $date_field = "date_start";
139 if($bean->object_name == 'Task')
140 $date_field = "date_due";
144 'type' => strtolower($bean->object_name),
145 'module_name' => $bean->module_dir,
146 'user_id' => $GLOBALS['current_user']->id,
149 'name' => $bean->name,
150 'record' => $bean->id,
151 'users' => $user_ids,
153 if(!empty($bean->repeat_parent_id))
154 $arr['repeat_parent_id'] = $bean->repeat_parent_id;
155 $arr = array_merge($arr,$field_arr);
156 $arr = array_merge($arr,CalendarUtils::get_time_data($bean));
162 * Get array of repeat data
163 * @param SugarBean $bean
166 static function get_sendback_repeat_data(SugarBean $bean){
167 if ($bean->module_dir == "Meetings" || $bean->module_dir == "Calls") {
168 if(!empty($bean->repeat_parent_id) || (!empty($bean->repeat_type) && empty($_REQUEST['edit_all_recurrences']))){
169 if(!empty($bean->repeat_parent_id))
170 $repeat_parent_id = $bean->repeat_parent_id;
172 $repeat_parent_id = $bean->id;
173 return array("repeat_parent_id" => $repeat_parent_id);
177 if(!empty($bean->repeat_type)){
179 'repeat_type' => $bean->repeat_type,
180 'repeat_interval' => $bean->repeat_interval,
181 'repeat_dow' => $bean->repeat_dow,
182 'repeat_until' => $bean->repeat_until,
183 'repeat_count' => $bean->repeat_count,
187 // TODO CHECK DATETIME VARIABLE
188 if(!empty($_REQUEST['date_start'])){
189 $date_start = $_REQUEST['date_start'];
191 $date_start = $bean->date_start;
193 $date = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$date_start);
194 $arr = array_merge($arr,array(
195 'current_dow' => $date->format("w"),
196 'default_repeat_until' => $date->get("+1 Month")->format($GLOBALS['timedate']->get_date_format()),
205 * Build array of datetimes for recurring meetings
206 * @param string $date_start
207 * @param array $params
210 static function build_repeat_sequence($date_start,$params){
214 $type = $params['type'];
215 $interval = intval($params['interval']);
219 if(!empty($params['count'])){
220 $count = $params['count'];
226 if(!empty($params['until'])){
227 $until = $params['until'];
229 $until = $date_start;
231 if($type == "Weekly"){
232 $dow = $params['dow'];
239 * @var SugarDateTime $start Recurrence start date.
241 $start = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$date_start);
243 * @var SugarDateTime $end Recurrence end date. Used if recurrence ends by date.
246 if (!empty($params['until'])) {
247 $end = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_format(), $until);
248 $end->modify("+1 Day");
252 $current = clone $start;
254 $i = 1; // skip the first iteration
255 $w = $interval; // for week iteration
256 $last_dow = $start->format("w");
258 $limit = SugarConfig::getInstance()->get('calendar.max_repeat_count',1000);
260 while($i < $count || ($count == 0 && $current->format("U") < $end->format("U"))){
264 $current->modify("+{$interval} Days");
267 $day_index = $last_dow;
268 for($d = $last_dow + 1; $d <= $last_dow + 7; $d++){
270 if(strpos($dow,(string)($day_index)) !== false){
274 $step = $day_index - $last_dow;
275 $last_dow = $day_index;
280 if($w % $interval != 0)
283 $current->modify("+{$step} Days");
286 $current->modify("+{$interval} Months");
289 $current->modify("+{$interval} Years");
298 if(($i < $count || $count == 0 && $current->format("U") < $end->format("U")) ){
299 $arr[] = $current->format($GLOBALS['timedate']->get_date_time_format());
303 if($i > $limit + 100)
310 * Save repeat activities
311 * @param SugarBean $bean
312 * @param array $time_arr array of datetimes
315 static function save_repeat_activities(SugarBean $bean,$time_arr){
317 // Here we will create single big inserting query for each invitee relationship
318 // rather than using relationships framework due to performance issues.
319 // Relationship framework runs very slowly
323 $date_modified = $GLOBALS['timedate']->nowDb();
324 $lower_name = strtolower($bean->object_name);
326 $qu = "SELECT * FROM {$bean->rel_users_table} WHERE deleted = 0 AND {$lower_name}_id = '{$id}'";
327 $re = $db->query($qu);
328 $users_rel_arr = array();
329 while($ro = $db->fetchByAssoc($re))
330 $users_rel_arr[] = $ro['user_id'];
332 INSERT INTO {$bean->rel_users_table}
333 (id,user_id,{$lower_name}_id,date_modified)
336 $users_filled = false;
338 $qu = "SELECT * FROM {$bean->rel_contacts_table} WHERE deleted = 0 AND {$lower_name}_id = '{$id}'";
339 $re = $db->query($qu);
340 $contacts_rel_arr = array();
341 while($ro = $db->fetchByAssoc($re))
342 $contacts_rel_arr[] = $ro['contact_id'];
344 INSERT INTO {$bean->rel_contacts_table}
345 (id,contact_id,{$lower_name}_id,date_modified)
348 $contacts_filled = false;
350 $qu = "SELECT * FROM {$bean->rel_leads_table} WHERE deleted = 0 AND {$lower_name}_id = '{$id}'";
351 $re = $db->query($qu);
352 $leads_rel_arr = array();
353 while($ro = $db->fetchByAssoc($re))
354 $leads_rel_arr[] = $ro['lead_id'];
356 INSERT INTO {$bean->rel_leads_table}
357 (id,lead_id,{$lower_name}_id,date_modified)
360 $leads_filled = false;
364 foreach($time_arr as $date_start){
365 $clone = $bean; // we don't use clone keyword cause not necessary
367 $clone->date_start = $date_start;
368 // TODO CHECK DATETIME VARIABLE
369 $date = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$date_start);
370 $date = $date->get("+{$bean->duration_hours} Hours")->get("+{$bean->duration_minutes} Minutes");
371 $date_end = $date->format($GLOBALS['timedate']->get_date_time_format());
372 $clone->date_end = $date_end;
373 $clone->recurring_source = "Sugar";
374 $clone->repeat_parent_id = $id;
375 $clone->update_vcal = false;
379 foreach($users_rel_arr as $user_id){
381 $qu_users .= ",".PHP_EOL;
382 $qu_users .= "('".create_guid()."','{$user_id}','{$clone->id}','{$date_modified}')";
383 $users_filled = true;
385 foreach($contacts_rel_arr as $contact_id){
387 $qu_contacts .= ",".PHP_EOL;
388 $qu_contacts .= "('".create_guid()."','{$contact_id}','{$clone->id}','{$date_modified}')";
389 $contacts_filled = true;
391 foreach($leads_rel_arr as $lead_id){
393 $qu_leads .= ",".PHP_EOL;
394 $qu_leads .= "('".create_guid()."','{$lead_id}','{$clone->id}','{$date_modified}')";
395 $leads_filled = true;
398 $clone->date_start = $date_start;
399 $clone->date_end = $date_end;
400 $arr[] = array_merge(array('id' => $clone->id),CalendarUtils::get_time_data($clone));
407 $db->query($qu_users);
409 if ($contacts_filled) {
410 $db->query($qu_contacts);
413 $db->query($qu_leads);
416 vCal::cache_sugar_vcal($GLOBALS['current_user']);
421 * Delete recurring activities and their invitee relationships
422 * @param SugarBean $bean
424 static function markRepeatDeleted(SugarBean $bean)
426 // we don't use mark_deleted method here because it runs very slowly
428 $date_modified = $GLOBALS['timedate']->nowDb();
429 if(!empty($GLOBALS['current_user']))
430 $modified_user_id = $GLOBALS['current_user']->id;
432 $modified_user_id = 1;
433 $lower_name = strtolower($bean->object_name);
435 $qu = "SELECT id FROM {$bean->table_name} WHERE repeat_parent_id = '{$bean->id}' AND deleted = 0";
436 $re = $db->query($qu);
437 while( $ro = $db->fetchByAssoc($re)) {
439 $date_modified = $GLOBALS['timedate']->nowDb();
440 $db->query("UPDATE {$bean->table_name} SET deleted = 1, date_modified = '{$date_modified}', modified_user_id = '{$modified_user_id}' WHERE id = '{$id}'");
441 $db->query("UPDATE {$bean->rel_users_table} SET deleted = 1, date_modified = '{$date_modified}' WHERE {$lower_name}_id = '{$id}'");
442 $db->query("UPDATE {$bean->rel_contacts_table} SET deleted = 1, date_modified = '{$date_modified}' WHERE {$lower_name}_id = '{$id}'");
443 $db->query("UPDATE {$bean->rel_leads_table} SET deleted = 1, date_modified = '{$date_modified}' WHERE {$lower_name}_id = '{$id}'");
445 vCal::cache_sugar_vcal($GLOBALS['current_user']);
449 * check if meeting has repeat children and pass repeat_parent over to the 2nd meeting in sequence
450 * @param SugarBean $bean
451 * @param string $beanId
453 static function correctRecurrences(SugarBean $bean, $beanId)
457 $qu = "SELECT id FROM {$bean->table_name} WHERE repeat_parent_id = '{$beanId}' AND deleted = 0 ORDER BY date_start";
458 $re = $db->query($qu);
461 while ($ro = $db->fetchByAssoc($re)) {
464 $new_parent_id = $id;
465 $qu = "UPDATE {$bean->table_name} SET repeat_parent_id = '' AND recurring_source = '' WHERE id = '{$id}'";
467 $qu = "UPDATE {$bean->table_name} SET repeat_parent_id = '{$new_parent_id}' WHERE id = '{$id}'";