* SPDX-License-Identifier: AGPL-3.0-only
************************************/
/* crmv@25356 crmv@44037 crmv@2051m crmv@49001 crmv@55137 */
global $table_prefix;
global $log;
global $app_strings;
global $app_list_strings;
global $mod_strings;
global $current_user;
global $currentModule;
global $default_charset;
$focus = CRMEntity::getInstance($currentModule);
$smarty = new VteSmarty();
$json = new Zend_Json();
$focus->setSignatureId();
$send_mode = 'single'; //crmv@26639
$send_action = $_REQUEST['message_mode']; //crmv@2963m
$relation = $_REQUEST['relation'];
$account = $_REQUEST['account'];
if($_REQUEST['upload_error'] == true)
{
echo '
The selected file has no data or a invalid file.
';
}
//Email Error handling
if($_REQUEST['mail_error'] != '') {
require_once("modules/Emails/mail.php");
echo parseEmailErrorString($_REQUEST['mail_error']);
}
//added to select the module in combobox of compose-popup
if(isset($_REQUEST['par_module']) && $_REQUEST['par_module']!='') {
$smarty->assign('select_module',vtlib_purify($_REQUEST['par_module']));
}
elseif(isset($_REQUEST['pmodule']) && $_REQUEST['pmodule']!='') {
$smarty->assign('select_module',vtlib_purify($_REQUEST['pmodule']));
}
// crmv@43147
if ($_REQUEST['mode'] == 'share' && !empty($_REQUEST['record'])) {
$CU = CRMVUtils::getInstance();
$data = $CU->getSharedEmailTemplate(intval($_REQUEST['record']));
$focus->column_fields['subject'] = $data['subject'];
$focus->column_fields['description'] = $data['body'];
$smarty->assign('SUBJECT',$data['subject']);
$smarty->assign('DESCRIPTION',$data['body']);
}
// crmv@43147e
//crmv@2963m
$focusMessages = CRMEntity::getInstance('Messages');
$message = $_REQUEST['message'];
if(isset($message) && $message != '') {
$focusMessages->id = $message;
$focusMessages->retrieve_entity_info($message,'Messages');
$account = $focusMessages->column_fields['account'];
$focusMessages->column_fields['mto'] = str_replace('undisclosed-recipients:;','',$focusMessages->column_fields['mto']);
$recipients = $focusMessages->getRecipientEmails(); //crmv@44429
switch ($send_action) {
case 'forward':
$focus->mode = '';
$focus->column_fields['subject'] = 'Fwd: '.$focusMessages->column_fields['subject'];
$focus->addSignature();
$focus->column_fields['description'] .= '
'.$focusMessages->column_fields['description'].''; $specialFolders = $focusMessages->getSpecialFolders(false); if (!empty($specialFolders['Sent']) && $focusMessages->column_fields['folder'] == $specialFolders['Sent']) { $idlists = $focusMessages->getRecipients('string'); $to_tmp = $focusMessages->column_fields['mto']; if (substr_count($focusMessages->column_fields['mto_f'],',') > substr_count($to_tmp,',')) { $to_tmp = trim($focusMessages->column_fields['mto_f']); } $addressInfo = setAddressInfo($idlists,explode(',',$to_tmp),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$idlists); } else { // crmv@128409 crmv@200330 if (!empty($focusMessages->column_fields['mreplyto_f'])) $arr = $focusMessages->parseAddressList($focusMessages->column_fields['mreplyto_f']); else $arr = $focusMessages->parseAddressList($focusMessages->column_fields['mfrom_f']); $smarty->assign('OTHER_TO_MAIL', (!empty($arr[0]['name'])) ? "\"{$arr[0]['name']}\" <{$arr[0]['email']}>" : $arr[0]['email']); // crmv@191584 // crmv@128409e crmv@200330e if ($send_action == 'reply_all') { //crmv@44429 $cc_tmp = $recipients['mto']; if (!empty($recipients['mcc'])) { $cc_tmp = array_merge($cc_tmp,$recipients['mcc']); } //crmv@44429e $from_email_tmp = $focus->getFromEmailList('',$account); foreach($from_email_tmp as $t) { if ($t['selected'] == 'selected') { $from_email_tmp = $t['email']; } } foreach($cc_tmp as $i => $t) { if (empty($t) || trim($t) == $from_email_tmp) { unset($cc_tmp[$i]); } else { trim($cc_tmp[$i]); } } } if (!empty($cc_tmp)) { // crmv@191584 $cc_mail = array(); foreach($cc_tmp as $name => $email) { ($name != $email) ? $cc_mail[] = "\"{$name}\" <{$email}>" : $cc_mail[] = $email; } $smarty->assign('CC_MAIL', trim(implode(', ',$cc_mail))); // crmv@191584e } } if (!empty($focusMessages->column_fields['send_mode'])) { $send_mode = $focusMessages->column_fields['send_mode']; } break; case 'draft': $focus->mode = ''; $focus->column_fields['subject'] = $focusMessages->column_fields['subject']; $focus->column_fields['description'] = $focusMessages->column_fields['description']; $idlists = $focusMessages->getRecipients('string'); $to_tmp = $focusMessages->column_fields['mto']; if (substr_count($focusMessages->column_fields['mto_f'],',') > substr_count($to_tmp,',')) { $to_tmp = trim($focusMessages->column_fields['mto_f']); } $addressInfo = setAddressInfo($idlists,explode(',',$to_tmp),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$idlists); $smarty->assign('CC_MAIL', $focusMessages->column_fields['mcc']); $smarty->assign('BCC_MAIL', $focusMessages->column_fields['mbcc']); $attachment_links = array(); $att_ar = $focusMessages->getAttachments(); foreach ($att_ar as $att) { if (!empty($att['target'])) { $target = "target='{$att['target']}'"; } $attachment_links[] = "{$att['name']}"; } $smarty->assign('webmail_attachments',$attachment_links); $result_draft = $adb->pquery("SELECT id FROM {$table_prefix}_messages_drafts WHERE userid = ? AND messagehash = ?",array($current_user->id,$focusMessages->column_fields['messagehash'])); if ($result_draft && $adb->num_rows($result_draft) > 0) { $draftid = $adb->query_result($result_draft,0,'id'); } // retrieve related entities $rm = RelationManager::getInstance(); $excludeMods = array('ModComments', 'Campaigns'); $ids = $rm->getRelatedIds('Messages', $focusMessages->id, null, $excludeMods); if (!empty($ids)) { $relation = implode('|',$ids); } if (!empty($focusMessages->column_fields['send_mode'])) { $send_mode = $focusMessages->column_fields['send_mode']; } break; } //crmv@2963me //crmv@48167 } elseif(isset($_REQUEST['sendmail']) && $_REQUEST['sendmail'] !='') { $relation = $_REQUEST['pid']; $mailids = get_to_emailids($_REQUEST['pmodule']); if (!empty($mailids['mailds'])) { $to_add = trim($mailids['mailds'],",").","; $addressInfo = setAddressInfo($mailids['idlists'],explode(',',$to_add),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$mailids['idlists']); } elseif (!empty($relation) && strpos($relation,',') === false) { //crmv@82025 $rm = RelationManager::getInstance(); $relatedPersons = $rm->getRelatedIds($_REQUEST['pmodule'], $relation, array('Contacts','Accounts','Leads','Vendors')); if (!empty($relatedPersons)) { foreach($relatedPersons as $rp) { $relation .= ",$rp"; //crmv@86302 // I found the first fieldid with not empty value $rp_module = getSalesEntityType($rp); $rp_focus = CRMEntity::getInstance($rp_module); $rp_instance = Vtecrm_Module::getInstance($rp_module); $res = $adb->pquery("select fieldid, fieldname, fieldlabel, columnname, tablename from {$table_prefix}_field where tabid=? and uitype=13 and presence in (0,2)", array($rp_instance->id)); $otherreturnvalue = Array(); while($row=$adb->fetchByAssoc($res)) { $permit = getFieldVisibilityPermission($rp_module, $userid, $row["fieldname"]); if($permit == '0') { $val = getSingleFieldValue($row['tablename'],$row['columnname'],$rp_focus->tab_name_index[$row['tablename']],$rp); if (!empty($val)) { $fieldid = $row['fieldid']; break(2); } } } } if (!empty($fieldid)) { saveListViewCheck($rp_module,$rp); $_REQUEST["field_lists"] = $fieldid; $mailids = get_to_emailids($rp_module); $to_add = trim($mailids['mailds'],",").","; $addressInfo = setAddressInfo($mailids['idlists'],explode(',',$to_add),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$mailids['idlists']); } } } $focus->addSignature(); //crmv@26111e $focus->mode = ''; //crmv@48167e //crmv@2043m } elseif(isset($_REQUEST['reply_mail_converter']) && $_REQUEST['reply_mail_converter'] != '') { $HelpDeskFocus = CRMEntity::getInstance('HelpDesk'); $HelpDeskFocus->retrieve_entity_info($_REQUEST['reply_mail_converter_record'],'HelpDesk'); $_REQUEST["field_lists"] = implode(':',getFieldList(getSalesEntityType($HelpDeskFocus->column_fields['parent_id']))); // crmv@140928 $parentModule = getSalesEntityType($HelpDeskFocus->column_fields['parent_id']); if ($parentModule) { saveListViewCheck($parentModule,$HelpDeskFocus->column_fields['parent_id']); } // crmv@140928e $mailids = get_to_emailids(getSalesEntityType($HelpDeskFocus->column_fields['parent_id'])); $addressInfo = setAddressInfo($mailids['idlists'],array(),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); if ($addressInfo['to_mail'] != '') { $smarty->assign('TO_MAIL',$addressInfo['to_mail']); } elseif ($mailids['idlists'] != '') { $tmp1 = explode('|',$mailids['idlists']); if ($tmp1[0] != '') { $tmp2 = explode('@',$tmp1[0]); if (!empty($tmp2)) { $smarty->assign('TO_MAIL',getFieldValue($tmp2[1], $tmp2[0])); } } } $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$mailids['idlists']); $focus->mode = ''; $focus->column_fields['subject'] = 'Re: '.$HelpDeskFocus->column_fields['ticket_title'].' - Ticket Id: '.$_REQUEST['reply_mail_converter_record']; if ($_REQUEST['reply_mail_user'] != 'mailconverter') { $focus->addSignature(); } if(isset($_REQUEST['reply_mail_converter']) && $_REQUEST['reply_mail_converter'] != '') { if ($_REQUEST['reply_mail_user'] == 'mailconverter') { $from_email = $HelpDeskFocus->column_fields['helpdesk_from']; } else { $from_email = $current_user->column_fields['email1']; } } //crmv@2043me } else { //crmv@78538 $relation = $_REQUEST['pid']; if (!empty($relation) && strpos($relation,',') === false) { //crmv@82025 $rm = RelationManager::getInstance(); $relatedRecords = $rm->getRelatedIds($_REQUEST['pmodule'], $relation, array('Contacts','Accounts','Leads','Vendors')); if (!empty($relatedRecords)) { foreach($relatedRecords as $rp) { $relation .= ",$rp"; //crmv@86302 // I found the first fieldid with not empty value $rp_module = getSalesEntityType($rp); $rp_focus = CRMEntity::getInstance($rp_module); $rp_instance = Vtecrm_Module::getInstance($rp_module); $res = $adb->pquery("select fieldid, fieldname, fieldlabel, columnname, tablename from {$table_prefix}_field where tabid=? and uitype=13 and presence in (0,2)", array($rp_instance->id)); $otherreturnvalue = Array(); while($row=$adb->fetchByAssoc($res)) { $permit = getFieldVisibilityPermission($rp_module, $userid, $row["fieldname"]); if($permit == '0') { $val = getSingleFieldValue($row['tablename'],$row['columnname'],$rp_focus->tab_name_index[$row['tablename']],$rp); if (!empty($val)) { $fieldid = $row['fieldid']; break(2); } } } } if (!empty($fieldid)) { saveListViewCheck($rp_module,$rp); $_REQUEST["field_lists"] = $fieldid; $mailids = get_to_emailids($rp_module); $to_add = trim($mailids['mailds'],",").","; $addressInfo = setAddressInfo($mailids['idlists'],explode(',',$to_add),true); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $smarty->assign('IDLISTS',$mailids['idlists']); } } } //crmv@78538e $focus->addSignature(); } if ($from_email == '') { $from_email = $focus->column_fields['from_email']; } if (empty($account)) { //crmv@57983 $main_account = $focusMessages->getMainUserAccount(); $account = $main_account['id']; } $from_email_list = $focus->getFromEmailList($from_email,$account); foreach($from_email_list as $e) { if ($e['selected'] == 'selected') { $signature_account = $e['account']; break; } } if (empty($signature_account)) { foreach($from_email_list as $e) { $signature_account = $e['account']; break; } } $smarty->assign('FROM_EMAIL_LIST',$from_email_list); $smarty->assign('SIGNATUREID',$focus->signatureId); $smarty->assign('SIGNATURE',$focusMessages->getAccountSignature($signature_account)); // INTERNAL MAILER if($_REQUEST["internal_mailer"] == "true") { $smarty->assign('INT_MAILER',"true"); $rec_type = $_REQUEST["type"]; $rec_id = $_REQUEST["rec_id"]; $fieldname = $_REQUEST["fieldname"]; //added for getting list-ids to compose email popup from list view(Accounts,Contacts,Leads) if(isset($_REQUEST['field_id']) && strlen($_REQUEST['field_id']) != 0) { if($_REQUEST['par_module'] == "Users") $id_list = $_REQUEST['rec_id'].'@'.'-1|'; else $id_list = $_REQUEST['rec_id'].'@'.$_REQUEST['field_id'].'|'; $smarty->assign("IDLISTS", $id_list); } if($rec_type == "record_id") { $type = $_REQUEST['par_module']; //check added for email link in user detail view $normal_tabs = Array('Users'=>$table_prefix.'_users', 'Leads'=>$table_prefix.'_leaddetails', 'Contacts'=>$table_prefix.'_contactdetails', 'Accounts'=>$table_prefix.'_account', 'Vendors'=>$table_prefix.'_vendor'); $cf_tabs = Array('Accounts'=>$table_prefix.'_accountscf', 'Campaigns'=>$table_prefix.'_campaignscf', 'Contacts'=>$table_prefix.'_contactscf', 'Invoice'=>$table_prefix.'_invoicecf', 'Leads'=>$table_prefix.'_leadscf', 'Potentials'=>$table_prefix.'_potentialscf', 'Products'=>$table_prefix.'_productcf', 'PurchaseOrder'=>$table_prefix.'_purchaseordercf', 'Quotes'=>$table_prefix.'_quotescf', 'SalesOrder'=>$table_prefix.'_salesordercf', 'HelpDesk'=>$table_prefix.'_ticketcf', 'Vendors'=>$table_prefix.'_vendorcf'); if(substr($fieldname,0,2)=="cf") $tablename = $cf_tabs[$type]; else $tablename = $normal_tabs[$type]; if($type == "Users") $q = "select $fieldname from $tablename where id=?"; elseif($type == "Leads") $q = "select $fieldname from $tablename where leadid=?"; elseif ($type == "Contacts") $q = "select $fieldname from $tablename where contactid=?"; elseif ($type == "Accounts") $q = "select $fieldname from $tablename where accountid=?"; elseif ($type == "Vendors") $q = "select $fieldname from $tablename where vendorid=?"; else { // vtlib customization: Support for email-type custom field for other modules. //crmv@21474 $module_focus = CRMEntity::getInstance($type); // crmv@187823 $finfo = getFieldTableAndColumn($type,$fieldname); $tablename = $finfo['tablename']; $columnname = $finfo['columnname']; $tablename_index = $module_focus->tab_name_index[$tablename]; $q = "select $columnname as $fieldname from $tablename where $tablename_index = ?"; // crmv@187823e //crmv@21474e // END } $email1 = $adb->query_result($adb->pquery($q, array($rec_id)),0,$fieldname); } elseif ($rec_type == "email_addy") { $email1 = $_REQUEST["email_addy"]; } $addressInfo = setAddressInfo($id_list,explode(',',$email1)); $smarty->assign('AUTOSUGGEST',$addressInfo['autosuggest']); $smarty->assign('TO_MAIL',$addressInfo['to_mail']); $smarty->assign('OTHER_TO_MAIL',$addressInfo['other_to_mail']); $focus->addSignature(); } //Added to set the cc when click reply all if(isset($_REQUEST['msg_cc']) && $_REQUEST['msg_cc'] != '') { $smarty->assign("MAIL_MSG_CC", $_REQUEST['msg_cc']); } if (!empty($relation)) { $links = array(); //crmv@60186 $ids = null; if (strpos($relation,'|') !== false) { $ids = array_filter(explode('|', $relation)); } elseif (strpos($relation,',') !== false) { $ids = array_filter(explode(',', $relation)); } if (is_array($ids)) { //crmv@60186e foreach ($ids as $relid) { list($elid, $fieldid) = explode('@', $relid, 2); if (strpos($elid,'x') !== false) { $elid = explode('x',$elid); $elid = $elid[1]; } $l = $focusMessages->getEntityPreview($elid); if ($l && (isPermitted($l['module'], 'DetailView', $l['id']) == 'yes' || $l['module'] != 'Documents')) $links[$elid] = $l; //crmv@193042 } } else { $l = $focusMessages->getEntityPreview($relation); if ($l && (isPermitted($l['module'], 'DetailView', $l['id']) == 'yes' || $l['module'] != 'Documents')) $links[$relation] = $l; //crmv@193042 } $smarty->assign('LINKS_STR',implode('|',array_keys($links))); $smarty->assign('LINKS',$links); } //crmv@25391 crmv@80155 $smarty->assign('USE_SIGNATURE',1); $result = $adb->pquery('SELECT templateid,subject,body,use_signature,overwrite_message FROM '.$table_prefix.'_emailtemplates WHERE templatename = ?',array($_REQUEST['templatename'])); if ($result && $adb->num_rows($result)>0) { $subject = $adb->query_result_no_html($result,0,'subject'); $body = $adb->query_result_no_html($result,0,'body'); $overwrite_message = $adb->query_result_no_html($result,0,'overwrite_message'); $use_signature = $adb->query_result_no_html($result,0,'use_signature'); if (intval($overwrite_message) == 0) { $body = "