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') ||
271 (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id))
274 $return_id = $focus->id;
277 if(empty($_REQUEST['return_module']) && empty($_REQUEST['return_action']) && $focus->status == 'Held'){
278 //if we are closing the call, and the request does not have a return module AND return action set, then
279 //the request is coming from a dashlet or subpanel close icon and there is no need to process user invitees,
280 //just save the current values.
283 ///////////////////////////////////////////////////////////////////////////
284 //// REMOVE INVITEE RELATIONSHIPS
285 if(!empty($_POST['user_invitees'])) {
286 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
288 $userInvitees = array();
291 // Calculate which users to flag as deleted and which to add
292 $deleteUsers = array();
293 $focus->load_relationship('users');
294 // Get all users for the call
295 $q = 'SELECT mu.user_id, mu.accept_status FROM calls_users mu WHERE mu.call_id = \''.$focus->id.'\'';
296 $r = $focus->db->query($q);
297 $acceptStatusUsers = array();
298 while($a = $focus->db->fetchByAssoc($r)) {
299 if(!in_array($a['user_id'], $userInvitees)) {
300 $deleteUsers[$a['user_id']] = $a['user_id'];
302 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
306 if(count($deleteUsers) > 0) {
308 foreach($deleteUsers as $u) {
309 $sql .= ",'" . $u . "'";
312 $sql = substr($sql, 1);
313 // We could run a delete SQL statement here, but will just mark as deleted instead
314 $sql = "UPDATE calls_users set deleted = 1 where user_id in ($sql) AND call_id = '". $focus->id . "'";
315 $focus->db->query($sql);
318 // Get all contacts for the call
319 if(!empty($_POST['contact_invitees'])) {
320 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
322 $contactInvitees = array();
325 $deleteContacts = array();
326 $focus->load_relationship('contacts');
327 $q = 'SELECT mu.contact_id, mu.accept_status FROM calls_contacts mu WHERE mu.call_id = \''.$focus->id.'\'';
328 $r = $focus->db->query($q);
329 $acceptStatusContacts = array();
330 while($a = $focus->db->fetchByAssoc($r)) {
331 if(!in_array($a['contact_id'], $contactInvitees)) {
332 $deleteContacts[$a['contact_id']] = $a['contact_id'];
334 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
338 if(count($deleteContacts) > 0) {
340 foreach($deleteContacts as $u) {
341 $sql .= ",'" . $u . "'";
343 $sql = substr($sql, 1);
344 // We could run a delete SQL statement here, but will just mark as deleted instead
345 $sql = "UPDATE calls_contacts set deleted = 1 where contact_id in ($sql) AND call_id = '". $focus->id . "'";
346 $focus->db->query($sql);
348 if(!empty($_POST['lead_invitees'])) {
349 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
351 $leadInvitees = array();
354 // Calculate which leads to flag as deleted and which to add
355 $deleteLeads = array();
356 $focus->load_relationship('leads');
357 // Get all leads for the call
358 $q = 'SELECT mu.lead_id, mu.accept_status FROM calls_leads mu WHERE mu.call_id = \''.$focus->id.'\'';
359 $r = $focus->db->query($q);
360 $acceptStatusLeads = array();
361 while($a = $focus->db->fetchByAssoc($r)) {
362 if(!in_array($a['lead_id'], $leadInvitees)) {
363 $deleteLeads[$a['lead_id']] = $a['lead_id'];
365 $acceptStatusLeads[$a['user_id']] = $a['accept_status'];
369 if(count($deleteLeads) > 0) {
371 foreach($deleteLeads as $u) {
372 // make sure we don't delete the assigned user
373 if ( $u != $focus->assigned_user_id )
374 $sql .= ",'" . $u . "'";
376 $sql = substr($sql, 1);
377 // We could run a delete SQL statement here, but will just mark as deleted instead
378 $sql = "UPDATE calls_leads set deleted = 1 where lead_id in ($sql) AND call_id = '". $focus->id . "'";
379 $focus->db->query($sql);
382 ///////////////////////////////////////////////////////////////////////////
385 ///////////////////////////////////////////////////////////////////////////
386 //// REBUILD INVITEE RELATIONSHIPS
387 $focus->users_arr = array();
388 $focus->users_arr = $userInvitees;
389 $focus->contacts_arr = array();
390 $focus->contacts_arr = $contactInvitees;
391 $focus->leads_arr = array();
392 $focus->leads_arr = $leadInvitees;
393 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
394 $focus->contacts_arr[] = $_POST['parent_id'];
396 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
397 $focus->leads_arr[] = $_POST['parent_id'];
399 // Call the Call module's save function to handle saving other fields besides
400 // the users and contacts relationships
402 $return_id = $focus->id;
405 $existing_users = array();
406 if(!empty($_POST['existing_invitees'])) {
407 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
410 foreach($focus->users_arr as $user_id) {
411 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
415 if(!isset($acceptStatusUsers[$user_id])) {
416 $focus->load_relationship('users');
417 $focus->users->add($user_id);
419 // update query to preserve accept_status
420 $qU = 'UPDATE calls_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
421 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
422 $qU .= 'AND user_id = \''.$user_id.'\'';
423 $focus->db->query($qU);
428 $existing_contacts = array();
429 if(!empty($_POST['existing_contact_invitees'])) {
430 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
433 foreach($focus->contacts_arr as $contact_id) {
434 if(empty($contact_id) || isset($existing_contacts[$contact_id]) || (isset($deleteContacts[$contact_id]) && $contact_id != $_POST['parent_id'])) {
438 if(!isset($acceptStatusContacts[$contact_id])) {
439 $focus->load_relationship('contacts');
440 $focus->contacts->add($contact_id);
442 // update query to preserve accept_status
443 $qU = 'UPDATE calls_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
444 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
445 $qU .= 'AND contact_id = \''.$contact_id.'\'';
446 $focus->db->query($qU);
450 $existing_leads = array();
451 if(!empty($_POST['existing_lead_invitees'])) {
452 $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ','));
455 foreach($focus->leads_arr as $lead_id) {
456 if(empty($lead_id) || isset($existing_leads[$lead_id]) || (isset($deleteLeads[$lead_id]) && $lead_id != $_POST['parent_id'])) {
460 if(!isset($acceptStatusLeads[$lead_id])) {
461 $focus->load_relationship('leads');
462 $focus->leads->add($lead_id);
464 // update query to preserve accept_status
465 $qU = 'UPDATE calls_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
466 $qU .= 'WHERE call_id = \''.$focus->id.'\' ';
467 $qU .= 'AND lead_id = \''.$lead_id.'\'';
468 $focus->db->query($qU);
472 // CCL - Comment out call to set $current_user as invitee
473 //set organizer to auto-accept
474 //$focus->set_accept_status($current_user, 'accept');
476 //// END REBUILD INVITEE RELATIONSHIPS
477 ///////////////////////////////////////////////////////////////////////////
480 if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
481 $_REQUEST['return_action'] = 'index';
482 handleRedirect('', 'Home');
485 handleRedirect($return_id, 'Calls');
490 } // end handleSave();
492 function getWideFormBody ($prefix, $mod='', $formname='', $wide =true){
493 if(!ACLController::checkAccess('Calls', 'edit', true)){
497 $temp_strings = $mod_strings;
499 global $current_language;
500 $mod_strings = return_module_language($current_language, $mod);
503 global $app_list_strings;
504 global $current_user;
507 $lbl_subject = $mod_strings['LBL_SUBJECT'];
508 // Unimplemented until jscalendar language files are fixed
509 // global $current_language;
510 // global $default_language;
511 // global $cal_codes;
512 // Unimplemented until jscalendar language files are fixed
513 // $cal_lang = (empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
517 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
518 $lbl_date = $mod_strings['LBL_DATE'];
519 $lbl_time = $mod_strings['LBL_TIME'];
521 $ntc_date_format = '('.$timedate->get_user_date_format(). ')';
522 $ntc_time_format = '('.$timedate->get_user_time_format(). ')';
523 $cal_dateformat = $timedate->get_cal_date_format();
525 $user_id = $current_user->id;
526 $default_status = $app_list_strings['call_status_default'];
527 $default_parent_type= $app_list_strings['record_type_default_key'];
528 $date = TimeDate::getInstance()->nowDb();
529 $default_date_start = $timedate->to_display_date($date);
530 $default_time_start = $timedate->to_display_time($date,true);
531 $time_ampm = $timedate->AMPMMenu($prefix,$default_time_start);
533 <input type="hidden" name="${prefix}direction" value="Outbound">
534 <input type="hidden" name="${prefix}record" value="">
535 <input type="hidden" name="${prefix}status" value="${default_status}">
536 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
537 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
538 <input type="hidden" name="${prefix}duration_hours" value="1">
539 <input type="hidden" name="${prefix}duration_minutes" value="0">
540 <input type="hidden" name="${prefix}user_id" value="${user_id}">
542 <table cellspacing='0' cellpadding='0' border='0' width="100%">
548 <td scope='row' width="20%"><input type='radio' name='appointment' value='Call' class='radio' checked> ${mod_strings['LNK_NEW_CALL']}</td>
549 <td scope='row' width="80%">${mod_strings['LBL_DESCRIPTION']}</td>
553 <td scope='row'><input type='radio' name='appointment' value='Meeting' class='radio'> ${mod_strings['LNK_NEW_MEETING']}</td>
555 <td rowspan='8' ><textarea name='Appointmentsdescription' cols='50' rows='5'></textarea></td>
560 <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>
564 <td scope='row'><input type='radio' name='appointment' value='Meeting' class='radio' onchange='document.$formname.module.value="Meetings";'> ${mod_strings['LNK_NEW_MEETING']}</td>
568 $jscalenderImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
573 <td scope='row'>$lbl_subject <span class="required">$lbl_required_symbol</span></td>
577 <td ><input name='${prefix}name' maxlength='255' type="text"></td>
581 <td scope='row'>$lbl_date <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_date_format</span></td>
584 <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>
588 <td scope='row'>$lbl_time <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_time_format</span></td>
591 <td ><input name='${prefix}time_start' size="12" type="text" maxlength='5' value="{$default_time_start}">$time_ampm</td>
596 <script type="text/javascript">
598 inputField : "${prefix}jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "${prefix}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
604 $javascript = new javascript();
605 $javascript->setFormName($formname);
606 $javascript->setSugarBean(new Call());
607 $javascript->addRequiredFields($prefix);
608 $form .=$javascript->getScript();
609 $mod_strings = $temp_strings;