]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/MeetingFormBase.php
Release 6.5.6
[Github/sugarcrm.git] / modules / Meetings / MeetingFormBase.php
1 <?php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  Base Form For Meetings
41  * Portions created by SugarCRM are Copyright(C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 require_once('include/SugarObjects/forms/FormBase.php');
47
48 class MeetingFormBase extends FormBase {
49
50         function getFormBody($prefix, $mod='', $formname=''){
51                 if(!ACLController::checkAccess('Meetings', 'edit', true)){
52                 return '';
53         }
54                 global $mod_strings;
55                 global $app_strings;
56                 global $app_list_strings;
57                 global $current_user;
58                 global $theme;
59                 global $timedate;
60
61                 $temp_strings = $mod_strings;
62                 if(!empty($mod)){
63                         global $current_language;
64                         $mod_strings = return_module_language($current_language, $mod);
65                 }
66                         // Unimplemented until jscalendar language files are fixed
67                         // global $current_language;
68                         // global $default_language;
69                         // global $cal_codes;
70
71         $cal_lang = "en";
72 $cal_dateformat = $timedate->get_cal_date_format();
73
74 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
75 $lbl_date = $mod_strings['LBL_DATE'];
76 $lbl_time = $mod_strings['LBL_TIME'];
77 $ntc_date_format = $timedate->get_user_date_format();
78 $ntc_time_format = '('.$timedate->get_user_time_format().')';
79
80         $user_id = $current_user->id;
81 $default_status = $app_list_strings['meeting_status_default'];
82 $default_parent_type= $app_list_strings['record_type_default_key'];
83 $default_date_start = $timedate->nowDbDate();
84 $default_time_start = $timedate->nowDbTime();
85 $time_ampm = $timedate->AMPMMenu($prefix, $timedate->nowDbTime());
86                         // Unimplemented until jscalendar language files are fixed
87                         // $cal_lang =(empty($cal_codes[$current_language])) ? $cal_codes[$default_language] : $cal_codes[$current_language];
88 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');
89                         $form = <<<EOF
90                                         <input type="hidden" name="${prefix}record" value="">
91                                         <input type="hidden" name="${prefix}status" value="${default_status}">
92                                         <input type="hidden" name="${prefix}parent_type" value="${default_parent_type}">
93                                         <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
94                                         <input type="hidden" name="${prefix}duration_hours" value="1">
95                                         <input type="hidden" name="${prefix}duration_minutes" value="00">
96                                         <p>$lbl_subject<span class="required">$lbl_required_symbol</span><br>
97                                         <input name='${prefix}name' size='25' maxlength='255' type="text"><br>
98                                         $lbl_date&nbsp;<span class="required">$lbl_required_symbol</span>&nbsp;<span class="dateFormat">$ntc_date_format</span><br>
99                                         <input name='${prefix}date_start' id='jscal_field' onblur="parseDate(this, '$cal_dateformat');" type="text" maxlength="10" value="${default_date_start}"> <!--not_in_theme!--><img src="{$jscalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}"  id="jscal_trigger" align="absmiddle"><br>
100                                         $lbl_time&nbsp;<span class="required">$lbl_required_symbol</span>&nbsp;<span class="dateFormat">$ntc_time_format</span><br>
101                                         <input name='${prefix}time_start' type="text" maxlength='5' value="${default_time_start}">{$time_ampm}</p>
102                                         <script type="text/javascript">
103                                         Calendar.setup({
104                                                 inputField : "jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "jscal_trigger", singleClick : true, step : 1, weekNumbers:false
105                                         });
106                                         </script>
107 EOF;
108
109
110 $javascript = new javascript();
111 $javascript->setFormName($formname);
112 $javascript->setSugarBean(new Meeting());
113 $javascript->addRequiredFields($prefix);
114 $form .=$javascript->getScript();
115 $mod_strings = $temp_strings;
116 return $form;
117 }
118
119
120
121 function getForm($prefix, $mod='Meetings'){
122         if(!ACLController::checkAccess('Meetings', 'edit', true)){
123                 return '';
124         }
125
126                 global $app_strings;
127                 global $app_list_strings;
128
129                 if(!empty($mod)){
130         global $current_language;
131         $mod_strings = return_module_language($current_language, $mod);
132                 } else {
133                         global $mod_strings;
134                 }
135
136                 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
137                 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
138                 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
139
140
141 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
142 $the_form .= <<<EOQ
143
144
145                 <form name="${prefix}MeetingSave" onSubmit="return check_form('${prefix}MeetingSave')" method="POST" action="index.php">
146                         <input type="hidden" name="${prefix}module" value="Meetings">
147
148                         <input type="hidden" name="${prefix}action" value="Save">
149
150 EOQ;
151 $the_form       .= $this->getFormBody($prefix, 'Meetings',"{$prefix}MeetingSave" );
152 $the_form .= <<<EOQ
153                 <p><input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="button" value="  $lbl_save_button_label  " ></p>
154                 </form>
155 EOQ;
156
157 $the_form .= get_left_form_footer();
158 $the_form .= get_validate_record_js();
159
160 return $the_form;
161 }
162
163
164 /**
165  * handles save functionality for meetings
166  * @param       string prefix
167  * @param       bool redirect default True
168  * @param       bool useRequired default True
169  */
170 function handleSave($prefix,$redirect=true, $useRequired=false) {
171
172
173         require_once('include/formbase.php');
174
175         global $current_user;
176         global $timedate;
177
178         $focus = new Meeting();
179
180         if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
181                 return null;
182         }
183
184         if( !isset($_POST['reminder_checked']) or ( isset($_POST['reminder_checked']) && $_POST['reminder_checked'] == '0')) {
185                 $_POST['reminder_time'] = -1;
186         }
187         if(!isset($_POST['reminder_time'])) {
188                 $_POST['reminder_time'] = $current_user->getPreference('reminder_time');
189                 $_POST['reminder_checked']=1;
190         }
191         
192         if(!isset($_POST['email_reminder_checked']) || (isset($_POST['email_reminder_checked']) && $_POST['email_reminder_checked'] == '0')) {
193                 $_POST['email_reminder_time'] = -1;
194         }
195         if(!isset($_POST['email_reminder_time'])){
196                 $_POST['email_reminder_time'] = $current_user->getPreference('email_reminder_time');
197                 $_POST['email_reminder_checked'] = 1;
198         }
199         
200         // don't allow to set recurring_source from a form
201         unset($_POST['recurring_source']);
202         
203         $time_format = $timedate->get_user_time_format();
204     $time_separator = ":";
205     if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
206        $time_separator = $match[1];
207     }
208
209         if(!empty($_POST[$prefix.'time_hour_start']) && empty($_POST['time_start'])) {
210                 $_POST[$prefix.'time_start'] = $_POST[$prefix.'time_hour_start']. $time_separator .$_POST[$prefix.'time_minute_start'];
211         }
212
213         if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) {
214                 $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'],$timedate->get_time_format(), $_POST[$prefix.'meridiem']);
215         }
216
217         if(isset($_POST[$prefix.'time_start']) && strlen($_POST[$prefix.'date_start']) == 10) {
218        $_POST[$prefix.'date_start'] = $_POST[$prefix.'date_start'] . ' ' . $_POST[$prefix.'time_start'];
219         }
220
221         // retrieve happens here
222         $focus = populateFromPost($prefix, $focus);
223         if(!$focus->ACLAccess('Save')) {
224            ACLController::displayNoAccess(true);
225            sugar_cleanup(true);
226         }
227
228         //add assigned user and current user if this is the first time bean is saved
229         if(empty($focus->id) && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] =='Meetings' && !empty($_REQUEST['return_action']) && $_REQUEST['return_action'] =='DetailView'){
230                 //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)
231                 //The current user is already added to UI and we want to give the current user the option of opting out of meeting.
232                 //add current user if the assigned to user is different than current user.
233                 if($current_user->id != $_POST['assigned_user_id']){
234                         $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
235                 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
236                 }
237         }elseif (empty($focus->id) ){
238                 //this is not from long form so add assigned and current user automatically as there is no invitee list UI.
239                 //This call could be through an ajax call from subpanels or shortcut bar
240         if(!isset($_POST['user_invitees']))
241         {
242            $_POST['user_invitees'] = '';
243         }
244
245                 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
246
247                 //add current user if the assigned to user is different than current user.
248                 if($current_user->id != $_POST['assigned_user_id'] && $_REQUEST['module'] != "Calendar"){
249                         $_POST['user_invitees'] .= ','.$current_user->id.', ';
250                 }
251
252                 //remove any double comma's introduced during appending
253             $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
254         }
255
256
257         if( (isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true') ||
258         (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id) ||
259         (isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') && !empty($focus->id))||
260          !isset($_POST['user_invitees']) // we need to check that user_invitees exists before processing, it is ok to be empty
261     ){
262         $focus->save(true);
263         $return_id = $focus->id;
264         }else{
265                 if($focus->status == 'Held' && $this->isEmptyReturnModuleAndAction() && !$this->isSaveFromDCMenu()){
266                 //if we are closing the meeting, and the request does not have a return module AND return action set and it is not a save
267             //being triggered by the DCMenu (shortcut bar) then the request is coming from a dashlet or subpanel close icon and there is no
268             //need to process user invitees, just save the current values.
269                 $focus->save(true);
270             }else{
271                 ///////////////////////////////////////////////////////////////////////////
272                 ////    REMOVE INVITEE RELATIONSHIPS
273                 if(!empty($_POST['user_invitees'])) {
274                    $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
275                 } else {
276                    $userInvitees = array();
277                 }
278
279                 // Calculate which users to flag as deleted and which to add
280                 $deleteUsers = array();
281                 $focus->load_relationship('users');
282                 // Get all users for the meeting
283                 $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$focus->id.'\'';
284                 $r = $focus->db->query($q);
285                 $acceptStatusUsers = array();
286                 while($a = $focus->db->fetchByAssoc($r)) {
287                           if(!in_array($a['user_id'], $userInvitees)) {
288                                  $deleteUsers[$a['user_id']] = $a['user_id'];
289                           } else {
290                              $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
291                           }
292                 }
293
294                 if(count($deleteUsers) > 0) {
295                         $sql = '';
296                         foreach($deleteUsers as $u) {
297                                 $sql .= ",'" . $u . "'";
298                         }
299                         $sql = substr($sql, 1);
300                         // We could run a delete SQL statement here, but will just mark as deleted instead
301                         $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $focus->id . "'";
302                         $focus->db->query($sql);
303                 }
304
305                 // Get all contacts for the meeting
306                 if(!empty($_POST['contact_invitees'])) {
307                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
308                 } else {
309                    $contactInvitees = array();
310                 }
311
312                 $deleteContacts = array();
313                 $focus->load_relationship('contacts');
314                 $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$focus->id.'\'';
315                 $r = $focus->db->query($q);
316                 $acceptStatusContacts = array();
317                 while($a = $focus->db->fetchByAssoc($r)) {
318                           if(!in_array($a['contact_id'], $contactInvitees)) {
319                                  $deleteContacts[$a['contact_id']] = $a['contact_id'];
320                           }     else {
321                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
322                           }
323                 }
324
325                 if(count($deleteContacts) > 0) {
326                         $sql = '';
327                         foreach($deleteContacts as $u) {
328                                 $sql .= ",'" . $u . "'";
329                         }
330                         $sql = substr($sql, 1);
331                         // We could run a delete SQL statement here, but will just mark as deleted instead
332                         $sql = "UPDATE meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $focus->id . "'";
333                         $focus->db->query($sql);
334                 }
335                 if(!empty($_POST['lead_invitees'])) {
336                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
337                 } else {
338                    $leadInvitees = array();
339                 }
340
341                 $deleteLeads = array();
342                 $focus->load_relationship('leads');
343                 $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$focus->id.'\'';
344                 $r = $focus->db->query($q);
345                 $acceptStatusLeads = array();
346                 while($a = $focus->db->fetchByAssoc($r)) {
347                           if(!in_array($a['lead_id'], $leadInvitees)) {
348                                  $deleteLeads[$a['lead_id']] = $a['lead_id'];
349                           }     else {
350                                  $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
351                           }
352                 }
353
354                 if(count($deleteLeads) > 0) {
355                         $sql = '';
356                         foreach($deleteLeads as $u) {
357                                 $sql .= ",'" . $u . "'";
358                         }
359                         $sql = substr($sql, 1);
360                         // We could run a delete SQL statement here, but will just mark as deleted instead
361                         $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $focus->id . "'";
362                         $focus->db->query($sql);
363                 }
364                 ////    END REMOVE
365                 ///////////////////////////////////////////////////////////////////////////
366
367
368                 ///////////////////////////////////////////////////////////////////////////
369                 ////    REBUILD INVITEE RELATIONSHIPS
370                 $focus->users_arr = array();
371                 $focus->users_arr = $userInvitees;
372                 $focus->contacts_arr = array();
373                 $focus->contacts_arr = $contactInvitees;
374                 $focus->leads_arr = array();
375                 $focus->leads_arr = $leadInvitees;
376
377                 // Call the Meeting module's save function to handle saving other fields besides
378                 // the users and contacts relationships
379             $focus->update_vcal = false;    // Bug #49195 : don't update vcal b/s related users aren't saved yet, create vcal cache below
380                 $focus->save(true);
381                 $return_id = $focus->id;
382                 if(empty($return_id)){
383                 //this is to handle the situation where the save fails, most likely because of a failure
384                 //in the external api. bug: 42200
385                 $_REQUEST['action'] = 'EditView';
386                 $_REQUEST['return_action'] = 'EditView';
387                 handleRedirect('', 'Meetings');
388             }
389                 // Process users
390                 $existing_users = array();
391                 if(!empty($_POST['existing_invitees'])) {
392                    $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
393                 }
394
395                 foreach($focus->users_arr as $user_id) {
396                     if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
397                                 continue;
398                         }
399
400                         if(!isset($acceptStatusUsers[$user_id])) {
401                                 $focus->users->add($user_id);
402                         } else {
403                                 // update query to preserve accept_status
404                                 $qU  = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
405                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
406                                 $qU .= 'AND user_id = \''.$user_id.'\'';
407                                 $focus->db->query($qU);
408                         }
409                 }
410
411                 // Process contacts
412                 $existing_contacts =  array();
413                 if(!empty($_POST['existing_contact_invitees'])) {
414                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
415                 }
416
417                 foreach($focus->contacts_arr as $contact_id) {
418                         if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
419                                 continue;
420                         }
421
422                         if(!isset($acceptStatusContacts[$contact_id])) {
423                             $focus->contacts->add($contact_id);
424                         } else {
425                                 // update query to preserve accept_status
426                                 $qU  = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
427                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
428                                 $qU .= 'AND contact_id = \''.$contact_id.'\'';
429                                 $focus->db->query($qU);
430                         }
431                 }
432                 // Process leads
433                 $existing_leads =  array();
434                 if(!empty($_POST['existing_lead_invitees'])) {
435                    $existing_leads =  explode(",", trim($_POST['existing_lead_invitees'], ','));
436                 }
437
438                 foreach($focus->leads_arr as $lead_id) {
439                         if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
440                                 continue;
441                         }
442
443                         if(!isset($acceptStatusLeads[$lead_id])) {
444                             $focus->leads->add($lead_id);
445                         } else {
446                                 // update query to preserve accept_status
447                                 $qU  = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
448                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
449                                 $qU .= 'AND lead_id = \''.$lead_id.'\'';
450                                 $focus->db->query($qU);
451                         }
452                 }
453
454             // Bug #49195 : update vcal
455             vCal::cache_sugar_vcal($current_user);
456             
457                 // CCL - Comment out call to set $current_user as invitee
458                 // set organizer to auto-accept
459                 //$focus->set_accept_status($current_user, 'accept');
460
461                 ////    END REBUILD INVITEE RELATIONSHIPS
462                 ///////////////////////////////////////////////////////////////////////////
463                 }
464         }
465         if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
466                 header("Location: index.php?module=Home&action=index");
467         }
468         else if($redirect) {
469                 handleRedirect($return_id, 'Meetings');
470         } else {
471                 return $focus;
472         }
473
474 } // end handleSave();
475
476 } // end Class def
477 ?>