]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/MeetingFormBase.php
Release 6.4.0
[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-2011 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         $time_format = $timedate->get_user_time_format();
192     $time_separator = ":";
193     if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
194        $time_separator = $match[1];
195     }
196
197         if(!empty($_POST[$prefix.'time_hour_start']) && empty($_POST['time_start'])) {
198                 $_POST[$prefix.'time_start'] = $_POST[$prefix.'time_hour_start']. $time_separator .$_POST[$prefix.'time_minute_start'];
199         }
200
201         if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) {
202                 $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'],$timedate->get_time_format(), $_POST[$prefix.'meridiem']);
203         }
204
205         if(isset($_POST[$prefix.'time_start']) && strlen($_POST[$prefix.'date_start']) == 10) {
206        $_POST[$prefix.'date_start'] = $_POST[$prefix.'date_start'] . ' ' . $_POST[$prefix.'time_start'];
207         }
208
209         // retrieve happens here
210         $focus = populateFromPost($prefix, $focus);
211         if(!$focus->ACLAccess('Save')) {
212            ACLController::displayNoAccess(true);
213            sugar_cleanup(true);
214         }
215
216         //add assigned user and current user if this is the first time bean is saved
217         if(empty($focus->id) && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] =='Meetings' && !empty($_REQUEST['return_action']) && $_REQUEST['return_action'] =='DetailView'){
218                 //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)
219                 //The current user is already added to UI and we want to give the current user the option of opting out of meeting.
220                 //add current user if the assigned to user is different than current user.
221                 if($current_user->id != $_POST['assigned_user_id']){
222                         $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
223                 $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
224                 }
225         }elseif (empty($focus->id) ){
226                 //this is not from long form so add assigned and current user automatically as there is no invitee list UI.
227                 //This call could be through an ajax call from subpanels or shortcut bar
228         if(!isset($_POST['user_invitees']))
229         {
230            $_POST['user_invitees'] = '';
231         }
232
233                 $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', ';
234
235                 //add current user if the assigned to user is different than current user.
236                 if($current_user->id != $_POST['assigned_user_id']){
237                         $_POST['user_invitees'] .= ','.$current_user->id.', ';
238                 }
239
240                 //remove any double comma's introduced during appending
241             $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
242         }
243
244
245         if( (isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true') ||
246         (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id) )
247     ){
248         $focus->save(true);
249         $return_id = $focus->id;
250         }else{
251                 if($focus->status == 'Held' && $this->isEmptyReturnModuleAndAction() && !$this->isSaveFromDCMenu()){
252                 //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
253             //being triggered by the DCMenu (shortcut bar) then the request is coming from a dashlet or subpanel close icon and there is no
254             //need to process user invitees, just save the current values.
255                 $focus->save(true);
256             }else{
257                 ///////////////////////////////////////////////////////////////////////////
258                 ////    REMOVE INVITEE RELATIONSHIPS
259                 if(!empty($_POST['user_invitees'])) {
260                    $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
261                 } else {
262                    $userInvitees = array();
263                 }
264
265                 // Calculate which users to flag as deleted and which to add
266                 $deleteUsers = array();
267                 $focus->load_relationship('users');
268                 // Get all users for the meeting
269                 $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$focus->id.'\'';
270                 $r = $focus->db->query($q);
271                 $acceptStatusUsers = array();
272                 while($a = $focus->db->fetchByAssoc($r)) {
273                           if(!in_array($a['user_id'], $userInvitees)) {
274                                  $deleteUsers[$a['user_id']] = $a['user_id'];
275                           } else {
276                              $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
277                           }
278                 }
279
280                 if(count($deleteUsers) > 0) {
281                         $sql = '';
282                         foreach($deleteUsers as $u) {
283                                 $sql .= ",'" . $u . "'";
284                         }
285                         $sql = substr($sql, 1);
286                         // We could run a delete SQL statement here, but will just mark as deleted instead
287                         $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $focus->id . "'";
288                         $focus->db->query($sql);
289                 }
290
291                 // Get all contacts for the meeting
292                 if(!empty($_POST['contact_invitees'])) {
293                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
294                 } else {
295                    $contactInvitees = array();
296                 }
297
298                 $deleteContacts = array();
299                 $focus->load_relationship('contacts');
300                 $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$focus->id.'\'';
301                 $r = $focus->db->query($q);
302                 $acceptStatusContacts = array();
303                 while($a = $focus->db->fetchByAssoc($r)) {
304                           if(!in_array($a['contact_id'], $contactInvitees)) {
305                                  $deleteContacts[$a['contact_id']] = $a['contact_id'];
306                           }     else {
307                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
308                           }
309                 }
310
311                 if(count($deleteContacts) > 0) {
312                         $sql = '';
313                         foreach($deleteContacts as $u) {
314                                 $sql .= ",'" . $u . "'";
315                         }
316                         $sql = substr($sql, 1);
317                         // We could run a delete SQL statement here, but will just mark as deleted instead
318                         $sql = "UPDATE meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $focus->id . "'";
319                         $focus->db->query($sql);
320                 }
321                 if(!empty($_POST['lead_invitees'])) {
322                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
323                 } else {
324                    $leadInvitees = array();
325                 }
326
327                 $deleteLeads = array();
328                 $focus->load_relationship('leads');
329                 $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$focus->id.'\'';
330                 $r = $focus->db->query($q);
331                 $acceptStatusLeads = array();
332                 while($a = $focus->db->fetchByAssoc($r)) {
333                           if(!in_array($a['lead_id'], $leadInvitees)) {
334                                  $deleteLeads[$a['lead_id']] = $a['lead_id'];
335                           }     else {
336                                  $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
337                           }
338                 }
339
340                 if(count($deleteLeads) > 0) {
341                         $sql = '';
342                         foreach($deleteLeads as $u) {
343                                 $sql .= ",'" . $u . "'";
344                         }
345                         $sql = substr($sql, 1);
346                         // We could run a delete SQL statement here, but will just mark as deleted instead
347                         $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $focus->id . "'";
348                         $focus->db->query($sql);
349                 }
350                 ////    END REMOVE
351                 ///////////////////////////////////////////////////////////////////////////
352
353
354                 ///////////////////////////////////////////////////////////////////////////
355                 ////    REBUILD INVITEE RELATIONSHIPS
356                 $focus->users_arr = array();
357                 $focus->users_arr = $userInvitees;
358                 $focus->contacts_arr = array();
359                 $focus->contacts_arr = $contactInvitees;
360                 $focus->leads_arr = array();
361                 $focus->leads_arr = $leadInvitees;
362
363                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
364                         $focus->contacts_arr[] = $_POST['parent_id'];
365                 }
366                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
367                         $focus->leads_arr[] = $_POST['parent_id'];
368                 }
369                 // Call the Meeting module's save function to handle saving other fields besides
370                 // the users and contacts relationships
371                 $focus->save(true);
372                 $return_id = $focus->id;
373                 if(empty($return_id)){
374                 //this is to handle the situation where the save fails, most likely because of a failure
375                 //in the external api. bug: 42200
376                 $_REQUEST['action'] = 'EditView';
377                 $_REQUEST['return_action'] = 'EditView';
378                 handleRedirect('', 'Meetings');
379             }
380                 // Process users
381                 $existing_users = array();
382                 if(!empty($_POST['existing_invitees'])) {
383                    $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
384                 }
385
386                 foreach($focus->users_arr as $user_id) {
387                     if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
388                                 continue;
389                         }
390
391                         if(!isset($acceptStatusUsers[$user_id])) {
392                                 $focus->users->add($user_id);
393                         } else {
394                                 // update query to preserve accept_status
395                                 $qU  = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
396                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
397                                 $qU .= 'AND user_id = \''.$user_id.'\'';
398                                 $focus->db->query($qU);
399                         }
400                 }
401
402                 // Process contacts
403                 $existing_contacts =  array();
404                 if(!empty($_POST['existing_contact_invitees'])) {
405                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
406                 }
407
408                 foreach($focus->contacts_arr as $contact_id) {
409                         if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
410                                 continue;
411                         }
412
413                         if(!isset($acceptStatusContacts[$contact_id])) {
414                             $focus->contacts->add($contact_id);
415                         } else {
416                                 // update query to preserve accept_status
417                                 $qU  = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
418                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
419                                 $qU .= 'AND contact_id = \''.$contact_id.'\'';
420                                 $focus->db->query($qU);
421                         }
422                 }
423                 // Process leads
424                 $existing_leads =  array();
425                 if(!empty($_POST['existing_lead_invitees'])) {
426                    $existing_leads =  explode(",", trim($_POST['existing_lead_invitees'], ','));
427                 }
428
429                 foreach($focus->leads_arr as $lead_id) {
430                         if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
431                                 continue;
432                         }
433
434                         if(!isset($acceptStatusLeads[$lead_id])) {
435                             $focus->leads->add($lead_id);
436                         } else {
437                                 // update query to preserve accept_status
438                                 $qU  = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
439                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
440                                 $qU .= 'AND lead_id = \''.$lead_id.'\'';
441                                 $focus->db->query($qU);
442                         }
443                 }
444
445                 // CCL - Comment out call to set $current_user as invitee
446                 // set organizer to auto-accept
447                 //$focus->set_accept_status($current_user, 'accept');
448
449                 ////    END REBUILD INVITEE RELATIONSHIPS
450                 ///////////////////////////////////////////////////////////////////////////
451                 }
452         }
453         if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
454                 header("Location: index.php?module=Home&action=index");
455         }
456         else if($redirect) {
457                 handleRedirect($return_id, 'Meetings');
458         } else {
459                 return $focus;
460         }
461
462 } // end handleSave();
463
464 } // end Class def
465 ?>