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

Miglioramento gestione destinatari per Newsletter e Liste relative

This commit is contained in:
Dasc3er
2021-08-03 14:59:42 +02:00
parent e4148b634b
commit c76eed4ff6
14 changed files with 647 additions and 363 deletions

View File

@@ -237,3 +237,8 @@ div.dataTables_wrapper {
.dataTables_info .select-info {
display: none;
}
.dataTables_processing
{
z-index: 1050
}

View File

@@ -20,7 +20,7 @@
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Modules\Newsletter\Lista;
use Modules\ListeNewsletter\Lista;
include_once __DIR__.'/../../core.php';

View File

@@ -17,9 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Models\Module;
include_once __DIR__.'/../../core.php';
@@ -49,9 +47,16 @@ echo '
<div class="row">
<div class="col-md-12">
{[ "type": "textarea", "label": "'.tr('Query dinamica').'", "name": "query", "required": 0, "value": "$query$", "help": "'.tr("La query SQL deve restituire gli identificativi delle anagrafiche da inserire nella lista, sotto un campo di nome ''id''").'. '.tr('Per esempio: _SQL_', [
'_SQL_' => 'SELECT idanagrafica AS id, \'anagrafica\' AS tipo FROM an_anagrafiche',
]).'" ]}
'.input([
'type' => 'textarea',
'label' => tr('Query dinamica'),
'name' => 'query',
'required' => 0,
'value' => $lista->query,
'help' => tr("La query SQL deve restituire gli identificativi delle anagrafiche da inserire nella lista, sotto un campo di nome ''id''").'. '.tr('Per esempio: _SQL_', [
'_SQL_' => 'SELECT idanagrafica AS id, \'Modules\\Anagrafiche\\Anagrafica\' AS tipo FROM an_anagrafiche',
]),
]).'
</div>
</div>
</div>
@@ -86,7 +91,8 @@ echo '
</div>
</form>';
$destinatari = $lista->getDestinatari();
$numero_destinatari = $lista->destinatari()->count();
$destinatari_senza_mail = $lista->getNumeroDestinatariSenzaEmail();
echo '
<!-- Destinatari -->
@@ -94,59 +100,64 @@ echo '
<div class="panel-heading">
<h3 class="panel-title">
'.tr('Destinatari').'
<span class="badge">'.$destinatari->count().'</span>
<span> ('.$numero_destinatari.')</span> <div class="pull-right" >
'.(($destinatari_senza_mail > 0) ? ' <span title="'.tr('Indirizzi e-mail mancanti').'" class="tip label label-danger clickable">'.$destinatari_senza_mail.'</span>' : '')
.'<span title="'.tr('Indirizzi e-mail senza consenso per newsletter').'" class="tip label label-warning clickable" id="numero_consenso_disabilitato"></span></div>
</h3>
</div>
<div class="panel-body">';
if (!$destinatari->isEmpty()) {
echo '
<table class="table table-hover table-condensed table-bordered">
<div class="panel-body">
<table class="table table-hover table-condensed table-bordered" id="destinatari">
<thead>
<tr>
<th>'.tr('Nome').'</th>
<th class="text-center">'.tr('Indirizzo').'</th>
<th>'.tr('Ragione sociale').'</th>
<th>'.tr('Tipo').'</th>
<th>'.tr('Tipologia').'</th>
<th class="text-center">'.tr('E-mail').'</th>
<th class="text-center">'.tr('Newsletter').'</th>
<th class="text-center" width="60">#</th>
</tr>
</thead>
<tbody>';
foreach ($destinatari as $destinatario) {
$anagrafica = $destinatario instanceof Anagrafica ? $destinatario : $destinatario->anagrafica;
$descrizione = $anagrafica->ragione_sociale;
if ($destinatario instanceof Sede) {
$descrizione .= ' ['.$destinatario->nomesede.']';
} elseif ($destinatario instanceof Referente) {
$descrizione .= ' ['.$destinatario->nome.']';
}
echo '
<tr '.(empty($destinatario->email) ? 'class="bg-danger"' : '').'>
<td>'.Modules::link('Anagrafiche', $anagrafica->id, $descrizione).'</td>
<td class="text-center">'.$destinatario->email.'</td>
<td class="text-center">
<a class="btn btn-danger ask btn-sm '.(!empty($lista->query) ? 'disabled' : '').'" data-backto="record-edit" data-op="remove_receiver" data-type="'.get_class($destinatario).'" data-id="'.$destinatario->id.'" '.(!empty($lista->query) ? 'disabled' : '').'>
<i class="fa fa-trash"></i>
</a>
</td>
</tr>';
}
echo '
</tbody>
</table>';
} else {
echo '
<p>'.tr('Nessun destinatario collegato alla lista').'.</p>';
}
echo '
</table>
</div>
</div>
<a class="btn btn-danger ask" data-backto="record-list">
<i class="fa fa-trash"></i> '.tr('Elimina').'
</a>';
echo '
<script>
globals.newsletter = {
senza_consenso: "'.$lista->getNumeroDestinatariSenzaConsenso().'",
table_url: "'.Module::pool('Newsletter')->fileurl('ajax/table.php').'?id_list='.$id_record.'",
};
$(document).ready(function() {
const senza_consenso = $("#numero_consenso_disabilitato");
if (globals.newsletter.senza_consenso > 0) {
senza_consenso.text(globals.newsletter.senza_consenso);
} else {
senza_consenso.hide();
}
$("#destinatari").DataTable({
language: globals.translations.datatables,
retrieve: true,
ordering: false,
searching: true,
paging: true,
order: [],
lengthChange: false,
processing: true,
serverSide: true,
ajax: {
url: globals.newsletter.table_url,
type: "GET",
dataSrc: "data",
},
searchDelay: 500,
pageLength: 50,
});
});
</script>';

View File

@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use Modules\Newsletter\Lista;
use Modules\ListeNewsletter\Lista;
include_once __DIR__.'/../../core.php';

View File

@@ -0,0 +1,67 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\ListeNewsletter;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
class Destinatario extends Model
{
use SimpleModelTrait;
protected $table = 'em_list_receiver';
protected $origine = null;
public static function build(Lista $lista, $origine)
{
$model = new static();
$model->id_list = $lista->id;
$model->record_type = get_class($origine);
$model->record_id = $origine->id;
$model->save();
return $model;
}
public function getEmailAttribute()
{
return $this->getOrigine()->email;
}
// Relazione Eloquent
public function getOrigine()
{
if (isset($this->origine)) {
return $this->origine;
}
$this->origine = ($this->record_type)::find($this->record_id);
return $this->origine;
}
public function lista()
{
return $this->belongsTo(Lista::class, 'id_list');
}
}

View File

@@ -0,0 +1,133 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\ListeNewsletter;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Traits\RecordTrait;
class Lista extends Model
{
use SimpleModelTrait;
use SoftDeletes;
use RecordTrait;
protected $table = 'em_lists';
public static function build($name)
{
$model = new static();
$model->name = $name;
$model->save();
return $model;
}
public function save(array $options = [])
{
$result = parent::save($options);
$query = $this->query;
if (!empty($query)) {
$database = database();
// Rimozione record precedenti
$database->delete('em_list_receiver', [
'id_list' => $this->id,
]);
// Ricerca nuovi record
$number = $database->fetchNum($query);
$database->query('INSERT INTO em_list_receiver (id_list, record_id, record_type) '.str_replace_first('SELECT', 'SELECT '.prepare($this->id).',', $query));
}
return $result;
}
public function getNumeroDestinatariSenzaEmail()
{
$anagrafiche = $this->getDestinatari(Anagrafica::class)
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
->where('email', '=', '')
->count();
$sedi = $this->getDestinatari(Sede::class)
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
->where('email', '=', '')
->count();
$referenti = $this->getDestinatari(Referente::class)
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
->where('email', '=', '')
->count();
return $anagrafiche + $sedi + $referenti;
}
public function getNumeroDestinatariSenzaConsenso()
{
$anagrafiche = $this->getDestinatari(Anagrafica::class)
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
$sedi = $this->getDestinatari(Sede::class)
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_sedi.idanagrafica')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
$referenti = $this->getDestinatari(Referente::class)
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_referenti.idanagrafica')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
return $anagrafiche + $sedi + $referenti;
}
public function getDestinatari($tipo)
{
return $this->destinatari()
->where('record_type', '=', $tipo);
}
// Relazione Eloquent
public function destinatari()
{
return $this->hasMany(Destinatario::class, 'id_list');
}
/**
* Restituisce il nome del modulo a cui l'oggetto è collegato.
*
* @return string
*/
public function getModuleAttribute()
{
return 'Liste';
}
}

View File

@@ -22,7 +22,7 @@ use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Modules\Emails\Mail;
use Modules\Emails\Template;
use Modules\Newsletter\Lista;
use Modules\ListeNewsletter\Lista;
use Modules\Newsletter\Newsletter;
include_once __DIR__.'/../../core.php';
@@ -60,33 +60,44 @@ switch (filter('op')) {
break;
case 'send':
$anagrafiche = $newsletter->anagrafiche;
$template = $newsletter->template;
$uploads = $newsletter->uploads()->pluck('id');
foreach ($anagrafiche as $anagrafica) {
if (empty($anagrafica['email']) || empty($anagrafica['enable_newsletter'])) {
$destinatari = $newsletter->destinatari();
foreach ($destinatari as $destinatario) {
$anagrafica = $destinatario instanceof Anagrafica ? $destinatario : $destinatario->anagrafica;
$abilita_newsletter = $anagrafica->enable_newsletter;
if (empty($destinatario->email) || empty($abilita_newsletter)) {
continue;
}
// Inizializzazione email
$mail = Mail::build($user, $template, $anagrafica->id);
$mail->addReceiver($anagrafica['email']);
// Completamento informazioni
$mail->addReceiver($destinatario->email);
$mail->subject = $newsletter->subject;
$mail->content = $newsletter->content;
$mail->id_newsletter = $newsletter->id;
// Registrazione allegati
foreach ($uploads as $upload) {
$mail->addUpload($upload);
}
$mail->save();
$newsletter->anagrafiche()->updateExistingPivot($anagrafica->id, ['id_email' => $mail->id]);
// Aggiornamento riferimento per la newsletter
$database->update('em_newsletter_receiver', [
'id_email' => $mail->id,
], [
'record_type' => get_class($destinatario),
'record_id' => $destinatario->id,
'id_newsletter' => $newsletter->id,
]);
}
// Aggiornamento stato newsletter
$newsletter->state = 'WAIT';
$newsletter->save();
@@ -98,13 +109,23 @@ switch (filter('op')) {
$mails = $newsletter->emails;
foreach ($mails as $mail) {
if (empty($mail->sent_at)) {
$newsletter->emails()->updateExistingPivot($mail->id, ['id_email' => null], false);
$mail->delete();
if (!empty($mail->sent_at)) {
continue;
}
// Rimozione riferimento email dalla newsletter
$database->update('em_newsletter_receiver', [
'id_email' => $null,
], [
'id_email' => $mail->id,
'id_newsletter' => $newsletter->id,
]);
// Rimozione email
$mail->delete();
}
// Aggiornamento stato newsletter
$newsletter->state = 'DEV';
$newsletter->save();
@@ -113,8 +134,6 @@ switch (filter('op')) {
break;
case 'add_receivers':
$destinatari = [];
// Selezione manuale
$id_receivers = post('receivers');
foreach ($id_receivers as $id_receiver) {
@@ -127,50 +146,55 @@ switch (filter('op')) {
$type = Referente::class;
}
$destinatari[] = [
// Dati di registrazione
$data = [
'record_type' => $type,
'record_id' => $id,
];
}
// Selezione da lista newsletter
$id_list = post('id_list');
if (!empty($id_list)) {
$list = Lista::find($id_list);
$receivers = $list->getDestinatari();
$receivers = $receivers->map(function ($item, $key) {
return [
'record_type' => get_class($item),
'record_id' => $item->id,
];
});
$destinatari = $receivers->toArray();
}
// Aggiornamento destinatari
foreach ($destinatari as $destinatario) {
$data = array_merge($destinatario, [
'id_newsletter' => $newsletter->id,
]);
];
// Aggiornamento destinatari
$registrato = $database->select('em_newsletter_receiver', '*', $data);
if (empty($registrato)) {
$database->insert('em_newsletter_receiver', $data);
}
}
// Controllo indirizzo e-mail aggiunto
foreach ($newsletter->anagrafiche as $anagrafica) {
if (!empty($anagrafica['email'])) {
$check = Validate::isValidEmail($anagrafica['email']);
// Selezione da lista newsletter
$id_list = post('id_list');
if (!empty($id_list)) {
// Rimozione preventiva dei record duplicati dalla newsletter
$database->query('DELETE em_newsletter_receiver.* FROM em_newsletter_receiver
INNER JOIN em_list_receiver ON em_list_receiver.record_type = em_newsletter_receiver.record_type AND em_list_receiver.record_id = em_newsletter_receiver.record_id
WHERE em_newsletter_receiver.id_newsletter = '.prepare($newsletter->id).' AND em_list_receiver.id_list = '.prepare($id_list));
// Copia dei record della lista newsletter
$database->query('INSERT INTO em_newsletter_receiver (id_newsletter, record_type, record_id) SELECT '.prepare($newsletter->id).', record_type, record_id FROM em_list_receiver WHERE id_list = '.prepare($id_list));
}
/*
// Controllo indirizzo e-mail presente
$destinatari = $newsletter->destinatari();
foreach ($destinatari as $destinatario) {
$anagrafica = $destinatario instanceof Anagrafica ? $destinatario : $destinatario->anagrafica;
if (!empty($destinatario->email)) {
$check = Validate::isValidEmail($destinatario->email);
if (empty($check['valid-format'])) {
$errors[] = $anagrafica['email'];
$errors[] = $destinatario->email;
}
} else {
$errors[] = tr('Indirizzo e-mail mancante per "_EMAIL_"', [
'_EMAIL_' => $anagrafica['ragione_sociale'],
$descrizione = $anagrafica->ragione_sociale;
if ($destinatario instanceof Sede) {
$descrizione .= ' ['.$destinatario->nomesede.']';
} elseif ($destinatario instanceof Referente) {
$descrizione .= ' ['.$destinatario->nome.']';
}
$errors[] = tr('Indirizzo e-mail mancante per "_NOME_"', [
'_NOME_' => $descrizione,
]);
}
}
@@ -181,7 +205,7 @@ switch (filter('op')) {
$message .= '<li>'.$error.'</li>';
}
$message .= '</ul>';
}
}*/
if (!empty($message)) {
flash()->warning(tr('Attenzione questi indirizzi e-mail non sembrano essere validi: _EMAIL_ ', [

View File

@@ -24,9 +24,9 @@ switch ($resource) {
// Gestione campi di ricerca
if (!empty($search)) {
$search_fields[] = '|nome| LIKE '.prepare('%'.$search.'%');
$search_fields[] = 'citta LIKE '.prepare('%'.$search.'%');
$search_fields[] = 'provincia LIKE '.prepare('%'.$search.'%');
$search_fields[] = 'email LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.citta LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.provincia LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.email LIKE '.prepare('%'.$search.'%');
}
// Aggiunta filtri di ricerca
@@ -39,12 +39,15 @@ switch ($resource) {
CONCAT(an_anagrafiche.ragione_sociale, IF(an_anagrafiche.citta != '' OR an_anagrafiche.provincia != '', CONCAT(' (', an_anagrafiche.citta, IF(an_anagrafiche.provincia != '', an_anagrafiche.provincia, ''), ')'), ''), ' [', email, ']') AS text,
`an_tipianagrafiche`.`descrizione` AS optgroup
FROM an_anagrafiche
INNER JOIN an_tipianagrafiche_anagrafiche ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica
INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica
INNER JOIN an_tipianagrafiche_anagrafiche ON an_anagrafiche.idanagrafica = an_tipianagrafiche_anagrafiche.idanagrafica
INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica = an_tipianagrafiche.idtipoanagrafica
WHERE an_anagrafiche.deleted_at IS NULL AND an_anagrafiche.enable_newsletter = 1 AND 1=1
ORDER BY `optgroup` ASC, ragione_sociale ASC";
$query = str_replace('1=1', !empty($where) ? replace($where, ['|nome|' => 'ragione_sociale']) : '', $query);
$query = str_replace('1=1', !empty($where) ? replace($where, [
'|nome|' => 'ragione_sociale',
'|table|' => 'an_anagrafiche',
]) : '', $query);
$anagrafiche = $database->fetchArray($query);
$destinatari = $destinatari->concat($anagrafiche);
@@ -57,7 +60,10 @@ switch ($resource) {
WHERE an_anagrafiche.deleted_at IS NULL AND an_anagrafiche.enable_newsletter = 1 AND 1=1
ORDER BY `optgroup` ASC, ragione_sociale ASC";
$query = str_replace('1=1', !empty($where) ? replace($where, ['|nome|' => 'nomesede LIKE '.prepare('%'.$search.'%').' AND ragione_sociale']) : '', $query);
$query = str_replace('1=1', !empty($where) ? replace($where, [
'|nome|' => 'nomesede LIKE '.prepare('%'.$search.'%').' OR ragione_sociale',
'|table|' => 'an_sedi',
]) : '', $query);
$sedi = $database->fetchArray($query);
$destinatari = $destinatari->concat($sedi);
@@ -70,7 +76,10 @@ switch ($resource) {
WHERE an_anagrafiche.deleted_at IS NULL AND an_anagrafiche.enable_newsletter = 1 AND 1=1
ORDER BY `optgroup` ASC, ragione_sociale ASC";
$query = str_replace('1=1', !empty($where) ? replace($where, ['|nome|' => 'nomesede LIKE '.prepare('%'.$search.'%').' AND ragione_sociale']) : '', $query);
$query = str_replace('1=1', !empty($where) ? replace($where, [
'|nome|' => 'an_referenti.nome LIKE '.prepare('%'.$search.'%').' OR ragione_sociale',
'|table|' => 'an_anagrafiche',
]) : '', $query);
$referenti = $database->fetchArray($query);
$destinatari = $destinatari->concat($referenti);

View File

@@ -0,0 +1,136 @@
<?php
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Modules\Emails\Mail;
use Modules\ListeNewsletter\Lista;
use Modules\Newsletter\Newsletter;
include_once __DIR__.'/../../../core.php';
$id_newsletter = filter('id_newsletter');
$id_list = filter('id_list');
$newsletter = Newsletter::find($id_newsletter);
$lista = Lista::find($id_list);
$riferimento = $newsletter ?: $lista;
$search = filter('search') ? filter('search')['value'] : null;
// Utilizzo della risorsa destinatari_newsletter per gestire la ricerca
if (!empty($search)) {
$resource = 'destinatari_newsletter';
include_once __DIR__.'/select.php';
$results = collect($results)->mapToGroups(function ($item, $key) {
list($tipo, $id) = explode('_', $item['id']);
return [$tipo => $id];
});
$destinatari = $riferimento->destinatari()
->where(function ($query) use ($results) {
$query->where('record_type', '=', Anagrafica::class)
->whereIn('record_id', $results['anagrafica']);
})
->orWhere(function ($query) use ($results) {
$query->where('record_type', '=', Sede::class)
->whereIn('record_id', $results['sede']);
})
->orWhere(function ($query) use ($results) {
$query->where('record_type', '=', Referente::class)
->whereIn('record_id', $results['referente']);
});
}
// Elenco di tutti i destinatari
else {
$destinatari = $riferimento->destinatari();
}
$start = filter('start');
$length = filter('length');
// Filtro dei record richiesti
$destinatari_filtrati = (clone $destinatari)
->skip($start)->take($length)
->get();
$righe = [];
foreach ($destinatari_filtrati as $destinatario) {
$origine = $destinatario->getOrigine();
$anagrafica = $origine instanceof Anagrafica ? $origine : $origine->anagrafica;
$descrizione = $anagrafica->ragione_sociale;
if ($origine instanceof Sede) {
$descrizione .= ' ['.$origine->nomesede.']';
} elseif ($origine instanceof Referente) {
$descrizione .= ' ['.$origine->nome.']';
}
$tipo_anagrafica = $database->fetchOne('SELECT GROUP_CONCAT(an_tipianagrafiche.descrizione) AS descrizione FROM an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica = an_tipianagrafiche.idtipoanagrafica WHERE an_tipianagrafiche_anagrafiche.idanagrafica='.prepare($anagrafica->id))['descrizione'];
$riga = [
Modules::link('Anagrafiche', $anagrafica->id, $descrizione),
$tipo_anagrafica,
$anagrafica->tipo,
'<div class="text-center">'.
(!empty($origine->email) ?
input([
'type' => 'text',
'name' => 'email',
'id' => 'email_'.rand(0, 99999),
'readonly' => '1',
'class' => 'email-mask',
'value' => $origine->email,
'validation' => 'email',
]) :
'<span class="text-danger"><i class="fa fa-close"></i> '.tr('Indirizzo e-mail mancante').'</span>'
).'
</div>',
];
// Informazioni di invio
if (empty($lista)) {
$mail_id = $origine->pivot->id_email;
$mail = Mail::find($mail_id);
if (!empty($mail) && !empty($mail->sent_at)) {
$info_invio = '
<span class="text-success">
<i class="fa fa-paper-plane"></i> '.timestampFormat($mail->sent_at).'
</span>';
} else {
$info_invio = '
<span class="text-info">
<i class="fa fa-clock-o"></i> '.tr('Non ancora inviata').'
</span>';
}
$riga[] = '<div class="text-center">'.$info_invio.'</div>';
}
$riga = array_merge($riga, [
'<div class="text-center">'.
(!empty($anagrafica->enable_newsletter) ?
'<span class="text-success"><i class="fa fa-check"></i> '.tr('Abilitato').'</span>' :
'<span class="text-warning"><i class="fa fa-exclamation-triangle"></i> '.tr('Disabilitato').'</span>'
).'
</div>',
'<div class="text-center">
<a class="btn btn-danger ask btn-xs" data-backto="record-edit" data-op="remove_receiver" data-type="'.get_class($origine).'" data-id="'.$origine->id.'">
<i class="fa fa-trash"></i>
</a>
</div>',
]);
$righe[] = $riga;
}
// Formattazione dei dati
echo json_encode([
'data' => $righe,
'recordsTotal' => $riferimento->destinatari()->count(),
'recordsFiltered' => $destinatari->count(),
'draw' => intval(filter('draw')),
]);

View File

@@ -17,15 +17,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Modules\Emails\Mail;
use Modules\Emails\Template;
include_once __DIR__.'/../../core.php';
//Controllo se il template è ancora attivo
// Controllo se il template è ancora attivo
if (empty($template)) {
echo '
<div class="alert alert-danger">'.tr('ATTENZIONE! Questa newsletter risulta collegata ad un template non più presente a sistema').'</div>';
@@ -148,7 +144,8 @@ $(document).ready(function() {
})
</script>';
$destinatari = $newsletter->getDestinatari();
$numero_destinatari = $newsletter->destinatari()->count();
$destinatari_senza_mail = $newsletter->getNumeroDestinatariSenzaEmail();
echo '
<!-- Destinatari -->
@@ -156,18 +153,16 @@ echo '
<div class="panel-heading">
<h3 class="panel-title">
'.tr('Destinatari').'
<span> ('.$destinatari->count().')</span> <div class="pull-right" >
'.(($destinatari->where('email', '')->count() > 0) ? ' <span title="'.tr('Indirizzi e-mail mancanti').'" class="tip label label-danger clickable">'.$destinatari->where('email', '')->count().'</span>' : '')
.'<span title="'.tr('Indirizzi e-mail senza consenso per newsletter').'" class="tip label label-warning clickable" id="numero_consenso_disabilitato"></span></div>
<span> ('.$numero_destinatari.')</span>
<div class="pull-right">
'.(($destinatari_senza_mail > 0) ? ' <span title="'.tr('Indirizzi e-mail mancanti').'" class="tip label label-danger clickable">'.$destinatari_senza_mail.'</span>' : '')
.'<span title="'.tr('Indirizzi e-mail senza consenso per newsletter').'" class="tip label label-warning clickable" id="numero_consenso_disabilitato"></span>
</div>
</h3>
</div>
<div class="panel-body" style="max-height:700px;overflow-y:auto;">';
$senza_consenso = 0;
if (!$destinatari->isEmpty()) {
echo '
<table class="table table-hover table-condensed table-bordered">
<div class="panel-body">
<table class="table table-hover table-condensed table-bordered" id="destinatari">
<thead>
<tr>
<th>'.tr('Ragione sociale').'</th>
@@ -179,74 +174,11 @@ if (!$destinatari->isEmpty()) {
<th class="text-center" width="60">#</th>
</tr>
</thead>
<tbody>';
$senza_consenso = 0;
foreach ($destinatari as $destinatario) {
$anagrafica = $destinatario instanceof Anagrafica ? $destinatario : $destinatario->anagrafica;
$descrizione = $anagrafica->ragione_sociale;
if ($destinatario instanceof Sede) {
$descrizione .= ' ['.$destinatario->nomesede.']';
} elseif ($destinatario instanceof Referente) {
$descrizione .= ' ['.$destinatario->nome.']';
}
if (empty($anagrafica->enable_newsletter)){
$senza_consenso ++;
}
$mail_id = $destinatario->pivot->id_email;
$mail = Mail::find($mail_id);
if (!empty($mail) && !empty($mail->sent_at)) {
$data = '
<span class="text-success">
<i class="fa fa-paper-plane"></i> '.timestampFormat($mail->sent_at).'
</span>';
} else {
$data = '
<span class="text-info">
<i class="fa fa-clock-o"></i> '.tr('Non ancora inviata').'
</span>';
}
echo '
<tr '.(empty($destinatario->email) ? 'class="bg-danger"' : (empty($anagrafica->enable_newsletter) ? 'class="bg-warning"' : '')).'>
<td>'.Modules::link('Anagrafiche', $anagrafica->id, $descrizione).'</td>
<td class="text-left">'.$database->fetchOne('SELECT GROUP_CONCAT(an_tipianagrafiche.descrizione) AS descrizione FROM an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica = an_tipianagrafiche.idtipoanagrafica WHERE an_tipianagrafiche_anagrafiche.idanagrafica='.prepare($anagrafica->id))['descrizione'].'</td>
<td class="text-left">'.$anagrafica->tipo.'</td>
<td class="text-left">
'.((!empty($destinatario->email) ? '
{[ "type": "text", "name": "email", "id": "email_'.rand(0, 99999).'", "readonly": "1", "class": "email-mask", "value": "'.$destinatario->email.'", "validation": "email" ]}' : '<span class="text-danger"><i class="fa fa-close"></i> '.tr('Indirizzo e-mail mancante').'</span>')).'</td>
<td class="text-center">'.$data.'</td>
<td class="text-left">
'.((!empty($anagrafica->enable_newsletter)) ? '<span class="text-success"><i class="fa fa-check"></i> '.tr('Abilitato').'</span>' : '<span class="text-warning"><i class="fa fa-exclamation-triangle"></i> '.tr('Disabilitato').'</span>').'
</td>
<td class="text-center">
<a class="btn btn-danger ask btn-xs" data-backto="record-edit" data-op="remove_receiver" data-type="'.get_class($destinatario).'" data-id="'.$destinatario->id.'">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>';
}
echo '
</tbody>
</table>
<a class="btn btn-danger ask pull-right" data-backto="record-edit" data-op="remove_all_receivers">
<i class="fa fa-trash"></i> '.tr('Elimina tutti').'
</a>';
} else {
echo '
<div class="alert alert-info">
<i class="fa fa-info-circle"></i> '.tr('Nessuna anagrafica collegata alla campagna').'.
</div>';
}
echo '
</a>
</div>
</div>
@@ -262,14 +194,15 @@ if ($block_edit) {
$(document).ready(function() {
$("#receivers").parent().hide();
$("#receivers-form .btn").hide();
});
});
</script>';
}
else {
echo '
echo '
<script>
globals.newsletter = {
senza_consenso: "'.$senza_consenso.'",
senza_consenso: "'.$newsletter->getNumeroDestinatariSenzaConsenso().'",
table_url: "'.$structure->fileurl('ajax/table.php').'?id_newsletter='.$id_record.'",
};
$(document).ready(function() {
@@ -279,6 +212,24 @@ $(document).ready(function() {
} else {
senza_consenso.hide();
}
$("#destinatari").DataTable({
language: globals.translations.datatables,
retrieve: true,
ordering: false,
searching: true,
paging: true,
order: [],
lengthChange: false,
processing: true,
serverSide: true,
ajax: {
url: globals.newsletter.table_url,
type: "GET",
dataSrc: "data",
},
searchDelay: 500,
pageLength: 50,
});
});
</script>';
}

View File

@@ -0,0 +1,67 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\Newsletter;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
class Destinatario extends Model
{
use SimpleModelTrait;
protected $table = 'em_newsletter_receiver';
protected $origine = null;
public static function build(Newsletter $newsletter, $origine)
{
$model = new static();
$model->id_newsletter = $newsletter->id;
$model->record_type = get_class($origine);
$model->record_id = $origine->id;
$model->save();
return $model;
}
public function getEmailAttribute()
{
return $this->getOrigine()->email;
}
// Relazione Eloquent
public function getOrigine()
{
if (isset($this->origine)) {
return $this->origine;
}
$this->origine = ($this->record_type)::find($this->record_id);
return $this->origine;
}
public function newsletter()
{
return $this->belongsTo(Newsletter::class, 'id_newsletter');
}
}

View File

@@ -1,141 +0,0 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\Newsletter;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Referente;
use Modules\Anagrafiche\Sede;
use Traits\RecordTrait;
class Lista extends Model
{
use SimpleModelTrait;
use SoftDeletes;
use RecordTrait;
protected $table = 'em_lists';
public static function build($name)
{
$model = new static();
$model->name = $name;
$model->save();
return $model;
}
public function save(array $options = [])
{
$result = parent::save($options);
$query = $this->query;
if (!empty($query)) {
$database = database();
// Rimozione record precedenti
$database->delete('em_list_receiver', [
'id_list' => $this->id,
]);
// Ricerca nuovi record
$results = $database->fetchArray($query);
$gruppi = collect($results)
->groupBy('tipo');
// Preparazione al salvataggio
$destinatari = [];
foreach ($gruppi as $tipo => $gruppo) {
if ($tipo == 'anagrafica') {
$type = Anagrafica::class;
} elseif ($tipo == 'sede') {
$type = Sede::class;
} else {
$type = Referente::class;
}
foreach ($gruppo as $record) {
$destinatari[] = [
'record_type' => $type,
'record_id' => $record['id'],
];
}
}
// Aggiornamento destinatari
foreach ($destinatari as $destinatario) {
$data = array_merge($destinatario, [
'id_list' => $this->id,
]);
$registrato = $database->select('em_list_receiver', '*', $data);
if (empty($registrato)) {
$database->insert('em_list_receiver', $data);
}
}
}
return $result;
}
// Relazione Eloquent
public function getDestinatari()
{
return $this->anagrafiche
->concat($this->sedi)
->concat($this->referenti);
}
public function anagrafiche()
{
return $this
->belongsToMany(Anagrafica::class, 'em_list_receiver', 'id_list', 'record_id')
->where('record_type', '=', Anagrafica::class)
->withTrashed();
}
public function sedi()
{
return $this
->belongsToMany(Sede::class, 'em_list_receiver', 'id_list', 'record_id')
->where('record_type', '=', Sede::class);
}
public function referenti()
{
return $this
->belongsToMany(Referente::class, 'em_list_receiver', 'id_list', 'record_id')
->where('record_type', '=', Referente::class);
}
/**
* Restituisce il nome del modulo a cui l'oggetto è collegato.
*
* @return string
*/
public function getModuleAttribute()
{
return 'Liste';
}
}

View File

@@ -84,45 +84,64 @@ class Newsletter extends Model
$this->save();
}
public function getNumeroDestinatariSenzaEmail()
{
$anagrafiche = $this->getDestinatari(Anagrafica::class)
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
->where('email', '=', '')
->count();
$sedi = $this->getDestinatari(Sede::class)
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
->where('email', '=', '')
->count();
$referenti = $this->getDestinatari(Referente::class)
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
->where('email', '=', '')
->count();
return $anagrafiche + $sedi + $referenti;
}
public function getNumeroDestinatariSenzaConsenso()
{
$anagrafiche = $this->getDestinatari(Anagrafica::class)
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
$sedi = $this->getDestinatari(Sede::class)
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_sedi.idanagrafica')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
$referenti = $this->getDestinatari(Referente::class)
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_referenti.idanagrafica')
->where('an_anagrafiche.enable_newsletter', '=', false)
->count();
return $anagrafiche + $sedi + $referenti;
}
public function getDestinatari($tipo)
{
return $this->destinatari()
->where('record_type', '=', $tipo);
}
// Relazione Eloquent
public function getDestinatari()
public function destinatari()
{
return $this->anagrafiche
->concat($this->sedi)
->concat($this->referenti);
}
public function anagrafiche()
{
return $this
->belongsToMany(Anagrafica::class, 'em_newsletter_receiver', 'id_newsletter', 'record_id')
->where('record_type', '=', Anagrafica::class)
->withPivot('id_email')
->orderByRaw('IF(email=\'\',email,enable_newsletter) ASC')
->orderBy('ragione_sociale', 'ASC')
->withTrashed();
}
public function sedi()
{
return $this
->belongsToMany(Sede::class, 'em_newsletter_receiver', 'id_newsletter', 'record_id')
->where('record_type', '=', Sede::class)
->withPivot('id_email');
}
public function referenti()
{
return $this
->belongsToMany(Referente::class, 'em_newsletter_receiver', 'id_newsletter', 'record_id')
->where('record_type', '=', Referente::class)
->withPivot('id_email');
return $this->hasMany(Destinatario::class, 'id_newsletter');
}
public function emails()
{
return $this->belongsToMany(Mail::class, 'em_newsletter_receiver', 'id_newsletter', 'id_email')->withPivot('id_anagrafica');
return $this->belongsToMany(Mail::class, 'em_newsletter_receiver', 'id_newsletter', 'id_email')->withPivot(['record_id', 'record_type']);
}
public function account()

View File

@@ -8,13 +8,13 @@ INSERT INTO `zz_settings` (`id`, `nome`, `valore`, `tipo`, `editable`, `sezione`
-- Aggiornamento gestione destinatari per newsletter e liste relative
ALTER TABLE `em_newsletter_anagrafica` DROP FOREIGN KEY `em_newsletter_anagrafica_ibfk_2`;
ALTER TABLE `em_newsletter_anagrafica`
ADD `record_type` VARCHAR(255) NOT NULL BEFORE `id_anagrafica`, CHANGE `id_anagrafica` `record_id` INT(11) NOT NULL;
ADD `record_type` VARCHAR(255) NOT NULL AFTER `id_newsletter`, CHANGE `id_anagrafica` `record_id` INT(11) NOT NULL;
UPDATE `em_newsletter_anagrafica`
SET `record_type` ='Modules\\Anagrafiche\\Anagrafica';
ALTER TABLE `em_list_anagrafica` DROP FOREIGN KEY `em_list_anagrafica_ibfk_2`;
ALTER TABLE `em_list_anagrafica`
ADD `record_type` VARCHAR(255) NOT NULL BEFORE `id_anagrafica`, CHANGE `id_anagrafica` `record_id` INT(11) NOT NULL;
ADD `record_type` VARCHAR(255) NOT NULL AFTER `id_list`, CHANGE `id_anagrafica` `record_id` INT(11) NOT NULL;
UPDATE `em_list_anagrafica`
SET `record_type` ='Modules\\Anagrafiche\\Anagrafica';
@@ -22,3 +22,6 @@ ALTER TABLE `em_list_anagrafica` RENAME TO `em_list_receiver`;
ALTER TABLE `em_newsletter_anagrafica` RENAME TO `em_newsletter_receiver`;
UPDATE `zz_views` SET `query` = '(SELECT COUNT(*) FROM `em_newsletter_receiver` WHERE `em_newsletter_receiver`.`id_newsletter` = `em_newsletters`.`id`)' WHERE `id_module` = (SELECT `id` FROM `zz_modules` WHERE name='Newsletter') AND `name` = 'Destinatari';
ALTER TABLE `em_newsletter_receiver` ADD `id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE `em_list_receiver` ADD `id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT;