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: Call Form Base
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
48 function getFormBody($prefix, $mod='', $formname='',$cal_date='',$cal_time=''){
49 if(!ACLController::checkAccess('Calls', 'edit', true)){
53 $temp_strings = $mod_strings;
55 global $current_language;
56 $mod_strings = return_module_language($current_language, $mod);
59 global $app_list_strings;
64 $lbl_subject = $mod_strings['LBL_SUBJECT'];
65 // Unimplemented until jscalendar language files are fixed
66 // global $current_language;
67 // global $default_language;
69 // Unimplemented until jscalendar language files are fixed
70 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
74 $cal_dateformat = $timedate->get_cal_date_format();
76 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
77 $lbl_date = $mod_strings['LBL_DATE'];
78 $lbl_time = $mod_strings['LBL_TIME'];
79 $ntc_date_format = $timedate->get_user_date_format();
80 $ntc_time_format = '('.$timedate->get_user_time_format().')';
82 $user_id = $current_user->id;
83 $default_status = $app_list_strings['call_status_default'];
84 $default_parent_type= $app_list_strings['record_type_default_key'];
85 $date = TimeDate::getInstance()->nowDb();
86 $default_date_start = $timedate->to_display_date($date,false);
87 $default_time_start = $timedate->to_display_time($date);
88 $time_ampm = $timedate->AMPMMenu($prefix,$default_time_start);
89 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
90 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
91 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
93 <form name="${formname}" onSubmit="return check_form('${formname}') "method="POST" action="index.php">
94 <input type="hidden" name="${prefix}module" value="Calls">
95 <input type="hidden" name="${prefix}action" value="Save">
96 <input type="hidden" name="${prefix}record" value="">
97 <input type="hidden" name="${prefix}direction" value="Outbound">
98 <input type="hidden" name="${prefix}status" value="${default_status}">
99 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
100 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
101 <input type="hidden" name="${prefix}duration_hours" value="1">
102 <input type="hidden" name="${prefix}duration_minutes" value="0">
103 <input type="hidden" name="${prefix}user_id" value="${user_id}">
105 <table cellspacing="1" cellpadding="0" border="0">
107 <td colspan="2"><input type='radio' name='appointment' value='Call' class='radio' onchange='document.${formname}.module.value="Calls";' style='vertical-align: middle;' checked> <span scope="row">${mod_strings['LNK_NEW_CALL']}</span>
110 <input type='radio' name='appointment' value='Meeting' class='radio' onchange='document.${formname}.module.value="Meetings";'><span scope="row">${mod_strings['LNK_NEW_MEETING']}</span></td>
113 <td colspan="2"><span scope="row">$lbl_subject</span> <span class="required">$lbl_required_symbol</span></td>
115 <tr><td valign=top><input name='${prefix}name' size='30' maxlength='255' type="text"></td>
116 <td><input name='${prefix}date_start' id='${formname}jscal_field' maxlength='10' type="hidden" value="${cal_date}"></td>
117 <td><input name='${prefix}time_start' type="hidden" maxlength='10' value="{$cal_time}"></td>
119 <script type="text/javascript">
121 // inputField : "${formname}jscal_field", daFormat : "$cal_dateformat" ifFormat : "$cal_dateformat", showsTime : false, button : "${formname}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
131 $javascript = new javascript();
132 $javascript->setFormName($formname);
133 $javascript->setSugarBean(new Call());
134 $javascript->addRequiredFields($prefix);
135 $form .=$javascript->getScript();
136 $form .= "<td align=\"left\" valign=top><input title='$lbl_save_button_title' accessKey='$lbl_save_button_key' class='button' type='submit' name='button' value=' $lbl_save_button_label ' ></td></tr></table></form>";
137 $mod_strings = $temp_strings;
141 function getFormHeader($prefix, $mod='', $title=''){
142 if(!ACLController::checkAccess('Calls', 'edit', true)){
146 global $current_language;
147 $mod_strings = return_module_language($current_language, $mod);
148 }else global $mod_strings;
156 $the_form = get_left_form_header($title);
158 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
161 <form name="${prefix}CallSave" onSubmit="return check_form('${prefix}CallSave') "method="POST" action="index.php">
162 <input type="hidden" name="${prefix}module" value="Calls">
163 <input type="hidden" name="${prefix}action" value="Save">
168 function getFormFooter($prefic, $mod=''){
169 if(!ACLController::checkAccess('Calls', 'edit', true)){
173 global $app_list_strings;
174 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
175 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
176 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
177 $the_form = " <p><input title='$lbl_save_button_title' accessKey='$lbl_save_button_key' class='button' type='submit' name='button' value=' $lbl_save_button_label ' ></p></form>";
178 $the_form .= get_left_form_footer();
179 $the_form .= get_validate_record_js();
183 function getForm($prefix, $mod=''){
184 if(!ACLController::checkAccess('Calls', 'edit', true)){
187 $the_form = $this->getFormHeader($prefix, $mod);
188 $the_form .= $this->getFormBody($prefix, $mod, "${prefix}CallSave");
189 $the_form .= $this->getFormFooter($prefix, $mod);
195 function handleSave($prefix,$redirect=true,$useRequired=false) {
198 require_once('include/formbase.php');
200 global $current_user;
204 if (isset($_POST[$prefix.'duration_hours'])){
205 $_POST[$prefix.'duration_hours'] = trim($_POST[$prefix.'duration_hours']);
210 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
213 if ( !isset($_POST[$prefix.'reminder_checked']) or ($_POST[$prefix.'reminder_checked'] == 0)) {
214 $GLOBALS['log']->debug(__FILE__.'('.__LINE__.'): No reminder checked, resetting the reminder_time');
215 $_POST[$prefix.'reminder_time'] = -1;
218 if(!isset($_POST[$prefix.'reminder_time'])) {
219 $GLOBALS['log']->debug(__FILE__.'('.__LINE__.'): Getting the users default reminder time');
220 $_POST[$prefix.'reminder_time'] = $current_user->getPreference('reminder_time');
223 $time_format = $timedate->get_user_time_format();
224 $time_separator = ":";
225 if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
226 $time_separator = $match[1];
229 if(!empty($_POST[$prefix.'time_hour_start']) && empty($_POST[$prefix.'time_start'])) {
230 $_POST[$prefix.'time_start'] = $_POST[$prefix.'time_hour_start']. $time_separator .$_POST[$prefix.'time_minute_start'];
233 if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) {
234 $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'],$timedate->get_time_format(), $_POST[$prefix.'meridiem']);
237 if(isset($_POST[$prefix.'time_start']) && strlen($_POST[$prefix.'date_start']) == 10) {
238 $_POST[$prefix.'date_start'] = $_POST[$prefix.'date_start'] . ' ' . $_POST[$prefix.'time_start'];
241 // retrieve happens here
242 $focus = populateFromPost($prefix, $focus);
243 if(!$focus->ACLAccess('Save')) {
244 ACLController::displayNoAccess(true);
248 //add assigned user and current user if this is the first time bean is saved
249 if(empty($focus->id) && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] =='Calls' && !empty($_REQUEST['return_action']) && $_REQUEST['return_action'] =='DetailView'){
250 //if return action is set to detail view and return module to call, then this is from the long form, do not add the assigned user (only the current user)
251 //The current user is already added to UI and we want to give the current user the option of opting out of meeting.
252 if($current_user->id != $_POST['assigned_user_id']){
253 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
254 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
256 }elseif (empty($focus->id) ){
257 //this is not from long form so add assigned and current user automatically as there is no invitee list UI.
258 //This call could be through an ajax call from subpanels or shortcut bar
259 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
261 //add current user if the assigned to user is different than current user.
262 if($current_user->id != $_POST['assigned_user_id']){
263 $_POST['user_invitees'] .= ','.$current_user->id.', ';
266 //remove any double comma's introduced during appending
267 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
270 if(isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true'){
272 $return_id = $focus->id;
275 if(empty($_REQUEST['return_module']) && empty($_REQUEST['return_action']) && $focus->status == 'Held'){
276 //if we are closing the call, and the request does not have a return module AND return action set, then
277 //the request is coming from a dashlet or subpanel close icon and there is no need to process user invitees,
278 //just save the current values.
281 ///////////////////////////////////////////////////////////////////////////
282 //// REMOVE INVITEE RELATIONSHIPS
283 if(!empty($_POST['user_invitees'])) {
284 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
286 $userInvitees = array();
289 // Calculate which users to flag as deleted and which to add
290 $deleteUsers = array();
291 $focus->load_relationship('users');
292 // Get all users for the call
293 $q = 'SELECT mu.user_id, mu.accept_status FROM calls_users mu WHERE mu.call_id = \''.$focus->id.'\'';
294 $r = $focus->db->query($q);
295 $acceptStatusUsers = array();
296 while($a = $focus->db->fetchByAssoc($r)) {
297 if(!in_array($a['user_id'], $userInvitees)) {
298 $deleteUsers[$a['user_id']] = $a['user_id'];
300 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
304 if(count($deleteUsers) > 0) {
306 foreach($deleteUsers as $u) {
307 $sql .= ",'" . $u . "'";
310 $sql = substr($sql, 1);
311 // We could run a delete SQL statement here, but will just mark as deleted instead
312 $sql = "UPDATE calls_users set deleted = 1 where user_id in ($sql) AND call_id = '". $focus->id . "'";
313 $focus->db->query($sql);
316 // Get all contacts for the call
317 if(!empty($_POST['contact_invitees'])) {
318 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
320 $contactInvitees = array();
323 $deleteContacts = array();
324 $focus->load_relationship('contacts');
325 $q = 'SELECT mu.contact_id, mu.accept_status FROM calls_contacts mu WHERE mu.call_id = \''.$focus->id.'\'';
326 $r = $focus->db->query($q);
327 $acceptStatusContacts = array();
328 while($a = $focus->db->fetchByAssoc($r)) {
329 if(!in_array($a['contact_id'], $contactInvitees)) {
330 $deleteContacts[$a['contact_id']] = $a['contact_id'];
332 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
336 if(count($deleteContacts) > 0) {
338 foreach($deleteContacts as $u) {
339 $sql .= ",'" . $u . "'";
341 $sql = substr($sql, 1);
342 // We could run a delete SQL statement here, but will just mark as deleted instead
343 $sql = "UPDATE calls_contacts set deleted = 1 where contact_id in ($sql) AND call_id = '". $focus->id . "'";
344 $focus->db->query($sql);
346 if(!empty($_POST['lead_invitees'])) {
347 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
349 $leadInvitees = array();
352 // Calculate which leads to flag as deleted and which to add
353 $deleteLeads = array();
354 $focus->load_relationship('leads');
355 // Get all leads for the call
356 $q = 'SELECT mu.lead_id, mu.accept_status FROM calls_leads mu WHERE mu.call_id = \''.$focus->id.'\'';
357 $r = $focus->db->query($q);
358 $acceptStatusLeads = array();
359 while($a = $focus->db->fetchByAssoc($r)) {
360 if(!in_array($a['lead_id'], $leadInvitees)) {
361 $deleteLeads[$a['lead_id']] = $a['lead_id'];
363 $acceptStatusLeads[$a['user_id']] = $a['accept_status'];
367 if(count($deleteLeads) > 0) {
369 foreach($deleteLeads as $u) {
370 // make sure we don't delete the assigned user
371 if ( $u != $focus->assigned_user_id )
372 $sql .= ",'" . $u . "'";
374 $sql = substr($sql, 1);
375 // We could run a delete SQL statement here, but will just mark as deleted instead
376 $sql = "UPDATE calls_leads set deleted = 1 where lead_id in ($sql) AND call_id = '". $focus->id . "'";
377 $focus->db->query($sql);
380 ///////////////////////////////////////////////////////////////////////////
383 ///////////////////////////////////////////////////////////////////////////
384 //// REBUILD INVITEE RELATIONSHIPS
385 $focus->users_arr = array();
386 $focus->users_arr = $userInvitees;
387 $focus->contacts_arr = array();
388 $focus->contacts_arr = $contactInvitees;
389 $focus->leads_arr = array();
390 $focus->leads_arr = $leadInvitees;
391 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
392 $focus->contacts_arr[] = $_POST['parent_id'];
394 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
395 $focus->leads_arr[] = $_POST['parent_id'];
397 // Call the Call module's save function to handle saving other fields besides
398 // the users and contacts relationships
400 $return_id = $focus->id;
403 $existing_users = array();
404 if(!empty($_POST['existing_invitees'])) {
405 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
408 foreach($focus->users_arr as $user_id) {
409 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
413 if(!isset($acceptStatusUsers[$user_id])) {
414 $focus->load_relationship('users');
415 $focus->users->add($user_id);
417 // update query to preserve accept_status
418 $qU = 'UPDATE calls_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
419 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
420 $qU .= 'AND user_id = \''.$user_id.'\'';
421 $focus->db->query($qU);
426 $existing_contacts = array();
427 if(!empty($_POST['existing_contact_invitees'])) {
428 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
431 foreach($focus->contacts_arr as $contact_id) {
432 if(empty($contact_id) || isset($existing_contacts[$contact_id]) || (isset($deleteContacts[$contact_id]) && $contact_id != $_POST['parent_id'])) {
436 if(!isset($acceptStatusContacts[$contact_id])) {
437 $focus->load_relationship('contacts');
438 $focus->contacts->add($contact_id);
440 // update query to preserve accept_status
441 $qU = 'UPDATE calls_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
442 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
443 $qU .= 'AND contact_id = \''.$contact_id.'\'';
444 $focus->db->query($qU);
448 $existing_leads = array();
449 if(!empty($_POST['existing_lead_invitees'])) {
450 $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ','));
453 foreach($focus->leads_arr as $lead_id) {
454 if(empty($lead_id) || isset($existing_leads[$lead_id]) || (isset($deleteLeads[$lead_id]) && $lead_id != $_POST['parent_id'])) {
458 if(!isset($acceptStatusLeads[$lead_id])) {
459 $focus->load_relationship('leads');
460 $focus->leads->add($lead_id);
462 // update query to preserve accept_status
463 $qU = 'UPDATE calls_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
464 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
465 $qU .= 'AND lead_id = \''.$lead_id.'\'';
466 $focus->db->query($qU);
470 // CCL - Comment out call to set $current_user as invitee
471 //set organizer to auto-accept
472 //$focus->set_accept_status($current_user, 'accept');
474 //// END REBUILD INVITEE RELATIONSHIPS
475 ///////////////////////////////////////////////////////////////////////////
478 if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
479 header("Location: index.php?module=Home&action=index");
482 handleRedirect($return_id, 'Calls');
487 } // end handleSave();
489 function getWideFormBody ($prefix, $mod='', $formname='', $wide =true){
490 if(!ACLController::checkAccess('Calls', 'edit', true)){
494 $temp_strings = $mod_strings;
496 global $current_language;
497 $mod_strings = return_module_language($current_language, $mod);
500 global $app_list_strings;
501 global $current_user;
504 $lbl_subject = $mod_strings['LBL_SUBJECT'];
505 // Unimplemented until jscalendar language files are fixed
506 // global $current_language;
507 // global $default_language;
508 // global $cal_codes;
509 // Unimplemented until jscalendar language files are fixed
510 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
514 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
515 $lbl_date = $mod_strings['LBL_DATE'];
516 $lbl_time = $mod_strings['LBL_TIME'];
518 $ntc_date_format = '('.$timedate->get_user_date_format(). ')';
519 $ntc_time_format = '('.$timedate->get_user_time_format(). ')';
520 $cal_dateformat = $timedate->get_cal_date_format();
522 $user_id = $current_user->id;
523 $default_status = $app_list_strings['call_status_default'];
524 $default_parent_type= $app_list_strings['record_type_default_key'];
525 $date = TimeDate::getInstance()->nowDb();
526 $default_date_start = $timedate->to_display_date($date);
527 $default_time_start = $timedate->to_display_time($date,true);
528 $time_ampm = $timedate->AMPMMenu($prefix,$default_time_start);
530 <input type="hidden" name="${prefix}direction" value="Outbound">
531 <input type="hidden" name="${prefix}record" value="">
532 <input type="hidden" name="${prefix}status" value="${default_status}">
533 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
534 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
535 <input type="hidden" name="${prefix}duration_hours" value="1">
536 <input type="hidden" name="${prefix}duration_minutes" value="0">
537 <input type="hidden" name="${prefix}user_id" value="${user_id}">
539 <table cellspacing='0' cellpadding='0' border='0' width="100%">
545 <td scope='row' width="20%"><input type='radio' name='appointment' value='Call' class='radio' checked> ${mod_strings['LNK_NEW_CALL']}</td>
546 <td scope='row' width="80%">${mod_strings['LBL_DESCRIPTION']}</td>
550 <td scope='row'><input type='radio' name='appointment' value='Meeting' class='radio'> ${mod_strings['LNK_NEW_MEETING']}</td>
552 <td rowspan='8' ><textarea name='Appointmentsdescription' cols='50' rows='5'></textarea></td>
557 <td scope='row' width="20%"><input type='radio' name='appointment' value='Call' class='radio' onchange='document.$formname.module.value="Calls";' checked> ${mod_strings['LNK_NEW_CALL']}</td>
561 <td scope='row'><input type='radio' name='appointment' value='Meeting' class='radio' onchange='document.$formname.module.value="Meetings";'> ${mod_strings['LNK_NEW_MEETING']}</td>
565 $jscalenderImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
570 <td scope='row'>$lbl_subject <span class="required">$lbl_required_symbol</span></td>
574 <td ><input name='${prefix}name' maxlength='255' type="text"></td>
578 <td scope='row'>$lbl_date <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_date_format</span></td>
581 <td ><input onblur="parseDate(this, '$cal_dateformat');" name='${prefix}date_start' size="12" id='${prefix}jscal_field' maxlength='10' type="text" value="${default_date_start}"> <img src="{$jscalenderImage}" alt="{$app_strings['LBL_ENTER_DATE']}" id="${prefix}jscal_trigger" align="absmiddle"></td>
585 <td scope='row'>$lbl_time <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_time_format</span></td>
588 <td ><input name='${prefix}time_start' size="12" type="text" maxlength='5' value="{$default_time_start}">$time_ampm</td>
593 <script type="text/javascript">
595 inputField : "${prefix}jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "${prefix}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
601 $javascript = new javascript();
602 $javascript->setFormName($formname);
603 $javascript->setSugarBean(new Call());
604 $javascript->addRequiredFields($prefix);
605 $form .=$javascript->getScript();
606 $mod_strings = $temp_strings;