openstamanager/mail.php

288 lines
10 KiB
PHP
Raw Normal View History

2018-02-20 17:57:16 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-02-20 17:57:16 +01:00
2024-03-05 16:01:45 +01:00
use Models\Module;
2024-03-22 15:52:24 +01:00
use Modules\Emails\Template;
2019-08-26 18:02:05 +02:00
2018-02-20 17:57:16 +01:00
include_once __DIR__.'/core.php';
2019-08-29 10:25:14 +02:00
$template = Template::find(get('id'));
2019-08-26 18:02:05 +02:00
$module = $template->module;
$smtp = $template->account;
2018-02-20 17:57:16 +01:00
2021-01-28 21:13:48 +01:00
$placeholder_options = [
'is_pec' => intval($smtp['pec']),
];
2024-05-21 18:03:38 +02:00
$body = $module->replacePlaceholders($id_record, $template->getTranslation('body'), $placeholder_options);
$subject = $module->replacePlaceholders($id_record, $template->getTranslation('subject'), $placeholder_options);
2022-02-28 11:38:04 +01:00
$emails = [];
if ($module->replacePlaceholders($id_record, '{email}')) {
2021-01-28 21:13:48 +01:00
$emails = explode(';', $module->replacePlaceholders($id_record, '{email}', $placeholder_options));
2023-08-04 14:54:28 +02:00
}
2022-02-28 11:38:04 +01:00
2021-01-28 21:13:48 +01:00
$id_anagrafica = $module->replacePlaceholders($id_record, '{id_anagrafica}', $placeholder_options);
2019-07-24 17:54:50 +02:00
// Aggiungo email referenti in base alla mansione impostata nel template
2023-09-15 18:06:15 +02:00
$mansioni = $dbo->select('em_mansioni_template', 'idmansione', [], ['id_template' => $template->id]);
foreach ($mansioni as $mansione) {
2022-02-28 11:38:04 +01:00
$referenti = $dbo->table('an_referenti')->where('idmansione', $mansione['idmansione'])->where('idanagrafica', $id_anagrafica)->where('email', '!=', '')->get();
foreach ($referenti as $referente) {
2022-02-28 11:38:04 +01:00
if (!in_array($referente->email, $emails)) {
$emails[] = $referente->email;
}
}
}
// Aggiungo email tecnici assegnati quando sono sul template Notifica intervento
2024-04-18 17:44:05 +02:00
if ($template->getTranslation('title') == 'Notifica intervento') {
2023-09-15 18:06:15 +02:00
$tecnici = $dbo->select('in_interventi_tecnici_assegnati', 'id_tecnico', [], ['id_intervento' => $id_record]);
foreach ($tecnici as $tecnico) {
$anagrafica = $dbo->table('an_anagrafiche')->where('idanagrafica', $tecnico['id_tecnico'])->where('email', '!=', '')->first();
if (!in_array($anagrafica->email, $emails)) {
$emails[] = $anagrafica->email;
2023-08-04 14:54:28 +02:00
}
}
}
2018-04-15 14:46:33 +02:00
// Campi mancanti
$campi_mancanti = [];
2018-04-15 14:46:33 +02:00
if (empty($smtp['from_address'])) {
$campi_mancanti[] = tr('Mittente');
}
2018-04-15 14:46:33 +02:00
if (empty($smtp['server'])) {
$campi_mancanti[] = tr('Server SMTP');
}
2018-04-15 14:46:33 +02:00
if (empty($smtp['port'])) {
$campi_mancanti[] = tr('Porta');
}
2018-04-15 14:46:33 +02:00
if (sizeof($campi_mancanti) > 0) {
2018-07-10 17:07:27 +02:00
echo '
<div class="alert alert-warning">
<i class="fa fa-warning"></i> '.tr("Prima di procedere all'invio completa: _VALUES_", [
2024-03-22 15:52:24 +01:00
'_VALUES_' => '<b>'.implode(', ', $campi_mancanti).'</b>',
2018-07-10 17:07:27 +02:00
]).'<br/>
'.Modules::link('Account email', $smtp['id'], tr('Vai alla scheda account email'), null).'
</div>';
}
// Form
2018-02-20 17:57:16 +01:00
echo '
<form action="" method="post" id="email-form">
2018-02-20 17:57:16 +01:00
<input type="hidden" name="op" value="send-email">
2019-09-11 18:11:19 +02:00
<input type="hidden" name="backto" value="'.(get('back') ? get('back') : 'record-edit').'">
2020-03-30 18:48:43 +02:00
2019-09-11 18:11:19 +02:00
<input type="hidden" name="id_module" value="'.$id_module.'">
<input type="hidden" name="id_record" value="'.$id_record.'">
2018-02-20 17:57:16 +01:00
<input type="hidden" name="template" value="'.$template['id'].'">
<p><b>'.tr('Mittente').'</b>: '.$smtp['from_name'].' &lt;'.$smtp['from_address'].'&gt;</p>';
2018-02-20 17:57:16 +01:00
2018-07-10 16:50:23 +02:00
if (!empty($template['cc'])) {
echo '
2018-07-10 16:50:23 +02:00
<p><b>'.tr('CC').'</b>: '.$template['cc'].'</p>';
}
2018-02-20 17:57:16 +01:00
2018-07-10 16:50:23 +02:00
if (!empty($template['bcc'])) {
2018-02-20 17:57:16 +01:00
echo '
2018-07-10 16:50:23 +02:00
<p><b>'.tr('CCN').'</b>: '.$template['bcc'].'</p>';
2018-02-20 17:57:16 +01:00
}
echo '
2020-01-22 10:49:44 +01:00
<b>'.tr('Destinatari').' <span class="tip" title="'.tr('Email delle sedi, dei referenti o agente collegato all\'anagrafica.').'"><i class="fa fa-question-circle-o"></i></span></b>
<div class="row" id="lista-destinatari">';
2024-01-15 15:30:45 +01:00
$idx = 0;
2024-01-15 15:30:45 +01:00
foreach ($emails as $email) {
echo '
<div class="col-md-12">
2024-10-30 12:16:43 +01:00
{[ "type": "text", "name": "destinatari['.$idx++.']", "value": "'.$email.'", "icon-before": "choice|email|'.$template['type'].'", "extra": "onkeyup=\'aggiungiDestinatario();\'", "class": "destinatari email-mask", "required": 0 ]}
</div>';
2024-01-15 15:30:45 +01:00
}
2022-02-28 11:38:04 +01:00
2024-01-15 15:30:45 +01:00
if (empty($emails)) {
echo '
2022-02-28 11:38:04 +01:00
<div class="col-md-12">
2024-10-30 12:16:43 +01:00
{[ "type": "text", "name": "destinatari['.$idx++.']", "value": "", "icon-before": "choice|email|'.$template['type'].'", "extra": "onkeyup=\'aggiungiDestinatario();\'", "class": "destinatari email-mask", "required": 0 ]}
2022-02-28 11:38:04 +01:00
</div>';
2024-01-15 15:30:45 +01:00
}
echo '
</div>
2018-02-20 17:57:16 +01:00
<br>
2018-02-20 17:57:16 +01:00
<div class="row">
<div class="col-md-8">
2024-11-18 11:52:12 +01:00
{[ "type": "text", "label": "'.tr('Oggetto').'", "name": "subject", "value": "'.$subject.'", "required": 1 ]}
2018-02-20 17:57:16 +01:00
</div>
<div class="col-md-4">
2018-12-28 00:04:41 +01:00
{[ "type": "checkbox", "label": "'.tr('Richiedi notifica di lettura').'", "name": "read_notify", "value": "'.$template['read_notify'].'" ]}
</div>
</div>';
2018-02-20 17:57:16 +01:00
// Stampe
2019-08-29 10:25:14 +02:00
$selected_prints = $dbo->fetchArray('SELECT id_print FROM em_print_template WHERE id_template = '.prepare($template['id']));
$selected = array_column($selected_prints, 'id_print');
2018-02-20 17:57:16 +01:00
echo '
2018-02-20 17:57:16 +01:00
<div class="row">
<div class="col-md-6">
2024-03-22 15:52:24 +01:00
{[ "type": "select", "multiple": "1", "label": "'.tr('Stampe').'", "name": "prints[]", "value": "'.implode(',', $selected).'", "values": "query=SELECT `zz_prints`.`id`, `title` AS text FROM `zz_prints` LEFT JOIN `zz_prints_lang` ON (`zz_prints`.`id` = `zz_prints_lang`.`id_record` AND `zz_prints_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') WHERE `id_module` = '.prepare($id_module).' AND `enabled`=1 AND `is_record`=1", "link": "stampa" ]}
</div>';
2018-02-20 17:57:16 +01:00
2019-08-29 10:25:14 +02:00
$uploads = [];
2024-04-18 17:44:05 +02:00
if ($smtp['pec'] == 1 && $module->getTranslation('title') == 'Fatture di vendita') {
2019-08-29 10:25:14 +02:00
$uploads = $dbo->fetchArray('SELECT id FROM zz_files WHERE id_module = '.prepare($module['id']).' AND id_record = '.prepare($id_record).' AND category = \'Fattura Elettronica\'');
$uploads = array_column($uploads, 'id');
}
// Allegati
echo '
<div class="col-md-6">
2024-06-11 16:58:27 +02:00
{[ "type": "select", "multiple": "1", "label": "'.tr('Allegati').'", "name": "uploads[]", "value": "'.implode(',', $uploads).'", "help": "'.tr('Allegati del documento o caricati nell\'anagrafica dell\'azienda.').'", "values": "query=SELECT `id`, `name` AS text FROM `zz_files` WHERE `id_module` = '.prepare($id_module).' AND `id_record` = '.prepare($id_record).' UNION SELECT `id`, CONCAT(`name`, \' (Azienda)\') AS text FROM `zz_files` WHERE `id_module` = '.Module::where('name', 'Anagrafiche')->first()->id.' AND `id_record` = '.setting('Azienda predefinita').'", "link": "allegato" ]}
</div>
</div>';
2018-02-20 17:57:16 +01:00
echo '
<div class="row">
2023-02-24 13:36:37 +01:00
<div class="col-md-12">';
2024-01-15 15:30:45 +01:00
echo input([
'type' => 'ckeditor',
'use_full_ckeditor' => 1,
2024-01-15 15:30:45 +01:00
'label' => tr('Contenuto'),
'name' => 'body',
'id' => 'body_'.rand(0, 999),
'value' => $body,
]);
echo '
2023-02-24 13:36:37 +01:00
</div>
</div>';
2018-02-20 17:57:16 +01:00
echo '
2018-02-20 17:57:16 +01:00
<!-- PULSANTI -->
<div class="row">
<div class="col-md-12 text-right">
2021-04-26 09:53:41 +02:00
<button type="button" class="btn btn-primary" onclick="inviaEmail()"><i class="fa fa-envelope"></i> '.tr('Invia').'</button>
2018-02-20 17:57:16 +01:00
</div>
</div>
</form>';
echo '
2021-01-08 14:41:17 +01:00
<div class="hidden" id="template-destinatario">
<div class="col-md-12">
2024-10-30 12:16:43 +01:00
{[ "type": "text", "name": "destinatari[-id-]", "icon-before": "choice|email|'.$template['type'].'", "extra": "onkeyup=\'aggiungiDestinatario();\'", "class": "destinatari email-mask" ]}
</div>
</div>';
2018-02-20 17:57:16 +01:00
echo '
<script>
var emails = [];
var id_anagrafica = "'.$id_anagrafica.'";
var pec = "'.$smtp['pec'].'";
var id_record = "'.$id_record.'";
var id_module = "'.$id_module.'";
var id_template = "'.$template->id.'";
$(document).ready(function() {
// Auto-completamento destinatario
if (id_anagrafica) {
$(document).load(globals.rootdir + "/ajax_complete.php?module=Anagrafiche&op=get_email&id_anagrafica=" + id_anagrafica + "&id_record=" + id_record + "&id_module=" + id_module + "&id_template=" + id_template + (pec ? "&type=pec" : ""), function(response) {
emails = JSON.parse(response);
2021-11-05 16:13:29 +01:00
let num = 0;
$(".destinatari").each(function(){
addAutoComplete(this);
2021-11-05 16:13:29 +01:00
if (num++==0) {
$(this).prop("required", true);
}
});
aggiungiDestinatario();
});
}';
2024-09-10 12:38:16 +02:00
if (!empty($template['indirizzi_proposti'])) {
echo '
$(document).load(globals.rootdir + "/modules/emails/ajax/complete.php?op=get_email&indirizzi_proposti='.$template['indirizzi_proposti'].'", function(response) {
emails = JSON.parse(response);
console.log(emails);
$(".destinatari").each(function(){
addAutoComplete(this);
});
aggiungiDestinatario();
});';
2024-09-10 12:38:16 +02:00
}
echo '
2018-02-20 17:57:16 +01:00
});
2021-04-26 09:53:41 +02:00
function inviaEmail() {
const form = $("#email-form");
if (form.parsley().validate() && confirm("Inviare e-mail?")) {
form.submit();
}
}
function addAutoComplete(input) {
autocomplete({
2021-04-26 09:53:41 +02:00
minLength: 0,
input: input,
emptyMsg: globals.translations.noResults,
fetch: function (text, update) {
text = text.toLowerCase();
const suggestions = emails.filter(n => n.value.toLowerCase().startsWith(text));
update(suggestions);
},
onSelect: function (item) {
input.value = item.value;
2021-04-26 09:53:41 +02:00
aggiungiDestinatario();
},
});
}
function aggiungiDestinatario() {
const last = $("#lista-destinatari input").last();
2019-07-26 17:40:52 +02:00
if (last.val()) {
2023-07-11 10:10:35 +02:00
const nuovaRiga = aggiungiContenuto("#lista-destinatari > div:last-of-type", "#template-destinatario", {"-id-": $("#lista-destinatari > div").length});
nuovaRiga.find(".destinatari").each(function(){
2021-04-26 09:53:41 +02:00
addAutoComplete(this);
});
}
}
2018-02-20 17:57:16 +01:00
</script>';
echo '
2019-07-26 17:40:52 +02:00
<script>$(document).ready(init)</script>';