Miglioramento log email

This commit is contained in:
Thomas Zilio 2018-11-23 15:17:52 +01:00
parent 6f2786b00e
commit b57c615cad
4 changed files with 92 additions and 41 deletions

View File

@ -95,36 +95,14 @@ if (filter('op') == 'link_file' || filter('op') == 'unlink_file') {
// Allegati originali
$files = post('attachments');
if (!empty($files)) {
// Allegati del record
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
foreach ($attachments as $attachment) {
$mail->addAttachment($upload_dir.'/'.$attachment['filename']);
}
// Allegati dell'Azienda predefinita
$anagrafiche = Modules::get('Anagrafiche');
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module != '.prepare($id_module));
$directory = DOCROOT.'/'.Uploads::getDirectory($anagrafiche['id']);
foreach ($attachments as $attachment) {
$mail->addAttachment($directory.'/'.$attachment['filename']);
}
foreach ($files as $file) {
$mail->addUpload($file);
}
// Invio mail
try {
$mail->send(true); // Il valore true impone la gestione degli errori tramite eccezioni
// Informazioni di log
Filter::set('get', 'id_email', $id_template);
Filter::set('get', 'operations_options', [
'receivers' => $receivers,
'prints' => post('prints'),
'attachments' => post('attachments'),
]);
flash()->info(tr('Email inviata correttamente!'));
} catch (PHPMailer\PHPMailer\Exception $e) {
flash()->error(tr("Errore durante l'invio dell'email").': '.$e->errorMessage());

View File

@ -210,7 +210,6 @@ function translateTemplate()
$id_record = filter('id_record');
$id_parent = filter('id_parent');
$id_email = filter('id_email');
$info = filter('operations_options');
$id_module = Modules::getCurrent()['id'];
$id_plugin = Plugins::getCurrent()['id'];
@ -224,18 +223,8 @@ function translateTemplate()
$template = \HTMLBuilder\HTMLBuilder::replace($template);
// Informazioni estese sulle azioni dell'utente
if (Auth::check() && !empty(post('op'))) {
$database = database();
$database->insert('zz_operations', [
'id_module' => $id_module,
'id_record' => $id_record,
'id_plugin' => !empty($id_plugin) ? $id_plugin : null,
'id_email' => !empty($id_email) ? $id_email : null,
'id_utente' => Auth::user()['id'],
'op' => post('op'),
'options' => !empty($info) ? json_encode($info) : null,
]);
if (!empty(post('op'))) {
operationLog(post('op'));
}
// Retrocompatibilità
@ -321,6 +310,8 @@ function redirectOperation($id_module, $id_record)
*
* @param string $string
*
* @since 2.3
*
* @return string
*/
function prepareToField($string)
@ -331,6 +322,8 @@ function prepareToField($string)
/**
* Restituisce se l'user-agent (browser web) è una versione mobile.
*
* @since 2.3
*
* @return bool
*/
function isMobile()
@ -341,6 +334,8 @@ function isMobile()
/**
* Restituisce il percorso derivante dal file in esecuzione.
*
* @since 2.4.1
*
* @return string
*/
function getURLPath()
@ -360,6 +355,8 @@ function getURLPath()
/**
* Sostituisce i caratteri speciali per la ricerca attraverso le tabelle Datatables.
*
* @since 2.4.2
*
* @param string $field
*
* @return string
@ -368,3 +365,38 @@ function searchFieldName($field)
{
return str_replace([' ', '.'], ['-', ''], $field);
}
/**
* Registra un'azione specifica nei log.
*
* @since 2.4.3
*
* @param string $operation
* @param int $id_record
* @param int $id_module
* @param int $id_plugin
* @param int $id_parent
* @param int $id_email
* @param array $options
* @return void
*/
function operationLog($operation, array $ids = [], array $options = [])
{
if (!Auth::check()) {
return false;
}
$ids['id_module'] = $ids['id_module'] ?: Modules::getCurrent()['id'];
$ids['id_plugin'] = $ids['id_plugin'] ?: Plugins::getCurrent()['id'];
$ids['id_record'] = $ids['id_record'] ?: filter('id_record');
//$ids['id_parent'] = $ids['id_parent'] ?: filter('id_parent');
database()->insert('zz_operations', array_merge($ids, [
'op' => $operation,
'id_utente' => Auth::user()['id'],
'options' => !empty($options) ? json_encode($options) : null,
]));
return true;
}

View File

@ -312,7 +312,7 @@ switch (post('op')) {
if (!empty(post('import'))) {
// Replicazione delle righe del preventivo sul documento
$righe = $dbo->fetchArray('SELECT idarticolo, idiva, desc_iva, iva, iva_indetraibile, descrizione, subtotale, um, qta, sconto, sconto_unitario, tipo_sconto, IFNULL( (SELECT mg_articoli.abilita_serial FROM mg_articoli WHERE mg_articoli.id=co_righe_preventivi.idarticolo), 0 ) AS abilita_serial FROM co_righe_preventivi WHERE idpreventivo='.prepare($idpreventivo));
$righe = $dbo->fetchArray('SELECT idarticolo, idiva, desc_iva, iva, iva_indetraibile, descrizione, subtotale, um, qta, sconto, sconto_unitario, tipo_sconto, is_descrizione, IFNULL( (SELECT mg_articoli.abilita_serial FROM mg_articoli WHERE mg_articoli.id=co_righe_preventivi.idarticolo), 0 ) AS abilita_serial FROM co_righe_preventivi WHERE idpreventivo='.prepare($idpreventivo));
foreach ($righe as $key => $riga) {
$subtot = $riga['subtotale'];
@ -339,6 +339,7 @@ switch (post('op')) {
'subtotale' => $riga['subtotale'],
'um' => $riga['um'],
'qta' => $riga['qta'],
'is_descrizione' => $riga['is_descrizione'],
'sconto' => $riga['sconto'],
'sconto_unitario' => $riga['sconto_unitario'],
'tipo_sconto' => $riga['tipo_sconto'],

View File

@ -4,6 +4,7 @@ namespace Notifications;
use Mail;
use Prints;
use Uploads;
class EmailNotification extends Notification
{
@ -14,6 +15,8 @@ class EmailNotification extends Notification
protected $account = null;
protected $attachments = null;
protected $logs = [];
/**
* Restituisce l'account email della notifica.
*
@ -116,10 +119,24 @@ class EmailNotification extends Notification
}
}
/**
* Aggiunge un allegato del gestionale alla notifica.
*
* @param string $file_id
*/
public function addUpload($file_id)
{
$attachment = database()->fetchOne('SELECT * FROM zz_files WHERE id = '.prepare($file_id));
$this->addAttachment(DOCROOT.'/'.Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename']);
$this->logs['attachments'][] = $attachment['id'];
}
/**
* Aggiunge un allegato alla notifica.
*
* @param string $value
* @param string $path
* @param string $name
*/
public function addAttachment($path, $name = null)
{
@ -150,6 +167,8 @@ class EmailNotification extends Notification
Prints::render($print['id'], $id_record, $path);
$this->addAttachment($path, $name);
$this->logs['prints'][] = $print['id'];
}
/**
@ -200,16 +219,22 @@ class EmailNotification extends Notification
*/
public function addReceiver($value, $type = null)
{
if (empty($value)) {
return;
}
$this->receivers[] = [
'email' => $value,
'type' => $type,
];
$this->logs['receivers'][] = $value;
}
public function send($exceptions = false)
{
$account = $this->getAccount();
$mail = new Mail($account['id'], $exceptions);
$mail = new Mail($account['id'], true);
// Template
$template = $this->getTemplate();
@ -240,6 +265,21 @@ class EmailNotification extends Notification
// Contenuto
$mail->Body = $this->getContent();
return $mail->send();
// Invio mail
try {
$mail->send();
operationLog('send-email', [
'id_email' => $template['id'],
], $this->logs);
return true;
} catch (PHPMailer\PHPMailer\Exception $e) {
if ($exceptions) {
throw $e;
}
return false;
}
}
}