openstamanager/modules/newsletter/actions.php

177 lines
4.8 KiB
PHP
Raw Normal View History

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
2019-08-29 10:25:14 +02:00
use Modules\Emails\Template;
2019-09-24 10:09:29 +02:00
use Modules\Newsletter\Lista;
2019-08-27 17:25:52 +02:00
use Modules\Newsletter\Newsletter;
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');
2021-01-27 11:44:22 +01:00
$newsletter->content = $_POST['content']; //filter('content');
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':
$anagrafiche = $newsletter->anagrafiche;
$template = $newsletter->template;
2019-08-28 16:58:47 +02:00
$uploads = $newsletter->uploads()->pluck('id');
2019-08-27 17:25:52 +02:00
foreach ($anagrafiche as $anagrafica) {
2020-06-22 14:22:24 +02:00
if (empty($anagrafica['email']) || empty($anagrafica['enable_newsletter'])) {
2019-08-27 17:25:52 +02:00
continue;
}
2019-08-29 10:25:14 +02:00
$mail = \Modules\Emails\Mail::build($user, $template, $anagrafica->id);
2019-08-27 17:25:52 +02:00
$mail->addReceiver($anagrafica['email']);
$mail->subject = $newsletter->subject;
$mail->content = $newsletter->content;
2019-08-29 10:25:14 +02:00
$mail->id_newsletter = $newsletter->id;
2019-08-28 16:58:47 +02:00
foreach ($uploads as $upload) {
2019-08-29 10:25:14 +02:00
$mail->addUpload($upload);
2019-08-28 16:58:47 +02:00
}
2019-08-27 17:25:52 +02:00
$mail->save();
2019-08-28 09:28:08 +02:00
2019-08-28 16:58:47 +02:00
$newsletter->anagrafiche()->updateExistingPivot($anagrafica->id, ['id_email' => $mail->id]);
2019-08-27 17:25:52 +02:00
}
$newsletter->state = 'WAIT';
$newsletter->save();
flash()->info(tr('Campagna newsletter in invio!'));
break;
2019-08-28 09:28:08 +02:00
case 'block':
$mails = $newsletter->emails;
foreach ($mails as $mail) {
if (empty($mail->sent_at)) {
$newsletter->emails()->updateExistingPivot($mail->id, ['id_email' => null], false);
$mail->delete();
}
}
$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':
$receivers = post('receivers');
2019-09-24 10:09:29 +02:00
$id_list = post('id_list');
if (!empty($id_list)) {
$list = Lista::find($id_list);
$receivers = $list->anagrafiche->pluck('idanagrafica');
}
$newsletter->anagrafiche()->syncWithoutDetaching($receivers);
2019-08-27 17:25:52 +02:00
//Controllo indirizzo e-mail aggiunto
foreach ($newsletter->anagrafiche as $anagrafica) {
$check = Validate::isValidEmail($anagrafica['email']);
if (empty($check['valid-format'])) {
$errors[] = $anagrafica['email'];
}
}
if (!empty($errors)) {
$message = '<ul>';
foreach ($errors as $error) {
$message .= '<li>'.$error.'</li>';
}
$message .= '</ul>';
}
if (!empty($message)) {
flash()->warning(tr('Attenzione questi indirizzi e-mail non sembrano essere validi: _EMAIL_ ', [
'_EMAIL_' => $message,
]));
}else{
flash()->info(tr('Nuovi destinatari aggiunti correttamente alla newsletter!'));
}
2019-08-27 17:25:52 +02:00
break;
case 'remove_receiver':
$receiver = post('id');
$newsletter->anagrafiche()->detach($receiver);
flash()->info(tr('Destinatario rimosso 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
}