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