2019-08-27 17:25:52 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-08-27 17:25:52 +02:00
|
|
|
|
2021-08-02 18:27:28 +02:00
|
|
|
use Modules\Anagrafiche\Anagrafica;
|
|
|
|
use Modules\Anagrafiche\Referente;
|
|
|
|
use Modules\Anagrafiche\Sede;
|
|
|
|
use Modules\Emails\Mail;
|
2019-08-29 10:25:14 +02:00
|
|
|
use Modules\Emails\Template;
|
2021-08-03 14:59:42 +02:00
|
|
|
use Modules\ListeNewsletter\Lista;
|
2019-08-27 17:25:52 +02:00
|
|
|
use Modules\Newsletter\Newsletter;
|
2021-09-23 10:04:44 +02:00
|
|
|
use Notifications\EmailNotification;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
include_once __DIR__.'/../../core.php';
|
|
|
|
|
|
|
|
switch (filter('op')) {
|
|
|
|
case 'add':
|
2019-08-29 10:25:14 +02:00
|
|
|
$template = Template::find(filter('id_template'));
|
2019-08-27 17:25:52 +02:00
|
|
|
$newsletter = Newsletter::build($user, $template, filter('name'));
|
|
|
|
|
|
|
|
$id_record = $newsletter->id;
|
|
|
|
|
|
|
|
flash()->info(tr('Nuova campagna newsletter creata!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update':
|
|
|
|
$newsletter->name = filter('name');
|
|
|
|
$newsletter->state = filter('state');
|
|
|
|
$newsletter->completed_at = filter('completed_at');
|
|
|
|
|
|
|
|
$newsletter->subject = filter('subject');
|
2022-07-30 19:49:15 +02:00
|
|
|
$newsletter->content = $_POST['content']; // post('content', true);
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
$newsletter->save();
|
|
|
|
|
|
|
|
flash()->info(tr('Campagna newsletter salvata!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
$newsletter->delete();
|
|
|
|
|
|
|
|
flash()->info(tr('Campagna newsletter rimossa!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send':
|
2021-09-23 10:04:44 +02:00
|
|
|
$newsletter = Newsletter::find($id_record);
|
2019-08-28 16:58:47 +02:00
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
$destinatari = $newsletter->destinatari();
|
2021-08-05 12:21:34 +02:00
|
|
|
$count = $destinatari->count();
|
2021-08-05 18:17:26 +02:00
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
2021-08-05 12:21:34 +02:00
|
|
|
$destinatario = $destinatari->skip($i)->first();
|
2021-09-23 10:04:44 +02:00
|
|
|
$mail = $newsletter->inviaDestinatario($destinatario);
|
2019-08-28 09:28:08 +02:00
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// Aggiornamento riferimento per la newsletter
|
2021-09-23 10:04:44 +02:00
|
|
|
if (!empty($mail)) {
|
|
|
|
$destinatario->id_email = $mail->id;
|
|
|
|
$destinatario->save();
|
|
|
|
}
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// Aggiornamento stato newsletter
|
2019-08-27 17:25:52 +02:00
|
|
|
$newsletter->state = 'WAIT';
|
|
|
|
$newsletter->save();
|
|
|
|
|
|
|
|
flash()->info(tr('Campagna newsletter in invio!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-09-23 10:04:44 +02:00
|
|
|
case 'test':
|
|
|
|
$receiver_id = post('id');
|
|
|
|
$receiver_type = post('type');
|
|
|
|
|
|
|
|
// Individuazione destinatario interessato
|
|
|
|
$newsletter = Newsletter::find($id_record);
|
|
|
|
$destinatario = $newsletter->destinatari()
|
|
|
|
->where('record_type', '=', $receiver_type)
|
|
|
|
->where('record_id', '=', $receiver_id)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
// Generazione email e tentativo di invio
|
|
|
|
$inviata = false;
|
|
|
|
if (!empty($destinatario)) {
|
|
|
|
$mail = $newsletter->inviaDestinatario($destinatario, true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$email = EmailNotification::build($mail, true);
|
|
|
|
$email->send();
|
|
|
|
|
|
|
|
$inviata = true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// $mail->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
'result' => $inviata,
|
|
|
|
]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2019-08-28 09:28:08 +02:00
|
|
|
case 'block':
|
|
|
|
$mails = $newsletter->emails;
|
|
|
|
|
|
|
|
foreach ($mails as $mail) {
|
2021-08-03 14:59:42 +02:00
|
|
|
if (!empty($mail->sent_at)) {
|
|
|
|
continue;
|
2019-08-28 09:28:08 +02:00
|
|
|
}
|
2021-08-03 14:59:42 +02:00
|
|
|
|
|
|
|
// 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();
|
2019-08-28 09:28:08 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// Aggiornamento stato newsletter
|
2019-08-28 09:28:08 +02:00
|
|
|
$newsletter->state = 'DEV';
|
|
|
|
$newsletter->save();
|
|
|
|
|
|
|
|
flash()->info(tr('Coda della campagna newsletter svuotata!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2019-08-27 17:25:52 +02:00
|
|
|
case 'add_receivers':
|
2021-08-02 18:27:28 +02:00
|
|
|
// Selezione manuale
|
|
|
|
$id_receivers = post('receivers');
|
|
|
|
foreach ($id_receivers as $id_receiver) {
|
|
|
|
list($tipo, $id) = explode('_', $id_receiver);
|
|
|
|
if ($tipo == 'anagrafica') {
|
|
|
|
$type = Anagrafica::class;
|
|
|
|
} elseif ($tipo == 'sede') {
|
|
|
|
$type = Sede::class;
|
|
|
|
} else {
|
|
|
|
$type = Referente::class;
|
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// Dati di registrazione
|
|
|
|
$data = [
|
2021-08-02 18:27:28 +02:00
|
|
|
'record_type' => $type,
|
|
|
|
'record_id' => $id,
|
2021-08-03 14:59:42 +02:00
|
|
|
'id_newsletter' => $newsletter->id,
|
2021-08-02 18:27:28 +02:00
|
|
|
];
|
2021-08-03 14:59:42 +02:00
|
|
|
|
|
|
|
// Aggiornamento destinatari
|
|
|
|
$registrato = $database->select('em_newsletter_receiver', '*', $data);
|
|
|
|
if (empty($registrato)) {
|
|
|
|
$database->insert('em_newsletter_receiver', $data);
|
|
|
|
}
|
2021-08-02 18:27:28 +02:00
|
|
|
}
|
2019-08-27 17:25:52 +02:00
|
|
|
|
2021-08-02 18:27:28 +02:00
|
|
|
// Selezione da lista newsletter
|
2019-09-24 10:09:29 +02:00
|
|
|
$id_list = post('id_list');
|
|
|
|
if (!empty($id_list)) {
|
2021-11-03 12:48:48 +01:00
|
|
|
|
|
|
|
//Aggiornamento della lista
|
2022-02-11 10:36:57 +01:00
|
|
|
$lista = Lista::find($id_list);
|
2021-11-03 12:48:48 +01:00
|
|
|
$query = $lista->query;
|
|
|
|
if (check_query($query)) {
|
|
|
|
$lista->query = html_entity_decode($query);
|
|
|
|
}
|
|
|
|
$lista->save();
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// 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));
|
2019-09-24 10:09:29 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
/*
|
|
|
|
// Controllo indirizzo e-mail presente
|
|
|
|
$destinatari = $newsletter->destinatari();
|
2021-08-02 18:27:28 +02:00
|
|
|
foreach ($destinatari as $destinatario) {
|
2021-08-03 14:59:42 +02:00
|
|
|
$anagrafica = $destinatario instanceof Anagrafica ? $destinatario : $destinatario->anagrafica;
|
2021-08-02 18:27:28 +02:00
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
if (!empty($destinatario->email)) {
|
|
|
|
$check = Validate::isValidEmail($destinatario->email);
|
2021-06-01 16:50:23 +02:00
|
|
|
|
|
|
|
if (empty($check['valid-format'])) {
|
2021-08-03 14:59:42 +02:00
|
|
|
$errors[] = $destinatario->email;
|
2021-06-01 16:50:23 +02:00
|
|
|
}
|
2021-07-07 07:57:10 +02:00
|
|
|
} else {
|
2021-08-03 14:59:42 +02:00
|
|
|
$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,
|
2021-06-01 16:50:23 +02:00
|
|
|
]);
|
2021-06-01 15:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
|
|
|
$message = '<ul>';
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
$message .= '<li>'.$error.'</li>';
|
|
|
|
}
|
|
|
|
$message .= '</ul>';
|
2021-08-03 14:59:42 +02:00
|
|
|
}*/
|
2021-06-01 15:17:52 +02:00
|
|
|
|
|
|
|
if (!empty($message)) {
|
|
|
|
flash()->warning(tr('Attenzione questi indirizzi e-mail non sembrano essere validi: _EMAIL_ ', [
|
|
|
|
'_EMAIL_' => $message,
|
|
|
|
]));
|
2021-07-07 07:57:10 +02:00
|
|
|
} else {
|
2021-06-01 15:17:52 +02:00
|
|
|
flash()->info(tr('Nuovi destinatari aggiunti correttamente alla newsletter!'));
|
|
|
|
}
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'remove_receiver':
|
2021-08-02 18:27:28 +02:00
|
|
|
$receiver_id = post('id');
|
|
|
|
$receiver_type = post('type');
|
2019-08-27 17:25:52 +02:00
|
|
|
|
2021-08-02 18:27:28 +02:00
|
|
|
$database->delete('em_newsletter_receiver', [
|
|
|
|
'record_type' => $receiver_type,
|
|
|
|
'record_id' => $receiver_id,
|
|
|
|
'id_newsletter' => $newsletter->id,
|
|
|
|
]);
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
flash()->info(tr('Destinatario rimosso dalla newsletter!'));
|
|
|
|
|
|
|
|
break;
|
2021-05-31 16:54:48 +02:00
|
|
|
|
2021-08-02 18:27:28 +02:00
|
|
|
case 'remove_all_receivers':
|
|
|
|
$database->delete('em_newsletter_receiver', [
|
|
|
|
'id_newsletter' => $newsletter->id,
|
|
|
|
]);
|
2021-06-01 17:22:27 +02:00
|
|
|
|
|
|
|
flash()->info(tr('Tutti i destinatari sono stati rimossi dalla newsletter!'));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2021-05-31 16:54:48 +02:00
|
|
|
// Duplica newsletter
|
|
|
|
case 'copy':
|
|
|
|
$new = $newsletter->replicate();
|
|
|
|
$new->state = 'DEV';
|
|
|
|
$new->completed_at = null;
|
|
|
|
$new->save();
|
|
|
|
|
|
|
|
$id_record = $new->id;
|
|
|
|
|
|
|
|
flash()->info(tr('Newsletter duplicata correttamente!'));
|
|
|
|
|
|
|
|
break;
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|