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'){
241 $return_id = $focus->id;
243 if(empty($_REQUEST['return_module']) && empty($_REQUEST['return_action']) && $focus->status == 'Held'){
244 //if we are closing the meeting, and the request does not have a return module AND return action set, then
245 //the request is coming from a dashlet or subpanel close icon and there is no need to process user invitees,
246 //just save the current values.
249 ///////////////////////////////////////////////////////////////////////////
250 //// REMOVE INVITEE RELATIONSHIPS
251 if(!empty($_POST['user_invitees'])) {
252 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
254 $userInvitees = array();
257 // Calculate which users to flag as deleted and which to add
258 $deleteUsers = array();
259 $focus->load_relationship('users');
260 // Get all users for the meeting
261 $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$focus->id.'\'';
262 $r = $focus->db->query($q);
263 $acceptStatusUsers = array();
264 while($a = $focus->db->fetchByAssoc($r)) {
265 if(!in_array($a['user_id'], $userInvitees)) {
266 $deleteUsers[$a['user_id']] = $a['user_id'];
268 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
272 if(count($deleteUsers) > 0) {
274 foreach($deleteUsers as $u) {
275 $sql .= ",'" . $u . "'";
277 $sql = substr($sql, 1);
278 // We could run a delete SQL statement here, but will just mark as deleted instead
279 $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $focus->id . "'";
280 $focus->db->query($sql);
283 // Get all contacts for the meeting
284 if(!empty($_POST['contact_invitees'])) {
285 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
287 $contactInvitees = array();
290 $deleteContacts = array();
291 $focus->load_relationship('contacts');
292 $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$focus->id.'\'';
293 $r = $focus->db->query($q);
294 $acceptStatusContacts = array();
295 while($a = $focus->db->fetchByAssoc($r)) {
296 if(!in_array($a['contact_id'], $contactInvitees)) {
297 $deleteContacts[$a['contact_id']] = $a['contact_id'];
299 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
303 if(count($deleteContacts) > 0) {
305 foreach($deleteContacts as $u) {
306 $sql .= ",'" . $u . "'";
308 $sql = substr($sql, 1);
309 // We could run a delete SQL statement here, but will just mark as deleted instead
310 $sql = "UPDATE meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $focus->id . "'";
311 $focus->db->query($sql);
313 if(!empty($_POST['lead_invitees'])) {
314 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
316 $leadInvitees = array();
319 $deleteLeads = array();
320 $focus->load_relationship('leads');
321 $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$focus->id.'\'';
322 $r = $focus->db->query($q);
323 $acceptStatusLeads = array();
324 while($a = $focus->db->fetchByAssoc($r)) {
325 if(!in_array($a['lead_id'], $leadInvitees)) {
326 $deleteLeads[$a['lead_id']] = $a['lead_id'];
328 $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
332 if(count($deleteLeads) > 0) {
334 foreach($deleteLeads as $u) {
335 $sql .= ",'" . $u . "'";
337 $sql = substr($sql, 1);
338 // We could run a delete SQL statement here, but will just mark as deleted instead
339 $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $focus->id . "'";
340 $focus->db->query($sql);
343 ///////////////////////////////////////////////////////////////////////////
346 ///////////////////////////////////////////////////////////////////////////
347 //// REBUILD INVITEE RELATIONSHIPS
348 $focus->users_arr = array();
349 $focus->users_arr = $userInvitees;
350 $focus->contacts_arr = array();
351 $focus->contacts_arr = $contactInvitees;
352 $focus->leads_arr = array();
353 $focus->leads_arr = $leadInvitees;
355 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
356 $focus->contacts_arr[] = $_POST['parent_id'];
358 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
359 $focus->leads_arr[] = $_POST['parent_id'];
361 // Call the Meeting module's save function to handle saving other fields besides
362 // the users and contacts relationships
364 $return_id = $focus->id;
365 if(empty($return_id)){
366 //this is to handle the situation where the save fails, most likely because of a failure
367 //in the external api. bug: 42200
368 $_REQUEST['action'] = 'EditView';
369 $_REQUEST['return_action'] = 'EditView';
370 handleRedirect('', 'Meetings');
373 $existing_users = array();
374 if(!empty($_POST['existing_invitees'])) {
375 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
378 foreach($focus->users_arr as $user_id) {
379 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
383 if(!isset($acceptStatusUsers[$user_id])) {
384 $focus->users->add($user_id);
386 // update query to preserve accept_status
387 $qU = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
388 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
389 $qU .= 'AND user_id = \''.$user_id.'\'';
390 $focus->db->query($qU);
395 $existing_contacts = array();
396 if(!empty($_POST['existing_contact_invitees'])) {
397 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
400 foreach($focus->contacts_arr as $contact_id) {
401 if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
405 if(!isset($acceptStatusContacts[$contact_id])) {
406 $focus->contacts->add($contact_id);
408 // update query to preserve accept_status
409 $qU = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
410 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
411 $qU .= 'AND contact_id = \''.$contact_id.'\'';
412 $focus->db->query($qU);
416 $existing_leads = array();
417 if(!empty($_POST['existing_lead_invitees'])) {
418 $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ','));
421 foreach($focus->leads_arr as $lead_id) {
422 if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
426 if(!isset($acceptStatusLeads[$lead_id])) {
427 $focus->leads->add($lead_id);
429 // update query to preserve accept_status
430 $qU = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
431 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
432 $qU .= 'AND lead_id = \''.$lead_id.'\'';
433 $focus->db->query($qU);
437 // CCL - Comment out call to set $current_user as invitee
438 // set organizer to auto-accept
439 //$focus->set_accept_status($current_user, 'accept');
441 //// END REBUILD INVITEE RELATIONSHIPS
442 ///////////////////////////////////////////////////////////////////////////
445 if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
446 header("Location: index.php?module=Home&action=index");
449 handleRedirect($return_id, 'Meetings');
454 } // end handleSave();