change: selecteurs/generique en API select2.

pull/3/head
Matthieu Marcillaud 5 months ago
parent 5b9c36a79a
commit 88a79c6776

@ -3,108 +3,115 @@
// Sécurité
if (!defined('_ECRIRE_INC_VERSION')) return;
function selecteurs_generique_dist() {
include_spip('base/objets');
include_spip('inc/filtres');
include_spip('inc/texte');
function selecteurs_generique_dist($params = []) {
$trouver_table = charger_fonction('trouver_table', 'base');
$tables = lister_tables_objets_sql();
$search = trim(_request('q'));
$resultats = array();
$limite = 5;
$search = trim($params['q'] ?? '');
$resultats = ['results' => []];
if (!$search) {
return json_encode($resultats);
}
// Pouvoir personnaliser le nombre de résultats
if ($limite_perso = intval(_request('limite')) and $limite_perso > 0) {
$limite = $limite_perso;
}
$limite = intval($params['limite'] ?? 0) ?: 5;
$objets = is_array($params['objets'] ?? null) ? $params['objets'] : [];
$objets_exclus = is_array($params['objets_exclus'] ?? null) ? $params['objets_exclus'] : [];
include_spip('base/objets');
include_spip('inc/filtres');
include_spip('inc/texte');
$tables = lister_tables_objets_sql();
// On ne garde que les objets demandés… si demandé
if ($objets = _request('objets') and is_array($objets)) {
$objets = array_flip(array_map('table_objet_sql', $objets));
$tables = array_intersect_key($tables, $objets);
if ($objets) {
$tables = array_intersect_key($tables, array_flip(array_map('table_objet_sql', $objets)));
}
// On exclut s'il faut
if ($objets_exclus = _request('objets_exclus') and is_array($objets_exclus)) {
$objets_exclus = array_flip(array_map('table_objet_sql', $objets_exclus));
$tables = array_diff_key($tables, $objets_exclus);
if ($objets_exclus) {
$tables = array_diff_key($tables, array_flip(array_map('table_objet_sql', $objets_exclus)));
}
// On parcourt ensuite toutes les tables, en cherchant les meilleurs résultats par titre
foreach ($tables as $table => $desc) {
// Seulement si on trouve un champ titre de la table
$champ_titre = selecteur_generique_table_titre($desc);
if (!$champ_titre) {
continue;
}
$cle_objet = id_table_objet($table);
$objet = objet_type($table);
// Seulement si on trouve un champ titre de la table
if (
(
// S'il y a une déclaration ET que c'est un truc super simple du genre "champ_simple AS titre"
(
isset($desc['titre'])
and preg_match(';(?:^|,)\s*([^,\s]+)\s*as\s*titre\s*(,|$);i', $desc['titre'], $champ)
and !preg_match(';\W;', $champ[1])
and $champ = trim($champ[1])
)
// Sinon si on trouve un champ titre
or (isset($desc['field']['titre']) and $champ = 'titre')
// Sinon si on trouve un champ nom
or (isset($desc['field']['nom']) and $champ = 'nom')
)
and (
// Seulement quand ça débute pareil en priorité
$trouve = sql_allfetsel(
$champ . ', ' . $cle_objet,
$table,
"$champ LIKE ".sql_quote("{$search}%"),
'',
'',
"0,$limite"
)
or
// Sinon n'importe où dans le titre
$trouve = sql_allfetsel(
$champ . ', ' . $cle_objet,
$table,
"$champ LIKE ".sql_quote("%{$search}%"),
'',
'',
"0,$limite"
)
)
) {
// On ajoute le titre de l'objet
$resultats[] = array(
'label' => '<strong>' . _T($desc['texte_objets']) . '</strong>',
'value' => ' ',
// Seulement quand ça débute pareil en priorité
$trouve = sql_allfetsel(
$champ_titre . ', ' . $cle_objet,
$table,
"$champ_titre LIKE " . sql_quote("{$search}%"),
'',
'',
"0,$limite"
);
if (!$trouve) {
// Sinon n'importe où dans le titre
$trouve = sql_allfetsel(
$champ_titre . ', ' . $cle_objet,
$table,
"$champ_titre LIKE " . sql_quote("%{$search}%"),
'',
'',
"0,$limite"
);
}
if (!$trouve) {
continue;
}
$group = [
'text' => _T($desc['texte_objets']),
'children' => [],
];
foreach ($trouve as $resultat) {
$id_objet = $resultat[$cle_objet];
$titre = appliquer_traitement_champ(
$resultat[$champ_titre],
'titre',
table_objet($table),
['objet' => $objet, 'id_objet' => $id_objet]
);
foreach ($trouve as $resultat) {
$id_objet = $resultat[$cle_objet];
if (function_exists('appliquer_traitement_champ')) {
$titre = appliquer_traitement_champ(
$resultat[$champ],
'titre',
table_objet($table),
array('objet' => $objet, 'id_objet' => $id_objet)
);
}
else {
$titre = typo($resultat[$champ]);
}
$resultats[] = array(
'label' => $titre,
'value' => $objet.$id_objet,
);
}
$group['children'][] = [
'id' => $objet.$id_objet,
'text' => $titre,
];
}
$resultats['results'][] = $group;
}
return json_encode($resultats);
}
/**
* Si la description de table contient un titre, retourne le nom du champ
* @param array $desc
* @return string
*/
function selecteur_generique_table_titre(array $desc = []) {
// S'il y a une déclaration ET que c'est un truc super simple du genre "champ_simple AS titre"
if (
isset($desc['titre'])
&& preg_match(';(?:^|,)\s*([^,\s]+)\s*as\s*titre\s*(,|$);i', $desc['titre'], $champ)
&& !preg_match(';\W;', $champ[1])
) {
return trim($champ[1]);
}
if (isset($desc['field']['titre'])) {
return 'titre';
}
if (isset($desc['field']['nom'])) {
return 'nom';
}
return '';
}
Loading…
Cancel
Save