2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
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 ********************************************************************************/
46 class Call extends SugarBean {
53 var $assigned_user_id;
54 var $modified_user_id;
61 var $duration_minutes;
64 var $parent_type_options;
71 var $reminder_time_options;
72 var $reminder_checked;
73 var $email_reminder_time;
74 var $email_reminder_checked;
75 var $email_reminder_sent;
80 var $modified_by_name;
88 var $assigned_user_name;
91 var $update_vcal = true;
95 var $default_call_name_values = array('Assemble catalogs', 'Make travel arrangements', 'Send a letter', 'Send contract', 'Send fax', 'Send a follow-up letter', 'Send literature', 'Send proposal', 'Send quote');
96 var $minutes_value_default = 15;
97 var $minutes_values = array('0'=>'00','15'=>'15','30'=>'30','45'=>'45');
98 var $table_name = "calls";
99 var $rel_users_table = "calls_users";
100 var $rel_contacts_table = "calls_contacts";
101 var $rel_leads_table = "calls_leads";
102 var $module_dir = 'Calls';
103 var $object_name = "Call";
104 var $new_schema = true;
105 var $importable = true;
106 var $syncing = false;
107 var $recurring_source;
109 // This is used to retrieve related fields from form posts.
110 var $additional_column_fields = array('assigned_user_name', 'assigned_user_id', 'contact_id', 'user_id', 'contact_name');
111 var $relationship_fields = array( 'account_id' => 'accounts',
112 'opportunity_id' => 'opportunities',
113 'contact_id' => 'contacts',
114 'case_id' => 'cases',
115 'user_id' => 'users',
116 'assigned_user_id' => 'users',
117 'note_id' => 'notes',
118 'lead_id' => 'leads',
123 global $app_list_strings;
125 $this->setupCustomFields('Calls');
127 foreach ($this->field_defs as $field) {
128 $this->field_name_map[$field['name']] = $field;
134 if(!empty($GLOBALS['app_list_strings']['duration_intervals']))
135 $this->minutes_values = $GLOBALS['app_list_strings']['duration_intervals'];
139 * Disable edit if call is recurring and source is not Sugar. It should be edited only from Outlook.
140 * @param $view string
141 * @param $is_owner bool
143 function ACLAccess($view,$is_owner = 'not_set'){
144 // don't check if call is being synced from Outlook
145 if($this->syncing == false){
146 $view = strtolower($view);
152 if(!empty($this->recurring_source) && $this->recurring_source != "Sugar"){
157 return parent::ACLAccess($view,$is_owner);
159 // save date_end by calculating user input
160 // this is for calendar
161 function save($check_notify = FALSE) {
162 global $timedate,$current_user;
164 if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
166 $td = $timedate->fromDb($this->date_start);
169 $this->date_end = $td->modify("+{$this->duration_hours} hours {$this->duration_minutes} mins")->asDb();
173 if(!empty($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == '1') {
174 $check_notify = true;
176 $check_notify = false;
178 if(empty($_REQUEST['send_invites'])) {
179 if(!empty($this->id)) {
180 $old_record = new Call();
181 $old_record->retrieve($this->id);
182 $old_assigned_user_id = $old_record->assigned_user_id;
184 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']) ){
185 $this->special_notification = true;
186 if(!isset($GLOBALS['resavingRelatedBeans']) || $GLOBALS['resavingRelatedBeans'] == false) {
187 $check_notify = true;
189 if(isset($_REQUEST['assigned_user_name'])) {
190 $this->new_assigned_user_name = $_REQUEST['assigned_user_name'];
194 if (empty($this->status) ) {
195 $this->status = $this->getDefaultStatus();
198 // prevent a mass mailing for recurring meetings created in Calendar module
199 if (empty($this->id) && !empty($_REQUEST['module']) && $_REQUEST['module'] == "Calendar" && !empty($_REQUEST['repeat_type']) && !empty($this->repeat_parent_id)) {
200 $check_notify = false;
202 /*nsingh 7/3/08 commenting out as bug #20814 is invalid
203 if($current_user->getPreference('reminder_time')!= -1 && isset($_POST['reminder_checked']) && isset($_POST['reminder_time']) && $_POST['reminder_checked']==0 && $_POST['reminder_time']==-1){
204 $this->reminder_checked = '1';
205 $this->reminder_time = $current_user->getPreference('reminder_time');
208 $return_id = parent::save($check_notify);
209 global $current_user;
212 if($this->update_vcal) {
213 vCal::cache_sugar_vcal($current_user);
219 /** Returns a list of the associated contacts
220 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
221 * All Rights Reserved..
222 * Contributor(s): ______________________________________..
224 function get_contacts()
226 // First, get the list of IDs.
227 $query = "SELECT contact_id as id from calls_contacts where call_id='$this->id' AND deleted=0";
229 return $this->build_related_list($query, new Contact());
233 function get_summary_text()
235 return "$this->name";
238 function create_list_query($order_by, $where, $show_deleted=0)
240 $custom_join = $this->custom_fields->getJOIN();
244 if ( preg_match("/calls_users\.user_id/",$where))
246 $query .= "calls_users.required,
247 calls_users.accept_status,";
251 users.user_name as assigned_user_name";
253 $query .= $custom_join['select'];
256 // this line will help generate a GMT-metric to compare to a locale's timezone
258 if ( preg_match("/contacts/",$where)){
259 $query .= ", contacts.first_name, contacts.last_name";
260 $query .= ", contacts.assigned_user_id contact_name_owner";
262 $query .= " FROM calls ";
264 if ( preg_match("/contacts/",$where)){
265 $query .= "LEFT JOIN calls_contacts
266 ON calls.id=calls_contacts.call_id
268 ON calls_contacts.contact_id=contacts.id ";
270 if ( preg_match('/calls_users\.user_id/',$where))
272 $query .= "LEFT JOIN calls_users
273 ON calls.id=calls_users.call_id and calls_users.deleted=0 ";
277 ON calls.assigned_user_id=users.id ";
279 $query .= $custom_join['join'];
282 if($show_deleted == 0){
283 $where_auto = " $this->table_name.deleted=0 ";
284 }else if($show_deleted == 1){
285 $where_auto = " $this->table_name.deleted=1 ";
288 //$where_auto .= " GROUP BY calls.id";
291 $query .= "where $where AND ".$where_auto;
293 $query .= "where ".$where_auto;
296 $query .= " ORDER BY ". $this->process_order_by($order_by, null);
298 $query .= " ORDER BY calls.name";
302 function create_export_query(&$order_by, &$where, $relate_link_join='')
304 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
306 $custom_join['join'] .= $relate_link_join;
307 $contact_required = stristr($where, "contacts");
308 if($contact_required)
310 $query = "SELECT calls.*, contacts.first_name, contacts.last_name, users.user_name as assigned_user_name ";
312 $query .= $custom_join['select'];
314 $query .= " FROM contacts, calls, calls_contacts ";
315 $where_auto = "calls_contacts.contact_id = contacts.id AND calls_contacts.call_id = calls.id AND calls.deleted=0 AND contacts.deleted=0";
319 $query = 'SELECT calls.*, users.user_name as assigned_user_name ';
321 $query .= $custom_join['select'];
323 $query .= ' FROM calls ';
324 $where_auto = "calls.deleted=0";
328 $query .= " LEFT JOIN users ON calls.assigned_user_id=users.id ";
331 $query .= $custom_join['join'];
335 $query .= "where $where AND ".$where_auto;
337 $query .= "where ".$where_auto;
340 $query .= " ORDER BY ". $this->process_order_by($order_by, null);
342 $query .= " ORDER BY calls.name";
351 function fill_in_additional_detail_fields()
354 parent::fill_in_additional_detail_fields();
355 if (!empty($this->contact_id)) {
356 $query = "SELECT first_name, last_name FROM contacts ";
357 $query .= "WHERE id='$this->contact_id' AND deleted=0";
358 $result = $this->db->limitQuery($query,0,1,true," Error filling in additional detail fields: ");
360 // Get the contact name.
361 $row = $this->db->fetchByAssoc($result);
362 $GLOBALS['log']->info("additional call fields $query");
365 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], '', '');
366 $GLOBALS['log']->debug("Call($this->id): contact_name = $this->contact_name");
367 $GLOBALS['log']->debug("Call($this->id): contact_id = $this->contact_id");
370 if (!isset($this->duration_minutes)) {
371 $this->duration_minutes = $this->minutes_value_default;
375 //setting default date and time
376 if (is_null($this->date_start)) {
377 $this->date_start = $timedate->now();
380 if (is_null($this->duration_hours))
381 $this->duration_hours = "0";
382 if (is_null($this->duration_minutes))
383 $this->duration_minutes = "1";
385 $this->fill_in_additional_parent_fields();
387 global $app_list_strings;
388 $parent_types = $app_list_strings['record_type_display'];
389 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
390 foreach($disabled_parent_types as $disabled_parent_type){
391 if($disabled_parent_type != $this->parent_type){
392 unset($parent_types[$disabled_parent_type]);
396 $this->parent_type_options = get_select_options_with_id($parent_types, $this->parent_type);
398 if (empty($this->reminder_time)) {
399 $this->reminder_time = -1;
402 if ( empty($this->id) ) {
403 $reminder_t = $GLOBALS['current_user']->getPreference('reminder_time');
404 if ( isset($reminder_t) )
405 $this->reminder_time = $reminder_t;
407 $this->reminder_checked = $this->reminder_time == -1 ? false : true;
409 if (empty($this->email_reminder_time)) {
410 $this->email_reminder_time = -1;
412 if(empty($this->id)){
413 $reminder_t = $GLOBALS['current_user']->getPreference('email_reminder_time');
414 if(isset($reminder_t))
415 $this->email_reminder_time = $reminder_t;
417 $this->email_reminder_checked = $this->email_reminder_time == -1 ? false : true;
419 if (isset ($_REQUEST['parent_type']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'SubpanelEdits')) {
420 $this->parent_type = $_REQUEST['parent_type'];
421 } elseif (is_null($this->parent_type)) {
422 $this->parent_type = $app_list_strings['record_type_default_key'];
427 function get_list_view_data(){
428 $call_fields = $this->get_list_view_array();
429 global $app_list_strings, $focus, $action, $currentModule;
430 if (isset($focus->id)) $id = $focus->id;
432 if (isset($this->parent_type) && $this->parent_type != null)
434 $call_fields['PARENT_MODULE'] = $this->parent_type;
436 if ($this->status == "Planned") {
437 //cn: added this if() to deal with sequential Closes in Meetings. this is a hack to a hack (formbase.php->handleRedirect)
441 $setCompleteUrl = "<a id='{$this->id}' onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Held\",\"listview\",\"1\");'>";
442 if ($this->ACLAccess('edit')) {
443 $call_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline"," border='0'",null,null,'.gif',translate('LBL_CLOSEINLINE'))."</a>";
445 $call_fields['SET_COMPLETE'] = '';
449 $today = $timedate->nowDb();
450 $nextday = $timedate->asDbDate($timedate->getNow()->modify("+1 day"));
451 $mergeTime = $call_fields['DATE_START']; //$timedate->merge_date_time($call_fields['DATE_START'], $call_fields['TIME_START']);
452 $date_db = $timedate->to_db($mergeTime);
453 if( $date_db < $today){
454 $call_fields['DATE_START']= "<font class='overdueTask'>".$call_fields['DATE_START']."</font>";
455 }else if($date_db < $nextday){
456 $call_fields['DATE_START'] = "<font class='todaysTask'>".$call_fields['DATE_START']."</font>";
458 $call_fields['DATE_START'] = "<font class='futureTask'>".$call_fields['DATE_START']."</font>";
460 $this->fill_in_additional_detail_fields();
462 //make sure we grab the localized version of the contact name, if a contact is provided
463 if (!empty($this->contact_id)) {
464 // Bug# 46125 - make first name, last name, salutation and title of Contacts respect field level ACLs
465 $contact_temp = BeanFactory::getBean("Contacts", $this->contact_id);
466 if(!empty($contact_temp)) {
467 $contact_temp->_create_proper_name_field();
468 $this->contact_name = $contact_temp->full_name;
472 $call_fields['CONTACT_ID'] = $this->contact_id;
473 $call_fields['CONTACT_NAME'] = $this->contact_name;
474 $call_fields['PARENT_NAME'] = $this->parent_name;
475 $call_fields['REMINDER_CHECKED'] = $this->reminder_time==-1 ? false : true;
476 $call_fields['EMAIL_REMINDER_CHECKED'] = $this->email_reminder_time==-1 ? false : true;
481 function set_notification_body($xtpl, $call) {
482 global $sugar_config;
483 global $app_list_strings;
484 global $current_user;
485 global $app_list_strings;
488 // rrs: bug 42684 - passing a contact breaks this call
489 $notifyUser =($call->current_notify_user->object_name == 'User') ? $call->current_notify_user : $current_user;
492 // Assumes $call dates are in user format
493 $calldate = $timedate->fromDb($call->date_start);
494 $xOffset = $timedate->asUser($calldate, $notifyUser).' '.$timedate->userTimezoneSuffix($calldate, $notifyUser);
496 if ( strtolower(get_class($call->current_notify_user)) == 'contact' ) {
497 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
498 '/index.php?entryPoint=acceptDecline&module=Calls&contact_id='.$call->current_notify_user->id.'&record='.$call->id);
499 } elseif ( strtolower(get_class($call->current_notify_user)) == 'lead' ) {
500 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
501 '/index.php?entryPoint=acceptDecline&module=Calls&lead_id='.$call->current_notify_user->id.'&record='.$call->id);
503 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
504 '/index.php?entryPoint=acceptDecline&module=Calls&user_id='.$call->current_notify_user->id.'&record='.$call->id);
507 $xtpl->assign("CALL_TO", $call->current_notify_user->new_assigned_user_name);
508 $xtpl->assign("CALL_SUBJECT", $call->name);
509 $xtpl->assign("CALL_STARTDATE", $xOffset);
510 $xtpl->assign("CALL_HOURS", $call->duration_hours);
511 $xtpl->assign("CALL_MINUTES", $call->duration_minutes);
512 $xtpl->assign("CALL_STATUS", ((isset($call->status))?$app_list_strings['call_status_dom'][$call->status] : ""));
513 $xtpl->assign("CALL_DESCRIPTION", $call->description);
519 function get_call_users() {
520 $template = new User();
521 // First, get the list of IDs.
522 $query = "SELECT calls_users.required, calls_users.accept_status, calls_users.user_id from calls_users where calls_users.call_id='$this->id' AND calls_users.deleted=0";
523 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
524 $result = $this->db->query($query, true);
527 while($row = $this->db->fetchByAssoc($result)) {
528 $template = new User(); // PHP 5 will retrieve by reference, always over-writing the "old" one
529 $record = $template->retrieve($row['user_id']);
530 $template->required = $row['required'];
531 $template->accept_status = $row['accept_status'];
533 if($record != null) {
534 // this copies the object into the array
542 function get_invite_calls(&$user)
545 // First, get the list of IDs.
546 $query = "SELECT calls_users.required, calls_users.accept_status, calls_users.call_id from calls_users where calls_users.user_id='$user->id' AND ( calls_users.accept_status IS NULL OR calls_users.accept_status='none') AND calls_users.deleted=0";
547 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
550 $result = $this->db->query($query, true);
556 while($row = $this->db->fetchByAssoc($result))
558 $record = $template->retrieve($row['call_id']);
559 $template->required = $row['required'];
560 $template->accept_status = $row['accept_status'];
565 // this copies the object into the array
574 function set_accept_status(&$user,$status)
576 if ( $user->object_name == 'User')
578 $relate_values = array('user_id'=>$user->id,'call_id'=>$this->id);
579 $data_values = array('accept_status'=>$status);
580 $this->set_relationship($this->rel_users_table, $relate_values, true, true,$data_values);
581 global $current_user;
583 if ( $this->update_vcal )
585 vCal::cache_sugar_vcal($user);
588 else if ( $user->object_name == 'Contact')
590 $relate_values = array('contact_id'=>$user->id,'call_id'=>$this->id);
591 $data_values = array('accept_status'=>$status);
592 $this->set_relationship($this->rel_contacts_table, $relate_values, true, true,$data_values);
594 else if ( $user->object_name == 'Lead')
596 $relate_values = array('lead_id'=>$user->id,'call_id'=>$this->id);
597 $data_values = array('accept_status'=>$status);
598 $this->set_relationship($this->rel_leads_table, $relate_values, true, true,$data_values);
604 function get_notification_recipients() {
605 if($this->special_notification) {
606 return parent::get_notification_recipients();
609 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($this,true));
611 if(!is_array($this->contacts_arr)) {
612 $this->contacts_arr = array();
615 if(!is_array($this->users_arr)) {
616 $this->users_arr = array();
619 if(!is_array($this->leads_arr)) {
620 $this->leads_arr = array();
623 foreach($this->users_arr as $user_id) {
624 $notify_user = new User();
625 $notify_user->retrieve($user_id);
626 $notify_user->new_assigned_user_name = $notify_user->full_name;
627 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
628 $list[$notify_user->id] = $notify_user;
631 foreach($this->contacts_arr as $contact_id) {
632 $notify_user = new Contact();
633 $notify_user->retrieve($contact_id);
634 $notify_user->new_assigned_user_name = $notify_user->full_name;
635 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
636 $list[$notify_user->id] = $notify_user;
639 foreach($this->leads_arr as $lead_id) {
640 $notify_user = new Lead();
641 $notify_user->retrieve($lead_id);
642 $notify_user->new_assigned_user_name = $notify_user->full_name;
643 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
644 $list[$notify_user->id] = $notify_user;
646 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($list,true));
650 function bean_implements($interface){
652 case 'ACL':return true;
657 function listviewACLHelper(){
658 $array_assign = parent::listviewACLHelper();
660 if(!empty($this->parent_name)){
662 if(!empty($this->parent_name_owner)){
663 global $current_user;
664 $is_owner = $current_user->id == $this->parent_name_owner;
667 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)){
668 $array_assign['PARENT'] = 'a';
670 $array_assign['PARENT'] = 'span';
673 if(!empty($this->contact_name)){
675 if(!empty($this->contact_name_owner)){
676 global $current_user;
677 $is_owner = $current_user->id == $this->contact_name_owner;
680 if( ACLController::checkAccess('Contacts', 'view', $is_owner)){
681 $array_assign['CONTACT'] = 'a';
683 $array_assign['CONTACT'] = 'span';
686 return $array_assign;
689 function save_relationship_changes($is_update) {
691 if(empty($this->in_workflow))
693 if(empty($this->in_import))
695 //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
696 //add assigned_user_id to exclude list and let the logic from MeetingFormBase determine whether assigned user id gets added to the relationship
697 if(!empty($GLOBALS['soap_server_object']))
699 $exclude = array('lead_id', 'contact_id', 'user_id');
703 $exclude = array('lead_id', 'contact_id', 'user_id', 'assigned_user_id');
708 $exclude = array('user_id');
713 parent::save_relationship_changes($is_update, $exclude);
716 public function getDefaultStatus()
718 $def = $this->field_defs['status'];
719 if (isset($def['default'])) {
720 return $def['default'];
722 $app = return_app_list_strings_language($GLOBALS['current_language']);
723 if (isset($def['options']) && isset($app[$def['options']])) {
724 $keys = array_keys($app[$def['options']]);
731 public function mark_deleted($id)
733 require_once("modules/Calendar/CalendarUtils.php");
734 CalendarUtils::correctRecurrences($this, $id);
736 parent::mark_deleted($id);