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.
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->getCustomJoin();
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";
252 $query .= $custom_join['select'];
254 // this line will help generate a GMT-metric to compare to a locale's timezone
256 if ( preg_match("/contacts/",$where)){
257 $query .= ", contacts.first_name, contacts.last_name";
258 $query .= ", contacts.assigned_user_id contact_name_owner";
260 $query .= " FROM calls ";
262 if ( preg_match("/contacts/",$where)){
263 $query .= "LEFT JOIN calls_contacts
264 ON calls.id=calls_contacts.call_id
266 ON calls_contacts.contact_id=contacts.id ";
268 if ( preg_match('/calls_users\.user_id/',$where))
270 $query .= "LEFT JOIN calls_users
271 ON calls.id=calls_users.call_id and calls_users.deleted=0 ";
275 ON calls.assigned_user_id=users.id ";
276 $query .= $custom_join['join'];
278 if($show_deleted == 0){
279 $where_auto = " $this->table_name.deleted=0 ";
280 }else if($show_deleted == 1){
281 $where_auto = " $this->table_name.deleted=1 ";
284 //$where_auto .= " GROUP BY calls.id";
287 $query .= "where $where AND ".$where_auto;
289 $query .= "where ".$where_auto;
291 $order_by = $this->process_order_by($order_by);
292 if (empty($order_by)) {
293 $order_by = 'calls.name';
295 $query .= ' ORDER BY ' . $order_by;
300 function create_export_query(&$order_by, &$where, $relate_link_join='')
302 $custom_join = $this->getCustomJoin(true, true, $where);
303 $custom_join['join'] .= $relate_link_join;
304 $contact_required = stristr($where, "contacts");
305 if($contact_required)
307 $query = "SELECT calls.*, contacts.first_name, contacts.last_name, users.user_name as assigned_user_name ";
308 $query .= $custom_join['select'];
309 $query .= " FROM contacts, calls, calls_contacts ";
310 $where_auto = "calls_contacts.contact_id = contacts.id AND calls_contacts.call_id = calls.id AND calls.deleted=0 AND contacts.deleted=0";
314 $query = 'SELECT calls.*, users.user_name as assigned_user_name ';
315 $query .= $custom_join['select'];
316 $query .= ' FROM calls ';
317 $where_auto = "calls.deleted=0";
321 $query .= " LEFT JOIN users ON calls.assigned_user_id=users.id ";
323 $query .= $custom_join['join'];
326 $query .= "where $where AND ".$where_auto;
328 $query .= "where ".$where_auto;
330 $order_by = $this->process_order_by($order_by);
331 if (empty($order_by)) {
332 $order_by = 'calls.name';
334 $query .= ' ORDER BY ' . $order_by;
343 function fill_in_additional_detail_fields()
346 parent::fill_in_additional_detail_fields();
347 if (!empty($this->contact_id)) {
348 $query = "SELECT first_name, last_name FROM contacts ";
349 $query .= "WHERE id='$this->contact_id' AND deleted=0";
350 $result = $this->db->limitQuery($query,0,1,true," Error filling in additional detail fields: ");
352 // Get the contact name.
353 $row = $this->db->fetchByAssoc($result);
354 $GLOBALS['log']->info("additional call fields $query");
357 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], '', '');
358 $GLOBALS['log']->debug("Call($this->id): contact_name = $this->contact_name");
359 $GLOBALS['log']->debug("Call($this->id): contact_id = $this->contact_id");
362 if (!isset($this->duration_minutes)) {
363 $this->duration_minutes = $this->minutes_value_default;
367 //setting default date and time
368 if (is_null($this->date_start)) {
369 $this->date_start = $timedate->now();
372 if (is_null($this->duration_hours))
373 $this->duration_hours = "0";
374 if (is_null($this->duration_minutes))
375 $this->duration_minutes = "1";
377 $this->fill_in_additional_parent_fields();
379 global $app_list_strings;
380 $parent_types = $app_list_strings['record_type_display'];
381 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
382 foreach($disabled_parent_types as $disabled_parent_type){
383 if($disabled_parent_type != $this->parent_type){
384 unset($parent_types[$disabled_parent_type]);
388 $this->parent_type_options = get_select_options_with_id($parent_types, $this->parent_type);
390 if (empty($this->reminder_time)) {
391 $this->reminder_time = -1;
394 if ( empty($this->id) ) {
395 $reminder_t = $GLOBALS['current_user']->getPreference('reminder_time');
396 if ( isset($reminder_t) )
397 $this->reminder_time = $reminder_t;
399 $this->reminder_checked = $this->reminder_time == -1 ? false : true;
401 if (empty($this->email_reminder_time)) {
402 $this->email_reminder_time = -1;
404 if(empty($this->id)){
405 $reminder_t = $GLOBALS['current_user']->getPreference('email_reminder_time');
406 if(isset($reminder_t))
407 $this->email_reminder_time = $reminder_t;
409 $this->email_reminder_checked = $this->email_reminder_time == -1 ? false : true;
411 if (isset ($_REQUEST['parent_type']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'SubpanelEdits')) {
412 $this->parent_type = $_REQUEST['parent_type'];
413 } elseif (is_null($this->parent_type)) {
414 $this->parent_type = $app_list_strings['record_type_default_key'];
419 function get_list_view_data(){
420 $call_fields = $this->get_list_view_array();
421 global $app_list_strings, $focus, $action, $currentModule;
422 if (isset($focus->id)) $id = $focus->id;
424 if (isset($this->parent_type) && $this->parent_type != null)
426 $call_fields['PARENT_MODULE'] = $this->parent_type;
428 if ($this->status == "Planned") {
429 //cn: added this if() to deal with sequential Closes in Meetings. this is a hack to a hack (formbase.php->handleRedirect)
433 $setCompleteUrl = "<a id='{$this->id}' onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Held\",\"listview\",\"1\");'>";
434 if ($this->ACLAccess('edit')) {
435 $call_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline"," border='0'",null,null,'.gif',translate('LBL_CLOSEINLINE'))."</a>";
437 $call_fields['SET_COMPLETE'] = '';
441 $today = $timedate->nowDb();
442 $nextday = $timedate->asDbDate($timedate->getNow()->modify("+1 day"));
443 $mergeTime = $call_fields['DATE_START']; //$timedate->merge_date_time($call_fields['DATE_START'], $call_fields['TIME_START']);
444 $date_db = $timedate->to_db($mergeTime);
445 if( $date_db < $today){
446 $call_fields['DATE_START']= "<font class='overdueTask'>".$call_fields['DATE_START']."</font>";
447 }else if($date_db < $nextday){
448 $call_fields['DATE_START'] = "<font class='todaysTask'>".$call_fields['DATE_START']."</font>";
450 $call_fields['DATE_START'] = "<font class='futureTask'>".$call_fields['DATE_START']."</font>";
452 $this->fill_in_additional_detail_fields();
454 //make sure we grab the localized version of the contact name, if a contact is provided
455 if (!empty($this->contact_id)) {
456 // Bug# 46125 - make first name, last name, salutation and title of Contacts respect field level ACLs
457 $contact_temp = BeanFactory::getBean("Contacts", $this->contact_id);
458 if(!empty($contact_temp)) {
459 $contact_temp->_create_proper_name_field();
460 $this->contact_name = $contact_temp->full_name;
464 $call_fields['CONTACT_ID'] = $this->contact_id;
465 $call_fields['CONTACT_NAME'] = $this->contact_name;
466 $call_fields['PARENT_NAME'] = $this->parent_name;
467 $call_fields['REMINDER_CHECKED'] = $this->reminder_time==-1 ? false : true;
468 $call_fields['EMAIL_REMINDER_CHECKED'] = $this->email_reminder_time==-1 ? false : true;
473 function set_notification_body($xtpl, $call) {
474 global $sugar_config;
475 global $app_list_strings;
476 global $current_user;
477 global $app_list_strings;
480 // rrs: bug 42684 - passing a contact breaks this call
481 $notifyUser =($call->current_notify_user->object_name == 'User') ? $call->current_notify_user : $current_user;
484 // Assumes $call dates are in user format
485 $calldate = $timedate->fromDb($call->date_start);
486 $xOffset = $timedate->asUser($calldate, $notifyUser).' '.$timedate->userTimezoneSuffix($calldate, $notifyUser);
488 if ( strtolower(get_class($call->current_notify_user)) == 'contact' ) {
489 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
490 '/index.php?entryPoint=acceptDecline&module=Calls&contact_id='.$call->current_notify_user->id.'&record='.$call->id);
491 } elseif ( strtolower(get_class($call->current_notify_user)) == 'lead' ) {
492 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
493 '/index.php?entryPoint=acceptDecline&module=Calls&lead_id='.$call->current_notify_user->id.'&record='.$call->id);
495 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
496 '/index.php?entryPoint=acceptDecline&module=Calls&user_id='.$call->current_notify_user->id.'&record='.$call->id);
499 $xtpl->assign("CALL_TO", $call->current_notify_user->new_assigned_user_name);
500 $xtpl->assign("CALL_SUBJECT", $call->name);
501 $xtpl->assign("CALL_STARTDATE", $xOffset);
502 $xtpl->assign("CALL_HOURS", $call->duration_hours);
503 $xtpl->assign("CALL_MINUTES", $call->duration_minutes);
504 $xtpl->assign("CALL_STATUS", ((isset($call->status))?$app_list_strings['call_status_dom'][$call->status] : ""));
505 $xtpl->assign("CALL_DESCRIPTION", $call->description);
511 function get_call_users() {
512 $template = new User();
513 // First, get the list of IDs.
514 $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";
515 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
516 $result = $this->db->query($query, true);
519 while($row = $this->db->fetchByAssoc($result)) {
520 $template = new User(); // PHP 5 will retrieve by reference, always over-writing the "old" one
521 $record = $template->retrieve($row['user_id']);
522 $template->required = $row['required'];
523 $template->accept_status = $row['accept_status'];
525 if($record != null) {
526 // this copies the object into the array
534 function get_invite_calls(&$user)
537 // First, get the list of IDs.
538 $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";
539 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
542 $result = $this->db->query($query, true);
548 while($row = $this->db->fetchByAssoc($result))
550 $record = $template->retrieve($row['call_id']);
551 $template->required = $row['required'];
552 $template->accept_status = $row['accept_status'];
557 // this copies the object into the array
566 function set_accept_status(&$user,$status)
568 if ( $user->object_name == 'User')
570 $relate_values = array('user_id'=>$user->id,'call_id'=>$this->id);
571 $data_values = array('accept_status'=>$status);
572 $this->set_relationship($this->rel_users_table, $relate_values, true, true,$data_values);
573 global $current_user;
575 if ( $this->update_vcal )
577 vCal::cache_sugar_vcal($user);
580 else if ( $user->object_name == 'Contact')
582 $relate_values = array('contact_id'=>$user->id,'call_id'=>$this->id);
583 $data_values = array('accept_status'=>$status);
584 $this->set_relationship($this->rel_contacts_table, $relate_values, true, true,$data_values);
586 else if ( $user->object_name == 'Lead')
588 $relate_values = array('lead_id'=>$user->id,'call_id'=>$this->id);
589 $data_values = array('accept_status'=>$status);
590 $this->set_relationship($this->rel_leads_table, $relate_values, true, true,$data_values);
596 function get_notification_recipients() {
597 if($this->special_notification) {
598 return parent::get_notification_recipients();
601 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($this,true));
603 if(!is_array($this->contacts_arr)) {
604 $this->contacts_arr = array();
607 if(!is_array($this->users_arr)) {
608 $this->users_arr = array();
611 if(!is_array($this->leads_arr)) {
612 $this->leads_arr = array();
615 foreach($this->users_arr as $user_id) {
616 $notify_user = new User();
617 $notify_user->retrieve($user_id);
618 $notify_user->new_assigned_user_name = $notify_user->full_name;
619 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
620 $list[$notify_user->id] = $notify_user;
623 foreach($this->contacts_arr as $contact_id) {
624 $notify_user = new Contact();
625 $notify_user->retrieve($contact_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->leads_arr as $lead_id) {
632 $notify_user = new Lead();
633 $notify_user->retrieve($lead_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;
638 global $sugar_config;
639 if(isset($sugar_config['disable_notify_current_user']) && $sugar_config['disable_notify_current_user']) {
640 global $current_user;
641 if(isset($list[$current_user->id]))
642 unset($list[$current_user->id]);
644 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($list,true));
648 function bean_implements($interface){
650 case 'ACL':return true;
655 function listviewACLHelper(){
656 $array_assign = parent::listviewACLHelper();
658 if(!empty($this->parent_name)){
660 if(!empty($this->parent_name_owner)){
661 global $current_user;
662 $is_owner = $current_user->id == $this->parent_name_owner;
665 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)){
666 $array_assign['PARENT'] = 'a';
668 $array_assign['PARENT'] = 'span';
671 if(!empty($this->contact_name)){
673 if(!empty($this->contact_name_owner)){
674 global $current_user;
675 $is_owner = $current_user->id == $this->contact_name_owner;
678 if( ACLController::checkAccess('Contacts', 'view', $is_owner)){
679 $array_assign['CONTACT'] = 'a';
681 $array_assign['CONTACT'] = 'span';
684 return $array_assign;
687 function save_relationship_changes($is_update) {
689 if(empty($this->in_workflow))
691 if(empty($this->in_import))
693 //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
694 //add assigned_user_id to exclude list and let the logic from MeetingFormBase determine whether assigned user id gets added to the relationship
695 if(!empty($GLOBALS['soap_server_object']))
697 $exclude = array('lead_id', 'contact_id', 'user_id');
701 $exclude = array('lead_id', 'contact_id', 'user_id', 'assigned_user_id');
706 $exclude = array('user_id');
711 parent::save_relationship_changes($is_update, $exclude);
714 public function getDefaultStatus()
716 $def = $this->field_defs['status'];
717 if (isset($def['default'])) {
718 return $def['default'];
720 $app = return_app_list_strings_language($GLOBALS['current_language']);
721 if (isset($def['options']) && isset($app[$def['options']])) {
722 $keys = array_keys($app[$def['options']]);
729 public function mark_deleted($id)
731 require_once("modules/Calendar/CalendarUtils.php");
732 CalendarUtils::correctRecurrences($this, $id);
734 parent::mark_deleted($id);