1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-06-05 22:09:38 +02:00

Aggiornamento log email

This commit is contained in:
Thomas Zilio
2019-08-28 16:58:47 +02:00
parent 47ef54ca65
commit 1eb60486fb
20 changed files with 179 additions and 82 deletions

View File

@@ -4,6 +4,7 @@ include_once __DIR__.'/core.php';
use Models\MailTemplate;
use Models\Note;
use Models\OperationLog;
use Modules\Checklists\Check;
use Modules\Checklists\Checklist;
@@ -241,6 +242,8 @@ elseif (post('op') == 'send-email') {
}
$mail->save();
OperationLog::setInfo('id_email', $mail->id);
}
// Inclusione di eventuale plugin personalizzato

View File

@@ -15,7 +15,7 @@ switch ($op) {
$_SESSION['keep_alive'] = true;
// Rimozione log vecchi
$dbo->query('DELETE FROM `zz_operations` WHERE DATE_ADD(`created_at`, INTERVAL 30*24*60*60 SECOND) <= NOW()');
//$dbo->query('DELETE FROM `zz_operations` WHERE DATE_ADD(`created_at`, INTERVAL 30*24*60*60 SECOND) <= NOW()');
} else {
$status = auth()->getCurrentStatus();

View File

@@ -6,6 +6,8 @@
* @since 2.3
*/
use Models\OperationLog;
/**
* Esegue il redirect.
*
@@ -194,27 +196,32 @@ function translateTemplate()
{
$id_record = filter('id_record');
$id_parent = filter('id_parent');
$id_email = filter('id_email');
$id_module = Modules::getCurrent()['id'];
$id_plugin = Plugins::getCurrent()['id'];
$template = ob_get_clean();
$template = str_replace('$id_module$', $id_module, $template);
$template = str_replace('$id_plugin$', $id_plugin, $template);
$template = str_replace('$id_record$', $id_record, $template);
$replaces = [
'$id_module$' => $id_module,
'$id_plugin$' => $id_plugin,
'$id_record$' => $id_record,
];
$template = replace($template, $replaces);
$template = \HTMLBuilder\HTMLBuilder::replace($template);
$template = str_replace('$id_module$', $id_module, $template);
$template = str_replace('$id_plugin$', $id_plugin, $template);
$template = str_replace('$id_record$', $id_record, $template);
$template = replace($template, $replaces);
// Informazioni estese sulle azioni dell'utente
if (!empty(post('op')) && post('op') != 'send-email') {
operationLog(post('op'));
$op = post('op');
if (!empty($op)) {
OperationLog::setInfo('id_module', $id_module);
OperationLog::setInfo('id_plugin', $id_plugin);
OperationLog::setInfo('id_record', $id_record);
OperationLog::build($op);
}
//dd("afssa", $op, $_POST);exit();
// Retrocompatibilità
if (!empty($_SESSION['infos'])) {
@@ -356,40 +363,6 @@ 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
*/
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;
}
/**
* Rimuove spazi e caratteri speciali da una stringa.
*

View File

@@ -164,6 +164,7 @@ switch (post('op')) {
$serials[] = $serial;
}
// no break
case 'add_serials':
$serials = $serials ?: filter('serials');
@@ -182,7 +183,7 @@ switch (post('op')) {
]));
if ($count != $totale) {
flash()->warning(tr('Alcuni seriali erano già presenti') . '...');
flash()->warning(tr('Alcuni seriali erano già presenti').'...');
}
break;

View File

@@ -259,13 +259,13 @@ $(document).ready(function() {
function addSerial(form_id, numero) {
if (numero > 0){
swal({
title: "' . tr('Nuovi seriali') . '",
html: "' . tr("Confermi l'inserimento di _NUM_ nuovi seriali?", [
title: "'.tr('Nuovi seriali').'",
html: "'.tr("Confermi l'inserimento di _NUM_ nuovi seriali?", [
'_NUM_' => '" + numero + "',
]) . '",
]).'",
type: "success",
showCancelButton: true,
confirmButtonText: "' . tr('Continua') . '"
confirmButtonText: "'.tr('Continua').'"
}).then(function (result) {
$(form_id).submit();
})

View File

@@ -52,7 +52,7 @@ class EmailHook extends Manager
$diff = date('Y-m-d', strtotime('-4 hours'));
$user = auth()->getUser();
$failed = function($query) use ($diff) {
$failed = function ($query) use ($diff) {
$query->whereDate('failed_at', '<', $diff)
->orWhereNull('failed_at');
};

View File

@@ -141,7 +141,7 @@ switch (post('op')) {
if ($result) {
++$added;
operationLog('export-xml-bulk', ['id_record' => $r['id']]);
//operationLog('export-xml-bulk', ['id_record' => $r['id']]);
} else {
$failed[] = $fattura->numero_esterno;
}

View File

@@ -31,7 +31,7 @@ elseif ($r['idconto_fornitore'] != '') {
$conto_descrizione = $dbo->fetchOne('SELECT CONCAT ((SELECT numero FROM co_pianodeiconti2 WHERE id=co_pianodeiconti3.idpianodeiconti2), ".", numero, " ", descrizione) AS descrizione FROM co_pianodeiconti3 WHERE id='.prepare($conto))['descrizione'];
}
$r_user = $dbo->fetchOne('SELECT * FROM an_anagrafiche WHERE idanagrafica='.Auth::user()['idanagrafica']);
$r_user = $dbo->fetchOne('SELECT * FROM an_anagrafiche WHERE idanagrafica='.prepare(Auth::user()['idanagrafica']));
$r_company = $dbo->fetchOne('SELECT * FROM an_anagrafiche WHERE idanagrafica='.prepare(setting('Azienda predefinita')));
// Variabili da sostituire

View File

@@ -13,7 +13,6 @@ use Modules\Interventi\Components\Sessione;
use Modules\Interventi\Intervento;
use Modules\Interventi\Stato;
use Modules\TipiIntervento\Tipo as TipoSessione;
use Notifications\EmailNotification;
switch (post('op')) {
case 'update':

View File

@@ -10,7 +10,6 @@ use Modules\Fatture\Components\Riga;
use Modules\Fatture\Fattura;
use Modules\Interventi\Components\Sessione;
use Modules\Interventi\Intervento;
use Notifications\EmailNotification;
/**
* Recupera il totale delle ore spese per un intervento.

View File

@@ -41,6 +41,8 @@ switch (filter('op')) {
$anagrafiche = $newsletter->anagrafiche;
$template = $newsletter->template;
$uploads = $newsletter->uploads()->pluck('id');
foreach ($anagrafiche as $anagrafica) {
if (empty($anagrafica['email'])) {
continue;
@@ -52,9 +54,15 @@ switch (filter('op')) {
$mail->subject = $newsletter->subject;
$mail->content = $newsletter->content;
$mail->id_campaign = $newsletter->id;
foreach ($uploads as $upload) {
$mail->addAttachment($upload);
}
$mail->save();
$newsletter->anagrafiche()->updateExistingPivot($anagrafica->id, ['id_email' => $mail->id], false);
$newsletter->anagrafiche()->updateExistingPivot($anagrafica->id, ['id_email' => $mail->id]);
}
$newsletter->state = 'WAIT';

View File

@@ -5,7 +5,7 @@ if ($newsletter->state == 'DEV') {
<button type="button" class="btn btn-primary ask" data-msg="'.tr('Procedere ad inviare la newsletter?').'" data-op="send" data-button="'.tr('Invia').'" data-class="btn btn-lg btn-warning">
<i class="fa fa-envelope"></i> '.tr('Invia newsletter').'
</button>';
} else if ($newsletter->state == 'WAIT') {
} elseif ($newsletter->state == 'WAIT') {
echo '
<button type="button" class="btn btn-danger ask" data-msg="'.tr('Svuotare la coda di invio della newsletter?').'" data-op="block" data-button="'.tr('Svuota').'" data-class="btn btn-lg btn-warning">
<i class="fa fa-envelope"></i> '.tr('Svuota coda di invio').'

View File

@@ -49,7 +49,7 @@ echo '
</div>
<div class="col-md-6">
{[ "type": "timestamp", "label": "'.tr('Data di completamento').'", "name": "completed_at", "value": "$completed_at$" ]}
{[ "type": "timestamp", "label": "'.tr('Data di completamento').'", "name": "completed_at", "value": "$completed_at$", "disabled": 1 ]}
</div>
</div>
@@ -108,9 +108,9 @@ if (!$anagrafiche->isEmpty()) {
foreach ($anagrafiche as $anagrafica) {
$mail_id = $anagrafica->pivot->id_email;
if (!empty($mail_id)) {
$data = Mail::find($mail_id)->sent_at;
$data = timestampFormat($data);
$mail = Mail::find($mail_id);
if (!empty($mail) && !empty($mail->sent_at)) {
$data = timestampFormat($mail->sent_at);
} else {
$data = tr('Non ancora inviata');
}
@@ -140,6 +140,8 @@ if (!$anagrafiche->isEmpty()) {
</div>
</form>
{( "name": "filelist_and_upload", "id_module": "$id_module$", "id_record": "$id_record$" )}
<a class="btn btn-danger ask" data-backto="record-list">
<i class="fa fa-trash"></i> '.tr('Elimina').'
</a>';

View File

@@ -9,10 +9,12 @@ use Models\MailAccount;
use Models\MailTemplate;
use Models\User;
use Modules\Anagrafiche\Anagrafica;
use Traits\RecordTrait;
class Newsletter extends Model
{
use SoftDeletes;
use RecordTrait;
protected $table = 'em_campaigns';
@@ -34,6 +36,33 @@ class Newsletter extends Model
return $model;
}
/**
* Restituisce il nome del modulo a cui l'oggetto è collegato.
*
* @return string
*/
public function getModuleAttribute()
{
return 'Newsletter';
}
public function fixStato()
{
$mails = $this->emails;
$completed = true;
foreach ($mails as $mail) {
if (empty($mail->sent_at)) {
$completed = false;
break;
}
}
$this->state = $completed ? 'OK' : $this->state;
$this->completed_at = $completed ? date('Y-m-d H:i:s') : $this->completed_at;
$this->save();
}
// Relazione Eloquent
public function anagrafiche()
@@ -43,7 +72,7 @@ class Newsletter extends Model
public function emails()
{
return $this->belongsToMany(Mail::class, 'em_campaign_anagrafica', 'id_campaign', 'id_email')->withPivot('id_anagrafica');
return $this->hasMany(Mail::class, 'id_campaign');
}
public function account()

View File

@@ -1327,7 +1327,7 @@ class FatturaElettronica
//In caso di acquisto di prodotti da un agricolo in regime agevolato (art. 34, comma 6, del d.P.R. n. 633/72) da parte di un operatore IVA obbligato alla FE, quest'ultimo emetterà una FE usando la tipologia "TD01" per conto dell'agricoltore venditore
if ($fattura->getDocumento()['is_fattura_conto_terzi']) {
$result['TerzoIntermediarioOSoggettoEmittente'] = [
'DatiAnagrafici' => static::getDatiAnagrafici(static::getAzienda())
'DatiAnagrafici' => static::getDatiAnagrafici(static::getAzienda()),
];
//1.6 Cessionario/Committente

View File

@@ -2,6 +2,7 @@
namespace HTMLBuilder\Manager;
use Models\Mail;
use Prints;
use Translator;
@@ -25,9 +26,11 @@ class EmailManager implements ManagerInterface
$database = database();
// Visualizzo il log delle operazioni di invio email
$operations = $database->fetchArray('SELECT created_at, options, (SELECT name FROM em_templates WHERE id = id_email) AS email, (SELECT username FROM zz_users WHERE id = id_utente) AS user FROM zz_operations WHERE id_record = '.prepare($options['id_record']).' AND id_module = '.prepare($options['id_module'])." AND op = 'send-email' AND id_email IS NOT NULL ORDER BY created_at DESC");
$emails = Mail::whereRaw('id IN (SELECT id_email FROM zz_operations WHERE id_record = '.prepare($options['id_record']).' AND id_module = '.prepare($options['id_module']).' AND id_email IS NOT NULL)')
->orderByDesc('created_at')
->get();
if (empty($operations)) {
if ($emails->isEmpty()) {
return ' ';
}
@@ -36,7 +39,7 @@ class EmailManager implements ManagerInterface
<div class="box box-info collapsable collapsed-box">
<div class="box-header with-border">
<h3 class="box-title"><i class="fa fa-envelope"></i> '.tr('Email inviate: _NUM_', [
'_NUM_' => count($operations),
'_NUM_' => $emails->count(),
]).'</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-plus"></i></button>
@@ -45,32 +48,36 @@ class EmailManager implements ManagerInterface
<div class="box-body">
<ul>';
foreach ($operations as $operation) {
$options = json_decode($operation['options'], true);
$receivers = $options['receivers'];
foreach ($emails as $operation) {
$receivers = array_column($operation->receivers, 'email');
$prints = [];
foreach ($options['prints'] as $print) {
$print = Prints::get($print);
$list = $operation->prints;
foreach ($list as $print) {
$print = Prints::get($print['id']);
$prints[] = $print['title'];
}
$attachments = [];
foreach ($options['attachments'] as $attachment) {
$list = $operation->attachments;
foreach ($list as $attachment) {
$attachment = $database->selectOne('zz_files', '*', ['id' => $attachment]);
$attachments[] = $attachment['name'];
}
$sent = !empty($operation['sent_at']) ? tr('inviata il _DATE_ alle _HOUR_', [
'_DATE_' => dateFormat($operation['sent_at']),
'_HOUR_' => timeFormat($operation['sent_at']),
]) : tr('in coda di invio') ;
$result .= '
<li>
'.tr('Email "_EMAIL_" inviata il _DATE_ alle _HOUR_ da _USER_', [
'_EMAIL_' => $operation['email'],
'_DATE_' => Translator::dateToLocale($operation['created_at']),
'_HOUR_' => Translator::timeToLocale($operation['created_at']),
'_USER_' => $operation['user'],
]).'.
'.tr('Email "_EMAIL_" da _USER_', [
'_EMAIL_' => $operation->template->name,
'_USER_' => $operation->user->username,
]).' ('.$sent.').
<ul>
<li><b>'.tr('Destinatari').'</b>: '.implode(', ', $receivers).'.</li>';

View File

@@ -3,12 +3,13 @@
namespace Models;
use Common\Model;
use Modules\Newsletter\Newsletter;
class Mail extends Model
{
protected $table = 'em_emails';
protected $receivers = [];
protected $receivers = null;
protected $attachments = null;
protected $prints = null;
@@ -128,6 +129,11 @@ class Mail extends Model
$this->setOptionsAttribute($this->options);
}
$newsletter = $this->newsletter;
if (!empty($newsletter)) {
$newsletter->fixStato();
}
return parent::save($options);
}
@@ -219,12 +225,12 @@ class Mail extends Model
public function account()
{
return $this->belongsTo(MailAccount::class, 'id_account');
return $this->belongsTo(MailAccount::class, 'id_account')->withTrashed();
}
public function template()
{
return $this->belongsTo(MailTemplate::class, 'id_template');
return $this->belongsTo(MailTemplate::class, 'id_template')->withTrashed();
}
public function user()
@@ -232,6 +238,11 @@ class Mail extends Model
return $this->belongsTo(User::class, 'created_by');
}
public function newsletter()
{
return $this->belongsTo(Newsletter::class, 'id_campaign');
}
protected function resetFromTemplate()
{
$template = $this->template;

View File

@@ -32,6 +32,6 @@ class MailTemplate extends Model
public function account()
{
return $this->belongsTo(MailAccount::class, 'id_smtp');
return $this->belongsTo(MailAccount::class, 'id_smtp')->withTrashed();
}
}

View File

@@ -8,6 +8,36 @@ class OperationLog extends Model
{
protected $table = 'zz_operations';
protected static $info = [];
public static function setInfo($name, $value){
self::$info[$name] = $value;
}
public static function getInfo($name){
return self::$info[$name];
}
public static function build($operation)
{
if (!\Auth::check()) {
return null;
}
$model = parent::build();
foreach (self::$info as $key => $value){
$model->{$key} = $value;
}
$model->op = $operation;
$model->id_utente = \Auth::user()->id;
$model->save();
return $model;
}
/* Relazioni Eloquent */
public function user()
@@ -24,4 +54,9 @@ class OperationLog extends Model
{
return $this->belongsTo(Module::class, 'id_module');
}
public function email()
{
return $this->belongsTo(Mail::class, 'id_email');
}
}

View File

@@ -1,5 +1,35 @@
<?php
// Correzione zz_operations
use Models\Mail;
use Models\MailTemplate;
use Models\User;
$database->query('ALTER TABLE `zz_operations` DROP FOREIGN KEY `zz_operations_ibfk_3`');
$logs = $database->fetchArray("SELECT * FROM `zz_operations` WHERE `op` = 'send-email'");
foreach ($logs as $log) {
$user = User::find($log['id_utente']);
$template = MailTemplate::find($log['id_email']);
$mail = Mail::build($user, $template, $log['id_record']);
$options = json_decode($log['options'], true);
$mail->attachments = $options['attachments'] ?: [];
$mail->prints = $options['prints'] ?: [];
foreach ($options['receivers'] as $receiver){
$mail->addReceiver($receiver);
}
$mail->created_at = $log['created_at'];
$mail->sent_at = $log['created_at'];
$mail->save();
$database->query('UPDATE `zz_operations` SET `id_email` = '.prepare($mail->id).' WHERE `id_module` = '.prepare($log['id_module']).' AND `id_email` = '.prepare($log['id_email']).' AND `id_record` = '.prepare($log['id_record']).' AND `options` = '.prepare($log['options']).' AND `created_at` = '.prepare($log['created_at']));
}
$database->query('ALTER TABLE `zz_operations` ADD FOREIGN KEY (`id_email`) REFERENCES `em_emails`(`id`) ON DELETE SET NULL');
// File e cartelle deprecate
$files = [
'src\API.php',