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