diff --git a/assets/src/css/datatables.css b/assets/src/css/datatables.css
index 277a89911..d2e9c4b9a 100755
--- a/assets/src/css/datatables.css
+++ b/assets/src/css/datatables.css
@@ -237,3 +237,8 @@ div.dataTables_wrapper {
.dataTables_info .select-info {
display: none;
}
+
+.dataTables_processing
+{
+ z-index: 1050
+}
diff --git a/modules/liste_newsletter/actions.php b/modules/liste_newsletter/actions.php
index 23660dcb2..6d271e7cd 100755
--- a/modules/liste_newsletter/actions.php
+++ b/modules/liste_newsletter/actions.php
@@ -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';
diff --git a/modules/liste_newsletter/edit.php b/modules/liste_newsletter/edit.php
index d2e289015..f0157368f 100755
--- a/modules/liste_newsletter/edit.php
+++ b/modules/liste_newsletter/edit.php
@@ -17,9 +17,7 @@
* along with this program. If not, see .
*/
-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 '
- {[ "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',
+ ]),
+ ]).'
@@ -86,7 +91,8 @@ echo '
';
-$destinatari = $lista->getDestinatari();
+$numero_destinatari = $lista->destinatari()->count();
+$destinatari_senza_mail = $lista->getNumeroDestinatariSenzaEmail();
echo '
@@ -94,59 +100,64 @@ echo '
'.tr('Destinatari').'
- '.$destinatari->count().'
+ ('.$numero_destinatari.')
+ '.(($destinatari_senza_mail > 0) ? ' '.$destinatari_senza_mail.'' : '')
+ .'
- ';
-
-if (!$destinatari->isEmpty()) {
- echo '
-
+
+
- '.tr('Nome').' |
- '.tr('Indirizzo').' |
+ '.tr('Ragione sociale').' |
+ '.tr('Tipo').' |
+ '.tr('Tipologia').' |
+ '.tr('E-mail').' |
+ '.tr('Newsletter').' |
# |
-
- ';
-
- 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 '
- email) ? 'class="bg-danger"' : '').'>
- '.Modules::link('Anagrafiche', $anagrafica->id, $descrizione).' |
- '.$destinatario->email.' |
-
- query) ? 'disabled' : '').'>
-
-
- |
-
';
- }
-
- echo '
-
-
';
-} else {
- echo '
-
'.tr('Nessun destinatario collegato alla lista').'.
';
-}
-
- echo '
+
'.tr('Elimina').'
';
+
+echo '
+';
diff --git a/modules/liste_newsletter/init.php b/modules/liste_newsletter/init.php
index 5d0acd1b9..1ccd3799d 100755
--- a/modules/liste_newsletter/init.php
+++ b/modules/liste_newsletter/init.php
@@ -17,7 +17,7 @@
* along with this program. If not, see .
*/
-use Modules\Newsletter\Lista;
+use Modules\ListeNewsletter\Lista;
include_once __DIR__.'/../../core.php';
diff --git a/modules/liste_newsletter/src/Destinatario.php b/modules/liste_newsletter/src/Destinatario.php
new file mode 100644
index 000000000..b3978f8a3
--- /dev/null
+++ b/modules/liste_newsletter/src/Destinatario.php
@@ -0,0 +1,67 @@
+.
+ */
+
+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');
+ }
+}
diff --git a/modules/liste_newsletter/src/Lista.php b/modules/liste_newsletter/src/Lista.php
new file mode 100644
index 000000000..479a31289
--- /dev/null
+++ b/modules/liste_newsletter/src/Lista.php
@@ -0,0 +1,133 @@
+.
+ */
+
+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';
+ }
+}
diff --git a/modules/newsletter/actions.php b/modules/newsletter/actions.php
index 7bc945fb9..d88c474ea 100755
--- a/modules/newsletter/actions.php
+++ b/modules/newsletter/actions.php
@@ -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 .= ''.$error.'';
}
$message .= '';
- }
+ }*/
if (!empty($message)) {
flash()->warning(tr('Attenzione questi indirizzi e-mail non sembrano essere validi: _EMAIL_ ', [
diff --git a/modules/newsletter/ajax/select.php b/modules/newsletter/ajax/select.php
index 4d9121cf5..0a281c969 100755
--- a/modules/newsletter/ajax/select.php
+++ b/modules/newsletter/ajax/select.php
@@ -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);
diff --git a/modules/newsletter/ajax/table.php b/modules/newsletter/ajax/table.php
new file mode 100644
index 000000000..6f44e52df
--- /dev/null
+++ b/modules/newsletter/ajax/table.php
@@ -0,0 +1,136 @@
+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,
+ ''.
+ (!empty($origine->email) ?
+ input([
+ 'type' => 'text',
+ 'name' => 'email',
+ 'id' => 'email_'.rand(0, 99999),
+ 'readonly' => '1',
+ 'class' => 'email-mask',
+ 'value' => $origine->email,
+ 'validation' => 'email',
+ ]) :
+ ' '.tr('Indirizzo e-mail mancante').''
+ ).'
+
',
+ ];
+
+ // 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 = '
+
+ '.timestampFormat($mail->sent_at).'
+';
+ } else {
+ $info_invio = '
+
+ '.tr('Non ancora inviata').'
+';
+ }
+
+ $riga[] = ''.$info_invio.'
';
+ }
+
+ $riga = array_merge($riga, [
+ ''.
+ (!empty($anagrafica->enable_newsletter) ?
+ ' '.tr('Abilitato').'' :
+ ' '.tr('Disabilitato').''
+ ).'
+
',
+ '',
+ ]);
+
+ $righe[] = $riga;
+}
+
+// Formattazione dei dati
+echo json_encode([
+ 'data' => $righe,
+ 'recordsTotal' => $riferimento->destinatari()->count(),
+ 'recordsFiltered' => $destinatari->count(),
+ 'draw' => intval(filter('draw')),
+]);
diff --git a/modules/newsletter/edit.php b/modules/newsletter/edit.php
index 00d2392f7..0dae86e44 100755
--- a/modules/newsletter/edit.php
+++ b/modules/newsletter/edit.php
@@ -17,15 +17,11 @@
* along with this program. If not, see .
*/
-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 '
'.tr('ATTENZIONE! Questa newsletter risulta collegata ad un template non più presente a sistema').'
';
@@ -148,7 +144,8 @@ $(document).ready(function() {
})
';
-$destinatari = $newsletter->getDestinatari();
+$numero_destinatari = $newsletter->destinatari()->count();
+$destinatari_senza_mail = $newsletter->getNumeroDestinatariSenzaEmail();
echo '
@@ -156,18 +153,16 @@ echo '
'.tr('Destinatari').'
- ('.$destinatari->count().')
- '.(($destinatari->where('email', '')->count() > 0) ? ' '.$destinatari->where('email', '')->count().'' : '')
- .'
+ ('.$numero_destinatari.')
+
+ '.(($destinatari_senza_mail > 0) ? ' '.$destinatari_senza_mail.'' : '')
+ .'
+
- ';
-
-$senza_consenso = 0;
-if (!$destinatari->isEmpty()) {
- echo '
-
+
+
'.tr('Ragione sociale').' |
@@ -179,74 +174,11 @@ if (!$destinatari->isEmpty()) {
# |
-
- ';
-
- $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 = '
-
-
- '.timestampFormat($mail->sent_at).'
- ';
- } else {
- $data = '
-
- '.tr('Non ancora inviata').'
- ';
- }
-
- echo '
- email) ? 'class="bg-danger"' : (empty($anagrafica->enable_newsletter) ? 'class="bg-warning"' : '')).'>
- '.Modules::link('Anagrafiche', $anagrafica->id, $descrizione).' |
- '.$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'].' |
- '.$anagrafica->tipo.' |
-
- '.((!empty($destinatario->email) ? '
- {[ "type": "text", "name": "email", "id": "email_'.rand(0, 99999).'", "readonly": "1", "class": "email-mask", "value": "'.$destinatario->email.'", "validation": "email" ]}' : ' '.tr('Indirizzo e-mail mancante').'')).' |
- '.$data.' |
-
- '.((!empty($anagrafica->enable_newsletter)) ? ' '.tr('Abilitato').'' : ' '.tr('Disabilitato').'').'
- |
-
-
-
-
- |
-
';
- }
-
- echo '
-
'.tr('Elimina tutti').'
- ';
-} else {
- echo '
-
- '.tr('Nessuna anagrafica collegata alla campagna').'.
-
';
-}
-
- echo '
+
@@ -262,14 +194,15 @@ if ($block_edit) {
$(document).ready(function() {
$("#receivers").parent().hide();
$("#receivers-form .btn").hide();
- });
+});
';
}
-else {
- echo '
+
+echo '
';
-}
diff --git a/modules/newsletter/src/Destinatario.php b/modules/newsletter/src/Destinatario.php
new file mode 100644
index 000000000..cc9d34e38
--- /dev/null
+++ b/modules/newsletter/src/Destinatario.php
@@ -0,0 +1,67 @@
+.
+ */
+
+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');
+ }
+}
diff --git a/modules/newsletter/src/Lista.php b/modules/newsletter/src/Lista.php
deleted file mode 100755
index 80c59f5ff..000000000
--- a/modules/newsletter/src/Lista.php
+++ /dev/null
@@ -1,141 +0,0 @@
-.
- */
-
-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';
- }
-}
diff --git a/modules/newsletter/src/Newsletter.php b/modules/newsletter/src/Newsletter.php
index 774370deb..335ffa4fa 100755
--- a/modules/newsletter/src/Newsletter.php
+++ b/modules/newsletter/src/Newsletter.php
@@ -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()
diff --git a/update/2_4_25.sql b/update/2_4_25.sql
index e73e6489e..1913ef099 100644
--- a/update/2_4_25.sql
+++ b/update/2_4_25.sql
@@ -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;