openstamanager/modules/newsletter/ajax/select.php

94 lines
3.6 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 'anagrafiche_newsletter':
2019-09-24 10:09:29 +02:00
$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";
2019-08-29 15:09:01 +02:00
foreach ($elements as $element) {
$filter[] = 'an_anagrafiche.idanagrafica='.prepare($element);
}
if (empty($filter)) {
$where[] = 'deleted_at IS NULL';
$where[] = 'enable_newsletter = 1';
}
if (!empty($search)) {
$search_fields[] = 'ragione_sociale LIKE '.prepare('%'.$search.'%');
$search_fields[] = 'citta LIKE '.prepare('%'.$search.'%');
$search_fields[] = 'provincia LIKE '.prepare('%'.$search.'%');
2019-09-24 10:09:29 +02:00
$search_fields[] = 'email LIKE '.prepare('%'.$search.'%');
2019-08-29 15:09:01 +02:00
}
// Aggiunta filtri di ricerca
if (!empty($search_fields)) {
$where[] = '('.implode(' OR ', $search_fields).')';
}
if (!empty($filter)) {
$where[] = '('.implode(' OR ', $filter).')';
}
$query = str_replace('|where|', !empty($where) ? 'WHERE '.implode(' AND ', $where) : '', $query);
$rs = $dbo->fetchArray($query);
foreach ($rs as $r) {
if ($prev != $r['optgroup']) {
$results[] = ['text' => $r['optgroup'], 'children' => []];
$prev = $r['optgroup'];
}
$results[count($results) - 1]['children'][] = [
'id' => $r['id'],
'text' => $r['descrizione'],
'descrizione' => $r['descrizione'],
];
}
break;
2019-09-24 10:09:29 +02:00
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;
2019-08-29 15:09:01 +02:00
}