query($query);
while (($row = $db->fetchByAssoc($result)) != null) {
if(!isset($rows[$row['id']])) {
$rows[]=$row;
}
}
$count = 0;
$emails = array();
$emailStr = '';
while(isset($_POST['emailAddress' . $count])) {
$emailStr .= ",'" . strtoupper(trim($_POST['emailAddress' . $count++])) . "'";
} //while
if($count > 0) {
$emailStr = substr($emailStr, 1);
$query = 'SELECT DISTINCT er.bean_id AS id FROM email_addr_bean_rel er, ' .
'email_addresses ea WHERE ea.id = er.email_address_id ' .
'AND ea.deleted = 0 AND er.deleted = 0 AND er.bean_module = \'Contacts\' ' .
'AND email_address_caps IN (' . $emailStr . ')';
$result = $db->query($query);
while (($row= $db->fetchByAssoc($result)) != null) {
if(!isset($rows[$row['id']])) {
$query2 = "SELECT id, first_name, last_name, title FROM contacts WHERE deleted = 0 AND id = '" . $row['id'] . "'";
$result2 = $db->query($query2);
$r = $db->fetchByAssoc($result2);
if(isset($r['id'])) {
$rows[]=$r;
}
} //if
}
} //if
return !empty($rows) ? $rows : null;
}
function buildTableForm($rows, $mod=''){
global $odd_bg, $even_bg, $action;
if(!empty($mod)){
global $current_language;
$mod_strings = return_module_language($current_language, $mod);
}else global $mod_strings;
global $app_strings;
$cols = sizeof($rows[0]) * 2 + 1;
if ($action != 'ShowDuplicates')
{
$form = '
| '.$mod_strings['MSG_DUPLICATE']. ' |
|
';
$form .= "";
else if (!empty($_POST['return_module']) && !empty($_POST['return_action']))
$form .= "";
else
$form .= "";
}
else
{
$form .= "
";
}
return $form;
}
function getWideFormBody($prefix, $mod='',$formname='', $contact = '', $portal = true){
if(!ACLController::checkAccess('Contacts', 'edit', true)){
return '';
}
require_once('modules/Contacts/Contact.php');
if(empty($contact)){
$contact = new Contact();
}
global $mod_strings;
$temp_strings = $mod_strings;
if(!empty($mod)){
global $current_language;
$mod_strings = return_module_language($current_language, $mod);
}
global $app_strings;
global $current_user;
global $app_list_strings;
$primary_address_country_options = get_select_options_with_id($app_list_strings['countries_dom'], $contact->primary_address_country);
$lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
$lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
$lbl_last_name = $mod_strings['LBL_LAST_NAME'];
$lbl_phone = $mod_strings['LBL_OFFICE_PHONE'];
$lbl_address = $mod_strings['LBL_PRIMARY_ADDRESS'];
if (isset($contact->assigned_user_id)) {
$user_id=$contact->assigned_user_id;
} else {
$user_id = $current_user->id;
}
//Retrieve Email address and set email1, email2
$sugarEmailAddress = new SugarEmailAddress();
$sugarEmailAddress->handleLegacyRetrieve($contact);
if(!isset($contact->email1)){
$contact->email1 = '';
}
if(!isset($contact->email2)){
$contact->email2 = '';
}
if(!isset($contact->email_opt_out)){
$contact->email_opt_out = '';
}
$lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
$salutation_options=get_select_options_with_id($app_list_strings['salutation_dom'], $contact->salutation);
if (isset($contact->lead_source)) {
$lead_source_options=get_select_options_with_id($app_list_strings['lead_source_dom'], $contact->lead_source);
} else {
$lead_source_options=get_select_options_with_id($app_list_strings['lead_source_dom'], '');
}
$form="";
if ($formname == 'ConvertProspect') {
$lead_source_label = " | ";
$lead_source_field = " | ";
} else {
$lead_source_label = "${mod_strings['LBL_LEAD_SOURCE']} | ";
$lead_source_field = " | ";
}
$form .= <<
| $lbl_first_name |
$lbl_last_name $lbl_required_symbol |
${mod_strings['LBL_TITLE']} |
${mod_strings['LBL_DEPARTMENT']} |
| |
|
|
|
| $lbl_address |
|
| ${mod_strings['LBL_CITY']} |
${mod_strings['LBL_STATE']} |
${mod_strings['LBL_POSTAL_CODE']} |
${mod_strings['LBL_COUNTRY']} |
|
|
|
|
| $lbl_phone |
${mod_strings['LBL_MOBILE_PHONE']} |
${mod_strings['LBL_FAX_PHONE']} |
${mod_strings['LBL_HOME_PHONE']} |
|
|
|
|
| ${mod_strings['LBL_OTHER_PHONE']} |
$lead_source_label
|
$lead_source_field
EOQ;
$form .= $sugarEmailAddress->getEmailAddressWidgetEditView($contact->id, $_REQUEST['action']=='ConvertLead'?'Leads':'Contacts', false, 'include/SugarEmailAddress/templates/forWideFormBodyView.tpl');
$form .= <<
${mod_strings['LBL_DESCRIPTION']} |
|
EOQ;
//carry forward custom lead fields common to contacts during Lead Conversion
$tempContact = new Contact();
if (method_exists($contact, 'convertCustomFieldsForm')) $contact->convertCustomFieldsForm($form, $tempContact, $prefix);
unset($tempContact);
$form .= <<
EOQ;
if ($portal == true){
if (isset($contact->portal_name)) {
$form.="";
} else {
$form.="";
}
if (isset($contact->portal_app)) {
$form.="";
} else {
$form.="";
}
if(!empty($contact->portal_name) && !empty($contact->portal_app)){
$form .= "";
}
if(isset($contact->portal_password)){
$form.="";
$form.="";
$form .= "";
}else{
$form.="";
$form.="";
$form .= "";
}
}
require_once('include/javascript/javascript.php');
require_once('modules/Contacts/Contact.php');
$javascript = new javascript();
$javascript->setFormName($formname);
$javascript->setSugarBean(new Contact());
$javascript->addField('email1','false',$prefix);
$javascript->addField('email2','false',$prefix);
$javascript->addRequiredFields($prefix);
$form .=$javascript->getScript();
$mod_strings = $temp_strings;
return $form;
}
function getFormBody($prefix, $mod='', $formname=''){
if(!ACLController::checkAccess('Contacts', 'edit', true)){
return '';
}
global $mod_strings;
$temp_strings = $mod_strings;
if(!empty($mod)){
global $current_language;
$mod_strings = return_module_language($current_language, $mod);
}
global $app_strings;
global $current_user;
$lbl_required_symbol = $app_strings['LBL_REQUIRED_SYMBOL'];
$lbl_first_name = $mod_strings['LBL_FIRST_NAME'];
$lbl_last_name = $mod_strings['LBL_LAST_NAME'];
$lbl_phone = $mod_strings['LBL_PHONE'];
$user_id = $current_user->id;
$lbl_email_address = $mod_strings['LBL_EMAIL_ADDRESS'];
if ($formname == 'EmailEditView')
{
$form = <<
$lbl_first_name
$lbl_last_name $lbl_required_symbol
$lbl_email_address $lbl_required_symbol
EOQ;
}
else
{
$form = <<
$lbl_first_name
$lbl_last_name $lbl_required_symbol
$lbl_phone
$lbl_email_address
EOQ;
}
require_once('include/javascript/javascript.php');
require_once('modules/Contacts/Contact.php');
$javascript = new javascript();
$javascript->setFormName($formname);
$javascript->setSugarBean(new Contact());
$javascript->addField('email1','false',$prefix);
$javascript->addRequiredFields($prefix);
$form .=$javascript->getScript();
$mod_strings = $temp_strings;
return $form;
}
function getForm($prefix, $mod=''){
if(!ACLController::checkAccess('Contacts', 'edit', true)){
return '';
}
if(!empty($mod)){
global $current_language;
$mod_strings = return_module_language($current_language, $mod);
}else global $mod_strings;
global $app_strings;
$lbl_save_button_title = $app_strings['LBL_SAVE_BUTTON_TITLE'];
$lbl_save_button_key = $app_strings['LBL_SAVE_BUTTON_KEY'];
$lbl_save_button_label = $app_strings['LBL_SAVE_BUTTON_LABEL'];
$the_form = get_left_form_header($mod_strings['LBL_NEW_FORM_TITLE']);
$the_form .= <<
EOQ;
$the_form .= $this->getFormBody($prefix,'Contacts', "${prefix}ContactSave");
$the_form .= <<
EOQ;
$the_form .= get_left_form_footer();
$the_form .= get_validate_record_js();
return $the_form;
}
function handleSave($prefix, $redirect=true, $useRequired=false){
global $theme, $current_user;
$theme_path="themes/".$theme."/";
require_once('modules/Contacts/Contact.php');
require_once ('include/utils.php');
require_once('include/formbase.php');
require_once('XTemplate/xtpl.php');
global $timedate;
$focus = new Contact();
if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))){
return null;
}
if (!empty($_POST[$prefix.'new_reports_to_id'])) {
$focus->retrieve($_POST[$prefix.'new_reports_to_id']);
$focus->reports_to_id = $_POST[$prefix.'record'];
} else {
$focus = populateFromPost($prefix, $focus);
if(isset($focus->portal_password) && $focus->portal_password != $_POST[$prefix.'old_portal_password']){
$focus->portal_password = md5($focus->portal_password);
}
if (!isset($_POST[$prefix.'email_opt_out'])) $focus->email_opt_out = 0;
if (!isset($_POST[$prefix.'do_not_call'])) $focus->do_not_call = 0;
}
if(!$focus->ACLAccess('Save')){
ACLController::displayNoAccess(true);
sugar_cleanup(true);
}
if($_REQUEST['action'] != 'BusinessCard' && $_REQUEST['action'] != 'ConvertLead' && $_REQUEST['action'] != 'ConvertProspect')
{
if ($_POST[$prefix.'sync_contact']){
$focus->contacts_users_id = $current_user->id;
}
else{
if (!isset($focus->users))
{
$focus->load_relationship('user_sync');
}
$focus->contacts_users_id = null;
$focus->user_sync->delete($focus->id, $current_user->id);
}
}
if (isset($GLOBALS['check_notify'])) {
$check_notify = $GLOBALS['check_notify'];
}
else {
$check_notify = FALSE;
}
if (empty($_POST['record']) && empty($_POST['dup_checked'])) {
$duplicateContacts = $this->checkForDuplicates($prefix);
if(isset($duplicateContacts)){
$location='module=Contacts&action=ShowDuplicates';
$get = '';
if(isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
$get .= '&inbound_email_id='.$_POST['inbound_email_id'];
}
//add all of the post fields to redirect get string
foreach ($focus->column_fields as $field)
{
if (!empty($focus->$field))
{
$get .= "&Contacts$field=".urlencode($focus->$field);
}
}
foreach ($focus->additional_column_fields as $field)
{
if (!empty($focus->$field))
{
$get .= "&Contacts$field=".urlencode($focus->$field);
}
}
if($focus->hasCustomFields()) {
foreach($focus->field_defs as $name=>$field) {
if (!empty($field['source']) && $field['source'] == 'custom_fields')
{
$get .= "&Contacts$name=".urlencode($focus->$name);
}
}
}
require_once('include/SugarEmailAddress/SugarEmailAddress.php');
$emailAddress = new SugarEmailAddress();
$get .= $emailAddress->getFormBaseURL($focus);
//create list of suspected duplicate contact id's in redirect get string
$i=0;
foreach ($duplicateContacts as $contact)
{
$get .= "&duplicate[$i]=".$contact['id'];
$i++;
}
//add return_module, return_action, and return_id to redirect get string
$get .= "&return_module=";
if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
else $get .= "Contacts";
$get .= "&return_action=";
if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
//else $get .= "DetailView";
if(!empty($_POST['return_id'])) $get .= "&return_id=".$_POST['return_id'];
if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
// for InboundEmail flow
if(!empty($_POST['start'])) $get .= '&start='.$_POST['start'];
//now redirect the post to modules/Contacts/ShowDuplicates.php
if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1')
{
ob_clean();
$json = getJSONobj();
$_SESSION['SHOW_DUPLICATES'] = $get;
echo $json->encode(array('status' => 'dupe', 'get' => $location . $get));
} else {
if(!empty($_POST['to_pdf'])) $location .= '&to_pdf='.$_POST['to_pdf'];
$_SESSION['SHOW_DUPLICATES'] = $get;
header("Location: index.php?$location");
}
return null;
}
}
global $current_user;
if(is_admin($current_user)){
if (!isset($_POST[$prefix.'portal_active'])) $focus->portal_active = '0';
//if no password is set set account to inactive for portal
if(empty($_POST[$prefix.'portal_name']))$focus->portal_active = '0';
}
///////////////////////////////////////////////////////////////////////////////
//// INBOUND EMAIL HANDLING
///////////////////////////////////////////////////////////////////////////////
if(isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) {
// fake this case like it's already saved.
$focus->save($check_notify);
require_once('modules/Emails/Email.php');
$email = new Email();
$email->retrieve($_REQUEST['inbound_email_id']);
$email->parent_type = 'Contacts';
$email->parent_id = $focus->id;
$email->assigned_user_id = $current_user->id;
$email->status = 'read';
$email->save();
$email->load_relationship('contacts');
$email->contacts->add($focus->id);
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);
exit();
}
//// END INBOUND EMAIL HANDLING
///////////////////////////////////////////////////////////////////////////////
$focus->save($check_notify);
$focus->savePositions55($_POST['position_list55']);
$return_id = $focus->id;
$GLOBALS['log']->debug("Saved record with id of ".$return_id);
if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') {
$json = getJSONobj();
echo $json->encode(array('status' => 'success',
'get' => ''));
return null;
}
if(isset($_POST['popup']) && $_POST['popup'] == 'true') {
$get = '&module=';
if(!empty($_POST['return_module'])) $get .= $_POST['return_module'];
else $get .= 'Contacts';
$get .= '&action=';
if(!empty($_POST['return_action'])) $get .= $_POST['return_action'];
else $get .= 'Popup';
if(!empty($_POST['return_id'])) $get .= '&return_id='.$_POST['return_id'];
if(!empty($_POST['popup'])) $get .= '&popup='.$_POST['popup'];
if(!empty($_POST['create'])) $get .= '&create='.$_POST['create'];
if(!empty($_POST['to_pdf'])) $get .= '&to_pdf='.$_POST['to_pdf'];
$get .= '&first_name=' . $focus->first_name;
$get .= '&last_name=' . $focus->last_name;
$get .= '&query=true';
header("Location: index.php?$get");
return;
}
if($redirect){
$this->handleRedirect($return_id);
}else{
return $focus;
}
}
function handleRedirect($return_id){
if(isset($_POST['return_module']) && $_POST['return_module'] != "") {
$return_module = $_POST['return_module'];
}
else {
$return_module = "Contacts";
}
if(isset($_POST['return_action']) && $_POST['return_action'] != "") {
if($_REQUEST['return_module'] == 'Emails') {
$return_action = $_REQUEST['return_action'];
}
// if we create a new record "Save", we want to redirect to the DetailView
elseif($_REQUEST['action'] == "Save" && $_REQUEST['return_module'] != "Home") {
$return_action = 'DetailView';
} else {
// if we "Cancel", we go back to the list view.
$return_action = $_REQUEST['return_action'];
}
}
else {
$return_action = "DetailView";
}
if(isset($_POST['return_id']) && $_POST['return_id'] != "") {
$return_id = $_POST['return_id'];
}
header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
}
}
?>