Miglioramento dell'invio email

Miglioramento dell'invio email, con aggiunta di autocompletamento degli indirizzi e miglioramenti grafici.
This commit is contained in:
Thomas Zilio 2018-02-21 17:47:43 +01:00
parent b09e739890
commit 9df593466c
8 changed files with 278 additions and 159 deletions

View File

@ -217,47 +217,57 @@ if (filter('op') == 'link_file' || filter('op') == 'unlink_file') {
} elseif (filter('op') == 'send-email') {
$template = Mail::getTemplate($post['template']);
$final_attachments = [];
$attachments = [];
$prints = Prints::getModulePrints($id_module);
foreach ($prints as $print) {
if (!empty($post['print-'.$print['id']])) {
$filename = $upload_dir.'/'.$print['title'].' - '.$id_record.'.pdf';
// Stampe
foreach ($post['prints'] as $print) {
$print = Prints::get($print);
Prints::render($print['id'], $id_record, $filename);
$filename = $upload_dir.'/'.$print['title'].' - '.$id_record.'.pdf';
$final_attachments[] = [
'path' => $filename,
'name' => $print['title'],
];
}
Prints::render($print['id'], $id_record, $filename);
$attachments[] = [
'path' => $filename,
'name' => $print['title'],
];
}
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
foreach ($attachments as $attachment) {
if (!empty($post['attachment-'.$attachment['id']])) {
$final_attachments[] = [
'path' => $upload_dir.'/'.$attachment['filename'],
'name' => $attachment['nome'],
];
}
// Allegati del record
$selected = [];
if (!empty($post['attachments'])) {
$selected = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode($post['attachments']).') AND id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
}
foreach ($selected as $attachment) {
$attachments[] = [
'path' => $upload_dir.'/'.$attachment['filename'],
'name' => $attachment['nome'],
];
}
// Allegati dell'Azienda predefinita
$anagrafiche = Modules::get('Anagrafiche');
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module = '.prepare($anagrafiche['id'])." AND id_record = (SELECT valore FROM zz_settings WHERE nome = 'Azienda predefinita')");
foreach ($attachments as $attachment) {
if (!empty($post['default-'.$attachment['id']])) {
$final_attachments[] = [
'path' => DOCROOT.'/files/'.$anagrafiche['directory'].'/'.$attachment['filename'],
'name' => $attachment['nome'],
];
}
$selected = [];
if (!empty($post['attachments'])) {
$selected = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $post['attachments']).') AND id_module != '.prepare($id_module));
}
foreach ($selected as $attachment) {
$attachments[] = [
'path' => DOCROOT.'/files/'.$anagrafiche['directory'].'/'.$attachment['filename'],
'name' => $attachment['nome'],
];
}
// Preparazione email
$mail = new Mail();
$mail->AddAddress($post['email']);
// Conferma di lettura
if (!empty($post['read_notify'])) {
$mail->ConfirmReadingTo = $mail->From;
}
// Reply To
if (!empty($template['reply_to'])) {
@ -274,6 +284,32 @@ if (filter('op') == 'link_file' || filter('op') == 'unlink_file') {
$mail->AddBCC($template['bcc']);
}
// Destinatari
foreach ($post['destinatari'] as $key => $destinatario) {
$type = $post['tipo_destinatari'][$key];
$pieces = explode('<', $destinatario);
$count = count($pieces);
$name = null;
if ($count > 1) {
$email = substr(end($pieces), 0, -1);
$name = substr($destinatario, 0, strpos($destinatario, '<'.$email));
} else {
$email = $destinatario;
}
if (!empty($email)) {
if ($type == 'a') {
$mail->AddAddress($email, $name);
} elseif ($type == 'cc') {
$mail->AddCC($email, $name);
} elseif ($type == 'bcc') {
$mail->AddBCC($email, $name);
}
}
}
// Oggetto
$mail->Subject = $post['subject'];
@ -282,14 +318,18 @@ if (filter('op') == 'link_file' || filter('op') == 'unlink_file') {
$mail->AddAttachment($attachment['path'], $attachment['name']);
}
// Contenuto
$mail->Body = $post['body'];
// Invio mail
if (!$mail->send()) {
$_SESSION['errors'][] = tr("Errore durante l'invio della segnalazione").': '.$mail->ErrorInfo;
$_SESSION['errors'][] = tr("Errore durante l'invio dell'email").': '.$mail->ErrorInfo;
} else {
$_SESSION['infos'][] = tr('Email inviata correttamente!');
}
redirect(ROOTDIR.'/editor.php?id_module='.$id_module.'&id_record='.$id_record);
exit();
}
if (Modules::getPermission($permesso) == 'r' || Modules::getPermission($permesso) == 'rw') {

View File

@ -75,6 +75,7 @@ a.disabled {
}
.ui-autocomplete {
z-index: 10000;
min-width: 160px;
padding: 10px;
margin: 2px;
@ -274,7 +275,6 @@ span.form-control {
margin-top: 0;
}
/* Aggiustamenti per i widget */
.widget li {
@ -301,7 +301,6 @@ span.form-control {
opacity: 0.1;
}
/* Tooltip dark */
.ui-tooltip {
@ -448,12 +447,11 @@ span.form-control {
margin: 5px;
}
/* Personalizzazione del plugin Select2 */
.select2-search,
.select2-search__field {
width: 100%
width: 100% !important
}
.select2-results__option[aria-selected=true] {
@ -476,7 +474,6 @@ span.form-control {
border-radius: 0px;
}
/* Interventi da pianificare in Dashboard */
#external-events .fc-event {
@ -519,11 +516,9 @@ input.small-width {
width: 90%;
margin-top: 20px
}
.main-header .logo {
display: none;
}
/*
.content-wrapper,
.right-side,
@ -554,7 +549,6 @@ input.small-width {
.push {
position: static;
}
.signature-pad {
top: 0;
left: 0;

208
mail.php
View File

@ -4,141 +4,149 @@ include_once __DIR__.'/core.php';
$template = Mail::getTemplate($get['id']);
$module = Modules::get($id_module);
$smtp = Mail::get($template['id_smtp']);
$body = $template['body'];
$subject = $template['subject'];
$variables = Mail::getTemplateVariables($template);
$variables = Mail::getTemplateVariables($template['id'], $id_record);
$email = $variables['email'];
// Sostituzione delle variabili di base
$replaces = [];
foreach ($variables as $key => $value) {
$replaces['{'.$key.'}'] = $value;
}
$body = str_replace(array_keys($replaces), array_values($replaces), $body);
$subject = str_replace(array_keys($replaces), array_values($replaces), $subject);
// Form
echo '
<form action="" method="post" id="add-form">
<form action="" method="post" id="email-form">
<input type="hidden" name="op" value="send-email">
<input type="hidden" name="backto" value="record-edit">
<input type="hidden" name="template" value="'.$template['id'].'">
<!-- Dati -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">'.tr('Dati').'</h3>
</div>
<p><b>'.tr('Mittente').'</b>: '.$smtp['from_name'].' &lt;'.$smtp['from_address'].'&gt;</p>';
<div class="panel-body">
<div class="row">
<div class="col-md-6">
{[ "type": "text", "label": "'.tr('Oggetto').'", "name": "subject", "value": "'.$subject.'", "required": 1 ]}
</div>
if (!empty($smtp['cc'])) {
echo '
<p><b>'.tr('CC').'</b>: '.$smtp['cc'].'</p>';
}
<div class="col-md-6">
{[ "type": "email", "label": "'.tr('Destinatario').'", "name": "email", "value": "'.$email.'", "required": 1 ]}
</div>
</div>
if (!empty($smtp['cc'])) {
echo '
<p><b>'.tr('CCN').'</b>: '.$smtp['bcc'].'</p>';
}
<div class="row">
<div class="col-md-12">
{[ "type": "textarea", "label": "'.tr('Contenuto').'", "name": "body", "value": '.json_encode($body).' ]}
</div>
</div>
echo '
<b>'.tr('Destinatari').'</b>
<div class="row" id="lista-destinatari">
<div class="col-md-12">
{[ "type": "email", "name": "destinatari[]", "value": "'.$email.'", "icon-before": "choice|email", "extra": "onkeyup=\'aggiungi_destinatario();\'", "class": "destinatari", "required": 1 ]}
</div>
</div>
<br>
<div class="row">
<div class="col-md-8">
{[ "type": "text", "label": "'.tr('Oggetto').'", "name": "subject", "value": "'.$subject.'", "required": 1 ]}
</div>
<div class="col-md-4">
{[ "type": "checkbox", "label": "'.tr('Notifica di lettura').'", "name": "read_notify", "value": "'.$template['read_notify'].'" ]}
</div>
</div>';
// Stampe
$selected_prints = $dbo->fetchArray('SELECT id_print FROM zz_email_print WHERE id_email = '.prepare($template['id']));
$selected = array_column($selected_prints, 'id_print');
echo '
<div class="row">
<div class="col-md-6">
{[ "type": "select", "multiple": "1", "label": "'.tr('Stampe').'", "name": "prints[]", "value": "'.implode(',', $selected).'", "values": "query=SELECT id, title AS text FROM zz_prints WHERE id_module = '.prepare($id_module).'" ]}
</div>';
// Allegati
echo '
<div class="col-md-6">
{[ "type": "select", "multiple": "1", "label": "'.tr('Allegati').'", "name": "attachments[]", "values": "query=SELECT id, nome AS text FROM zz_files WHERE id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record)." UNION SELECT id, CONCAT(nome, ' (Azienda)') AS text FROM zz_files WHERE id_module = ".prepare(Modules::get('Anagrafiche')['id'])." AND id_record = (SELECT valore FROM zz_settings WHERE nome = 'Azienda predefinita')\" ]}
</div>
</div>";
echo '
<div class="row">
<div class="col-md-12">
{[ "type": "textarea", "label": "'.tr('Contenuto').'", "name": "body", "value": '.json_encode($body).' ]}
</div>
</div>';
echo '
<div class="row">
<!-- Stampe -->
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">'.tr('Stampe').'</h3>
</div>
<div class="panel-body">';
$selected_prints = $dbo->fetchArray('SELECT id_print FROM zz_email_print WHERE id_email = '.prepare($template['id']));
$selected = array_column($selected_prints, 'id_print');
$prints = Prints::getModulePrints($id_module);
foreach ($prints as $print) {
echo '
{[ "type": "checkbox", "label": "'.$print['title'].'", "name": "print-'.$print['id'].'", "value": "'.in_array($print['id'], $selected).'" ]}';
}
echo '
</div>
</div>
</div>
<!-- Allegati -->
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">'.tr('Allegati').'</h3>
</div>
<div class="panel-body">';
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
if (empty($attachments)) {
echo '
<p>'.tr('Nessun allegato disponibile').'.</p>';
}
foreach ($attachments as $attachment) {
echo '
{[ "type": "checkbox", "label": "'.$attachment['nome'].'", "name": "attachment-'.$attachment['id'].'" ]}';
}
echo '
</div>
</div>
</div>
<!-- Anagrafica -->
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">'.tr('Anagrafica').'</h3>
</div>
<div class="panel-body">';
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module = '.prepare(Modules::get('Anagrafiche')['id'])." AND id_record = (SELECT valore FROM zz_settings WHERE nome = 'Azienda predefinita')");
if (empty($attachments)) {
echo '
<p>'.tr('Nessun allegato disponibile').'.</p>';
}
foreach ($attachments as $attachment) {
echo '
{[ "type": "checkbox", "label": "'.$attachment['nome'].'", "name": "default-'.$attachment['id'].'" ]}';
}
echo '
</div>
</div>
</div>
</div>
<!-- PULSANTI -->
<div class="row">
<div class="col-md-12 text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-envelope"></i> '.tr('Invia').'</button>
<button type="button" class="btn btn-primary" onclick="send()"><i class="fa fa-envelope"></i> '.tr('Invia').'</button>
</div>
</div>
</form>';
echo '
<div id="destinatari_input" class="hide">
<div class="col-md-12">
{[ "type": "email", "name": "destinatari[]", "icon-before": "choice|email|cc", "extra": "onkeyup=\'aggiungi_destinatario();\'", "class": "destinatari" ]}
</div>
</div>';
echo '
<script src="'.$rootdir.'/assets/dist/js/ckeditor/ckeditor.js"></script>';
echo '
<script>
var emails = [];
$(document).ready(function(){
// Autocompletamento destinatario
$(document).load(globals.rootdir + "/ajax_complete.php?module=Anagrafiche&op=get_email&id_anagrafica='.$variables['id_anagrafica'].'", function(response) {
emails = JSON.parse(response);
$(".destinatari").each(function(){
$(this).autocomplete({source: emails});
});
});
CKEDITOR.replace("body");
});
function send(){
if($("#email-form").parsley().validate() && confirm("Inviare e-mail?")) {
$("#email-form").submit();
}
}
function aggiungi_destinatario(){
var last = $("#lista-destinatari input").last();
if(last.val()){
$("#destinatari_input").find(".select2").remove()
$("#lista-destinatari").append($("#destinatari_input").html());
$(".destinatari").each(function(){
$(this).autocomplete({source: emails});
});
start_superselect();
}
}
</script>';
echo '

View File

@ -0,0 +1,7 @@
<?php
$rs = $dbo->fetchArray('SELECT * FROM in_interventi WHERE id='.prepare($id_record))[0];
return [
'id_anagrafica' => $rs['idanagrafica'],
];

View File

@ -0,0 +1,52 @@
<?php
include_once __DIR__.'/../../../core.php';
switch ($resource) {
// Elenco e-mail
case 'get_email':
$id_anagrafica = get('id_anagrafica');
if (!empty($id_anagrafica)) {
$where = 'AND idanagrafica = '.prepare($id_anagrafica);
}
$results = [];
// Tutti i referenti per questo cliente
$q = "SELECT DISTINCT(email), idanagrafica, nome AS ragione_sociale FROM an_referenti WHERE email != '' ".$where.' ORDER BY idanagrafica';
$rs = $dbo->fetchArray($q);
foreach ($rs as $r) {
$results[] = [
'value' => $r['email'],
'label' => $r['ragione_sociale'].' <'.$r['email'].'>',
];
}
// Tutti gli agenti
$q = "SELECT DISTINCT(email), ragione_sociale, an_anagrafiche.idanagrafica FROM an_anagrafiche INNER JOIN an_tipianagrafiche_anagrafiche ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica WHERE idtipoanagrafica = (SELECT idtipoanagrafica FROM an_tipianagrafiche WHERE descrizione='Agente') AND email != '' ORDER BY idanagrafica";
$rs = $dbo->fetchArray($q);
foreach ($rs as $r) {
$results[] = [
'value' => $r['email'],
'label' => $r['ragione_sociale'].' <'.$r['email'].'>',
];
}
// Email del cliente
$q = "SELECT DISTINCT(email), ragione_sociale, idanagrafica FROM an_anagrafiche WHERE email != '' ".$where.' ORDER BY idanagrafica';
$rs = $dbo->fetchArray($q);
foreach ($rs as $r) {
$results[] = [
'value' => $r['email'],
'label' => $r['ragione_sociale'].' <'.$r['email'].'>',
];
}
echo json_encode($results);
break;
}

View File

@ -60,14 +60,14 @@ if ($self_edit) {
echo '
<div class="row">
<div class="">
<div class="col-md-12">
{[ "type": "password", "label": "'.tr('Password').'", "name": "password1", "required": 1, "value": "" ]}
</div>
</div>';
echo '
<div class="row">
<div class="">
<div class="col-md-12">
{[ "type": "password", "label": "'.tr('Ripeti la password').'", "name": "password2", "value": "" ]}
</div>
</div>';

View File

@ -118,28 +118,43 @@ class HTMLWrapper implements WrapperInterface
{
$result = null;
$choices = [
[
'id' => 'UNT',
'descrizione' => tr('&euro;'),
],
[
'id' => 'PRC',
'descrizione' => '%',
],
];
$pieces = explode('|', $string);
$type = $pieces[1];
$value = (empty($pieces[2]) || !in_array($pieces[2], array_column($choices, 'id'))) ? 'UNT' : $pieces[2];
if ($type == 'untprc') {
$result = '{[ "type": "select", "name": "tipo_'.prepareToField($values['name']).'", "value": "'.prepareToField($value).'", "values": '.json_encode($choices).', "class": "no-search" ]}';
$result = \HTMLBuilder\HTMLBuilder::replace($result);
$choices = [
[
'id' => 'UNT',
'descrizione' => tr('&euro;'),
],
[
'id' => 'PRC',
'descrizione' => '%',
],
];
} elseif ($type == 'email') {
$choices = [
[
'id' => 'a',
'descrizione' => tr('A').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
],
[
'id' => 'cc',
'descrizione' => tr('CC').'&nbsp;&nbsp;',
],
[
'id' => 'bcc',
'descrizione' => tr('CCN'),
],
];
}
$value = (empty($pieces[2]) || !in_array($pieces[2], array_column($choices, 'id'))) ? $choices[0]['id'] : $pieces[2];
$result = '{[ "type": "select", "name": "tipo_'.prepareToField($values['name']).'", "value": "'.prepareToField($value).'", "values": '.json_encode($choices).', "class": "no-search" ]}';
$result = \HTMLBuilder\HTMLBuilder::replace($result);
return $result;
}
}

View File

@ -120,15 +120,18 @@ class Mail extends PHPMailer\PHPMailer\PHPMailer
*
* @return array
*/
public static function getTemplateVariables($template)
public static function getTemplateVariables($template, $id_record)
{
$template = self::getTemplate($template);
$module = Modules::get($template['id_module']);
$directory = DOCROOT.'/modules/'.$module['directory'].'|custom|/variables.php';
$file = DOCROOT.'/modules/'.$module['directory'].'|custom|/variables.php';
$original_file = str_replace('|custom|', '', $directory).'form.php';
$custom_file = str_replace('|custom|', '/custom', $directory).'form.php';
$original_file = str_replace('|custom|', '', $file);
$custom_file = str_replace('|custom|', '/custom', $file);
$database = Database::getConnection();
$dbo = $database;
// Lettura delle variabili nei singoli moduli
if (file_exists($custom_file)) {