]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Meetings/MeetingFormBase.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Meetings / MeetingFormBase.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM 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->to_display_date(date('Y-m-d'),false);
83 $default_time_start = $timedate->to_display_time((date('H:i')), true, false);
84 $time_ampm = $timedate->AMPMMenu($prefix,date('H:i'));
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(true), $_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         $focus->save(true);
241         $return_id = $focus->id;
242         }else{
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.
247                 $focus->save(true);
248             }else{              
249                 ///////////////////////////////////////////////////////////////////////////
250                 ////    REMOVE INVITEE RELATIONSHIPS
251                 if(!empty($_POST['user_invitees'])) {
252                    $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
253                 } else {
254                    $userInvitees = array();     
255                 }
256             
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'];
267                           } else {
268                              $acceptStatusUsers[$a['user_id']] = $a['accept_status'];  
269                           }
270                 }
271                         
272                 if(count($deleteUsers) > 0) {
273                         $sql = '';
274                         foreach($deleteUsers as $u) {
275                                 $sql .= ",'" . $u . "'";
276                         }
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);
281                 }         
282             
283                 // Get all contacts for the meeting
284                 if(!empty($_POST['contact_invitees'])) {
285                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
286                 } else {
287                    $contactInvitees = array();  
288                 }
289             
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'];
298                           }     else {
299                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
300                           }
301                 }
302                 
303                 if(count($deleteContacts) > 0) {
304                         $sql = '';
305                         foreach($deleteContacts as $u) {
306                                 $sql .= ",'" . $u . "'";
307                         }
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);
312                 }
313                 if(!empty($_POST['lead_invitees'])) {
314                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
315                 } else {
316                    $leadInvitees = array();     
317                 }
318             
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'];
327                           }     else {
328                                  $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
329                           }
330                 }
331                 
332                 if(count($deleteLeads) > 0) {
333                         $sql = '';
334                         foreach($deleteLeads as $u) {
335                                 $sql .= ",'" . $u . "'";
336                         }
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);
341                 }
342                 ////    END REMOVE
343                 ///////////////////////////////////////////////////////////////////////////
344                 
345             
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;
354                 
355                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
356                         $focus->contacts_arr[] = $_POST['parent_id'];
357                 }
358                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
359                         $focus->leads_arr[] = $_POST['parent_id'];
360                 }       
361                 // Call the Meeting module's save function to handle saving other fields besides
362                 // the users and contacts relationships
363                 $focus->save(true);
364                 $return_id = $focus->id;
365                 
366                 // Process users
367                 $existing_users = array();
368                 if(!empty($_POST['existing_invitees'])) {
369                    $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
370                 }
371             
372                 foreach($focus->users_arr as $user_id) {
373                     if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
374                                 continue;
375                         }
376                         
377                         if(!isset($acceptStatusUsers[$user_id])) {
378                                 $focus->users->add($user_id);
379                         } else {
380                                 // update query to preserve accept_status
381                                 $qU  = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
382                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
383                                 $qU .= 'AND user_id = \''.$user_id.'\'';
384                                 $focus->db->query($qU);
385                         } 
386                 }
387                 
388                 // Process contacts
389                 $existing_contacts =  array();
390                 if(!empty($_POST['existing_contact_invitees'])) {
391                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
392                 }
393                     
394                 foreach($focus->contacts_arr as $contact_id) {
395                         if(empty($contact_id) || isset($exiting_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
396                                 continue;
397                         }
398                         
399                         if(!isset($acceptStatusContacts[$contact_id])) {
400                             $focus->contacts->add($contact_id);
401                         } else {
402                                 // update query to preserve accept_status
403                                 $qU  = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
404                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
405                                 $qU .= 'AND contact_id = \''.$contact_id.'\'';
406                                 $focus->db->query($qU);
407                         }
408                 }
409                 // Process leads
410                 $existing_leads =  array();
411                 if(!empty($_POST['existing_lead_invitees'])) {
412                    $existing_leads =  explode(",", trim($_POST['existing_lead_invitees'], ','));
413                 }
414                     
415                 foreach($focus->leads_arr as $lead_id) {
416                         if(empty($lead_id) || isset($exiting_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
417                                 continue;
418                         }
419                         
420                         if(!isset($acceptStatusLeads[$lead_id])) {
421                             $focus->leads->add($lead_id);
422                         } else {
423                                 // update query to preserve accept_status
424                                 $qU  = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
425                                 $qU .= 'WHERE meeting_id = \''.$focus->id.'\' ';
426                                 $qU .= 'AND lead_id = \''.$lead_id.'\'';
427                                 $focus->db->query($qU);
428                         }
429                 }
430                 
431                 // CCL - Comment out call to set $current_user as invitee
432                 // set organizer to auto-accept
433                 //$focus->set_accept_status($current_user, 'accept');
434                 
435                 ////    END REBUILD INVITEE RELATIONSHIPS
436                 ///////////////////////////////////////////////////////////////////////////
437                 }
438         }
439         if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){
440                 header("Location: index.php?module=Home&action=index");
441         }
442         else if($redirect) {
443                 handleRedirect($return_id, 'Meetings');
444         } else {
445                 return $focus;
446         }
447
448 } // end handleSave();
449
450 } // end Class def
451 ?>