]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/Meeting.php
Release 6.5.8
[Github/sugarcrm.git] / modules / Meetings / Meeting.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 Meeting extends SugarBean {
40         // Stored fields
41         var $id;
42         var $date_entered;
43         var $date_modified;
44         var $assigned_user_id;
45         var $modified_user_id;
46         var $created_by;
47         var $created_by_name;
48         var $modified_by_name;
49         var $description;
50         var $name;
51         var $location;
52         var $status;
53         var $type;
54         var $date_start;
55         var $time_start;
56         var $date_end;
57         var $duration_hours;
58         var $duration_minutes;
59         var $time_meridiem;
60         var $parent_type;
61         var $parent_type_options;
62         var $parent_id;
63         var $field_name_map;
64         var $contact_id;
65         var $user_id;
66         var $meeting_id;
67         var $reminder_time;
68         var $reminder_checked;
69         var $email_reminder_time;
70         var $email_reminder_checked;
71         var $email_reminder_sent;
72         var $required;
73         var $accept_status;
74         var $parent_name;
75         var $contact_name;
76         var $contact_phone;
77         var $contact_email;
78         var $account_id;
79         var $opportunity_id;
80         var $case_id;
81         var $assigned_user_name;
82         var $outlook_id;
83         var $sequence;
84         var $syncing = false;
85         var $recurring_source;
86
87         var $update_vcal = true;
88         var $contacts_arr;
89         var $users_arr;
90         var $meetings_arr;
91         // when assoc w/ a user/contact:
92         var $minutes_value_default = 15;
93         var $minutes_values = array('0'=>'00','15'=>'15','30'=>'30','45'=>'45');
94         var $table_name = "meetings";
95         var $rel_users_table = "meetings_users";
96         var $rel_contacts_table = "meetings_contacts";
97         var $rel_leads_table = "meetings_leads";
98         var $module_dir = "Meetings";
99         var $object_name = "Meeting";
100
101         var $importable = true;
102         // This is used to retrieve related fields from form posts.
103         var $additional_column_fields = array('assigned_user_name', 'assigned_user_id', 'contact_id', 'user_id', 'contact_name', 'accept_status');
104         var $relationship_fields = array('account_id'=>'accounts','opportunity_id'=>'opportunity','case_id'=>'case',
105                                                                          'assigned_user_id'=>'users','contact_id'=>'contacts', 'user_id'=>'users', 'meeting_id'=>'meetings');
106         // so you can run get_users() twice and run query only once
107         var $cached_get_users = null;
108         var $new_schema = true;
109
110         /**
111          * sole constructor
112          */
113         function Meeting() {
114                 parent::SugarBean();
115                 $this->setupCustomFields('Meetings');
116                 foreach($this->field_defs as $field) {
117                         $this->field_name_map[$field['name']] = $field;
118                 }
119 //              $this->fill_in_additional_detail_fields();
120         if(!empty($GLOBALS['app_list_strings']['duration_intervals'])) {
121             $this->minutes_values = $GLOBALS['app_list_strings']['duration_intervals'];
122         }
123         }
124
125         /**
126          * Disable edit if meeting is recurring and source is not Sugar. It should be edited only from Outlook.
127          * @param $view string
128          * @param $is_owner bool
129          */
130         function ACLAccess($view,$is_owner = 'not_set'){
131                 // don't check if meeting is being synced from Outlook
132                 if($this->syncing == false){
133                         $view = strtolower($view);
134                         switch($view){
135                                 case 'edit':
136                                 case 'save':
137                                 case 'editview':
138                                 case 'delete':
139                                         if(!empty($this->recurring_source) && $this->recurring_source != "Sugar"){
140                                                 return false;
141                                         }
142                         }
143                 }
144                 return parent::ACLAccess($view,$is_owner);
145         }
146
147         /**
148          * Stub for integration
149          * @return bool
150          */
151         function hasIntegratedMeeting() {
152                 return false;
153         }
154
155         // save date_end by calculating user input
156         // this is for calendar
157         function save($check_notify = FALSE) {
158                 global $timedate;
159                 global $current_user;
160
161                 global $disable_date_format;
162
163             if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
164         {
165                 if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
166                 {
167                     $td = $timedate->fromDb($this->date_start);
168                     if(!$td){
169                                 $this->date_start = $timedate->to_db($this->date_start);
170                                 $td = $timedate->fromDb($this->date_start);
171                     }
172                     if($td)
173                     {
174                                 $this->date_end = $td->modify("+{$this->duration_hours} hours {$this->duration_minutes} mins")->asDb();
175                     }
176                 }
177                 }
178
179                 $check_notify =(!empty($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == '1') ? true : false;
180                 if(empty($_REQUEST['send_invites'])) {
181                         if(!empty($this->id)) {
182                                 $old_record = new Meeting();
183                                 $old_record->retrieve($this->id);
184                                 $old_assigned_user_id = $old_record->assigned_user_id;
185                         }
186                         if((empty($this->id) && isset($_REQUEST['assigned_user_id']) && !empty($_REQUEST['assigned_user_id']) && $GLOBALS['current_user']->id != $_REQUEST['assigned_user_id']) || (isset($old_assigned_user_id) && !empty($old_assigned_user_id) && isset($_REQUEST['assigned_user_id']) && !empty($_REQUEST['assigned_user_id']) && $old_assigned_user_id != $_REQUEST['assigned_user_id']) ){
187                                 $this->special_notification = true;
188                                 $check_notify = true;
189                 if(isset($_REQUEST['assigned_user_name'])) {
190                     $this->new_assigned_user_name = $_REQUEST['assigned_user_name'];
191                 }
192                         }
193                 }
194                 /*nsingh 7/3/08  commenting out as bug #20814 is invalid
195                 if($current_user->getPreference('reminder_time')!= -1 &&  isset($_POST['reminder_checked']) && isset($_POST['reminder_time']) && $_POST['reminder_checked']==0  && $_POST['reminder_time']==-1){
196                         $this->reminder_checked = '1';
197                         $this->reminder_time = $current_user->getPreference('reminder_time');
198                 }*/
199
200                 // prevent a mass mailing for recurring meetings created in Calendar module
201                 if(empty($this->id) && !empty($_REQUEST['module']) && $_REQUEST['module'] == "Calendar" && !empty($_REQUEST['repeat_type']) && !empty($this->repeat_parent_id))
202                         $check_notify = false;
203
204         if (empty($this->status) ) {
205             $this->status = $this->getDefaultStatus();
206         }
207
208         // Do any external API saving
209         // Clear out the old external API stuff if we have changed types
210         if (isset($this->fetched_row) && $this->fetched_row['type'] != $this->type ) {
211             $this->join_url = '';
212             $this->host_url = '';
213             $this->external_id = '';
214             $this->creator = '';
215         }
216
217         if (!empty($this->type) && $this->type != 'Sugar' ) {
218             require_once('include/externalAPI/ExternalAPIFactory.php');
219             $api = ExternalAPIFactory::loadAPI($this->type);
220         }
221
222         if (empty($this->type)) {
223                         $this->type = 'Sugar';
224                 }
225
226         if ( isset($api) && is_a($api,'WebMeeting') && empty($this->in_relationship_update) ) {
227             // Make sure the API initialized and it supports Web Meetings
228             // Also make suer we have an ID, the external site needs something to reference
229             if ( !isset($this->id) || empty($this->id) ) {
230                 $this->id = create_guid();
231                 $this->new_with_id = true;
232             }
233             $response = $api->scheduleMeeting($this);
234             if ( $response['success'] == TRUE ) {
235                 // Need to send out notifications
236                 if ( $api->canInvite ) {
237                     $notifyList = $this->get_notification_recipients();
238                     foreach($notifyList as $person) {
239                         $api->inviteAttendee($this,$person,$check_notify);
240                     }
241
242                 }
243             } else {
244                 // Generic Message Provides no value to End User - Log the issue with message detail and continue
245                 // SugarApplication::appendErrorMessage($GLOBALS['app_strings']['ERR_EXTERNAL_API_SAVE_FAIL']);
246                 $GLOBALS['log']->warn('ERR_EXTERNAL_API_SAVE_FAIL' . ": " . $this->type . " - " .  $response['errorMessage']);
247             }
248
249             $api->logoff();
250         }
251
252                 $return_id = parent::save($check_notify);
253
254                 if($this->update_vcal) {
255                         vCal::cache_sugar_vcal($current_user);
256                 }
257
258
259
260                 return $return_id;
261         }
262
263         // this is for calendar
264         function mark_deleted($id) {
265
266                 require_once("modules/Calendar/CalendarUtils.php");
267                 CalendarUtils::correctRecurrences($this, $id);          
268                 
269                 global $current_user;
270
271                 parent::mark_deleted($id);
272
273                 if($this->update_vcal) {
274                         vCal::cache_sugar_vcal($current_user);
275                 }
276         }
277
278         function get_summary_text() {
279                 return "$this->name";
280         }
281
282     function create_export_query(&$order_by, &$where, $relate_link_join='')
283     {
284         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
285                 if($custom_join)
286                                 $custom_join['join'] .= $relate_link_join;
287                 $contact_required = stristr($where, "contacts");
288
289                 if($contact_required) {
290                         $query = "SELECT meetings.*, contacts.first_name, contacts.last_name, contacts.assigned_user_id contact_name_owner, users.user_name as assigned_user_name   ";
291                         if($custom_join) {
292                                 $query .= $custom_join['select'];
293                         }
294                         $query .= " FROM contacts, meetings, meetings_contacts ";
295                         $where_auto = " meetings_contacts.contact_id = contacts.id AND meetings_contacts.meeting_id = meetings.id AND meetings.deleted=0 AND contacts.deleted=0";
296                 } else {
297                         $query = 'SELECT meetings.*, users.user_name as assigned_user_name  ';
298                         if($custom_join) {
299                                 $query .= $custom_join['select'];
300                         }
301                         $query .= ' FROM meetings ';
302                         $where_auto = "meetings.deleted=0";
303                 }
304                 $query .= "  LEFT JOIN users ON meetings.assigned_user_id=users.id ";
305
306                 if($custom_join) {
307                         $query .= $custom_join['join'];
308                 }
309
310                 if($where != "")
311                         $query .= " where $where AND ".$where_auto;
312                 else
313                         $query .= " where ".$where_auto;
314
315                 if($order_by != "") {
316                         $query .= " ORDER BY $order_by";
317                 } else {
318                         $alternate_order_by =   $this->process_order_by($order_by, null);
319                         if($alternate_order_by != "")
320                                 $query .=       " ORDER BY ". $alternate_order_by;
321                 }
322                 return $query;
323         }
324
325
326
327         function fill_in_additional_detail_fields() {
328                 global $locale;
329                 // Fill in the assigned_user_name
330                 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
331
332                 if (!empty($this->contact_id)) {
333                         $query  = "SELECT first_name, last_name FROM contacts ";
334                         $query .= "WHERE id='$this->contact_id' AND deleted=0";
335                         $result = $this->db->limitQuery($query,0,1,true," Error filling in additional detail fields: ");
336
337                         // Get the contact name.
338                         $row = $this->db->fetchByAssoc($result);
339                         $GLOBALS['log']->info("additional call fields $query");
340                         if($row != null)
341                         {
342                                 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], '', '');
343                                 $GLOBALS['log']->debug("Call($this->id): contact_name = $this->contact_name");
344                                 $GLOBALS['log']->debug("Call($this->id): contact_id = $this->contact_id");
345                         }
346                 }
347
348
349
350                 $this->created_by_name = get_assigned_user_name($this->created_by);
351                 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
352                 $this->fill_in_additional_parent_fields();
353
354                 if (!isset($this->time_hour_start)) {
355                         $this->time_start_hour = intval(substr($this->time_start, 0, 2));
356                 } //if-else
357
358                 if (isset($this->time_minute_start)) {
359                         $time_start_minutes = $this->time_minute_start;
360                 } else {
361                         $time_start_minutes = substr($this->time_start, 3, 5);
362                         if ($time_start_minutes > 0 && $time_start_minutes < 15) {
363                                 $time_start_minutes = "15";
364                         } else if ($time_start_minutes > 15 && $time_start_minutes < 30) {
365                                 $time_start_minutes = "30";
366                         } else if ($time_start_minutes > 30 && $time_start_minutes < 45) {
367                                 $time_start_minutes = "45";
368                         } else if ($time_start_minutes > 45) {
369                                 $this->time_start_hour += 1;
370                                 $time_start_minutes = "00";
371                     } //if-else
372                 } //if-else
373
374
375                 if (isset($this->time_hour_start)) {
376                         $time_start_hour = $this->time_hour_start;
377                 } else {
378                         $time_start_hour = intval(substr($this->time_start, 0, 2));
379                 }
380
381                 global $timedate;
382         $this->time_meridiem = $timedate->AMPMMenu('', $this->time_start, 'onchange="SugarWidgetScheduler.update_time();"');
383                 $hours_arr = array ();
384                 $num_of_hours = 13;
385                 $start_at = 1;
386
387                 if (empty ($time_meridiem)) {
388                         $num_of_hours = 24;
389                         $start_at = 0;
390                 } //if
391
392                 for ($i = $start_at; $i < $num_of_hours; $i ++) {
393                         $i = $i."";
394                         if (strlen($i) == 1) {
395                                 $i = "0".$i;
396                         }
397                         $hours_arr[$i] = $i;
398                 } //for
399
400         if (!isset($this->duration_minutes)) {
401                         $this->duration_minutes = $this->minutes_value_default;
402                 }
403
404         //setting default date and time
405                 if (is_null($this->date_start))
406                         $this->date_start = $timedate->now();
407                 if (is_null($this->time_start))
408                         $this->time_start = $timedate->to_display_time(TimeDate::getInstance()->nowDb(), true);
409                 if (is_null($this->duration_hours)) {
410                         $this->duration_hours = "0";
411                 }
412                 if (is_null($this->duration_minutes))
413                         $this->duration_minutes = "1";
414
415                 if(empty($this->id) && !empty($_REQUEST['date_start'])){
416                         $this->date_start = $_REQUEST['date_start'];
417                 }
418                 if(!empty($this->date_start)) {
419                     $start = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$this->date_start);
420                     if (!empty($start)) {
421                         if (!empty($this->duration_hours) || !empty($this->duration_minutes)) {
422                             $this->date_end = $start->modify("+{$this->duration_hours} Hours +{$this->duration_minutes} Minutes")
423                             ->format($GLOBALS['timedate']->get_date_time_format());
424                         }
425                     } else {
426                         $GLOBALS['log']->fatal("Meeting::save: Bad date {$this->date_start} for format ".$GLOBALS['timedate']->get_date_time_format());
427                     }
428                 }
429
430                 global $app_list_strings;
431                 $parent_types = $app_list_strings['record_type_display'];
432                 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
433                 foreach($disabled_parent_types as $disabled_parent_type){
434                         if($disabled_parent_type != $this->parent_type){
435                                 unset($parent_types[$disabled_parent_type]);
436                         }
437                 }
438
439                 $this->parent_type_options = get_select_options_with_id($parent_types, $this->parent_type);
440                 if (empty($this->reminder_time)) {
441                         $this->reminder_time = -1;
442                 }
443
444                 if ( empty($this->id) ) {
445                     $reminder_t = $GLOBALS['current_user']->getPreference('reminder_time');
446                     if ( isset($reminder_t) )
447                         $this->reminder_time = $reminder_t;
448                 }
449                 $this->reminder_checked = $this->reminder_time == -1 ? false : true;
450
451                 if (empty($this->email_reminder_time)) {
452                         $this->email_reminder_time = -1;
453                 }
454                 if(empty($this->id)){ 
455                         $reminder_t = $GLOBALS['current_user']->getPreference('email_reminder_time');
456                         if(isset($reminder_t))
457                                 $this->email_reminder_time = $reminder_t;
458                 }
459                 $this->email_reminder_checked = $this->email_reminder_time == -1 ? false : true;
460
461                 if (isset ($_REQUEST['parent_type']) && empty($this->parent_type)) {
462                         $this->parent_type = $_REQUEST['parent_type'];
463                 } elseif (is_null($this->parent_type)) {
464                         $this->parent_type = $app_list_strings['record_type_default_key'];
465                 }
466
467         }
468
469         function get_list_view_data() {
470                 $meeting_fields = $this->get_list_view_array();
471
472                 global $app_list_strings, $focus, $action, $currentModule;
473                 if(isset($this->parent_type))
474                         $meeting_fields['PARENT_MODULE'] = $this->parent_type;
475                 if($this->status == "Planned") {
476                         //cn: added this if() to deal with sequential Closes in Meetings.       this is a hack to a hack(formbase.php->handleRedirect)
477                         if(empty($action))
478                              $action = "index";
479             $setCompleteUrl = "<a id='{$this->id}' onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Held\",\"listview\",\"1\");'>";
480                         if ($this->ACLAccess('edit')) {
481                 $meeting_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline"," border='0'",null,null,'.gif',translate('LBL_CLOSEINLINE'))."</a>";
482             } else {
483                 $meeting_fields['SET_COMPLETE'] = '';
484             }
485                 }
486                 global $timedate;
487                 $today = $timedate->nowDb();
488                 $nextday = $timedate->asDbDate($timedate->getNow()->get("+1 day"));
489                 $mergeTime = $meeting_fields['DATE_START']; //$timedate->merge_date_time($meeting_fields['DATE_START'], $meeting_fields['TIME_START']);
490                 $date_db = $timedate->to_db($mergeTime);
491                 if($date_db     < $today        ) {
492                         $meeting_fields['DATE_START']= "<font class='overdueTask'>".$meeting_fields['DATE_START']."</font>";
493                 }else if($date_db       < $nextday) {
494                         $meeting_fields['DATE_START'] = "<font class='todaysTask'>".$meeting_fields['DATE_START']."</font>";
495                 } else {
496                         $meeting_fields['DATE_START'] = "<font class='futureTask'>".$meeting_fields['DATE_START']."</font>";
497                 }
498                 $this->fill_in_additional_detail_fields();
499
500         // make sure we grab the localized version of the contact name, if a contact is provided
501         if (!empty($this->contact_id))
502         {
503             $contact_temp = BeanFactory::getBean("Contacts", $this->contact_id);
504             if (!empty($contact_temp))
505             {
506                 // Make first name, last name, salutation and title of Contacts respect field level ACLs
507                 $contact_temp->_create_proper_name_field();
508                 $this->contact_name = $contact_temp->full_name;
509             }
510         }
511
512         $meeting_fields['CONTACT_ID'] = $this->contact_id;
513         $meeting_fields['CONTACT_NAME'] = $this->contact_name;
514                 $meeting_fields['PARENT_NAME'] = $this->parent_name;
515         $meeting_fields['REMINDER_CHECKED'] = $this->reminder_time==-1 ? false : true;
516         $meeting_fields['EMAIL_REMINDER_CHECKED'] = $this->email_reminder_time==-1 ? false : true;
517
518
519                 return $meeting_fields;
520         }
521
522         function set_notification_body($xtpl, &$meeting) {
523                 global $sugar_config;
524                 global $app_list_strings;
525                 global $current_user;
526                 global $timedate;
527
528
529                 // cn: bug 9494 - passing a contact breaks this call
530                 $notifyUser =($meeting->current_notify_user->object_name == 'User') ? $meeting->current_notify_user : $current_user;
531                 // cn: bug 8078 - fixed call to $timedate
532                 if(strtolower(get_class($meeting->current_notify_user)) == 'contact') {
533                         $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
534                                                         '/index.php?entryPoint=acceptDecline&module=Meetings&contact_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
535                 } elseif(strtolower(get_class($meeting->current_notify_user)) == 'lead') {
536                         $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
537                                                         '/index.php?entryPoint=acceptDecline&module=Meetings&lead_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
538                 } else {
539                         $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
540                                                         '/index.php?entryPoint=acceptDecline&module=Meetings&user_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
541                 }
542                 $xtpl->assign("MEETING_TO", $meeting->current_notify_user->new_assigned_user_name);
543                 $xtpl->assign("MEETING_SUBJECT", trim($meeting->name));
544                 $xtpl->assign("MEETING_STATUS",(isset($meeting->status)? $app_list_strings['meeting_status_dom'][$meeting->status]:""));
545                 $typekey = strtolower($meeting->type);
546                 if(isset($meeting->type)) {
547                     if(!empty($app_list_strings['eapm_list'][$typekey])) {
548                     $typestring = $app_list_strings['eapm_list'][$typekey];
549                 } else {
550                         $typestring = $app_list_strings['meeting_type_dom'][$meeting->type];
551                     }
552                 }
553                 $xtpl->assign("MEETING_TYPE", isset($meeting->type)? $typestring:"");
554                 $startdate = $timedate->fromDb($meeting->date_start);
555                 $xtpl->assign("MEETING_STARTDATE", $timedate->asUser($startdate, $notifyUser)." ".TimeDate::userTimezoneSuffix($startdate, $notifyUser));
556                 $enddate = $timedate->fromDb($meeting->date_end);
557                 $xtpl->assign("MEETING_ENDDATE", $timedate->asUser($enddate, $notifyUser)." ".TimeDate::userTimezoneSuffix($enddate, $notifyUser));             
558                 $xtpl->assign("MEETING_HOURS", $meeting->duration_hours);
559                 $xtpl->assign("MEETING_MINUTES", $meeting->duration_minutes);
560                 $xtpl->assign("MEETING_DESCRIPTION", $meeting->description);
561         if ( !empty($meeting->join_url) ) {
562             $xtpl->assign('MEETING_URL', $meeting->join_url);
563             $xtpl->parse('Meeting.Meeting_External_API');
564         }
565
566                 return $xtpl;
567         }
568         
569         /**
570          * Redefine method to attach ics file to notification email
571          */
572         public function create_notification_email($notify_user){
573                 $notify_mail = parent::create_notification_email($notify_user);
574                                                 
575                 $path = SugarConfig::getInstance()->get('upload_dir','upload/') . $this->id;
576
577                 require_once("modules/vCals/vCal.php");
578                 $content = vCal::get_ical_event($this, $GLOBALS['current_user']);
579                                 
580                 if(file_put_contents($path,$content)){
581                         $notify_mail->AddAttachment($path, 'meeting.ics', 'base64', 'text/calendar');
582                 }
583                 return $notify_mail;            
584         }
585         
586         /**
587          * Redefine method to remove ics after email is sent
588          */
589         public function send_assignment_notifications($notify_user, $admin){
590                 parent::send_assignment_notifications($notify_user, $admin);
591                 
592                 $path = SugarConfig::getInstance()->get('upload_dir','upload/') . $this->id;
593                 unlink($path);
594         }
595
596         function get_meeting_users() {
597                 $template = new User();
598                 // First, get the list of IDs.
599                 $query = "SELECT meetings_users.required, meetings_users.accept_status, meetings_users.user_id from meetings_users where meetings_users.meeting_id='$this->id' AND meetings_users.deleted=0";
600                 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
601                 $result = $this->db->query($query, true);
602                 $list = Array();
603
604                 while($row = $this->db->fetchByAssoc($result)) {
605                         $template = new User(); // PHP 5 will retrieve by reference, always over-writing the "old" one
606                         $record = $template->retrieve($row['user_id']);
607                         $template->required = $row['required'];
608                         $template->accept_status = $row['accept_status'];
609
610                         if($record != null) {
611                                 // this copies the object into the array
612                                 $list[] = $template;
613                         }
614                 }
615                 return $list;
616         }
617
618         function get_invite_meetings(&$user) {
619                 $template = $this;
620                 // First, get the list of IDs.
621                 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
622                 $query = "SELECT meetings_users.required, meetings_users.accept_status, meetings_users.meeting_id from meetings_users where meetings_users.user_id='$user->id' AND( meetings_users.accept_status IS NULL OR     meetings_users.accept_status='none') AND meetings_users.deleted=0";
623                 $result = $this->db->query($query, true);
624                 $list = Array();
625
626                 while($row = $this->db->fetchByAssoc($result)) {
627                         $record = $template->retrieve($row['meeting_id']);
628                         $template->required = $row['required'];
629                         $template->accept_status = $row['accept_status'];
630
631
632                         if($record != null)
633                         {
634                         // this copies the object into the array
635                         $list[] = $template;
636                         }
637                 }
638                 return $list;
639         }
640
641
642         function set_accept_status(&$user,$status)
643         {
644                 if($user->object_name == 'User')
645                 {
646                         $relate_values = array('user_id'=>$user->id,'meeting_id'=>$this->id);
647                         $data_values = array('accept_status'=>$status);
648                         $this->set_relationship($this->rel_users_table, $relate_values, true, true,$data_values);
649                         global $current_user;
650
651                         if($this->update_vcal)
652                         {
653                                 vCal::cache_sugar_vcal($user);
654                         }
655                 }
656                 else if($user->object_name == 'Contact')
657                 {
658                         $relate_values = array('contact_id'=>$user->id,'meeting_id'=>$this->id);
659                         $data_values = array('accept_status'=>$status);
660                         $this->set_relationship($this->rel_contacts_table, $relate_values, true, true,$data_values);
661                 }
662         else if($user->object_name == 'Lead')
663                 {
664                         $relate_values = array('lead_id'=>$user->id,'meeting_id'=>$this->id);
665                         $data_values = array('accept_status'=>$status);
666                         $this->set_relationship($this->rel_leads_table, $relate_values, true, true,$data_values);
667                 }
668         }
669
670
671         function get_notification_recipients() {
672                 if($this->special_notification) {
673                         return parent::get_notification_recipients();
674                 }
675
676                 $list = array();
677                 if(!is_array($this->contacts_arr)) {
678                         $this->contacts_arr =   array();
679                 }
680
681                 if(!is_array($this->users_arr)) {
682                         $this->users_arr =      array();
683                 }
684
685         if(!is_array($this->leads_arr)) {
686                         $this->leads_arr =      array();
687                 }
688
689                 foreach($this->users_arr as $user_id) {
690                         $notify_user = new User();
691                         $notify_user->retrieve($user_id);
692                         $notify_user->new_assigned_user_name = $notify_user->full_name;
693                         $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
694                         $list[$notify_user->id] = $notify_user;
695                 }
696
697                 foreach($this->contacts_arr as $contact_id) {
698                         $notify_user = new Contact();
699                         $notify_user->retrieve($contact_id);
700                         $notify_user->new_assigned_user_name = $notify_user->full_name;
701                         $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
702                         $list[$notify_user->id] = $notify_user;
703                 }
704
705         foreach($this->leads_arr as $lead_id) {
706                         $notify_user = new Lead();
707                         $notify_user->retrieve($lead_id);
708                         $notify_user->new_assigned_user_name = $notify_user->full_name;
709                         $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
710                         $list[$notify_user->id] = $notify_user;
711                 }
712
713                 return $list;
714         }
715
716
717         function bean_implements($interface) {
718                 switch($interface) {
719                         case 'ACL':return true;
720                 }
721                 return false;
722         }
723
724         function listviewACLHelper() {
725                 $array_assign = parent::listviewACLHelper();
726                 $is_owner = false;
727                 if(!empty($this->parent_name)) {
728
729                         if(!empty($this->parent_name_owner)) {
730                                 global $current_user;
731                                 $is_owner = $current_user->id == $this->parent_name_owner;
732                         }
733                 }
734
735                 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)) {
736                         $array_assign['PARENT'] = 'a';
737                 } else {
738                         $array_assign['PARENT'] = 'span';
739                 }
740
741                 $is_owner = false;
742
743                 if(!empty($this->contact_name)) {
744                         if(!empty($this->contact_name_owner)) {
745                                 global $current_user;
746                                 $is_owner = $current_user->id == $this->contact_name_owner;
747                         }
748                 }
749
750                 if(ACLController::checkAccess('Contacts', 'view', $is_owner)) {
751                         $array_assign['CONTACT'] = 'a';
752                 } else {
753                         $array_assign['CONTACT'] = 'span';
754                 }
755                 return $array_assign;
756         }
757
758
759         function save_relationship_changes($is_update) {
760                 $exclude = array();
761             if(empty($this->in_workflow)) {
762            if(empty($this->in_import)){//if a meeting is being imported then contact_id  should not be excluded
763            //if the global soap_server_object variable is not empty (as in from a soap/OPI call), then process the assigned_user_id relationship, otherwise
764            //add assigned_user_id to exclude list and let the logic from MeetingFormBase determine whether assigned user id gets added to the relationship
765                 if(!empty($GLOBALS['soap_server_object'])){
766                         $exclude = array('contact_id', 'user_id');
767                 }else{
768                         $exclude = array('contact_id', 'user_id','assigned_user_id');
769                 }
770            }
771            else{
772                 $exclude = array('user_id');
773            }
774         }
775        parent::save_relationship_changes($is_update, $exclude);
776         }
777
778
779         /**
780          * @see SugarBean::afterImportSave()
781          */
782         public function afterImportSave()
783         {
784             if ( $this->parent_type == 'Contacts' ) {
785                 $this->load_relationship('contacts');
786                 if ( !$this->contacts->relationship_exists('contacts',array('id'=>$this->parent_id)) )
787                     $this->contacts->add($this->parent_id);
788             }
789             elseif ( $this->parent_type == 'Leads' ) {
790                 $this->load_relationship('leads');
791                 if ( !$this->leads->relationship_exists('leads',array('id'=>$this->parent_id)) )
792                     $this->leads->add($this->parent_id);
793             }
794
795             parent::afterImportSave();
796         }
797
798     public function getDefaultStatus()
799     {
800          $def = $this->field_defs['status'];
801          if (isset($def['default'])) {
802              return $def['default'];
803          } else {
804             $app = return_app_list_strings_language($GLOBALS['current_language']);
805             if (isset($def['options']) && isset($app[$def['options']])) {
806                 $keys = array_keys($app[$def['options']]);
807                 return $keys[0];
808             }
809         }
810         return '';
811     }
812
813 } // end class def
814
815 // External API integration, for the dropdown list of what external API's are available
816 //TODO: do we really need focus, name and view params for this function
817 function getMeetingsExternalApiDropDown($focus = null, $name = null, $value = null, $view = null)
818 {
819         global $dictionary, $app_list_strings;
820
821         $cacheKeyName = 'meetings_type_drop_down';
822
823     $apiList = sugar_cache_retrieve($cacheKeyName);
824     if ($apiList === null)
825     {
826         require_once('include/externalAPI/ExternalAPIFactory.php');
827
828         $apiList = ExternalAPIFactory::getModuleDropDown('Meetings');
829         $apiList = array_merge(array('Sugar'=>$GLOBALS['app_list_strings']['eapm_list']['Sugar']), $apiList);
830         sugar_cache_put($cacheKeyName, $apiList);
831     }
832
833         if(!empty($value) && empty($apiList[$value]))
834         {
835                 $apiList[$value] = $value;
836     }
837         //bug 46294: adding list of options to dropdown list (if it is not the default list)
838     if ($dictionary['Meeting']['fields']['type']['options'] != "eapm_list")
839     {
840         $apiList = array_merge(getMeetingTypeOptions($dictionary, $app_list_strings), $apiList);
841     }
842
843         return $apiList;
844 }
845
846 /**
847  * Meeting Type Options Array for dropdown list
848  * @param array $dictionary - getting type name
849  * @param array $app_list_strings - getting type options
850  * @return array Meeting Type Options Array for dropdown list
851  */
852 function getMeetingTypeOptions($dictionary, $app_list_strings)
853 {
854         $result = array();
855
856     // getting name of meeting type to fill dropdown list by its values
857     if (isset($dictionary['Meeting']['fields']['type']['options']))
858         {
859         $typeName = $dictionary['Meeting']['fields']['type']['options'];
860
861         if (!empty($app_list_strings[$typeName]))
862                 {
863                 $typeList = $app_list_strings[$typeName];
864
865             foreach ($typeList as $key => $value)
866                         {
867                                 $result[$value] = $value;
868             }
869         }
870     }
871
872     return $result;
873 }