]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Contacts/ContactFormBase.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Contacts / ContactFormBase.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 contact
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/PersonFormBase.php');
47
48 class ContactFormBase extends PersonFormBase {
49
50 var $moduleName = 'Contacts';
51 var $objectName = 'Contact';
52
53 /**
54  * getDuplicateQuery
55  *
56  * This function returns the SQL String used for initial duplicate Contacts check
57  *
58  * @see checkForDuplicates (method), ContactFormBase.php, LeadFormBase.php, ProspectFormBase.php
59  * @param $focus sugarbean
60  * @param $prefix String value of prefix that may be present in $_POST variables
61  * @return SQL String of the query that should be used for the initial duplicate lookup check
62  */
63 public function getDuplicateQuery($focus, $prefix='')
64 {
65         $query = 'SELECT id, first_name, last_name, title FROM contacts ';
66     
67     // Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
68     // add team security
69     
70     $query .= ' where deleted = 0 AND ';
71         if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0){
72                 $query .= " first_name LIKE '". $_POST[$prefix.'first_name'] . "%' AND last_name = '". $_POST[$prefix.'last_name'] ."'";
73         } else {
74                 $query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
75         }
76
77         if(!empty($_POST[$prefix.'record'])) {
78                 $query .= " AND  id != '". $_POST[$prefix.'record'] ."'";
79         }
80     return $query;
81 }
82
83
84 function getWideFormBody($prefix, $mod='',$formname='',  $contact = '', $portal = true){
85         
86         if(!ACLController::checkAccess('Contacts', 'edit', true)){
87                 return '';
88         }
89         
90         if(empty($contact)){
91                 $contact = new Contact();
92         }
93
94         global $mod_strings;
95         $temp_strings = $mod_strings;
96         if(!empty($mod)){
97                 global $current_language;
98                 $mod_strings = return_module_language($current_language, $mod);
99         }
100         global $app_strings;
101         global $current_user;
102         global $app_list_strings;
103         $primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], $contact->primary_address_country);
104         $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
105         $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
106         $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
107         $lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
108         $lbl_address =  $mod_strings['LBL_PRIMARY_ADDRESS'];
109
110         if (isset($contact->assigned_user_id)) {
111                 $user_id=$contact->assigned_user_id;
112         } else {
113                 $user_id = $current_user->id;
114         }
115
116         //Retrieve Email address and set email1, email2
117         $sugarEmailAddress = new SugarEmailAddress();
118         $sugarEmailAddress->handleLegacyRetrieve($contact);
119         if(!isset($contact->email1)){
120         $contact->email1 = '';
121     }
122     if(!isset($contact->email2)){
123         $contact->email2 = '';
124     }
125     if(!isset($contact->email_opt_out)){
126         $contact->email_opt_out = '';
127     }
128         $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
129         $salutation_options=get_select_options_with_id($app_list_strings['salutation_dom'], $contact->salutation);
130         
131         if (isset($contact->lead_source)) {
132                 $lead_source_options=get_select_options_with_id($app_list_strings['lead_source_dom'], $contact->lead_source);
133         } else {
134                 $lead_source_options=get_select_options_with_id($app_list_strings['lead_source_dom'], '');
135         }
136
137         $form="";
138
139
140         if ($formname == 'ConvertProspect') {
141                 $lead_source_label = "<td scope='row'>&nbsp;</td>";
142                 $lead_source_field = "<td >&nbsp;</td>";
143         } else {
144                 $lead_source_label = "<td scope='row' nowrap>${mod_strings['LBL_LEAD_SOURCE']}</td>";
145                 $lead_source_field = "<td ><select name='${prefix}lead_source'>$lead_source_options</select></td>";
146         }
147
148
149 global $timedate;
150 $birthdate = '';
151 if(!empty($_REQUEST['birthdate'])){
152         $birthdate=$_REQUEST['birthdate'];
153    }
154
155
156 $jsCalendarImage = SugarThemeRegistry::current()->getImageURL('jscalendar.gif');        
157 $ntc_date_format = $timedate->get_user_date_format();
158 $cal_dateformat = $timedate->get_cal_date_format();
159 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
160
161 $form .= <<<EOQ
162                 <input type="hidden" name="${prefix}record" value="">
163                 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
164                 <table border='0' celpadding="0" cellspacing="0" width='100%'>
165                 <tr>
166                 <td nowrap scope='row'>$lbl_first_name</td>
167                 <td scope='row'>$lbl_last_name&nbsp;<span class="required">$lbl_required_symbol</span></td>
168                 <td scope='row' nowrap>${mod_strings['LBL_TITLE']}</td>
169                 <td scope='row' nowrap>${mod_strings['LBL_DEPARTMENT']}</td>
170                 </tr>
171                 <tr>
172                 <td ><select name='${prefix}salutation'>$salutation_options</select>&nbsp;<input name="${prefix}first_name" type="text" value="{$contact->first_name}"></td>
173                 <td ><input name='${prefix}last_name' type="text" value="{$contact->last_name}"></td>
174                 <td  nowrap><input name='${prefix}title' type="text" value="{$contact->title}"></td>
175                 <td  nowrap><input name='${prefix}department' type="text" value="{$contact->department}"></td>
176                 </tr>
177                 <tr>
178                 <td nowrap colspan='4' scope='row'>$lbl_address</td>
179                 </tr>
180
181                 <tr>
182                 <td nowrap colspan='4' ><textarea cols='80' rows='2' name='${prefix}primary_address_street'>{$contact->primary_address_street}</textarea></td>
183                 </tr>
184
185                 <tr>
186                 <td scope='row'>${mod_strings['LBL_CITY']}</td>
187                 <td scope='row'>${mod_strings['LBL_STATE']}</td>
188                 <td scope='row'>${mod_strings['LBL_POSTAL_CODE']}</td>
189                 <td scope='row'>${mod_strings['LBL_COUNTRY']}</td>
190                 </tr>
191
192                 <tr>
193                 <td ><input name='${prefix}primary_address_city'  maxlength='100' value='{$contact->primary_address_city}'></td>
194                 <td ><input name='${prefix}primary_address_state'  maxlength='100' value='{$contact->primary_address_state}'></td>
195                 <td ><input name='${prefix}primary_address_postalcode'  maxlength='100' value='{$contact->primary_address_postalcode}'></td>
196                 <td ><input name='${prefix}primary_address_country'  maxlength='100' value='{$contact->primary_address_country}'></td>
197                 </tr>
198
199
200                 <tr>
201                 <td nowrap scope='row'>$lbl_phone</td>
202                 <td nowrap scope='row'>${mod_strings['LBL_MOBILE_PHONE']}</td>
203                 <td nowrap scope='row'>${mod_strings['LBL_FAX_PHONE']}</td>
204                 <td nowrap scope='row'>${mod_strings['LBL_HOME_PHONE']}</td>
205                 </tr>
206
207                 <tr>
208                 <td nowrap ><input name='${prefix}phone_work' type="text" value="{$contact->phone_work}"></td>
209                 <td nowrap ><input name='${prefix}phone_mobile' type="text" value="{$contact->phone_mobile}"></td>
210                 <td nowrap ><input name='${prefix}phone_fax' type="text" value="{$contact->phone_fax}"></td>
211                 <td nowrap ><input name='${prefix}phone_home' type="text" value="{$contact->phone_home}"></td>
212                 </tr>
213
214                 <tr>
215                 <td scope='row' nowrap>${mod_strings['LBL_OTHER_PHONE']}</td>
216                 $lead_source_label
217
218                 <td scope="row">${mod_strings['LBL_BIRTHDATE']}&nbsp;</td>
219                 </tr>
220
221
222                 <tr>
223                 <td  nowrap><input name='${prefix}phone_other' type="text" value="{$contact->phone_other}"></td>
224                 $lead_source_field
225
226                 <td  nowrap>
227                         <input name='{$prefix}birthdate' onblur="parseDate(this, '$cal_dateformat');" size='12' maxlength='10' id='${prefix}jscal_field' type="text" value="{$birthdate}">&nbsp;
228                         <!--not_in_theme!--><img src="{$jsCalendarImage}" alt="{$app_strings['LBL_ENTER_DATE']}"  id="${prefix}jscal_trigger" align="absmiddle">
229                 </td>           
230                 </tr>
231
232 EOQ;
233
234 $form .= $sugarEmailAddress->getEmailAddressWidgetEditView($contact->id, $_REQUEST['action']=='ConvertLead'?'Leads':'Contacts', false, 'include/SugarEmailAddress/templates/forWideFormBodyView.tpl');
235
236 require_once('include/SugarFields/Fields/Text/SugarFieldText.php');
237 $sugarfield = new SugarFieldText('Text');
238 $description_text = $sugarfield->getClassicEditView('description', $contact->description, $prefix, true);
239
240 $form .= <<<EOQ
241                 <tr>
242                 <td nowrap colspan='4' scope='row'>${mod_strings['LBL_DESCRIPTION']}</td>
243                 </tr>
244                 <tr>
245                 <td nowrap colspan='4' >{$description_text}</td>
246                 </tr>
247 EOQ;
248
249
250
251         //carry forward custom lead fields common to contacts during Lead Conversion
252         $tempContact = new Contact();
253
254         if (method_exists($contact, 'convertCustomFieldsForm')) $contact->convertCustomFieldsForm($form, $tempContact, $prefix);
255         unset($tempContact);
256
257 $form .= <<<EOQ
258                 </table>
259                 
260                 <input type='hidden' name='${prefix}alt_address_street'  value='{$contact->alt_address_street}'>
261                 <input type='hidden' name='${prefix}alt_address_city' value='{$contact->alt_address_city}'><input type='hidden' name='${prefix}alt_address_state'   value='{$contact->alt_address_state}'><input type='hidden' name='${prefix}alt_address_postalcode'   value='{$contact->alt_address_postalcode}'><input type='hidden' name='${prefix}alt_address_country'  value='{$contact->alt_address_country}'>
262                 <input type='hidden' name='${prefix}do_not_call'  value='{$contact->do_not_call}'>
263                 <input type='hidden' name='${prefix}email_opt_out'  value='{$contact->email_opt_out}'>
264 EOQ;
265
266         if ($portal == true){
267                 if (isset($contact->portal_name)) {
268                         $form.="<input type='hidden' name='${prefix}portal_name'  value='{$contact->portal_name}'>";
269                 } else {
270                         $form.="<input type='hidden' name='${prefix}portal_name'  value=''>";
271                 }
272                 if (isset($contact->portal_app)) {
273                         $form.="<input type='hidden' name='${prefix}portal_app'  value='{$contact->portal_app}'>";
274                 } else {
275                         $form.="<input type='hidden' name='${prefix}portal_app'  value=''>";
276                 }
277
278
279                 if(!empty($contact->portal_name) && !empty($contact->portal_app)){
280                         $form .= "<input name='${prefix}portal_active' type='hidden' size='25'  value='1' >";
281                 }
282
283             if(isset($contact->portal_password)){
284                 $form.="<input type='password' name='${prefix}portal_password1'  value='{$contact->portal_password}'>";
285                 $form.="<input type='password' name='${prefix}portal_password'  value='{$contact->portal_password}'>";
286                 $form .= "<input name='${prefix}old_portal_password' type='hidden' size='25'  value='{$contact->portal_password}' >";
287             }else{
288                 $form.="<input type='password' name='${prefix}portal_password1'  value=''>";
289                 $form.="<input type='password' name='${prefix}portal_password'  value=''>";
290                 $form .= "<input name='${prefix}old_portal_password' type='hidden' size='25'  value='' >";
291             }
292         }
293         
294 $form .= <<<EOQ
295                         <script type="text/javascript">
296                                 Calendar.setup ({
297                                 inputField : "{$prefix}jscal_field", daFormat : "$cal_dateformat", ifFormat : "$cal_dateformat", showsTime : false, button : "{$prefix}jscal_trigger", singleClick : true, step : 1, weekNumbers:false
298                                 });
299                         </script>
300 EOQ;
301         
302         
303         
304         $javascript = new javascript();
305         $javascript->setFormName($formname);
306         $javascript->setSugarBean(new Contact());
307         $javascript->addField('email1','false',$prefix);
308         $javascript->addField('email2','false',$prefix);
309         $javascript->addRequiredFields($prefix);
310
311         $form .=$javascript->getScript();
312         $mod_strings = $temp_strings;
313         
314   
315         return $form;
316 }
317
318 function getFormBody($prefix, $mod='', $formname=''){
319         if(!ACLController::checkAccess('Contacts', 'edit', true)){
320                 return '';
321         }
322 global $mod_strings;
323 $temp_strings = $mod_strings;
324 if(!empty($mod)){
325         global $current_language;
326         $mod_strings = return_module_language($current_language, $mod);
327 }
328                 global $app_strings;
329                 global $current_user;
330                 $lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
331                 $lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
332                 $lbl_last_name = $mod_strings['LBL_LAST_NAME'];
333                 $lbl_phone = $mod_strings['LBL_PHONE'];
334                 $user_id = $current_user->id;
335                 $lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
336 if ($formname == 'EmailEditView')
337 {
338                 $form = <<<EOQ
339                 <input type="hidden" name="${prefix}record" value="">
340                 <input type="hidden" name="${prefix}email2" value="">
341                 <input type="hidden" name="${prefix}phone_work" value="">
342                 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
343                 $lbl_first_name<br>
344                 <input name="${prefix}first_name" type="text" value="" size=10><br>
345                 $lbl_last_name&nbsp;<span class="required">$lbl_required_symbol</span><br>
346                 <input name='${prefix}last_name' type="text" value="" size=10><br>
347                 $lbl_email_address&nbsp;<span class="required">$lbl_required_symbol</span><br>
348                 <input name='${prefix}email1' type="text" value=""><br><br>
349
350 EOQ;
351 }
352 else
353 {
354                 $form = <<<EOQ
355                 <input type="hidden" name="${prefix}record" value="">
356                 <input type="hidden" name="${prefix}email2" value="">
357                 <input type="hidden" name="${prefix}assigned_user_id" value='${user_id}'>
358                 $lbl_first_name<br>
359                 <input name="${prefix}first_name" type="text" value=""><br>
360                 $lbl_last_name&nbsp;<span class="required">$lbl_required_symbol</span><br>
361                 <input name='${prefix}last_name' type="text" value=""><br>
362                 $lbl_phone<br>
363                 <input name='${prefix}phone_work' type="text" value=""><br>
364                 $lbl_email_address<br>
365                 <input name='${prefix}email1' type="text" value=""><br><br>
366
367 EOQ;
368 }
369
370
371 $javascript = new javascript();
372 $javascript->setFormName($formname);
373 $javascript->setSugarBean(new Contact());
374 $javascript->addField('email1','false',$prefix);
375 $javascript->addRequiredFields($prefix);
376
377 $form .=$javascript->getScript();
378 $mod_strings = $temp_strings;
379 return $form;
380
381 }
382 function getForm($prefix, $mod=''){
383         if(!ACLController::checkAccess('Contacts', 'edit', true)){
384                 return '';
385         }
386 if(!empty($mod)){
387         global $current_language;
388         $mod_strings = return_module_language($current_language, $mod);
389 }else global $mod_strings;
390 global $app_strings;
391
392 $lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
393 $lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
394 $lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
395
396
397 $the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
398 $the_form .= <<<EOQ
399
400                 <form name="${prefix}ContactSave" onSubmit="return check_form('${prefix}ContactSave')" method="POST" action="index.php">
401                         <input type="hidden" name="${prefix}module" value="Contacts">
402                         <input type="hidden" name="${prefix}action" value="Save">
403 EOQ;
404 $the_form .= $this->getFormBody($prefix,'Contacts', "${prefix}ContactSave");
405 $the_form .= <<<EOQ
406                 <input title="$lbl_save_button_title" accessKey="$lbl_save_button_key" class="button" type="submit" name="${prefix}button" value="  $lbl_save_button_label  " >
407                 </form>
408
409 EOQ;
410 $the_form .= get_left_form_footer();
411 $the_form .= get_validate_record_js();
412
413 return $the_form;
414
415
416 }
417
418
419 function handleSave($prefix, $redirect=true, $useRequired=false){
420         global $theme, $current_user;
421         
422         
423         
424     
425         require_once('include/formbase.php');
426         
427         global $timedate;
428
429         $focus = new Contact();
430
431         if($useRequired &&  !checkRequired($prefix, array_keys($focus->required_fields))){
432                 return null;
433         }
434         
435         if (!empty($_POST[$prefix.'new_reports_to_id'])) {
436                 $focus->retrieve($_POST[$prefix.'new_reports_to_id']);
437                 $focus->reports_to_id = $_POST[$prefix.'record'];
438         } else {
439
440         $focus = populateFromPost($prefix, $focus);
441         if( isset($_POST[$prefix.'old_portal_password']) && !empty($focus->portal_password) && $focus->portal_password != $_POST[$prefix.'old_portal_password']){
442             $focus->portal_password = md5($focus->portal_password);
443         }
444                 if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
445                 if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
446
447         }
448         if(!$focus->ACLAccess('Save')){
449                         ACLController::displayNoAccess(true);
450                         sugar_cleanup(true);
451         }
452         if($_REQUEST['action'] != 'BusinessCard' && $_REQUEST['action'] != 'ConvertLead' && $_REQUEST['action'] != 'ConvertProspect')
453         {
454                 
455                 if (!empty($_POST[$prefix.'sync_contact']) || !empty($focus->sync_contact)){
456                          $focus->contacts_users_id = $current_user->id;
457                 }
458                 else{
459                         if (!isset($focus->users))
460                         {
461                         $focus->load_relationship('user_sync');
462                         }
463                 $focus->contacts_users_id = null;
464                         $focus->user_sync->delete($focus->id, $current_user->id);
465                 }
466         }
467
468         if (isset($GLOBALS['check_notify'])) {
469                 $check_notify = $GLOBALS['check_notify'];
470         }
471         else {
472                 $check_notify = FALSE;
473         }
474
475     
476         if (empty($_POST['record']) && empty($_POST['dup_checked'])) {
477
478                 $duplicateContacts = $this->checkForDuplicates($prefix);
479                 if(isset($duplicateContacts)){
480                         $location='module=Contacts&action=ShowDuplicates';
481                         $get = '';
482                         if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
483                                 $get .= '&inbound_email_id='.$_POST['inbound_email_id'];
484                         }
485                         
486                         // Bug 25311 - Add special handling for when the form specifies many-to-many relationships
487                         if(isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
488                                 $get .= '&Contactsrelate_to='.$_POST['relate_to'];
489                         }
490                         if(isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
491                                 $get .= '&Contactsrelate_id='.$_POST['relate_id'];
492                         }
493
494                         //add all of the post fields to redirect get string
495                         foreach ($focus->column_fields as $field)
496                         {
497                                 if (!empty($focus->$field) && !is_object($focus->$field))
498                                 {
499                                         $get .= "&Contacts$field=".urlencode($focus->$field);
500                                 }
501                         }
502
503                         foreach ($focus->additional_column_fields as $field)
504                         {
505                                 if (!empty($focus->$field))
506                                 {
507                                         $get .= "&Contacts$field=".urlencode($focus->$field);
508                                 }
509                         }
510
511                         if($focus->hasCustomFields()) {
512                                 foreach($focus->field_defs as $name=>$field) {  
513                                         if (!empty($field['source']) && $field['source'] == 'custom_fields')
514                                         {
515                                                 $get .= "&Contacts$name=".urlencode($focus->$name);
516                                         }                           
517                                 }
518                         }                       
519                         
520                         
521                         $emailAddress = new SugarEmailAddress();
522                         $get .= $emailAddress->getFormBaseURL($focus);
523
524                         
525                         //create list of suspected duplicate contact id's in redirect get string
526                         $i=0;
527                         foreach ($duplicateContacts as $contact)
528                         {
529                                 $get .= "&duplicate[$i]=".$contact['id'];
530                                 $i++;
531                         }
532
533                         //add return_module, return_action, and return_id to redirect get string
534                         $get .= "&return_module=";
535                         if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
536                         else $get .= "Contacts";
537                         $get .= "&return_action=";
538                         if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
539                         //else $get .= "DetailView";
540                         if(!empty($_POST['return_id'])) $get .= "&return_id=".$_POST['return_id'];
541                         if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
542                         if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
543
544                         // for InboundEmail flow
545                         if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
546
547
548             $_SESSION['SHOW_DUPLICATES'] = $get;
549             //now redirect the post to modules/Contacts/ShowDuplicates.php
550             if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
551             {
552                 ob_clean();
553                 $json = getJSONobj();
554                 echo $json->encode(array('status' => 'dupe', 'get' => $location));
555             }
556             else if(!empty($_REQUEST['ajax_load']))
557             {
558                 echo "<script>SUGAR.ajaxUI.loadContent('index.php?$location');</script>";
559             }
560             else {
561                 if(!empty($_POST['to_pdf'])) $location .= '&to_pdf='.$_POST['to_pdf'];
562                 header("Location: index.php?$location");
563             }
564             return null;
565                 }
566         }
567
568         global $current_user;
569         if(is_admin($current_user)){
570                 if (!isset($_POST[$prefix.'portal_active'])) $focus->portal_active = '0';
571                 //if no password is set set account to inactive for portal
572                 if(empty($_POST[$prefix.'portal_name']))$focus->portal_active = '0';
573
574         }
575
576         ///////////////////////////////////////////////////////////////////////////////
577         ////    INBOUND EMAIL HANDLING
578         ///////////////////////////////////////////////////////////////////////////////
579         if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
580                 // fake this case like it's already saved.
581                 $focus->save($check_notify);
582                 
583                 $email = new Email();
584                 $email->retrieve($_REQUEST['inbound_email_id']);
585                 $email->parent_type = 'Contacts';
586                 $email->parent_id = $focus->id;
587                 $email->assigned_user_id = $current_user->id;
588                 $email->status = 'read';
589                 $email->save();
590                 $email->load_relationship('contacts');
591                 $email->contacts->add($focus->id);
592
593                 header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=".$_REQUEST['inbound_email_id']."&parent_id=".$email->parent_id."&parent_type=".$email->parent_type.'&start='.$_REQUEST['start'].'&assigned_user_id='.$current_user->id);
594                 exit();
595         }
596         ////    END INBOUND EMAIL HANDLING
597         ///////////////////////////////////////////////////////////////////////////////
598
599         $focus->save($check_notify);
600         $return_id = $focus->id;
601     
602         $GLOBALS['log']->debug("Saved record with id of ".$return_id);
603
604     if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') {
605         $json = getJSONobj();
606         echo $json->encode(array('status' => 'success',
607                                  'get' => ''));
608         $trackerManager = TrackerManager::getInstance();
609         $timeStamp = TimeDate::getInstance()->nowDb();
610         if($monitor = $trackerManager->getMonitor('tracker')){ 
611                 $monitor->setValue('action', 'detailview');
612                 $monitor->setValue('user_id', $GLOBALS['current_user']->id);
613                 $monitor->setValue('module_name', 'Contacts');
614                 $monitor->setValue('date_modified', $timeStamp);
615                 $monitor->setValue('visible', 1);
616         
617                 if (!empty($this->bean->id)) {
618                     $monitor->setValue('item_id', $return_id);
619                     $monitor->setValue('item_summary', $focus->get_summary_text());
620                 }
621                         $trackerManager->saveMonitor($monitor, true, true);
622                 }
623         return null;
624     }
625
626         if(isset($_POST['popup']) && $_POST['popup'] == 'true') {
627                 $get = '&module=';
628                 if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
629                 else $get .= 'Contacts';
630                 $get .= '&action=';
631                 if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
632                 else $get .= 'Popup';
633                 if(!empty($_POST['return_id'])) $get .= '&return_id='.$_POST['return_id'];
634                 if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
635                 if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
636                 if(!empty($_POST['to_pdf'])) $get .= '&to_pdf='.$_POST['to_pdf'];
637                 $get .= '&first_name=' . urlencode($focus->first_name);
638                 $get .= '&last_name=' . urlencode($focus->last_name);
639                 $get .= '&query=true';
640                 header("Location: index.php?$get");
641                 return;
642         }
643
644         if($redirect){
645                 $this->handleRedirect($return_id);
646         }else{
647                 return $focus;
648         }
649 }
650
651 function handleRedirect($return_id){
652         if(isset($_POST['return_module']) && $_POST['return_module'] != "") {
653                 $return_module = $_POST['return_module'];
654         }
655         else {
656                 $return_module = "Contacts";
657         }
658
659         if(isset($_POST['return_action']) && $_POST['return_action'] != "") {
660                 if($_REQUEST['return_module'] == 'Emails') {
661                         $return_action = $_REQUEST['return_action'];
662                 }
663                 // if we create a new record "Save", we want to redirect to the DetailView
664                 elseif($_REQUEST['action'] == "Save" && $_REQUEST['return_module'] != "Home") {
665                         $return_action = 'DetailView';
666                 } else {
667                         // if we "Cancel", we go back to the list view.
668                         $return_action = $_REQUEST['return_action'];
669                 }
670         }
671         else {
672                 $return_action = "DetailView";
673         }
674
675         if(isset($_POST['return_id']) && $_POST['return_id'] != "") {
676         $return_id = $_POST['return_id'];
677         }
678
679         //eggsurplus Bug 23816: maintain VCR after an edit/save. If it is a duplicate then don't worry about it. The offset is now worthless.
680         $redirect_url = "index.php?action=$return_action&module=$return_module&record=$return_id";
681         if(isset($_REQUEST['offset']) && empty($_REQUEST['duplicateSave'])) {
682             $redirect_url .= "&offset=".$_REQUEST['offset'];
683         }
684
685     if(!empty($_REQUEST['ajax_load'])){
686         echo "<script>SUGAR.ajaxUI.loadContent('$redirect_url');</script>\n";
687     }
688     else {
689         header("Location: ". $redirect_url);
690     }
691 }
692
693 }
694
695
696 ?>