From 5412002e55136060f69ba9b54613f3d64c2cbd7c Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Tue, 24 Sep 2019 10:09:29 +0200 Subject: [PATCH] Supporto liste per le newsletter --- lib/functions.php | 14 +++ modules/liste_newsletter/actions.php | 55 ++++++++++++ modules/liste_newsletter/add.php | 22 +++++ modules/liste_newsletter/edit.php | 122 ++++++++++++++++++++++++++ modules/liste_newsletter/init.php | 11 +++ modules/newsletter/actions.php | 11 ++- modules/newsletter/ajax/select.php | 29 +++++- modules/newsletter/edit.php | 104 +++++++++++++++------- modules/newsletter/src/Lista.php | 48 ++++++++++ modules/newsletter/src/Newsletter.php | 2 +- modules/viste/actions.php | 14 --- update/2_4_11.sql | 37 ++++++-- update/tables.php | 3 + 13 files changed, 417 insertions(+), 55 deletions(-) create mode 100644 modules/liste_newsletter/actions.php create mode 100644 modules/liste_newsletter/add.php create mode 100644 modules/liste_newsletter/edit.php create mode 100644 modules/liste_newsletter/init.php create mode 100644 modules/newsletter/src/Lista.php diff --git a/lib/functions.php b/lib/functions.php index 01c4a7f3c..6a8564500 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -375,3 +375,17 @@ function clean($string, $permitted = '') { return preg_replace('/[^A-Za-z0-9'.$permitted.']/', '', $string); // Removes special chars. } + +function check_query($query) +{ + $query = mb_strtoupper($query); + + $blacklist = ['INSERT', 'UPDATE', 'TRUNCATE', 'DELETE', 'DROP', 'GRANT', 'CREATE', 'REVOKE']; + foreach ($blacklist as $value) { + if (preg_match("/\b".preg_quote($value)."\b/", $query)) { + return false; + } + } + + return true; +} diff --git a/modules/liste_newsletter/actions.php b/modules/liste_newsletter/actions.php new file mode 100644 index 000000000..528a37e39 --- /dev/null +++ b/modules/liste_newsletter/actions.php @@ -0,0 +1,55 @@ +id; + + flash()->info(tr('Nuova lista newsletter creata!')); + + break; + + case 'update': + $lista->name = filter('name'); + $lista->description = filter('description'); + + $query = filter('query'); + if (check_query($query)) { + $lista->query = $query; + } + + $lista->save(); + + flash()->info(tr('Lista newsletter salvata!')); + + break; + + case 'delete': + $lista->delete(); + + flash()->info(tr('Lista newsletter rimossa!')); + + break; + + case 'add_receivers': + $receivers = post('receivers'); + + $lista->anagrafiche()->syncWithoutDetaching($receivers); + + flash()->info(tr('Aggiunti nuovi destinatari alla newsletter!')); + + break; + + case 'remove_receiver': + $receiver = post('id'); + + $lista->anagrafiche()->detach($receiver); + + flash()->info(tr('Destinatario rimosso dalla newsletter!')); + + break; +} diff --git a/modules/liste_newsletter/add.php b/modules/liste_newsletter/add.php new file mode 100644 index 000000000..0c0a5dd94 --- /dev/null +++ b/modules/liste_newsletter/add.php @@ -0,0 +1,22 @@ + + + + +
+
+ {[ "type": "text", "label": "'.tr('Nome').'", "name": "name", "required": 1 ]} +
+
+ + +
+
+ +
+
+'; diff --git a/modules/liste_newsletter/edit.php b/modules/liste_newsletter/edit.php new file mode 100644 index 000000000..176fe40f0 --- /dev/null +++ b/modules/liste_newsletter/edit.php @@ -0,0 +1,122 @@ + + + + + +
+
+

'.tr('Dati campagna').'

+
+ +
+
+
+ {[ "type": "text", "label": "'.tr('Nome').'", "name": "name", "required": 1, "value": "$name$" ]} +
+
+ +
+
+ {[ "type": "textarea", "label": "'.tr('Descrizione').'", "name": "description", "required": 0, "value": "$description$" ]} +
+
+ +
+
+ {[ "type": "textarea", "label": "'.tr('Query dinamica').'", "name": "query", "required": 0, "value": "$query$", "help": "'.tr("La query SQL deve restituire gli identificativi delle anagrafiche da inserire nella lista, sotto un campo di nome ''id''").'. '.tr('Per esempio: _SQL_', [ + '_SQL_' => 'SELECT idanagrafica AS id FROM an_anagrafiche', + ]).'" ]} +
+
+
+
+ + +
+ + + + +
+
+

'.tr('Aggiunta destinatari').'

+
+ +
+
+
+ {[ "type": "select", "label": "'.tr('Destinatari').'", "name": "receivers[]", "ajax-source": "anagrafiche_newsletter", "multiple": 1, "disabled": '.intval(!empty($lista->query)).' ]} +
+
+ +
+
+ +
+
+
+
+
'; + +$anagrafiche = $lista->anagrafiche; + +echo ' + +
+
+

+ '.tr('Destinatari').' + '.$anagrafiche->count().' +

+
+ +
'; + +if (!$anagrafiche->isEmpty()) { + echo ' + + + + + + + + + + '; + + foreach ($anagrafiche as $anagrafica) { + echo ' + email) ? 'class="bg-danger"' : '').'> + + + + '; + } + + echo ' + +
'.tr('Nome').''.tr('Indirizzo').'#
'.Modules::link('Anagrafiche', $anagrafica->id, $anagrafica->ragione_sociale).''.$anagrafica->email.' + query) ? 'disabled' : '').'> + + +
'; +} else { + echo ' +

'.tr('Nessuna anagrafica collegata alla lista').'.

'; +} + + echo ' +
+
+ + + '.tr('Elimina').' +'; diff --git a/modules/liste_newsletter/init.php b/modules/liste_newsletter/init.php new file mode 100644 index 000000000..5ca7aa7a1 --- /dev/null +++ b/modules/liste_newsletter/init.php @@ -0,0 +1,11 @@ +toArray(); +} diff --git a/modules/newsletter/actions.php b/modules/newsletter/actions.php index 9cc690061..60dd1060f 100644 --- a/modules/newsletter/actions.php +++ b/modules/newsletter/actions.php @@ -1,6 +1,7 @@ anagrafiche()->attach($receivers); + $id_list = post('id_list'); + if (!empty($id_list)) { + $list = Lista::find($id_list); + $receivers = $list->anagrafiche->pluck('idanagrafica'); + } - flash()->info(tr('Nuovi destinatari della newsletter aggiunti!')); + $newsletter->anagrafiche()->syncWithoutDetaching($receivers); + + flash()->info(tr('Aggiunti nuovi destinatari alla newsletter!')); break; diff --git a/modules/newsletter/ajax/select.php b/modules/newsletter/ajax/select.php index 6101ceb60..16313a79f 100644 --- a/modules/newsletter/ajax/select.php +++ b/modules/newsletter/ajax/select.php @@ -4,7 +4,7 @@ include_once __DIR__.'/../../../core.php'; switch ($resource) { case 'anagrafiche_newsletter': - $query = "SELECT an_anagrafiche.idanagrafica AS id, CONCAT_WS('', ragione_sociale, IF(citta !='' OR provincia != '', CONCAT(' (', citta, IF(provincia!='', CONCAT(' ', provincia), ''), ')'), ''), IF(deleted_at IS NULL, '', ' (".tr('eliminata').")')) AS descrizione, `an_tipianagrafiche`.`descrizione` AS optgroup FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY `optgroup` ASC, ragione_sociale ASC"; + $query = "SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta != '' OR provincia != '', CONCAT(' (', citta, IF(provincia != '', provincia, ''), ')'), ''), ' [', email, ']') AS descrizione, `an_tipianagrafiche`.`descrizione` AS optgroup FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY `optgroup` ASC, ragione_sociale ASC"; foreach ($elements as $element) { $filter[] = 'an_anagrafiche.idanagrafica='.prepare($element); @@ -19,6 +19,7 @@ switch ($resource) { $search_fields[] = 'ragione_sociale LIKE '.prepare('%'.$search.'%'); $search_fields[] = 'citta LIKE '.prepare('%'.$search.'%'); $search_fields[] = 'provincia LIKE '.prepare('%'.$search.'%'); + $search_fields[] = 'email LIKE '.prepare('%'.$search.'%'); } // Aggiunta filtri di ricerca @@ -46,4 +47,30 @@ switch ($resource) { ]; } break; + + case 'liste_newsletter': + $query = "SELECT id, CONCAT(name, ' (', (SELECT COUNT(*) FROM em_list_anagrafica WHERE em_lists.id = em_list_anagrafica.id_list), ' destinatari)') AS descrizione FROM em_lists |where| ORDER BY `name` ASC"; + + foreach ($elements as $element) { + $filter[] = 'id='.prepare($element); + } + + if (empty($filter)) { + $where[] = 'deleted_at IS NULL'; + } + + if (!empty($search)) { + $search_fields[] = 'name LIKE '.prepare('%'.$search.'%'); + } + + // Aggiunta filtri di ricerca + if (!empty($search_fields)) { + $where[] = '('.implode(' OR ', $search_fields).')'; + } + + if (!empty($filter)) { + $where[] = '('.implode(' OR ', $filter).')'; + } + + break; } diff --git a/modules/newsletter/edit.php b/modules/newsletter/edit.php index 7202ad326..7864da9fe 100644 --- a/modules/newsletter/edit.php +++ b/modules/newsletter/edit.php @@ -74,37 +74,79 @@ echo ' -
-
-

'.tr('Destinatari').'

+
+
+

'.tr('Aggiunta destinatari').'

-
+
-
+
{[ "type": "select", "label": "'.tr('Destinatari').'", "name": "receivers[]", "ajax-source": "anagrafiche_newsletter", "multiple": 1 ]}
-
+
+ {[ "type": "select", "label": "'.tr('Lista').'", "name": "id_list", "ajax-source": "liste_newsletter" ]} +
+
+ +
+
-
'; +
+
+
+ +'; $anagrafiche = $newsletter->anagrafiche; + +echo ' + +
+
+

+ '.tr('Destinatari').' + '.$anagrafiche->count().' +

+
+ +
'; + if (!$anagrafiche->isEmpty()) { echo ' - - - - - - - - - - '; +
'.tr('Nome').''.tr('Data di invio').'#
+ + + + + + + + + + '; foreach ($anagrafiche as $anagrafica) { $mail_id = $anagrafica->pivot->id_email; @@ -116,29 +158,29 @@ if (!$anagrafiche->isEmpty()) { } echo ' - - - - - '; + email) ? 'class="bg-danger"' : '').'> + + + + + '; } echo ' - -
'.tr('Nome').''.tr('Indirizzo').''.tr('Data di invio').'#
'.Modules::link('Anagrafiche', $anagrafica->id, $anagrafica->ragione_sociale).''.$data.' - - - -
'.Modules::link('Anagrafiche', $anagrafica->id, $anagrafica->ragione_sociale).''.$anagrafica->email.''.$data.' + + + +
'; + + '; } else { echo ' -

'.tr('Nessuna anagrafica collegata alla campagna').'.

'; +

'.tr('Nessuna anagrafica collegata alla campagna').'.

'; } echo ' -
- +
{( "name": "filelist_and_upload", "id_module": "$id_module$", "id_record": "$id_record$" )} diff --git a/modules/newsletter/src/Lista.php b/modules/newsletter/src/Lista.php new file mode 100644 index 000000000..fd4587341 --- /dev/null +++ b/modules/newsletter/src/Lista.php @@ -0,0 +1,48 @@ +name = $name; + + $model->save(); + + return $model; + } + + public function save(array $options = []) + { + $result = parent::save($options); + + $query = $this->query; + if (!empty($query)) { + $results = database()->fetchArray($query); + + $anagrafiche = array_column($results, 'id'); + $this->anagrafiche()->sync($anagrafiche); + } + + return $result; + } + + // Relazione Eloquent + + public function anagrafiche() + { + return $this->belongsToMany(Anagrafica::class, 'em_list_anagrafica', 'id_list', 'id_anagrafica')->withTrashed(); + } +} diff --git a/modules/newsletter/src/Newsletter.php b/modules/newsletter/src/Newsletter.php index c5ebebc37..904457948 100644 --- a/modules/newsletter/src/Newsletter.php +++ b/modules/newsletter/src/Newsletter.php @@ -67,7 +67,7 @@ class Newsletter extends Model public function anagrafiche() { - return $this->belongsToMany(Anagrafica::class, 'em_newsletter_anagrafica', 'id_newsletter', 'id_anagrafica')->withPivot('id_email'); + return $this->belongsToMany(Anagrafica::class, 'em_newsletter_anagrafica', 'id_newsletter', 'id_anagrafica')->withPivot('id_email')->withTrashed(); } public function emails() diff --git a/modules/viste/actions.php b/modules/viste/actions.php index 8c68e6da6..fc80dda2d 100644 --- a/modules/viste/actions.php +++ b/modules/viste/actions.php @@ -4,20 +4,6 @@ include_once __DIR__.'/../../core.php'; use Models\Module; -function check_query($query) -{ - $query = mb_strtoupper($query); - - $blacklist = ['INSERT', 'UPDATE', 'TRUNCATE', 'DELETE', 'DROP', 'GRANT', 'CREATE', 'REVOKE']; - foreach ($blacklist as $value) { - if (preg_match("/\b".preg_quote($value)."\b/", $query)) { - return false; - } - } - - return true; -} - switch (filter('op')) { case 'update': $options2 = htmlspecialchars_decode(post('options2'), ENT_QUOTES); diff --git a/update/2_4_11.sql b/update/2_4_11.sql index 42553d363..f1502a792 100644 --- a/update/2_4_11.sql +++ b/update/2_4_11.sql @@ -267,11 +267,11 @@ UPDATE `zz_modules` SET `use_notes` = 1 WHERE `name` IN ('Anagrafiche', 'Interve UPDATE `zz_modules` SET `use_checklists` = 1 WHERE `name` IN ('Interventi', 'MyImpianti'); -- Modulo per i template delle Checklist -INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Checklists', 'Checklists', 'checklists', 'SELECT |select| FROM `zz_checklists` WHERE 1=1 HAVING 2=2', '', 'fa fa-check-square-o', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Strumenti'), '1', '1'); +INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Checklists', 'Checklists', 'checklists', 'SELECT |select| FROM `zz_checklists` WHERE 1=1 HAVING 2=2', '', 'fa fa-check-square-o', '2.4.11', '2.*', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Strumenti'), '1', '1'); INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`) VALUES ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Checklists'), 'id', 'id', 1, 0, 0, 1, 0), -((SELECT `id` FROM `zz_modules` WHERE `name` = 'Checklists'), 'Nome', 'name', 2, 1, 0, 0, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Checklists'), 'Nome', 'name', 2, 1, 0, 1, 1), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Checklists'), 'Modulo', '(SELECT name FROM zz_modules WHERE id = zz_checklists.id_module)', 5, 1, 0, 1, 1), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Checklists'), 'Plugin', '(SELECT name FROM zz_plugins WHERE id = zz_checklists.id_plugin)', 5, 1, 0, 1, 1); @@ -399,11 +399,11 @@ ALTER TABLE `zz_hooks` CHANGE `id_module` `id_module` INT(11) NULL; INSERT INTO `zz_hooks` (`id`, `name`, `class`, `frequency`, `id_module`) VALUES (NULL, 'Email', 'Modules\\Emails\\EmailHook', '1 minute', NULL); -- Modulo Newsletter -INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Newsletter', 'Newsletter', 'newsletter', 'SELECT |select| FROM `em_newsletters` WHERE 1=1 AND deleted_at IS NULL HAVING 2=2', '', 'fa fa-newspaper-o ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); +INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Newsletter', 'Newsletter', 'newsletter', 'SELECT |select| FROM `em_newsletters` WHERE 1=1 AND deleted_at IS NULL HAVING 2=2', '', 'fa fa-newspaper-o ', '2.4.11', '2.*', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`) VALUES ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'id', 'id', 1, 0, 0, 1, 0), -((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Nome', 'name', 2, 1, 0, 0, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Nome', 'name', 2, 1, 0, 1, 1), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Template', '(SELECT name FROM em_templates WHERE id = em_newsletters.id_template)', 3, 1, 0, 1, 1), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Completato', 'IF(completed_at IS NULL, ''No'', ''Si'')', 4, 1, 0, 1, 1); @@ -414,11 +414,11 @@ FROM `em_emails` INNER JOIN `zz_users` ON `zz_users`.`id` = `em_emails`.`created_by` WHERE 1=1 AND (`em_emails`.`created_at` BETWEEN ''|period_start|'' AND ''|period_end|'' OR `em_emails`.`sent_at` IS NULL) HAVING 2=2 -ORDER BY `em_emails`.`created_at` DESC', '', 'fa fa-spinner ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); +ORDER BY `em_emails`.`created_at` DESC', '', 'fa fa-spinner ', '2.4.11', '2.*', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1'); INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`, `format`) VALUES ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'em_emails.id', 'id', 1, 0, 0, 1, 0, 0), -((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Oggetto', 'em_emails.subject', 2, 1, 0, 0, 1, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Oggetto', 'em_emails.subject', 2, 1, 0, 1, 1, 0), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Contenuto', 'em_emails.content', 3, 1, 0, 0, 1, 0), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Template', 'em_templates.name', 3, 1, 0, 1, 1, 0), ((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'Data invio', 'em_emails.sent_at', 4, 1, 0, 1, 1, 1), @@ -653,3 +653,28 @@ INSERT INTO `zz_plugins` (`id`, `name`, `title`, `idmodule_from`, `idmodule_to`, INSERT INTO `zz_settings` (`id`, `nome`, `valore`, `tipo`, `editable`, `sezione`, `order`) VALUES (NULL, 'Iva per lettere d''intento', '', 'query=SELECT id, descrizione FROM `co_iva` WHERE codice_natura_fe = ''N3'' AND deleted_at IS NULL ORDER BY descrizione ASC', 1, 'Fatturazione', 11); + +-- Liste per le newsletter +CREATE TABLE IF NOT EXISTS `em_lists` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `description` TEXT, + `query` TEXT, + `deleted_at` TIMESTAMP NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB; + +CREATE TABLE IF NOT EXISTS `em_list_anagrafica` ( + `id_list` int(11) NOT NULL, + `id_anagrafica` int(11) NOT NULL, + FOREIGN KEY (`id_list`) REFERENCES `em_newsletters`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_anagrafica`) REFERENCES `an_anagrafiche`(`idanagrafica`) ON DELETE CASCADE +) ENGINE=InnoDB; + +INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Liste newsletter', 'Liste', 'liste_newsletter', 'SELECT |select| FROM `em_lists` WHERE deleted_at IS NULL AND 1=1 HAVING 2=2', '', 'fa fa-list', '2.4.11', '2.*', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '0'); + +INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`) VALUES +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Liste newsletter'), 'id', 'id', 1, 0, 0, 1, 0), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Liste newsletter'), 'Nome', 'name', 2, 1, 0, 1, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Liste newsletter'), 'Descrizione', 'description', 3, 1, 0, 1, 1), +((SELECT `id` FROM `zz_modules` WHERE `name` = 'Liste newsletter'), 'Dinamica', 'IF(query IS NULL, ''No'', ''Si'')', 4, 1, 0, 1, 1); diff --git a/update/tables.php b/update/tables.php index 37b7a0362..66e327cc1 100644 --- a/update/tables.php +++ b/update/tables.php @@ -12,6 +12,7 @@ return [ 'an_zone', 'co_banche', 'co_contratti', + 'co_dichiarazioni_intento', 'co_promemoria', 'co_contratti_tipiintervento', 'co_documenti', @@ -52,6 +53,8 @@ return [ 'em_accounts', 'em_templates', 'em_newsletters', + 'em_lists', + 'em_list_anagrafica', 'em_emails', 'em_email_receiver', 'em_email_upload',