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: Base Form For Meetings
41 * Portions created by SugarCRM are Copyright(C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
47 class MeetingFormBase{
49 function getFormBody($prefix, $mod='', $formname=''){
50 if(!ACLController::checkAccess('Meetings', 'edit', true)){
55 global $app_list_strings;
60 $temp_strings = $mod_strings;
62 global $current_language;
63 $mod_strings = return_module_language($current_language, $mod);
65 // Unimplemented until jscalendar language files are fixed
66 // global $current_language;
67 // global $default_language;
71 $cal_dateformat = $timedate->get_cal_date_format();
73 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
74 $lbl_date = $mod_strings['LBL_DATE'];
75 $lbl_time = $mod_strings['LBL_TIME'];
76 $ntc_date_format = $timedate->get_user_date_format();
77 $ntc_time_format = '('.$timedate->get_user_time_format().')';
79 $user_id = $current_user->id;
80 $default_status = $app_list_strings['meeting_status_default'];
81 $default_parent_type= $app_list_strings['record_type_default_key'];
82 $default_date_start = $timedate->nowDbDate();
83 $default_time_start = $timedate->nowDbTime();
84 $time_ampm = $timedate->AMPMMenu($prefix, $timedate->nowDbTime());
85 // Unimplemented until jscalendar language files are fixed
86 // $cal_lang =(empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
87 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
89 <input type="hidden" name="${prefix}record" value="">
90 <input type="hidden" name="${prefix}status" value="${default_status}">
91 <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
92 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
93 <input type="hidden" name="${prefix}duration_hours" value="1">
94 <input type="hidden" name="${prefix}duration_minutes" value="00">
95 <p>$lbl_subject<span class="required">$lbl_required_symbol</span><br>
96 <input name='${prefix}name' size='25' maxlength='255' type="text"><br>
97 $lbl_date <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_date_format</span><br>
98 <input name='${prefix}date_start' id='jscal_field' onblur="parseDate(this, '$cal_dateformat');" type="text" maxlength="10" value="${default_date_start}"> <img src="{$jscalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}" id="jscal_trigger" align="absmiddle"><br>
99 $lbl_time <span class="required">$lbl_required_symbol</span> <span class="dateFormat">$ntc_time_format</span><br>
100 <input name='${prefix}time_start' type="text" maxlength='5' value="${default_time_start}">{$time_ampm}</p>
101 <script type="text/javascript">
103 inputField : "jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "jscal_trigger", singleClick : true, step : 1, weekNumbers:false
109 $javascript = new javascript();
110 $javascript->setFormName($formname);
111 $javascript->setSugarBean(new Meeting());
112 $javascript->addRequiredFields($prefix);
113 $form .=$javascript->getScript();
114 $mod_strings = $temp_strings;
120 function getForm($prefix, $mod='Meetings'){
121 if(!ACLController::checkAccess('Meetings', 'edit', true)){
126 global $app_list_strings;
129 global $current_language;
130 $mod_strings = return_module_language($current_language, $mod);
135 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
136 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
137 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
140 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
144 <form name="${prefix}MeetingSave" onSubmit="return check_form('${prefix}MeetingSave')" method="POST" action="index.php">
145 <input type="hidden" name="${prefix}module" value="Meetings">
147 <input type="hidden" name="${prefix}action" value="Save">
150 $the_form .= $this->getFormBody($prefix, 'Meetings',"{$prefix}MeetingSave" );
152 <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value=" $lbl_save_button_label " ></p>
156 $the_form .= get_left_form_footer();
157 $the_form .= get_validate_record_js();
164 * handles save functionality for meetings
165 * @param string prefix
166 * @param bool redirect default True
167 * @param bool useRequired default True
169 function handleSave($prefix,$redirect=true, $useRequired=false) {
172 require_once('include/formbase.php');
174 global $current_user;
177 $focus = new Meeting();
179 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
183 if( !isset($_POST['reminder_checked']) or ( isset($_POST['reminder_checked']) && $_POST['reminder_checked'] == '0')) {
184 $_POST['reminder_time'] = -1;
186 if(!isset($_POST['reminder_time'])) {
187 $_POST['reminder_time'] = $current_user->getPreference('reminder_time');
188 $_POST['reminder_checked']=1;
190 $time_format = $timedate->get_user_time_format();
191 $time_separator = ":";
192 if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
193 $time_separator = $match[1];
196 if(!empty($_POST[$prefix.'time_hour_start']) && empty($_POST['time_start'])) {
197 $_POST[$prefix.'time_start'] = $_POST[$prefix.'time_hour_start']. $time_separator .$_POST[$prefix.'time_minute_start'];
200 if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) {
201 $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'],$timedate->get_time_format(), $_POST[$prefix.'meridiem']);
204 if(isset($_POST[$prefix.'time_start']) && strlen($_POST[$prefix.'date_start']) == 10) {
205 $_POST[$prefix.'date_start'] = $_POST[$prefix.'date_start'] . ' ' . $_POST[$prefix.'time_start'];
208 // retrieve happens here
209 $focus = populateFromPost($prefix, $focus);
210 if(!$focus->ACLAccess('Save')) {
211 ACLController::displayNoAccess(true);
215 //add assigned user and current user if this is the first time bean is saved
216 if(empty($focus->id) && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] =='Meetings' && !empty($_REQUEST['return_action']) && $_REQUEST['return_action'] =='DetailView'){
217 //if return action is set to detail view and return module to meeting, then this is from the long form, do not add the assigned user (only the current user)
218 //The current user is already added to UI and we want to give the current user the option of opting out of meeting.
219 //add current user if the assigned to user is different than current user.
220 if($current_user->id != $_POST['assigned_user_id']){
221 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
222 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
224 }elseif (empty($focus->id) ){
225 //this is not from long form so add assigned and current user automatically as there is no invitee list UI.
226 //This call could be through an ajax call from subpanels or shortcut bar
227 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
229 //add current user if the assigned to user is different than current user.
230 if($current_user->id != $_POST['assigned_user_id']){
231 $_POST['user_invitees'] .= ','.$current_user->id.', ';
234 //remove any double comma's introduced during appending
235 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
239 if( (isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true') ||
240 (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id) )
243 $return_id = $focus->id;
245 if(empty($_REQUEST['return_module']) && empty($_REQUEST['return_action']) && $focus->status == 'Held'){
246 //if we are closing the meeting, and the request does not have a return module AND return action set, then
247 //the request is coming from a dashlet or subpanel close icon and there is no need to process user invitees,
248 //just save the current values.
251 ///////////////////////////////////////////////////////////////////////////
252 //// REMOVE INVITEE RELATIONSHIPS
253 if(!empty($_POST['user_invitees'])) {
254 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
256 $userInvitees = array();
259 // Calculate which users to flag as deleted and which to add
260 $deleteUsers = array();
261 $focus->load_relationship('users');
262 // Get all users for the meeting
263 $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$focus->id.'\'';
264 $r = $focus->db->query($q);
265 $acceptStatusUsers = array();
266 while($a = $focus->db->fetchByAssoc($r)) {
267 if(!in_array($a['user_id'], $userInvitees)) {
268 $deleteUsers[$a['user_id']] = $a['user_id'];
270 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
274 if(count($deleteUsers) > 0) {
276 foreach($deleteUsers as $u) {
277 $sql .= ",'" . $u . "'";
279 $sql = substr($sql, 1);
280 // We could run a delete SQL statement here, but will just mark as deleted instead
281 $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $focus->id . "'";
282 $focus->db->query($sql);
285 // Get all contacts for the meeting
286 if(!empty($_POST['contact_invitees'])) {
287 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
289 $contactInvitees = array();
292 $deleteContacts = array();
293 $focus->load_relationship('contacts');
294 $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$focus->id.'\'';
295 $r = $focus->db->query($q);
296 $acceptStatusContacts = array();
297 while($a = $focus->db->fetchByAssoc($r)) {
298 if(!in_array($a['contact_id'], $contactInvitees)) {
299 $deleteContacts[$a['contact_id']] = $a['contact_id'];
301 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
305 if(count($deleteContacts) > 0) {
307 foreach($deleteContacts as $u) {
308 $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 meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $focus->id . "'";
313 $focus->db->query($sql);
315 if(!empty($_POST['lead_invitees'])) {
316 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
318 $leadInvitees = array();
321 $deleteLeads = array();
322 $focus->load_relationship('leads');
323 $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$focus->id.'\'';
324 $r = $focus->db->query($q);
325 $acceptStatusLeads = array();
326 while($a = $focus->db->fetchByAssoc($r)) {
327 if(!in_array($a['lead_id'], $leadInvitees)) {
328 $deleteLeads[$a['lead_id']] = $a['lead_id'];
330 $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
334 if(count($deleteLeads) > 0) {
336 foreach($deleteLeads as $u) {
337 $sql .= ",'" . $u . "'";
339 $sql = substr($sql, 1);
340 // We could run a delete SQL statement here, but will just mark as deleted instead
341 $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $focus->id . "'";
342 $focus->db->query($sql);
345 ///////////////////////////////////////////////////////////////////////////
348 ///////////////////////////////////////////////////////////////////////////
349 //// REBUILD INVITEE RELATIONSHIPS
350 $focus->users_arr = array();
351 $focus->users_arr = $userInvitees;
352 $focus->contacts_arr = array();
353 $focus->contacts_arr = $contactInvitees;
354 $focus->leads_arr = array();
355 $focus->leads_arr = $leadInvitees;
357 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
358 $focus->contacts_arr[] = $_POST['parent_id'];
360 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
361 $focus->leads_arr[] = $_POST['parent_id'];
363 // Call the Meeting module's save function to handle saving other fields besides
364 // the users and contacts relationships
366 $return_id = $focus->id;
367 if(empty($return_id)){
368 //this is to handle the situation where the save fails, most likely because of a failure
369 //in the external api. bug: 42200
370 $_REQUEST['action'] = 'EditView';
371 $_REQUEST['return_action'] = 'EditView';
372 handleRedirect('', 'Meetings');
375 $existing_users = array();
376 if(!empty($_POST['existing_invitees'])) {
377 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
380 foreach($focus->users_arr as $user_id) {
381 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
385 if(!isset($acceptStatusUsers[$user_id])) {
386 $focus->users->add($user_id);
388 // update query to preserve accept_status
389 $qU = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
390 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
391 $qU .= 'AND user_id = \''.$user_id.'\'';
392 $focus->db->query($qU);
397 $existing_contacts = array();
398 if(!empty($_POST['existing_contact_invitees'])) {
399 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
402 foreach($focus->contacts_arr as $contact_id) {
403 if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
407 if(!isset($acceptStatusContacts[$contact_id])) {
408 $focus->contacts->add($contact_id);
410 // update query to preserve accept_status
411 $qU = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
412 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
413 $qU .= 'AND contact_id = \''.$contact_id.'\'';
414 $focus->db->query($qU);
418 $existing_leads = array();
419 if(!empty($_POST['existing_lead_invitees'])) {
420 $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ','));
423 foreach($focus->leads_arr as $lead_id) {
424 if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
428 if(!isset($acceptStatusLeads[$lead_id])) {
429 $focus->leads->add($lead_id);
431 // update query to preserve accept_status
432 $qU = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
433 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
434 $qU .= 'AND lead_id = \''.$lead_id.'\'';
435 $focus->db->query($qU);
439 // CCL - Comment out call to set $current_user as invitee
440 // set organizer to auto-accept
441 //$focus->set_accept_status($current_user, 'accept');
443 //// END REBUILD INVITEE RELATIONSHIPS
444 ///////////////////////////////////////////////////////////////////////////
447 if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
448 header("Location: index.php?module=Home&action=index");
451 handleRedirect($return_id, 'Meetings');
456 } // end handleSave();