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