Skip to content

Commit 8ce9926

Browse files
feat: bulk crea lista in anagrafiche
1 parent 908cc9f commit 8ce9926

5 files changed

Lines changed: 124 additions & 4 deletions

File tree

modules/anagrafiche/bulk.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Modules\Anagrafiche\Anagrafica;
2222
use Modules\Anagrafiche\Export\CSV;
2323
use Modules\Anagrafiche\Tipo;
24+
use Modules\ListeNewsletter\Lista;
25+
use Util\Query;
2426

2527
include_once __DIR__.'/../../core.php';
2628

@@ -185,6 +187,54 @@
185187
// Download del file
186188
download($file, $filename);
187189

190+
break;
191+
192+
case 'crea-lista':
193+
$lista = Lista::build(post('nome_lista'));
194+
$lista->setTranslation('title', post('nome_lista'));
195+
$modalita_dinamica = post('modalita_dinamica');
196+
$includi_disiscritti = post('includi_disiscritti');
197+
198+
// Se modalità dinamica è attiva, salvo la query, altrimenti la lista sarà statica
199+
if ($modalita_dinamica) {
200+
// Aggiungo i filtri di ricerca applicati nel modulo
201+
$where = [];
202+
if (count(getSearchValues($id_module)) > 0) {
203+
foreach (getSearchValues($id_module) as $key => $value) {
204+
$where[$key] = $value;
205+
}
206+
}
207+
$query = Query::getQuery($structure, $where);
208+
$pos = strpos($query, 'SELECT');
209+
if ($pos !== false) {
210+
$query = substr_replace($query, "SELECT 'Modules\\\\\\Anagrafiche\\\\\\Anagrafica' AS tipo_lista, ", $pos, 6);
211+
}
212+
213+
// Se non includo i disiscritti, aggiungo il filtro per enable_newsletter = 1
214+
if (!$includi_disiscritti) {
215+
$query = str_replace('1=1', '1=1 AND an_anagrafiche.enable_newsletter = 1', $query);
216+
}
217+
218+
if (check_query($query)) {
219+
$lista->query = html_entity_decode($query);
220+
}
221+
} else {
222+
foreach ($id_records as $id) {
223+
$anagrafica = Anagrafica::find($id);
224+
if (!$includi_disiscritti && !$anagrafica->enable_newsletter) {
225+
continue;
226+
}
227+
$dbo->insert("em_list_receiver", [
228+
'id_list' => $lista->id,
229+
'record_type' => Anagrafica::class,
230+
'record_id' => $id,
231+
]);
232+
}
233+
}
234+
$lista->save();
235+
236+
flash()->info(tr('Lista creata correttamente!'));
237+
188238
break;
189239
}
190240

@@ -246,4 +296,16 @@
246296
],
247297
];
248298

299+
$operations['crea-lista'] = [
300+
'text' => '<span><i class="fa fa-envelope"></i> '.tr('Crea lista').'</span>',
301+
'data' => [
302+
'msg' => tr('Vuoi creare una nuova lista?').'<br><br>{[ "type": "text", "label": "'.tr('Nome lista').'", "name": "nome_lista", "required": 1 ]}
303+
{[ "type": "checkbox", "label": "'.tr('Modalità').'", "name": "modalita_dinamica", "help": "'.tr('Se Dinamica prende in considerazione tutte le righe della tabella con i filtri applicati, mentre Statica esporta solo le righe selezionate.').'", "values": "Dinamica,Statica" ]}
304+
{[ "type": "checkbox", "label": "'.tr('Includi disiscritti').'", "name": "includi_disiscritti", "value": 0 ]}',
305+
'button' => tr('Procedi'),
306+
'class' => 'btn btn-lg btn-success',
307+
'blank' => false,
308+
],
309+
];
310+
249311
return $operations;

modules/liste_newsletter/actions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,35 @@
139139

140140
flash()->info(tr('Tutti i destinatari sono stati rimossi dalla lista newsletter!'));
141141

142+
break;
143+
144+
case 'export_list':
145+
$results = $lista->destinatari()->get();
146+
147+
// Creazione del file CSV
148+
$file = temp_file();
149+
$handle = fopen($file, 'w');
150+
151+
// Scrittura dell'intestazione
152+
fputcsv($handle, ['Email', 'Ragione sociale'], ';');
153+
154+
// Scrittura dei dati
155+
foreach ($results as $destinatario) {
156+
$origine = $destinatario->getOrigine();
157+
$anagrafica = $origine instanceof Anagrafica ? $origine : $origine->anagrafica;
158+
159+
fputcsv($handle, [
160+
$origine->email,
161+
$anagrafica->ragione_sociale,
162+
], ';');
163+
}
164+
fclose($handle);
165+
166+
$filename = 'export_lista_'.$lista->name.'.csv';
167+
168+
// Download del file
169+
download($file, $filename);
170+
exit;
171+
142172
break;
143173
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
5+
* Copyright (C) DevCode s.r.l.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
include_once __DIR__.'/../../core.php';
22+
23+
// Esporta lista
24+
echo '
25+
<button type="button" class="btn btn-primary ask" data-title="'.tr('Esportare questa lista?').'" data-msg="'.tr('Clicca su tasto Esporta per procedere.').'" data-op="export_list" data-button="'.tr('Esporta lista').'" data-class="btn btn-lg btn-primary" data-backto="record-edit" data-blank="true">
26+
<i class="fa fa-list"></i> '.tr('Esporta lista').'
27+
</button>';

modules/liste_newsletter/edit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
'required' => 0,
7878
'value' => $lista->query,
7979
'help' => tr("La query SQL deve restituire gli identificativi delle anagrafiche da inserire nella lista, sotto un campo di nome ''id''").'. <br>'.tr('Per esempio: _SQL_', [
80-
'_SQL_' => 'SELECT idanagrafica AS id, \'Modules\\\\Anagrafiche\\\\Anagrafica\' AS tipo FROM an_anagrafiche',
80+
'_SQL_' => 'SELECT idanagrafica AS id, \'Modules\\\\Anagrafiche\\\\Anagrafica\' AS tipo_lista FROM an_anagrafiche',
8181
]).'. <br>'.tr('Sono supportati i seguenti oggetti: _LIST_', [
8282
'_LIST_' => implode(', ', [
8383
slashes(Modules\Anagrafiche\Anagrafica::class),
@@ -214,7 +214,7 @@ function generaQuery() {
214214
var tipologia = $("#tipologia").val();
215215

216216

217-
var query = "SELECT an_anagrafiche.idanagrafica AS id, 'Modules\\\\Anagrafiche\\\\Anagrafica' AS tipo 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.id WHERE deleted_at IS NULL AND email!=''";
217+
var query = "SELECT an_anagrafiche.idanagrafica AS id, 'Modules\\\\Anagrafiche\\\\Anagrafica' AS tipo_lista 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.id WHERE deleted_at IS NULL AND email!=''";
218218

219219
if(tipologia) {
220220
query += " AND an_tipianagrafiche.id="+tipologia;

modules/liste_newsletter/src/Lista.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public function save(array $options = [])
6262
'id_list' => $this->id,
6363
]);
6464

65-
// Ricerca nuovi record
66-
$database->query('INSERT INTO em_list_receiver (id_list, record_id, record_type) '.preg_replace('/'.preg_quote('SELECT', '/').'/', 'SELECT '.prepare($this->id).',', $query, 1));
65+
// Ricerca nuovi record - usa subquery per limitare le colonne
66+
$wrapped_query = 'SELECT '.prepare($this->id).', subq.id, subq.tipo_lista FROM (' . $query . ') AS subq';
67+
$database->query('INSERT INTO em_list_receiver (id_list, record_id, record_type) ' . $wrapped_query);
6768
}
6869

6970
return $result;

0 commit comments

Comments
 (0)