openstamanager/modules/newsletter/ajax/select.php

126 lines
5.9 KiB
PHP
Raw Normal View History

2019-08-29 15:09:01 +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-29 15:09:01 +02:00
include_once __DIR__.'/../../../core.php';
switch ($resource) {
case 'destinatari_newsletter':
// Gestione campi di ricerca
2019-08-29 15:09:01 +02:00
if (!empty($search)) {
$search_fields[] = '|nome| LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.citta LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.provincia LIKE '.prepare('%'.$search.'%');
$search_fields[] = '|table|.email LIKE '.prepare('%'.$search.'%');
2019-08-29 15:09:01 +02:00
}
// Aggiunta filtri di ricerca
$where = empty($search_fields) ? '1=1' : '('.implode(' OR ', $search_fields).')';
$destinatari = collect();
// Gestione anagrafiche come destinatari
2024-02-05 17:07:33 +01:00
$query = "SELECT CONCAT('anagrafica_', `an_anagrafiche`.`idanagrafica`) AS id,
CONCAT(`an_anagrafiche`.`ragione_sociale`, IF(`an_anagrafiche`.`citta` != '' OR `an_anagrafiche`.`provincia` != '', CONCAT(' (', `an_anagrafiche`.`citta`, IF(`an_anagrafiche`.`provincia` != '', `an_anagrafiche`.`provincia`, ''), ')'), ''), ' [', `email`, ']') AS text,
`an_tipianagrafiche_lang`.`name` AS optgroup
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`
LEFT JOIN `an_tipianagrafiche_lang` ON (`an_tipianagrafiche_lang`.`id_record` = `an_tipianagrafiche`.id` AND `an_tipianagrafiche_lang`.`id_lang` = ".prepare(setting('Lingua')).')
WHERE
`an_anagrafiche`.`deleted_at` IS NULL AND `an_anagrafiche`.`enable_newsletter` = 1 AND 1=1
ORDER BY
`optgroup` ASC, `ragione_sociale` ASC';
$query = str_replace('1=1', !empty($where) ? replace($where, [
'|nome|' => 'ragione_sociale',
'|table|' => 'an_anagrafiche',
]) : '', $query);
$anagrafiche = $database->fetchArray($query);
$destinatari = $destinatari->concat($anagrafiche);
// Gestione sedi come destinatari
2024-02-05 17:07:33 +01:00
$query = "SELECT CONCAT('sede_', `an_sedi`.`id`) AS id,
CONCAT(`an_anagrafiche`.`ragione_sociale`, ' (', `an_sedi`.`nomesede`, IF(`an_sedi`.`citta` != '' OR `an_sedi`.`provincia` != '', CONCAT(' :', `an_sedi`.`citta`, IF(`an_sedi`.`provincia` != '', `an_sedi`.`provincia`, ''), ''), ''), ')', ' [', `an_sedi`.`email`, ']') AS text,
'Sedi' AS optgroup
2024-02-05 17:07:33 +01:00
FROM
`an_sedi`
INNER JOIN `an_anagrafiche` ON `an_anagrafiche`.`idanagrafica` = `an_sedi`.`idanagrafica`
WHERE
`an_anagrafiche`.`deleted_at` IS NULL AND `an_anagrafiche`.`enable_newsletter` = 1 AND 1=1
ORDER BY
`optgroup` ASC, `ragione_sociale` ASC";
$query = str_replace('1=1', !empty($where) ? replace($where, [
2024-02-05 17:07:33 +01:00
'|nome|' => '`nomesede` LIKE '.prepare('%'.$search.'%').' OR `ragione_sociale`',
'|table|' => '`an_sedi`',
]) : '', $query);
$sedi = $database->fetchArray($query);
$destinatari = $destinatari->concat($sedi);
// Gestione referenti come destinatari
2024-02-05 17:07:33 +01:00
$query = "SELECT CONCAT('referente_', `an_referenti`.`id`) AS id,
CONCAT(`an_anagrafiche`.`ragione_sociale`, ' (', `an_referenti`.`nome`, ') [', `an_referenti`.`email`, ']') AS text,
'Referenti' AS optgroup
2024-02-05 17:07:33 +01:00
FROM
`an_referenti`
INNER JOIN `an_anagrafiche` ON `an_anagrafiche`.`idanagrafica` = `an_referenti`.`idanagrafica`
WHERE
`an_anagrafiche`.`deleted_at` IS NULL AND `an_anagrafiche`.`enable_newsletter` = 1 AND 1=1
ORDER BY
`optgroup` ASC, `ragione_sociale` ASC";
$query = str_replace('1=1', !empty($where) ? replace($where, [
2024-02-05 17:07:33 +01:00
'|nome|' => '`an_referenti`.`nome` LIKE '.prepare('%'.$search.'%').' OR ragione_sociale',
'|table|' => '`an_anagrafiche`',
]) : '', $query);
$referenti = $database->fetchArray($query);
$destinatari = $destinatari->concat($referenti);
$results = $destinatari->toArray();
2019-08-29 15:09:01 +02:00
break;
2019-09-24 10:09:29 +02:00
case 'liste_newsletter':
2024-02-27 16:34:12 +01:00
$query = "SELECT `em_lists`.`id`, CONCAT(`em_lists_lang`.`name`, ' (', COUNT(*), ' `destinatari`)') AS descrizione FROM `em_lists` LEFT JOIN `em_lists_lang` ON (`em_lists_lang`.`id_record` = `em_lists`.`id` AND `em_lists_lang`.`id_lang` = ".prepare(setting('Lingua')).") INNER JOIN `em_list_receiver` ON `em_lists`.`id` = `em_list_receiver`.`id_list` WHERE 1=1 |where| ORDER BY `name` ASC";
2019-09-24 10:09:29 +02:00
foreach ($elements as $element) {
2024-02-05 17:07:33 +01:00
$filter[] = '`id`='.prepare($element);
2019-09-24 10:09:29 +02:00
}
if (empty($filter)) {
2024-02-05 17:07:33 +01:00
$where[] = '`deleted_at` IS NULL';
2019-09-24 10:09:29 +02:00
}
if (!empty($search)) {
2024-02-05 17:07:33 +01:00
$search_fields[] = '`name` LIKE '.prepare('%'.$search.'%');
2019-09-24 10:09:29 +02:00
}
// Aggiunta filtri di ricerca
if (!empty($search_fields)) {
$where[] = '('.implode(' OR ', $search_fields).')';
}
if (!empty($filter)) {
$where[] = '('.implode(' OR ', $filter).')';
}
break;
2019-08-29 15:09:01 +02:00
}