]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/CalendarUtils.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Calendar / CalendarUtils.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 class CalendarUtils {
40
41         /**
42          * Find first day of week according to user's settings
43          * @param SugarDateTime $date
44          * @return SugarDateTime $date
45          */
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);
51         }
52
53
54         /**
55          * Get list of needed fields for modules
56          * @return array
57          */
58         static function get_fields(){
59                 return array(
60                         'Meetings' => array(
61                                 'name',
62                                 'duration_hours',
63                                 'duration_minutes',
64                                 'status',
65                         ),
66                         'Calls' => array(
67                                 'name',
68                                 'duration_hours',
69                                 'duration_minutes',
70                                 'status',
71                         ),
72                         'Tasks' => array(
73                                 'name',
74                                 'status',
75                         ),
76                 );
77         }
78
79         /**
80          * Get array of needed time data
81          * @param SugarBean $bean
82          * @return array
83          */
84         static function get_time_data(SugarBean $bean){
85                                         $arr = array();
86
87                                         $start_field = "date_start";
88                                         $end_field = "date_end";
89
90                                         if($bean->object_name == 'Task')
91                                                 $start_field = $end_field = "date_due";
92                                         if(empty($bean->$start_field))
93                                                 return array();
94                                         if(empty($bean->$end_field))
95                                                 $bean->$end_field = $bean->$start_field;
96
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);
108
109                                         return $arr;
110         }
111
112
113         /**
114          * Get array that will be sent back to ajax frontend
115          * @param SugarBean $bean
116          * @return array
117          */
118         static function get_sendback_array(SugarBean $bean){
119
120                         if(isset($bean->parent_name) && isset($_REQUEST['parent_name']))
121                                 $bean->parent_name = $_REQUEST['parent_name'];
122
123                         $users = array();
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();
128                         $user_ids = array();
129                         foreach($users as $u)
130                                 $user_ids[] = $u->id;
131
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;
136                         }
137
138                         $date_field = "date_start";
139                         if($bean->object_name == 'Task')
140                                 $date_field = "date_due";
141
142                         $arr = array(
143                                 'access' => 'yes',
144                                 'type' => strtolower($bean->object_name),
145                                 'module_name' => $bean->module_dir,
146                                 'user_id' => $GLOBALS['current_user']->id,
147                                 'detail' => 1,
148                                 'edit' => 1,
149                                 'name' => $bean->name,
150                                 'record' => $bean->id,
151                                 'users' => $user_ids,
152                         );
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));
157
158                         return $arr;
159         }
160
161         /**
162          * Get array of repeat data
163          * @param SugarBean $bean
164          * @return array
165          */
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;
171                                 else
172                                         $repeat_parent_id = $bean->id;
173                                 return array("repeat_parent_id" => $repeat_parent_id);
174                         }
175
176                         $arr = array();
177                         if(!empty($bean->repeat_type)){
178                                 $arr = array(
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,
184                                 );
185                         }
186
187                         // TODO CHECK DATETIME VARIABLE
188                         if(!empty($_REQUEST['date_start'])){
189                                 $date_start = $_REQUEST['date_start'];
190                         }else
191                                 $date_start = $bean->date_start;
192
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()),
197                         ));
198
199                         return $arr;
200                 }
201                 return false;
202          }
203
204         /**
205          * Build array of datetimes for recurring meetings
206          * @param string $date_start
207          * @param array $params
208          * @return array
209          */
210         static function build_repeat_sequence($date_start,$params){
211
212                 $arr = array();
213
214                 $type = $params['type'];
215                 $interval = intval($params['interval']);
216                 if($interval < 1)
217                         $interval = 1;
218
219                 if(!empty($params['count'])){
220                         $count = $params['count'];
221                         if($count < 1)
222                                 $count = 1;
223                 }else
224                         $count = 0;
225
226                 if(!empty($params['until'])){
227                         $until = $params['until'];
228                 }else
229                         $until = $date_start;
230
231                 if($type == "Weekly"){
232                         $dow = $params['dow'];
233                         if($dow == ""){
234                                 return array();
235                         }
236                 }
237
238                 /** 
239                  * @var SugarDateTime $start Recurrence start date.
240                  */
241                 $start = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$date_start);
242                 /** 
243                  * @var SugarDateTime $end Recurrence end date. Used if recurrence ends by date.
244                  */
245                  
246                 if (!empty($params['until'])) {
247                         $end = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_format(), $until);
248                         $end->modify("+1 Day");
249                 } else {
250                         $end = $start;
251                 }
252                 $current = clone $start;
253
254                 $i = 1; // skip the first iteration
255                 $w = $interval; // for week iteration
256                 $last_dow = $start->format("w");
257
258                 $limit = SugarConfig::getInstance()->get('calendar.max_repeat_count',1000);
259
260                 while($i < $count || ($count == 0 && $current->format("U") < $end->format("U"))){
261                         $skip = false;
262                         switch($type){
263                                 case "Daily":
264                                         $current->modify("+{$interval} Days");
265                                         break;
266                                 case "Weekly":
267                                         $day_index = $last_dow;
268                                         for($d = $last_dow + 1; $d <= $last_dow + 7; $d++){
269                                                 $day_index = $d % 7;
270                                                 if(strpos($dow,(string)($day_index)) !== false){
271                                                         break;
272                                                 }
273                                         }
274                                         $step = $day_index - $last_dow;
275                                         $last_dow = $day_index;
276                                         if($step <= 0){
277                                                 $step += 7;
278                                                 $w++;
279                                         }
280                                         if($w % $interval != 0)
281                                                 $skip = true;
282
283                                         $current->modify("+{$step} Days");
284                                         break;
285                                 case "Monthly":
286                                         $current->modify("+{$interval} Months");
287                                         break;
288                                 case "Yearly":
289                                         $current->modify("+{$interval} Years");
290                                         break;
291                                 default:
292                                         return array();
293                         }
294
295                         if($skip)
296                                 continue;
297
298                         if(($i < $count || $count == 0 && $current->format("U") < $end->format("U"))  ){
299                                 $arr[] = $current->format($GLOBALS['timedate']->get_date_time_format());
300                         }
301                         $i++;
302
303                         if($i > $limit + 100)
304                                 break;
305                 }
306                 return $arr;
307         }
308
309         /**
310          * Save repeat activities
311          * @param SugarBean $bean
312          * @param array $time_arr array of datetimes
313          * @return array
314          */
315         static function save_repeat_activities(SugarBean $bean,$time_arr){
316
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
320
321                 global $db;
322                 $id = $bean->id;
323                 $date_modified = $GLOBALS['timedate']->nowDb();
324                 $lower_name = strtolower($bean->object_name);
325
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'];
331                 $qu_users = "
332                                 INSERT INTO {$bean->rel_users_table}
333                                 (id,user_id,{$lower_name}_id,date_modified)
334                                 VALUES
335                 ";
336                 $users_filled = false;
337
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'];
343                 $qu_contacts = "
344                                 INSERT INTO {$bean->rel_contacts_table}
345                                 (id,contact_id,{$lower_name}_id,date_modified)
346                                 VALUES
347                 ";
348                 $contacts_filled = false;
349
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'];
355                 $qu_leads = "
356                                 INSERT INTO {$bean->rel_leads_table}
357                                 (id,lead_id,{$lower_name}_id,date_modified)
358                                 VALUES
359                 ";
360                 $leads_filled = false;
361
362                 $arr = array();
363                 $i = 0;
364                 foreach($time_arr as $date_start){
365                         $clone = $bean; // we don't use clone keyword cause not necessary
366                         $clone->id = "";
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;
376                         $clone->save(false);
377
378                         if($clone->id){
379                                 foreach($users_rel_arr as $user_id){
380                                         if($users_filled)
381                                                 $qu_users .= ",".PHP_EOL;
382                                         $qu_users .= "('".create_guid()."','{$user_id}','{$clone->id}','{$date_modified}')";
383                                         $users_filled = true;
384                                 }
385                                 foreach($contacts_rel_arr as $contact_id){
386                                         if($contacts_filled)
387                                                 $qu_contacts .= ",".PHP_EOL;
388                                         $qu_contacts .= "('".create_guid()."','{$contact_id}','{$clone->id}','{$date_modified}')";
389                                         $contacts_filled = true;
390                                 }
391                                 foreach($leads_rel_arr as $lead_id){
392                                         if($leads_filled)
393                                                 $qu_leads .= ",".PHP_EOL;
394                                         $qu_leads .= "('".create_guid()."','{$lead_id}','{$clone->id}','{$date_modified}')";
395                                         $leads_filled = true;
396                                 }
397                                 if($i < 44){
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));
401                                 }
402                                 $i++;
403                         }
404                 }
405                 
406                 if ($users_filled) {
407                         $db->query($qu_users);
408                 }
409                 if ($contacts_filled) {
410                         $db->query($qu_contacts);
411                 }               
412                 if ($leads_filled) {
413                         $db->query($qu_leads);
414                 }
415                 
416                 vCal::cache_sugar_vcal($GLOBALS['current_user']);
417                 return $arr;
418         }
419
420         /**
421          * Delete recurring activities and their invitee relationships
422          * @param SugarBean $bean
423          */
424         static function markRepeatDeleted(SugarBean $bean)
425         {
426                 // we don't use mark_deleted method here because it runs very slowly
427                 global $db;
428                 $date_modified = $GLOBALS['timedate']->nowDb();
429                 if(!empty($GLOBALS['current_user']))
430                         $modified_user_id = $GLOBALS['current_user']->id;
431                 else
432                         $modified_user_id = 1;
433                 $lower_name = strtolower($bean->object_name);
434
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)) {
438                         $id = $ro['id'];
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}'");
444                 }
445                 vCal::cache_sugar_vcal($GLOBALS['current_user']);
446         }
447
448         /**
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
452          */
453         static function correctRecurrences(SugarBean $bean, $beanId)
454         {
455                 global $db;
456                 
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);
459
460                 $i = 0;
461                 while ($ro = $db->fetchByAssoc($re)) {
462                         $id = $ro['id'];
463                         if($i == 0){
464                                 $new_parent_id = $id;
465                                 $qu = "UPDATE {$bean->table_name} SET repeat_parent_id = '' AND recurring_source = '' WHERE id = '{$id}'";
466                         }else{
467                                 $qu = "UPDATE {$bean->table_name} SET repeat_parent_id = '{$new_parent_id}' WHERE id = '{$id}'";
468                         }
469                         $db->query($qu);
470                 $i++;
471                 }
472         }
473
474 }