Files
thalim-theme/search.php

205 lines
7.8 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();
// 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' => $search_query], $search_base);
// Base params preserved across filter links (preserves search term)
$base_filter_params = array_filter([
's' => $search_query,
'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_query,
'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' => '*' . $search_query . '*',
'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',
],
];
}
$user_query = new WP_User_Query( $user_query_args );
$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 ( $user_query->get_results() 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 = get_terms([
'taxonomy' => [ 'axe_thematique', 'programme_de_recherche' ],
'hide_empty' => false,
'name__like' => $search_query,
]);
if ( ! is_wp_error( $matching_terms ) ) {
foreach ( $matching_terms as $term ) {
$tax_obj = get_taxonomy( $term->taxonomy );
$taxonomy_cards[] = [
'name' => $term->name,
'url' => get_term_link( $term ),
'taxonomy_label' => $tax_obj ? $tax_obj->labels->singular_name : $term->taxonomy,
'count' => $term->count,
];
}
}
}
$context['taxonomy_cards'] = $taxonomy_cards;
Timber::render('search.twig', $context);