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