. */ use Modules\Anagrafiche\Anagrafica; use Modules\Anagrafiche\Referente; use Modules\Anagrafiche\Sede; use Modules\Emails\Mail; use Modules\Emails\Template; use Modules\ListeNewsletter\Lista; use Modules\Newsletter\Newsletter; use Respect\Validation\Validator as v; include_once __DIR__.'/../../core.php'; switch (filter('op')) { case 'add': $template = Template::find(filter('id_template')); $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'); $newsletter->content = $_POST['content']; //filter('content'); $newsletter->save(); flash()->info(tr('Campagna newsletter salvata!')); break; case 'delete': $newsletter->delete(); flash()->info(tr('Campagna newsletter rimossa!')); break; case 'send': $template = $newsletter->template; $uploads = $newsletter->uploads()->pluck('id'); $destinatari = $newsletter->destinatari(); $count = $destinatari->count(); for ($i = 0; $i < $count; ++$i) { $destinatario = $destinatari->skip($i)->first(); $origine = $destinatario->getOrigine(); $anagrafica = $origine instanceof Anagrafica ? $origine : $origine->anagrafica; $abilita_newsletter = $anagrafica->enable_newsletter; $email = $destinatario->email; if (empty($email) || empty($abilita_newsletter) || !v::email()->validate($email)) { continue; } // Inizializzazione email $mail = Mail::build($user, $template, $anagrafica->id); // Completamento informazioni $mail->addReceiver($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(); // Aggiornamento riferimento per la newsletter $destinatario->id_email = $mail->id; $destinatario->save(); } // Aggiornamento stato newsletter $newsletter->state = 'WAIT'; $newsletter->save(); flash()->info(tr('Campagna newsletter in invio!')); break; case 'block': $mails = $newsletter->emails; foreach ($mails as $mail) { 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(); flash()->info(tr('Coda della campagna newsletter svuotata!')); break; case 'add_receivers': // 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; } // Dati di registrazione $data = [ 'record_type' => $type, 'record_id' => $id, 'id_newsletter' => $newsletter->id, ]; // Aggiornamento destinatari $registrato = $database->select('em_newsletter_receiver', '*', $data); if (empty($registrato)) { $database->insert('em_newsletter_receiver', $data); } } // 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[] = $destinatario->email; } } else { $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, ]); } } if (!empty($errors)) { $message = '