Files
ms1inscription-v5/superadm/ajax_newpromoteur.php

89 lines
2.7 KiB
PHP

<?php
require_once('php/inc_functions.php');
header('Content-Type: application/json; charset=utf-8');
$db = $GLOBALS['db'] ?? null;
if (!$db) { http_response_code(500); echo json_encode([]); exit; }
$action = $_GET['action'] ?? '';
if ($action == 'search_email') {
$q = trim($_GET['q'] ?? '');
if ($q === '' || strlen($q) < 2) { echo json_encode([]); exit; }
$eve_id = isset($_GET['eve_id']) ? (int)$_GET['eve_id'] : 0; // << param évènement
$like = $db->fxEscape($q);
// Filtre de base (recherche e-mail)
$where = "com_courriel LIKE '%$like%'";
// Si on connaît l'événement, exclure les comptes qui l'ont déjà dans la liste
if ($eve_id > 0) {
// Pas d'espaces dans tes listes, donc FIND_IN_SET direct
$where .= " AND (com_eve_promoteur IS NULL OR com_eve_promoteur = '' OR FIND_IN_SET($eve_id, com_eve_promoteur) = 0)";
}
$sql = "
SELECT com_id, com_courriel, com_prenom, com_nom
FROM inscriptions_comptes
WHERE $where
ORDER BY com_courriel ASC
LIMIT 15
";
$res = $db->fxQuery($sql);
$out = [];
if ($res) {
while ($r = mysqli_fetch_assoc($res)) {
$out[] = [
'id' => (int)$r['com_id'],
'com_id' => (int)$r['com_id'],
'email' => $r['com_courriel'],
'com_courriel' => $r['com_courriel'],
'com_prenom' => $r['com_prenom'],
'com_nom' => $r['com_nom'],
];
}
}
echo json_encode($out, JSON_UNESCAPED_UNICODE);
}
if ($action == 'search_evenement') {
header('Content-Type: application/json; charset=utf-8');
$q = trim($_GET['q'] ?? '');
if ($q === '' || strlen($q) < 2) { echo json_encode([]); exit; }
$like = $db->fxEscape($q);
$intQ = (int)$q; // pour matcher / prioriser le numero d'evenement
// MSIN-4457 — Prioriser les evenements recents (sinon LIMIT + tri alpha
// enterre les editions 2026 derriere "… 2020/2021/…" pour un meme radical).
// Limite haute : beaucoup d'editions historiques pour un meme lieu/radical
// (ex. Tremblant). La liste UI est scrollable.
$sql = "
SELECT eve_id, eve_nom_fr
FROM inscriptions_evenements
WHERE eve_nom_fr LIKE '%$like%'
OR CAST(eve_id AS CHAR) LIKE '%$like%'
ORDER BY (eve_id = $intQ) DESC, eve_date_fin DESC, eve_id DESC
LIMIT 200
";
$res = $db->fxQuery($sql);
$out = [];
if ($res) {
while ($r = mysqli_fetch_assoc($res)) {
$out[] = [
// 2 clés pour être robustes côté JS
'eve_id' => (int)$r['eve_id'],
'eveId' => (int)$r['eve_id'],
'eve_nom_fr'=> $r['eve_nom_fr'],
];
}
}
echo json_encode($out, JSON_UNESCAPED_UNICODE);
exit;
}