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 ********************************************************************************/
46 // Call is used to store customer information.
47 class Call extends SugarBean
55 var $assigned_user_id;
56 var $modified_user_id;
63 var $duration_minutes;
66 var $parent_type_options;
73 var $reminder_time_options;
74 var $reminder_checked;
79 var $modified_by_name;
87 var $assigned_user_name;
90 var $update_vcal = true;
94 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');
95 var $minutes_value_default = 15;
96 var $minutes_values = array('0'=>'00','15'=>'15','30'=>'30','45'=>'45');
97 var $table_name = "calls";
98 var $rel_users_table = "calls_users";
99 var $rel_contacts_table = "calls_contacts";
100 var $rel_leads_table = "calls_leads";
101 var $module_dir = 'Calls';
102 var $object_name = "Call";
103 var $new_schema = true;
104 var $importable = true;
106 // This is used to retrieve related fields from form posts.
107 var $additional_column_fields = array('assigned_user_name', 'assigned_user_id', 'contact_id', 'user_id', 'contact_name');
108 var $relationship_fields = array( 'account_id' => 'accounts',
109 'opportunity_id' => 'opportunities',
110 'contact_id' => 'contacts',
111 'case_id' => 'cases',
112 'user_id' => 'users',
113 'assigned_user_id' => 'users',
114 'note_id' => 'notes',
119 global $app_list_strings;
121 $this->setupCustomFields('Calls');
123 foreach ($this->field_defs as $field) {
124 $this->field_name_map[$field['name']] = $field;
130 if(!empty($GLOBALS['app_list_strings']['duration_intervals']))
131 $this->minutes_values = $GLOBALS['app_list_strings']['duration_intervals'];
134 // save date_end by calculating user input
135 // this is for calendar
136 function save($check_notify = FALSE) {
137 global $timedate,$current_user;
139 if(isset($this->date_start) && isset($this->duration_hours) && isset($this->duration_minutes))
141 $td = $timedate->fromDb($this->date_start);
144 $this->date_end = $td->modify("+{$this->duration_hours} hours {$this->duration_minutes} mins")->asDb();
148 if(!empty($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == '1') {
149 $check_notify = true;
151 $check_notify = false;
153 if(empty($_REQUEST['send_invites'])) {
154 if(!empty($this->id)) {
155 $old_record = new Call();
156 $old_record->retrieve($this->id);
157 $old_assigned_user_id = $old_record->assigned_user_id;
159 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']) ){
160 $this->special_notification = true;
161 $check_notify = true;
162 if(isset($_REQUEST['assigned_user_name'])) {
163 $this->new_assigned_user_name = $_REQUEST['assigned_user_name'];
167 if (empty($this->status) ) {
168 $this->status = $this->getDefaultStatus();
170 /*nsingh 7/3/08 commenting out as bug #20814 is invalid
171 if($current_user->getPreference('reminder_time')!= -1 && isset($_POST['reminder_checked']) && isset($_POST['reminder_time']) && $_POST['reminder_checked']==0 && $_POST['reminder_time']==-1){
172 $this->reminder_checked = '1';
173 $this->reminder_time = $current_user->getPreference('reminder_time');
176 $return_id = parent::save($check_notify);
177 global $current_user;
180 if($this->update_vcal) {
181 vCal::cache_sugar_vcal($current_user);
187 /** Returns a list of the associated contacts
188 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
189 * All Rights Reserved..
190 * Contributor(s): ______________________________________..
192 function get_contacts()
194 // First, get the list of IDs.
195 $query = "SELECT contact_id as id from calls_contacts where call_id='$this->id' AND deleted=0";
197 return $this->build_related_list($query, new Contact());
201 function get_summary_text()
203 return "$this->name";
206 function create_list_query($order_by, $where, $show_deleted=0)
208 $custom_join = $this->custom_fields->getJOIN();
212 if ( preg_match("/calls_users\.user_id/",$where))
214 $query .= "calls_users.required,
215 calls_users.accept_status,";
219 users.user_name as assigned_user_name";
221 $query .= $custom_join['select'];
224 // this line will help generate a GMT-metric to compare to a locale's timezone
226 if ( preg_match("/contacts/",$where)){
227 $query .= ", contacts.first_name, contacts.last_name";
228 $query .= ", contacts.assigned_user_id contact_name_owner";
230 $query .= " FROM calls ";
233 if ( $this->db->dbType == 'mssql' )
235 $query .= ", calls.date_start ";
236 if ( preg_match("/contacts/",$where)){
237 $query .= ", contacts.first_name, contacts.last_name";
238 $query .= ", contacts.assigned_user_id contact_name_owner";
240 $query .= " FROM calls ";
243 if ( preg_match("/contacts/",$where)){
244 $query .= "LEFT JOIN calls_contacts
245 ON calls.id=calls_contacts.call_id
247 ON calls_contacts.contact_id=contacts.id ";
249 if ( preg_match("/calls_users\.user_id/",$where))
251 $query .= "LEFT JOIN calls_users
252 ON calls.id=calls_users.call_id and calls_users.deleted=0 ";
256 ON calls.assigned_user_id=users.id ";
258 $query .= $custom_join['join'];
261 if($show_deleted == 0){
262 $where_auto = " $this->table_name.deleted=0 ";
263 }else if($show_deleted == 1){
264 $where_auto = " $this->table_name.deleted=1 ";
267 //$where_auto .= " GROUP BY calls.id";
270 $query .= "where $where AND ".$where_auto;
272 $query .= "where ".$where_auto;
275 $query .= " ORDER BY ". $this->process_order_by($order_by, null);
277 $query .= " ORDER BY calls.name";
281 function create_export_query(&$order_by, &$where, $relate_link_join='')
283 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
285 $custom_join['join'] .= $relate_link_join;
286 $contact_required = stristr($where, "contacts");
287 if($contact_required)
289 $query = "SELECT calls.*, contacts.first_name, contacts.last_name, users.user_name as assigned_user_name ";
291 $query .= $custom_join['select'];
293 $query .= " FROM contacts, calls, calls_contacts ";
294 $where_auto = "calls_contacts.contact_id = contacts.id AND calls_contacts.call_id = calls.id AND calls.deleted=0 AND contacts.deleted=0";
298 $query = 'SELECT calls.*, users.user_name as assigned_user_name ';
300 $query .= $custom_join['select'];
302 $query .= ' FROM calls ';
303 $where_auto = "calls.deleted=0";
307 $query .= " LEFT JOIN users ON calls.assigned_user_id=users.id ";
310 $query .= $custom_join['join'];
314 $query .= "where $where AND ".$where_auto;
316 $query .= "where ".$where_auto;
319 $query .= " ORDER BY ". $this->process_order_by($order_by, null);
321 $query .= " ORDER BY calls.name";
330 function fill_in_additional_detail_fields()
333 parent::fill_in_additional_detail_fields();
334 if (!empty($this->contact_id)) {
335 $query = "SELECT first_name, last_name FROM contacts ";
336 $query .= "WHERE id='$this->contact_id' AND deleted=0";
337 $result = $this->db->limitQuery($query,0,1,true," Error filling in additional detail fields: ");
339 // Get the contact name.
340 $row = $this->db->fetchByAssoc($result);
341 $GLOBALS['log']->info("additional call fields $query");
344 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], '', '');
345 $GLOBALS['log']->debug("Call($this->id): contact_name = $this->contact_name");
346 $GLOBALS['log']->debug("Call($this->id): contact_id = $this->contact_id");
349 if (!isset($this->duration_minutes)) {
350 $this->duration_minutes = $this->minutes_value_default;
354 //setting default date and time
355 if (is_null($this->date_start)) {
356 $this->date_start = $timedate->now();
359 if (is_null($this->duration_hours))
360 $this->duration_hours = "0";
361 if (is_null($this->duration_minutes))
362 $this->duration_minutes = "1";
364 $this->fill_in_additional_parent_fields();
366 global $app_list_strings;
367 $parent_types = $app_list_strings['record_type_display'];
368 $disabled_parent_types = ACLController::disabledModuleList($parent_types,false, 'list');
369 foreach($disabled_parent_types as $disabled_parent_type){
370 if($disabled_parent_type != $this->parent_type){
371 unset($parent_types[$disabled_parent_type]);
375 $this->parent_type_options = get_select_options_with_id($parent_types, $this->parent_type);
377 if (empty($this->reminder_time)) {
378 $this->reminder_time = -1;
381 if ( empty($this->id) ) {
382 $reminder_t = $GLOBALS['current_user']->getPreference('reminder_time');
383 if ( isset($reminder_t) )
384 $this->reminder_time = $reminder_t;
386 $this->reminder_checked = $this->reminder_time == -1 ? false : true;
389 if (isset ($_REQUEST['parent_type'])) {
390 $this->parent_type = $_REQUEST['parent_type'];
391 } elseif (is_null($this->parent_type)) {
392 $this->parent_type = $app_list_strings['record_type_default_key'];
397 function get_list_view_data(){
398 $call_fields = $this->get_list_view_array();
399 global $app_list_strings, $focus, $action, $currentModule;
400 if (isset($focus->id)) $id = $focus->id;
402 if (isset($this->parent_type) && $this->parent_type != null)
404 $call_fields['PARENT_MODULE'] = $this->parent_type;
406 if ($this->status == "Planned") {
407 //cn: added this if() to deal with sequential Closes in Meetings. this is a hack to a hack (formbase.php->handleRedirect)
411 $setCompleteUrl = "<a onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Held\",\"listview\",\"1\");'>";
412 $call_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline","title=".translate('LBL_LIST_CLOSE','Calls')." border='0'")."</a>";
415 $today = $timedate->nowDb();
416 $nextday = $timedate->asDbDate($timedate->getNow()->modify("+1 day"));
417 $mergeTime = $call_fields['DATE_START']; //$timedate->merge_date_time($call_fields['DATE_START'], $call_fields['TIME_START']);
418 $date_db = $timedate->to_db($mergeTime);
419 if( $date_db < $today){
420 $call_fields['DATE_START']= "<font class='overdueTask'>".$call_fields['DATE_START']."</font>";
421 }else if($date_db < $nextday){
422 $call_fields['DATE_START'] = "<font class='todaysTask'>".$call_fields['DATE_START']."</font>";
424 $call_fields['DATE_START'] = "<font class='futureTask'>".$call_fields['DATE_START']."</font>";
426 $this->fill_in_additional_detail_fields();
428 //make sure we grab the localized version of the contact name, if a contact is provided
429 if (!empty($this->contact_id)) {
431 // Bug# 46125 - make first name, last name, salutation and title of Contacts respect field level ACLs
432 $contact_temp = new Contact();
433 $contact_temp->retrieve($this->contact_id);
434 $contact_temp->_create_proper_name_field();
435 $this->contact_name = $contact_temp->full_name;
438 $call_fields['CONTACT_ID'] = $this->contact_id;
439 $call_fields['CONTACT_NAME'] = $this->contact_name;
441 $call_fields['PARENT_NAME'] = $this->parent_name;
443 $call_fields['REMINDER_CHECKED'] = $this->reminder_time==-1 ? false : true;
448 function set_notification_body($xtpl, $call) {
449 global $sugar_config;
450 global $app_list_strings;
451 global $current_user;
452 global $app_list_strings;
455 // rrs: bug 42684 - passing a contact breaks this call
456 $notifyUser =($call->current_notify_user->object_name == 'User') ? $call->current_notify_user : $current_user;
459 // Assumes $call dates are in user format
460 $calldate = $timedate->fromDb($call->date_start);
461 $xOffset = $timedate->asUser($calldate, $notifyUser).' '.$timedate->userTimezoneSuffix($calldate, $notifyUser);
463 if ( strtolower(get_class($call->current_notify_user)) == 'contact' ) {
464 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
465 '/index.php?entryPoint=acceptDecline&module=Calls&contact_id='.$call->current_notify_user->id.'&record='.$call->id);
466 } elseif ( strtolower(get_class($call->current_notify_user)) == 'lead' ) {
467 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
468 '/index.php?entryPoint=acceptDecline&module=Calls&lead_id='.$call->current_notify_user->id.'&record='.$call->id);
470 $xtpl->assign("ACCEPT_URL", $sugar_config['site_url'].
471 '/index.php?entryPoint=acceptDecline&module=Calls&user_id='.$call->current_notify_user->id.'&record='.$call->id);
474 $xtpl->assign("CALL_TO", $call->current_notify_user->new_assigned_user_name);
475 $xtpl->assign("CALL_SUBJECT", $call->name);
476 $xtpl->assign("CALL_STARTDATE", $xOffset);
477 $xtpl->assign("CALL_HOURS", $call->duration_hours);
478 $xtpl->assign("CALL_MINUTES", $call->duration_minutes);
479 $xtpl->assign("CALL_STATUS", ((isset($call->status))?$app_list_strings['call_status_dom'][$call->status] : ""));
480 $xtpl->assign("CALL_DESCRIPTION", $call->description);
486 function get_call_users() {
487 $template = new User();
488 // First, get the list of IDs.
489 $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";
490 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
491 $result = $this->db->query($query, true);
494 while($row = $this->db->fetchByAssoc($result)) {
495 $template = new User(); // PHP 5 will retrieve by reference, always over-writing the "old" one
496 $record = $template->retrieve($row['user_id']);
497 $template->required = $row['required'];
498 $template->accept_status = $row['accept_status'];
500 if($record != null) {
501 // this copies the object into the array
509 function get_invite_calls(&$user)
512 // First, get the list of IDs.
513 $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";
514 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
517 $result = $this->db->query($query, true);
523 while($row = $this->db->fetchByAssoc($result))
525 $record = $template->retrieve($row['call_id']);
526 $template->required = $row['required'];
527 $template->accept_status = $row['accept_status'];
532 // this copies the object into the array
541 function set_accept_status(&$user,$status)
543 if ( $user->object_name == 'User')
545 $relate_values = array('user_id'=>$user->id,'call_id'=>$this->id);
546 $data_values = array('accept_status'=>$status);
547 $this->set_relationship($this->rel_users_table, $relate_values, true, true,$data_values);
548 global $current_user;
550 if ( $this->update_vcal )
552 vCal::cache_sugar_vcal($user);
555 else if ( $user->object_name == 'Contact')
557 $relate_values = array('contact_id'=>$user->id,'call_id'=>$this->id);
558 $data_values = array('accept_status'=>$status);
559 $this->set_relationship($this->rel_contacts_table, $relate_values, true, true,$data_values);
561 else if ( $user->object_name == 'Lead')
563 $relate_values = array('lead_id'=>$user->id,'call_id'=>$this->id);
564 $data_values = array('accept_status'=>$status);
565 $this->set_relationship($this->rel_leads_table, $relate_values, true, true,$data_values);
571 function get_notification_recipients() {
572 if($this->special_notification) {
573 return parent::get_notification_recipients();
576 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($this,true));
578 if(!is_array($this->contacts_arr)) {
579 $this->contacts_arr = array();
582 if(!is_array($this->users_arr)) {
583 $this->users_arr = array();
586 if(!is_array($this->leads_arr)) {
587 $this->leads_arr = array();
590 foreach($this->users_arr as $user_id) {
591 $notify_user = new User();
592 $notify_user->retrieve($user_id);
593 $notify_user->new_assigned_user_name = $notify_user->full_name;
594 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
595 $list[$notify_user->id] = $notify_user;
598 foreach($this->contacts_arr as $contact_id) {
599 $notify_user = new Contact();
600 $notify_user->retrieve($contact_id);
601 $notify_user->new_assigned_user_name = $notify_user->full_name;
602 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
603 $list[$notify_user->id] = $notify_user;
606 foreach($this->leads_arr as $lead_id) {
607 $notify_user = new Lead();
608 $notify_user->retrieve($lead_id);
609 $notify_user->new_assigned_user_name = $notify_user->full_name;
610 $GLOBALS['log']->info("Notifications: recipient is $notify_user->new_assigned_user_name");
611 $list[$notify_user->id] = $notify_user;
613 // $GLOBALS['log']->debug('Call.php->get_notification_recipients():'.print_r($list,true));
617 function bean_implements($interface){
619 case 'ACL':return true;
624 function listviewACLHelper(){
625 $array_assign = parent::listviewACLHelper();
627 if(!empty($this->parent_name)){
629 if(!empty($this->parent_name_owner)){
630 global $current_user;
631 $is_owner = $current_user->id == $this->parent_name_owner;
634 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)){
635 $array_assign['PARENT'] = 'a';
637 $array_assign['PARENT'] = 'span';
640 if(!empty($this->contact_name)){
642 if(!empty($this->contact_name_owner)){
643 global $current_user;
644 $is_owner = $current_user->id == $this->contact_name_owner;
647 if( ACLController::checkAccess('Contacts', 'view', $is_owner)){
648 $array_assign['CONTACT'] = 'a';
650 $array_assign['CONTACT'] = 'span';
653 return $array_assign;
656 function save_relationship_changes($is_update) {
658 if(empty($this->in_workflow))
660 if(empty($this->in_import))
662 //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
663 //add assigned_user_id to exclude list and let the logic from MeetingFormBase determine whether assigned user id gets added to the relationship
664 if(!empty($GLOBALS['soap_server_object']))
666 $exclude = array('lead_id', 'contact_id', 'user_id');
670 $exclude = array('lead_id', 'contact_id', 'user_id', 'assigned_user_id');
675 $exclude = array('user_id');
680 parent::save_relationship_changes($is_update, $exclude);
683 public function getDefaultStatus()
685 $def = $this->field_defs['status'];
686 if (isset($def['default'])) {
687 return $def['default'];
689 $app = return_app_list_strings_language($GLOBALS['current_language']);
690 if (isset($def['options']) && isset($app[$def['options']])) {
691 $keys = array_keys($app[$def['options']]);