2021-08-03 14:59:42 +02:00
< ? php
2021-08-05 12:21:34 +02:00
use Models\Module ;
2021-08-03 14:59:42 +02:00
use Modules\Anagrafiche\Anagrafica ;
use Modules\Anagrafiche\Referente ;
use Modules\Anagrafiche\Sede ;
2023-09-08 17:30:27 +02:00
use Modules\Emails\Mail ;
2021-08-03 14:59:42 +02:00
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 )
2022-07-30 16:03:32 +02:00
-> whereIn ( 'record_id' , ( ! empty ( $results [ 'anagrafica' ]) ? $results [ 'anagrafica' ] -> toArray () : []));
2021-08-03 14:59:42 +02:00
})
-> orWhere ( function ( $query ) use ( $results ) {
$query -> where ( 'record_type' , '=' , Sede :: class )
2022-07-30 16:03:32 +02:00
-> whereIn ( 'record_id' , ( ! empty ( $results [ 'sede' ]) ? $results [ 'sede' ] -> toArray () : []));
2021-08-03 14:59:42 +02:00
})
-> orWhere ( function ( $query ) use ( $results ) {
$query -> where ( 'record_type' , '=' , Referente :: class )
2022-07-30 16:03:32 +02:00
-> whereIn ( 'record_id' , ( ! empty ( $results [ 'referente' ]) ? $results [ 'referente' ] -> toArray () : []));
2021-08-03 14:59:42 +02:00
});
}
// 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 ();
2021-08-05 12:21:34 +02:00
$modulo_anagrafiche = Module :: pool ( 'Anagrafiche' );
2021-08-03 14:59:42 +02:00
$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 ,
2021-08-05 12:21:34 +02:00
'validation' => 'email|' . $modulo_anagrafiche -> id . '|' . $destinatario -> record_id ,
2021-08-03 14:59:42 +02:00
]) :
'<span class="text-danger"><i class="fa fa-close"></i> ' . tr ( 'Indirizzo e-mail mancante' ) . '</span>'
) . '
</ div > ' ,
];
// Informazioni di invio
if ( empty ( $lista )) {
2023-09-08 17:30:27 +02:00
$mail_id = $destinatario -> id_email ;
$mail = Mail :: find ( $mail_id );
if ( ! empty ( $mail ) && ! empty ( $mail -> sent_at )) {
2021-08-03 14:59:42 +02:00
$info_invio = '
2023-07-25 15:59:06 +02:00
< span class = " text-success " >
2023-09-08 17:30:27 +02:00
< i class = " fa fa-paper-plane " ></ i > '.timestampFormat($mail->sent_at).'
2023-07-25 15:59:06 +02:00
</ span > ' ;
2021-08-03 14:59:42 +02:00
} else {
$info_invio = '
2023-07-25 15:59:06 +02:00
< span class = " text-info " >
< i class = " fa fa-clock-o " ></ i > '.tr(' Non ancora inviata ').'
</ span > ' ;
2021-08-03 14:59:42 +02:00
}
$riga [] = '<div class="text-center">' . $info_invio . '</div>' ;
}
$riga = array_merge ( $riga , [
'<div class="text-center">' .
2021-09-23 17:55:58 +02:00
( ! empty ( $origine -> enable_newsletter ) ?
2021-08-03 14:59:42 +02:00
'<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 > ' ,
2021-09-23 17:55:58 +02:00
'<div class="text-center">' . ( empty ( $lista ) && ! empty ( $origine -> email ) && ! empty ( $origine -> enable_newsletter ) ? '
2021-09-23 10:04:44 +02:00
< a class = " btn btn-warning btn-xs " data - type = " '.get_class( $origine ).' " data - id = " '. $origine->id .' " data - email = " '. $origine->email .' " onclick = " testInvio(this) " >
< i class = " fa fa-paper-plane " ></ i >
</ a > ' : ' ').'
2021-08-03 14:59:42 +02:00
< 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' )),
]);