2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright(C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
49 // Meeting is used to store customer information.
50 class Meeting extends SugarBean {
55 var $assigned_user_id;
56 var $modified_user_id;
59 var $modified_by_name;
69 var $duration_minutes;
72 var $parent_type_options;
79 var $reminder_checked;
89 var $assigned_user_name;
92 var $update_vcal = true;
96 // when assoc w/ a user/contact:
97 var $minutes_value_default = 15;
98 var $minutes_values = array('0'=>'00','15'=>'15','30'=>'30','45'=>'45');
99 var $table_name = "meetings";
100 var $rel_users_table = "meetings_users";
101 var $rel_contacts_table = "meetings_contacts";
102 var $rel_leads_table = "meetings_leads";
103 var $module_dir = "Meetings";
104 var $object_name = "Meeting";
106 var $importable = true;
107 // This is used to retrieve related fields from form posts.
108 var $additional_column_fields = array('assigned_user_name', 'assigned_user_id', 'contact_id', 'user_id', 'contact_name', 'accept_status');
109 var $relationship_fields = array('account_id'=>'accounts','opportunity_id'=>'opportunity','case_id'=>'case',
110 'assigned_user_id'=>'users','contact_id'=>'contacts', 'user_id'=>'users', 'meeting_id'=>'meetings');
111 // so you can run get_users() twice and run query only once
112 var $cached_get_users = null;
113 var $new_schema = true;
120 $this->setupCustomFields('Meetings');
121 foreach($this->field_defs as $field) {
122 $this->field_name_map[$field['name']] = $field;
124 // $this->fill_in_additional_detail_fields();
128 * Stub for integration
131 function hasIntegratedMeeting() {
135 // save date_end by calculating user input
136 // this is for calendar
137 function save($check_notify = FALSE) {
139 global $current_user;
140 global $disable_date_format;
142 if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
144 if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
146 $td = $timedate->fromDb($this->date_start);
149 $this->date_end = $td->modify("+{$this->duration_hours} hours {$this->duration_minutes} mins")->asDb();
154 $check_notify =(!empty($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == '1') ? true : false;
155 if(empty($_REQUEST['send_invites'])) {
156 if(!empty($this->id)) {
157 $old_record = new Meeting();
158 $old_record->retrieve($this->id);
159 $old_assigned_user_id = $old_record->assigned_user_id;
161 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']) ){
162 $this->special_notification = true;
163 $check_notify = true;
164 if(isset($_REQUEST['assigned_user_name'])) {
165 $this->new_assigned_user_name = $_REQUEST['assigned_user_name'];
169 /*nsingh 7/3/08 commenting out as bug #20814 is invalid
170 if($current_user->getPreference('reminder_time')!= -1 && isset($_POST['reminder_checked']) && isset($_POST['reminder_time']) && $_POST['reminder_checked']==0 && $_POST['reminder_time']==-1){
171 $this->reminder_checked = '1';
172 $this->reminder_time = $current_user->getPreference('reminder_time');
175 if (empty($this->status) ) {
176 $this->status = $this->getDefaultStatus();
179 // Do any external API saving
180 // Clear out the old external API stuff if we have changed types
181 if (isset($this->fetched_row) && $this->fetched_row['type'] != $this->type ) {
182 $this->join_url = '';
183 $this->host_url = '';
184 $this->external_id = '';
188 if (!empty($this->type) && $this->type != 'Sugar' ) {
189 require_once('include/externalAPI/ExternalAPIFactory.php');
190 $api = ExternalAPIFactory::loadAPI($this->type);
193 if (empty($this->type)) {
194 $this->type = 'Sugar';
197 if ( isset($api) && is_a($api,'WebMeeting') ) {
198 // Make sure the API initialized and it supports Web Meetings
199 // Also make suer we have an ID, the external site needs something to reference
200 if ( !isset($this->id) || empty($this->id) ) {
201 $this->id = create_guid();
202 $this->new_with_id = true;
204 $response = $api->scheduleMeeting($this);
205 if ( $response['success'] == TRUE ) {
206 // Need to send out notifications
207 if ( $api->canInvite ) {
208 $notifyList = $this->get_notification_recipients();
209 foreach($notifyList as $person) {
210 $api->inviteAttendee($this,$person,$check_notify);
215 SugarApplication::appendErrorMessage($GLOBALS['app_strings']['ERR_EXTERNAL_API_SAVE_FAIL']);
222 $return_id = parent::save($check_notify);
224 if($this->update_vcal) {
225 vCal::cache_sugar_vcal($current_user);
233 // this is for calendar
234 function mark_deleted($id) {
236 global $current_user;
238 parent::mark_deleted($id);
240 if($this->update_vcal) {
241 vCal::cache_sugar_vcal($current_user);
245 function get_summary_text() {
246 return "$this->name";
249 function create_export_query(&$order_by, &$where, $relate_link_join='')
251 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
253 $custom_join['join'] .= $relate_link_join;
254 $contact_required = stristr($where, "contacts");
256 if($contact_required) {
257 $query = "SELECT meetings.*, contacts.first_name, contacts.last_name, contacts.assigned_user_id contact_name_owner ";
259 $query .= $custom_join['select'];
261 $query .= " FROM contacts, meetings, meetings_contacts ";
262 $where_auto = " meetings_contacts.contact_id = contacts.id AND meetings_contacts.meeting_id = meetings.id AND meetings.deleted=0 AND contacts.deleted=0";
264 $query = 'SELECT meetings.*';
266 $query .= $custom_join['select'];
268 $query .= ' FROM meetings ';
269 $where_auto = "meetings.deleted=0";
273 $query .= $custom_join['join'];
277 $query .= " where $where AND ".$where_auto;
279 $query .= " where ".$where_auto;
281 if($order_by != "") {
282 $query .= " ORDER BY $order_by";
284 $alternate_order_by = $this->process_order_by($order_by, null);
285 if($alternate_order_by != "")
286 $query .= " ORDER BY ". $alternate_order_by;
293 function fill_in_additional_detail_fields() {
295 // Fill in the assigned_user_name
296 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
298 if (!empty($this->contact_id)) {
299 $query = "SELECT first_name, last_name FROM contacts ";
300 $query .= "WHERE id='$this->contact_id' AND deleted=0";
301 $result = $this->db->limitQuery($query,0,1,true," Error filling in additional detail fields: ");
303 // Get the contact name.
304 $row = $this->db->fetchByAssoc($result);
305 $GLOBALS['log']->info("additional call fields $query");
308 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], '', '');
309 $GLOBALS['log']->debug("Call($this->id): contact_name = $this->contact_name");
310 $GLOBALS['log']->debug("Call($this->id): contact_id = $this->contact_id");
316 $this->created_by_name = get_assigned_user_name($this->created_by);
317 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
318 $this->fill_in_additional_parent_fields();
320 if (!isset($this->time_hour_start)) {
321 $this->time_start_hour = intval(substr($this->time_start, 0, 2));
324 if (isset($this->time_minute_start)) {
325 $time_start_minutes = $this->time_minute_start;
327 $time_start_minutes = substr($this->time_start, 3, 5);
328 if ($time_start_minutes > 0 && $time_start_minutes < 15) {
329 $time_start_minutes = "15";
330 } else if ($time_start_minutes > 15 && $time_start_minutes < 30) {
331 $time_start_minutes = "30";
332 } else if ($time_start_minutes > 30 && $time_start_minutes < 45) {
333 $time_start_minutes = "45";
334 } else if ($time_start_minutes > 45) {
335 $this->time_start_hour += 1;
336 $time_start_minutes = "00";
341 if (isset($this->time_hour_start)) {
342 $time_start_hour = $this->time_hour_start;
344 $time_start_hour = intval(substr($this->time_start, 0, 2));
348 $this->time_meridiem = $timedate->AMPMMenu('', $this->time_start, 'onchange="SugarWidgetScheduler.update_time();"');
349 $hours_arr = array ();
353 if (empty ($time_meridiem)) {
358 for ($i = $start_at; $i < $num_of_hours; $i ++) {
360 if (strlen($i) == 1) {
366 if (!isset($this->duration_minutes)) {
367 $this->duration_minutes = $this->minutes_value_default;
370 //setting default date and time
371 if (is_null($this->date_start))
372 $this->date_start = $timedate->now();
373 if (is_null($this->time_start))
374 $this->time_start = $timedate->to_display_time(TimeDate::getInstance()->nowDb(), true);
375 if (is_null($this->duration_hours)) {
376 $this->duration_hours = "0";
378 if (is_null($this->duration_minutes))
379 $this->duration_minutes = "1";
381 global $app_list_strings;
382 $parent_types = $app_list_strings['record_type_display'];
383 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
384 foreach($disabled_parent_types as $disabled_parent_type){
385 if($disabled_parent_type != $this->parent_type){
386 unset($parent_types[$disabled_parent_type]);
390 $this->parent_type_options = get_select_options_with_id($parent_types, $this->parent_type);
391 if (empty($this->reminder_time)) {
392 $this->reminder_time = -1;
395 if ( empty($this->id) ) {
396 $reminder_t = $GLOBALS['current_user']->getPreference('reminder_time');
397 if ( isset($reminder_t) )
398 $this->reminder_time = $reminder_t;
400 $this->reminder_checked = $this->reminder_time == -1 ? false : true;
402 if (isset ($_REQUEST['parent_type'])) {
403 $this->parent_type = $_REQUEST['parent_type'];
404 } elseif (is_null($this->parent_type)) {
405 $this->parent_type = $app_list_strings['record_type_default_key'];
410 function get_list_view_data() {
411 $meeting_fields = $this->get_list_view_array();
412 global $app_list_strings, $focus, $action, $currentModule;
413 if(isset($this->parent_type))
414 $meeting_fields['PARENT_MODULE'] = $this->parent_type;
415 if($this->status == "Planned") {
416 //cn: added this if() to deal with sequential Closes in Meetings. this is a hack to a hack(formbase.php->handleRedirect)
419 $setCompleteUrl = "<a onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Held\",\"listview\",\"1\");'>";
420 $meeting_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline","title=".translate('LBL_LIST_CLOSE','Meetings')." border='0'")."</a>";
423 $today = $timedate->nowDb();
424 $nextday = $timedate->asDbDate($timedate->getNow()->get("+1 day"));
425 $mergeTime = $meeting_fields['DATE_START']; //$timedate->merge_date_time($meeting_fields['DATE_START'], $meeting_fields['TIME_START']);
426 $date_db = $timedate->to_db($mergeTime);
427 if($date_db < $today ) {
428 $meeting_fields['DATE_START']= "<font class='overdueTask'>".$meeting_fields['DATE_START']."</font>";
429 }else if($date_db < $nextday) {
430 $meeting_fields['DATE_START'] = "<font class='todaysTask'>".$meeting_fields['DATE_START']."</font>";
432 $meeting_fields['DATE_START'] = "<font class='futureTask'>".$meeting_fields['DATE_START']."</font>";
434 $this->fill_in_additional_detail_fields();
436 //make sure we grab the localized version of the contact name, if a contact is provided
437 if (!empty($this->contact_id)) {
439 $query = "SELECT first_name, last_name, salutation, title FROM contacts ";
440 $query .= "WHERE id='$this->contact_id' AND deleted=0";
441 $result = $this->db->limitQuery($query,0,1,true," Error filling in contact name fields: ");
443 // Get the contact name.
444 $row = $this->db->fetchByAssoc($result);
448 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], $row['salutation'], $row['title']);
452 $meeting_fields['CONTACT_ID'] = $this->contact_id;
453 $meeting_fields['CONTACT_NAME'] = $this->contact_name;
455 $meeting_fields['PARENT_NAME'] = $this->parent_name;
457 $meeting_fields['REMINDER_CHECKED'] = $this->reminder_time==-1 ? false : true;
460 return $meeting_fields;
463 function set_notification_body($xtpl, &$meeting) {
464 global $sugar_config;
465 global $app_list_strings;
466 global $current_user;
470 // cn: bug 9494 - passing a contact breaks this call
471 $notifyUser =($meeting->current_notify_user->object_name == 'User') ? $meeting->current_notify_user : $current_user;
472 // cn: bug 8078 - fixed call to $timedate
473 $prefDate = $notifyUser->getUserDateTimePreferences();
475 if(strtolower(get_class($meeting->current_notify_user)) == 'contact') {
476 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
477 '/index.php?entryPoint=acceptDecline&module=Meetings&contact_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
478 } elseif(strtolower(get_class($meeting->current_notify_user)) == 'lead') {
479 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
480 '/index.php?entryPoint=acceptDecline&module=Meetings&lead_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
482 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
483 '/index.php?entryPoint=acceptDecline&module=Meetings&user_id='.$meeting->current_notify_user->id.'&record='.$meeting->id);
485 $xtpl->assign("MEETING_TO", $meeting->current_notify_user->new_assigned_user_name);
486 $xtpl->assign("MEETING_SUBJECT", trim($meeting->name));
487 $xtpl->assign("MEETING_STATUS",(isset($meeting->status)? $app_list_strings['meeting_status_dom'][$meeting->status]:""));
488 $typekey = strtolower($meeting->type);
489 if(isset($meeting->type)) {
490 if(!empty($app_list_strings['eapm_list'][$typekey])) {
491 $typestring = $app_list_strings['eapm_list'][$typekey];
493 $typestring = $app_list_strings['meeting_type_dom'][$meeting->type];
496 $xtpl->assign("MEETING_TYPE", isset($meeting->type)? $typestring:"");
497 $xtpl->assign("MEETING_STARTDATE", $timedate->to_display_date_time($meeting->date_start,true,true,$notifyUser)." ".$prefDate['userGmt']);
498 $xtpl->assign("MEETING_HOURS", $meeting->duration_hours);
499 $xtpl->assign("MEETING_MINUTES", $meeting->duration_minutes);
500 $xtpl->assign("MEETING_DESCRIPTION", $meeting->description);
501 if ( !empty($meeting->join_url) ) {
502 $xtpl->assign('MEETING_URL', $meeting->join_url);
503 $xtpl->parse('Meeting.Meeting_External_API');
509 function get_meeting_users() {
510 $template = new User();
511 // First, get the list of IDs.
512 $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";
513 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
514 $result = $this->db->query($query, true);
517 while($row = $this->db->fetchByAssoc($result)) {
518 $template = new User(); // PHP 5 will retrieve by reference, always over-writing the "old" one
519 $record = $template->retrieve($row['user_id']);
520 $template->required = $row['required'];
521 $template->accept_status = $row['accept_status'];
523 if($record != null) {
524 // this copies the object into the array
531 function get_invite_meetings(&$user) {
533 // First, get the list of IDs.
534 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
535 $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";
536 $result = $this->db->query($query, true);
539 while($row = $this->db->fetchByAssoc($result)) {
540 $record = $template->retrieve($row['meeting_id']);
541 $template->required = $row['required'];
542 $template->accept_status = $row['accept_status'];
547 // this copies the object into the array
555 function set_accept_status(&$user,$status)
557 if($user->object_name == 'User')
559 $relate_values = array('user_id'=>$user->id,'meeting_id'=>$this->id);
560 $data_values = array('accept_status'=>$status);
561 $this->set_relationship($this->rel_users_table, $relate_values, true, true,$data_values);
562 global $current_user;
564 if($this->update_vcal)
566 vCal::cache_sugar_vcal($user);
569 else if($user->object_name == 'Contact')
571 $relate_values = array('contact_id'=>$user->id,'meeting_id'=>$this->id);
572 $data_values = array('accept_status'=>$status);
573 $this->set_relationship($this->rel_contacts_table, $relate_values, true, true,$data_values);
575 else if($user->object_name == 'Lead')
577 $relate_values = array('lead_id'=>$user->id,'meeting_id'=>$this->id);
578 $data_values = array('accept_status'=>$status);
579 $this->set_relationship($this->rel_leads_table, $relate_values, true, true,$data_values);
584 function get_notification_recipients() {
585 if($this->special_notification) {
586 return parent::get_notification_recipients();
590 if(!is_array($this->contacts_arr)) {
591 $this->contacts_arr = array();
594 if(!is_array($this->users_arr)) {
595 $this->users_arr = array();
598 if(!is_array($this->leads_arr)) {
599 $this->leads_arr = array();
602 foreach($this->users_arr as $user_id) {
603 $notify_user = new User();
604 $notify_user->retrieve($user_id);
605 $notify_user->new_assigned_user_name = $notify_user->full_name;
606 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
607 $list[$notify_user->id] = $notify_user;
610 foreach($this->contacts_arr as $contact_id) {
611 $notify_user = new Contact();
612 $notify_user->retrieve($contact_id);
613 $notify_user->new_assigned_user_name = $notify_user->full_name;
614 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
615 $list[$notify_user->id] = $notify_user;
618 foreach($this->leads_arr as $lead_id) {
619 $notify_user = new Lead();
620 $notify_user->retrieve($lead_id);
621 $notify_user->new_assigned_user_name = $notify_user->full_name;
622 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
623 $list[$notify_user->id] = $notify_user;
630 function bean_implements($interface) {
632 case 'ACL':return true;
637 function listviewACLHelper() {
638 $array_assign = parent::listviewACLHelper();
640 if(!empty($this->parent_name)) {
642 if(!empty($this->parent_name_owner)) {
643 global $current_user;
644 $is_owner = $current_user->id == $this->parent_name_owner;
648 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)) {
649 $array_assign['PARENT'] = 'a';
651 $array_assign['PARENT'] = 'span';
656 if(!empty($this->contact_name)) {
657 if(!empty($this->contact_name_owner)) {
658 global $current_user;
659 $is_owner = $current_user->id == $this->contact_name_owner;
663 if(ACLController::checkAccess('Contacts', 'view', $is_owner)) {
664 $array_assign['CONTACT'] = 'a';
666 $array_assign['CONTACT'] = 'span';
668 return $array_assign;
672 function save_relationship_changes($is_update) {
674 if(empty($this->in_workflow)) {
675 if(empty($this->in_import)){//if a meeting is being imported then contact_id should not be excluded
676 //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
677 //add assigned_user_id to exclude list and let the logic from MeetingFormBase determine whether assigned user id gets added to the relationship
678 if(!empty($GLOBALS['soap_server_object'])){
679 $exclude = array('contact_id', 'user_id');
681 $exclude = array('contact_id', 'user_id','assigned_user_id');
685 $exclude = array('user_id');
688 parent::save_relationship_changes($is_update, $exclude);
693 * @see SugarBean::afterImportSave()
695 public function afterImportSave()
697 if ( $this->parent_type == 'Contacts' ) {
698 $this->load_relationship('contacts');
699 if ( !$this->contacts->relationship_exists('contacts',array('id'=>$this->parent_id)) )
700 $this->contacts->add($this->parent_id);
702 elseif ( $this->parent_type == 'Leads' ) {
703 $this->load_relationship('leads');
704 if ( !$this->leads->relationship_exists('leads',array('id'=>$this->parent_id)) )
705 $this->leads->add($this->parent_id);
708 parent::afterImportSave();
711 public function getDefaultStatus()
713 $def = $this->field_defs['status'];
714 if (isset($def['default'])) {
715 return $def['default'];
717 $app = return_app_list_strings_language($GLOBALS['current_language']);
718 if (isset($def['options']) && isset($app[$def['options']])) {
719 $keys = array_keys($app[$def['options']]);
727 // External API integration, for the dropdown list of what external API's are available
728 function getMeetingsExternalApiDropDown($focus = null, $name = null, $value = null, $view = null) {
729 require_once('include/externalAPI/ExternalAPIFactory.php');
731 $apiList = ExternalAPIFactory::getModuleDropDown('Meetings');
732 $apiList = array_merge(array('Sugar'=>$GLOBALS['app_list_strings']['eapm_list']['Sugar']),$apiList);
733 if(!empty($value) && empty($apiList[$value])){
734 $apiList[$value] = $value;