]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/MeetingFormBase.php
Release 6.3.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
47 class MeetingFormBase{
48
49         function getFormBody($prefix, $mod='', $formname=''){
50                 if(!ACLController::checkAccess('Meetings', 'edit', true)){
51                 return '';
52         }
53                 global $mod_strings;
54                 global $app_strings;
55                 global $app_list_strings;
56                 global $current_user;
57                 global $theme;
58                 global $timedate;
59
60                 $temp_strings = $mod_strings;
61                 if(!empty($mod)){
62                         global $current_language;
63                         $mod_strings = return_module_language($current_language, $mod);
64                 }
65                         // Unimplemented until jscalendar language files are fixed
66                         // global $current_language;
67                         // global $default_language;
68                         // global $cal_codes;
69
70         $cal_lang = "en";
71 $cal_dateformat = $timedate->get_cal_date_format();
72
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().')';
78
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');
88                         $form = <<<EOF
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&nbsp;<span class="required">$lbl_required_symbol</span>&nbsp;<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&nbsp;<span class="required">$lbl_required_symbol</span>&nbsp;<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">
102                                         Calendar.setup({
103                                                 inputField : "jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "jscal_trigger", singleClick : true, step : 1, weekNumbers:false
104                                         });
105                                         </script>
106 EOF;
107
108
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;
115 return $form;
116 }
117
118
119
120 function getForm($prefix, $mod='Meetings'){
121         if(!ACLController::checkAccess('Meetings', 'edit', true)){
122                 return '';
123         }
124
125                 global $app_strings;
126                 global $app_list_strings;
127
128                 if(!empty($mod)){
129         global $current_language;
130         $mod_strings = return_module_language($current_language, $mod);
131                 } else {
132                         global $mod_strings;
133                 }
134
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'];
138
139
140 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
141 $the_form .= <<<EOQ
142
143
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">
146
147                         <input type="hidden" name="${prefix}action" value="Save">
148
149 EOQ;
150 $the_form       .= $this->getFormBody($prefix, 'Meetings',"{$prefix}MeetingSave" );
151 $the_form .= <<<EOQ
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>
153                 </form>
154 EOQ;
155
156 $the_form .= get_left_form_footer();
157 $the_form .= get_validate_record_js();
158
159 return $the_form;
160 }
161
162
163 /**
164  * handles save functionality for meetings
165  * @param       string prefix
166  * @param       bool redirect default True
167  * @param       bool useRequired default True
168  */
169 function handleSave($prefix,$redirect=true, $useRequired=false) {
170
171
172         require_once('include/formbase.php');
173
174         global $current_user;
175         global $timedate;
176
177         $focus = new Meeting();
178
179         if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
180                 return null;
181         }
182
183         if( !isset($_POST['reminder_checked']) or ( isset($_POST['reminder_checked']) && $_POST['reminder_checked'] == '0')) {
184                 $_POST['reminder_time'] = -1;
185         }
186         if(!isset($_POST['reminder_time'])) {
187                 $_POST['reminder_time'] = $current_user->getPreference('reminder_time');
188                 $_POST['reminder_checked']=1;
189         }
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];
194     }
195
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'];
198         }
199
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']);
202         }
203
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'];
206         }
207
208         // retrieve happens here
209         $focus = populateFromPost($prefix, $focus);
210         if(!$focus->ACLAccess('Save')) {
211            ACLController::displayNoAccess(true);
212            sugar_cleanup(true);
213         }
214
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']);
223                 }
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'].', ';
228
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.', ';
232                 }
233
234                 //remove any double comma's introduced during appending
235             $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']);
236         }
237
238
239         if( (isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true') ||
240         (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id) )
241     ){
242         $focus->save(true);
243         $return_id = $focus->id;
244         }else{
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.
249                 $focus->save(true);
250             }else{
251                 ///////////////////////////////////////////////////////////////////////////
252                 ////    REMOVE INVITEE RELATIONSHIPS
253                 if(!empty($_POST['user_invitees'])) {
254                    $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
255                 } else {
256                    $userInvitees = array();
257                 }
258
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'];
269                           } else {
270                              $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
271                           }
272                 }
273
274                 if(count($deleteUsers) > 0) {
275                         $sql = '';
276                         foreach($deleteUsers as $u) {
277                                 $sql .= ",'" . $u . "'";
278                         }
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);
283                 }
284
285                 // Get all contacts for the meeting
286                 if(!empty($_POST['contact_invitees'])) {
287                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
288                 } else {
289                    $contactInvitees = array();
290                 }
291
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'];
300                           }     else {
301                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
302                           }
303                 }
304
305                 if(count($deleteContacts) > 0) {
306                         $sql = '';
307                         foreach($deleteContacts as $u) {
308                                 $sql .= ",'" . $u . "'";
309                         }
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);
314                 }
315                 if(!empty($_POST['lead_invitees'])) {
316                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
317                 } else {
318                    $leadInvitees = array();
319                 }
320
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'];
329                           }     else {
330                                  $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
331                           }
332                 }
333
334                 if(count($deleteLeads) > 0) {
335                         $sql = '';
336                         foreach($deleteLeads as $u) {
337                                 $sql .= ",'" . $u . "'";
338                         }
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);
343                 }
344                 ////    END REMOVE
345                 ///////////////////////////////////////////////////////////////////////////
346
347
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;
356
357                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
358                         $focus->contacts_arr[] = $_POST['parent_id'];
359                 }
360                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
361                         $focus->leads_arr[] = $_POST['parent_id'];
362                 }
363                 // Call the Meeting module's save function to handle saving other fields besides
364                 // the users and contacts relationships
365                 $focus->save(true);
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');
373             }
374                 // Process users
375                 $existing_users = array();
376                 if(!empty($_POST['existing_invitees'])) {
377                    $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
378                 }
379
380                 foreach($focus->users_arr as $user_id) {
381                     if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
382                                 continue;
383                         }
384
385                         if(!isset($acceptStatusUsers[$user_id])) {
386                                 $focus->users->add($user_id);
387                         } else {
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);
393                         }
394                 }
395
396                 // Process contacts
397                 $existing_contacts =  array();
398                 if(!empty($_POST['existing_contact_invitees'])) {
399                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
400                 }
401
402                 foreach($focus->contacts_arr as $contact_id) {
403                         if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
404                                 continue;
405                         }
406
407                         if(!isset($acceptStatusContacts[$contact_id])) {
408                             $focus->contacts->add($contact_id);
409                         } else {
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);
415                         }
416                 }
417                 // Process leads
418                 $existing_leads =  array();
419                 if(!empty($_POST['existing_lead_invitees'])) {
420                    $existing_leads =  explode(",", trim($_POST['existing_lead_invitees'], ','));
421                 }
422
423                 foreach($focus->leads_arr as $lead_id) {
424                         if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
425                                 continue;
426                         }
427
428                         if(!isset($acceptStatusLeads[$lead_id])) {
429                             $focus->leads->add($lead_id);
430                         } else {
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);
436                         }
437                 }
438
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');
442
443                 ////    END REBUILD INVITEE RELATIONSHIPS
444                 ///////////////////////////////////////////////////////////////////////////
445                 }
446         }
447         if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
448                 header("Location: index.php?module=Home&action=index");
449         }
450         else if($redirect) {
451                 handleRedirect($return_id, 'Meetings');
452         } else {
453                 return $focus;
454         }
455
456 } // end handleSave();
457
458 } // end Class def
459 ?>