Files
thalim-theme/search.php
T
valentin_le_moign 0036cc14d5 Recherche : chercher le texte saisi, pas sa version échappée pour le HTML
get_search_query() renvoie un texte échappé : `l'Oulipo` devenait
`l'Oulipo` en base. Les noms contenant une apostrophe, un guillemet ou une
esperluette n'étaient pas trouvés (programmes, axes, membres), et une expression
entre guillemets ne fonctionnait pas en première page d'annonces. La version
échappée ne sert plus qu'à l'affichage, les liens de filtre encodent le texte.

La recherche de termes et de membres essaie aussi les deux formes d'apostrophe
et les deux formes enregistrées de l'esperluette (`&`, `&`).
2026-11-05 13:47:27 +01:00

239 lines
9.2 KiB
PHP

<?php
$context = Timber::context();
// Les séances de séminaire sont incluses : post-card-helpers réécrit leur
// lien vers le séminaire parent + ancre #seance-{ID}.
$excluded_cat_ids = thalim_archive_excluded_cat_ids( false ); // Non classé (+ Vie du labo non connectés)
$search_query = get_search_query(); // échappé : affichage uniquement (autoescape Twig désactivé)
$search_raw = get_search_query( false ); // texte tapé : requêtes et URLs
// Read filter query params
$f = thalim_archive_read_filters();
$active_axe = $f['axe'];
$active_date_from = $f['date_from'];
$active_date_to = $f['date_to'];
$active_cat_id = $f['cat_id'];
$filter_autres = $f['filter_autres'];
$context['search_query'] = $search_query;
$context['active_axe'] = $active_axe;
$context['active_date_from'] = $active_date_from;
$context['active_date_to'] = $active_date_to;
$context['active_category_id'] = $filter_autres ? 'autres' : $active_cat_id;
$context['active_cat_id'] = $active_cat_id;
$context['filter_autres'] = $filter_autres;
// Determine active rubrique
$active_rubrique_id = thalim_archive_active_rubrique($active_cat_id);
$context['active_rubrique'] = $active_rubrique_id;
// Base URL for search filter links (language-aware)
$search_base = thalim_en_url( home_url('/') );
// Override annonces_url: rubrique reset stays on search page (no filter_cat)
$context['annonces_url'] = add_query_arg(['s' => urlencode($search_raw)], $search_base);
// Base params preserved across filter links (preserves search term)
$base_filter_params = array_filter([
's' => urlencode($search_raw),
'axe' => $active_axe ?: null,
'date_from' => $active_date_from ?: null,
'date_to' => $active_date_to ?: null,
]);
// Build tax_query
$tax_query = [
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $excluded_cat_ids,
'operator' => 'NOT IN',
],
];
if ($active_cat_id) {
$tax_query[] = [
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [$active_cat_id],
'include_children' => !$filter_autres,
];
}
$query_args = [
'post_type' => 'post',
's' => $search_raw,
'relevanssi' => true,
'posts_per_page' => 12,
'orderby' => 'relevance',
'order' => 'DESC',
'tax_query' => $tax_query,
];
if ($active_axe) {
$query_args['meta_query'] = [[
'key' => 'axes_thematiques',
'value' => $active_axe,
'type' => 'NUMERIC',
]];
}
if ($active_date_from || $active_date_to) {
$date_query = ['inclusive' => true];
if ($active_date_from) $date_query['after'] = $active_date_from;
if ($active_date_to) $date_query['before'] = $active_date_to;
$query_args['date_query'] = [$date_query];
}
// Axes thématiques for filter dropdown
$axes_groups = thalim_get_axes_filter_groups();
$current_axes = $axes_groups[0]['terms'] ?? [];
$context['filter_axes'] = $current_axes;
$context['axe_stay_on_page'] = true;
// Rubrique/catégorie filter links (all preserve search term)
$all_cats = get_categories(['taxonomy' => 'category', 'hide_empty' => false, 'exclude' => $excluded_cat_ids]);
// Liens de filtre : on reste sur la recherche avec un paramètre filter_cat
$make_filter_link = function ($cat) use ($base_filter_params, $search_base) {
$params = array_filter(array_merge($base_filter_params, ['filter_cat' => $cat->term_id]));
return add_query_arg($params, $search_base);
};
$context['filter_parents'] = thalim_archive_filter_parents($all_cats, $make_filter_link);
$filter_categories = thalim_archive_filter_children($all_cats, $active_rubrique_id, $make_filter_link);
// Add "Autres" entry if active rubrique has posts directly assigned to it
if ($active_rubrique_id && !empty($filter_categories) && thalim_rubrique_has_direct_posts($active_rubrique_id)) {
$params = array_filter(array_merge($base_filter_params, ['filter_cat' => $active_rubrique_id, 'filter_autres' => 1]));
$filter_categories[] = thalim_archive_autres_entry(add_query_arg($params, $search_base));
}
$context['filter_categories'] = $filter_categories;
$posts = Timber::get_posts($query_args);
$context['cards'] = thalim_get_cards_data($posts);
$context['posts'] = $posts;
// Search users (members) by display_name
$author_cards = [];
if ( $search_query ) {
$excluded_role_ids = thalim_excluded_role_ids(); // « À ranger », « Archive » (résolus par slug)
$user_query_args = [
'search_columns' => ['display_name'],
'number' => 6,
'orderby' => 'display_name',
'order' => 'ASC',
];
if ( $excluded_role_ids ) {
$user_query_args['meta_query'] = [
[
'key' => 'role_1',
'value' => $excluded_role_ids,
'compare' => 'NOT IN',
],
];
}
$found_users = [];
foreach ( thalim_search_text_variants( $search_raw ) as $variant ) {
$user_query_args['search'] = '*' . $variant . '*';
foreach ( ( new WP_User_Query( $user_query_args ) )->get_results() as $found_user ) {
$found_users[ $found_user->ID ] = $found_user;
}
}
$lang = thalim_current_language();
// Direction IDs (same source as membres page and author page)
$labo_page = get_page_by_path( 'le-laboratoire' );
$labo_directeur_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur', true ) ) : 0;
$labo_adjoint_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur_adjoint', true ) ) : 0;
foreach ( array_slice( $found_users, 0, 6 ) as $user ) {
$avatar_url = thalim_get_user_avatar_url( $user->ID );
$role_id = get_user_meta( $user->ID, 'role_1', true );
$role_label = '';
if ( $role_id ) {
$role_term = get_term( intval( $role_id ), 'role' );
if ( $role_term && ! is_wp_error( $role_term ) ) {
$override = thalim_bilingual( get_user_meta( $user->ID, 'affichage_du_statut_1', true ) ?: '', $lang );
$role_label = $override ?: $role_term->name;
}
}
if ( $user->ID === $labo_directeur_id ) {
$role_label = 'Directeur' . ( $role_label ? ', ' . $role_label : '' );
} elseif ( $user->ID === $labo_adjoint_id ) {
$role_label = 'Directeur adjoint' . ( $role_label ? ', ' . $role_label : '' );
}
$affiliation = get_user_meta( $user->ID, 'affiliation', true ) ?: '';
if ( strtolower( $affiliation ) === 'autre' ) {
$affiliation = thalim_bilingual( get_user_meta( $user->ID, 'affiliation_autre', true ) ?: '', $lang );
}
$words = preg_split( '/\s+/', trim( $user->display_name ) );
$initials = implode( '', array_map( fn( $w ) => mb_substr( $w, 0, 1 ), $words ) );
$author_cards[] = [
'id' => $user->ID,
'name' => $user->display_name,
'url' => get_author_posts_url( $user->ID ),
'avatar_url' => $avatar_url,
'initials' => mb_strtoupper( $initials ),
'role_label' => $role_label,
'affiliation' => $affiliation,
];
}
}
$context['author_cards'] = $author_cards;
// Search taxonomy terms (axes thématiques + programmes de recherche)
$taxonomy_cards = [];
if ( $search_query ) {
$matching_terms = [];
foreach ( thalim_search_text_variants( $search_raw ) as $variant ) {
$found_terms = get_terms([
'taxonomy' => [ 'axe_thematique', 'programme_de_recherche' ],
'hide_empty' => false,
'name__like' => $variant,
]);
if ( ! is_wp_error( $found_terms ) ) {
foreach ( $found_terms as $found_term ) {
$matching_terms[ $found_term->term_id ] = $found_term;
}
}
}
foreach ( $matching_terms as $term ) {
$tax_obj = get_taxonomy( $term->taxonomy );
// Comptage réel, mêmes critères que taxonomy.php : $term->count n'est pas
// fiable sur ces taxonomies Pods, sans object_type (WordPress le remet à 0).
$count_query = new WP_Query([
'post_type' => 'post',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => 1,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => [ $term->term_id ],
],
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [ thalim_cat_id('seance') ],
'operator' => 'NOT IN',
],
],
]);
$taxonomy_cards[] = [
'name' => $term->name,
'url' => get_term_link( $term ),
'taxonomy_label' => $tax_obj ? $tax_obj->labels->singular_name : $term->taxonomy,
'count' => $count_query->found_posts,
];
}
}
$context['taxonomy_cards'] = $taxonomy_cards;
Timber::render('search.twig', $context);