Files
thalim-theme/search.php
2026-05-12 23:33:46 +02:00

249 lines
9.1 KiB
PHP

<?php
$context = Timber::context();
// Séances de séminaire (cat 12) are included: post-card-helpers rewrites their
// link to the parent séminaire + #seance-{ID} hash.
$excluded_cat_ids = [31]; // Non classé
if ( ! is_user_logged_in() ) $excluded_cat_ids[] = 9; // Vie du labo
$search_query = get_search_query();
// Read filter query params
$active_axe = isset($_GET['axe']) ? intval($_GET['axe']) : 0;
$active_date_from = isset($_GET['date_from']) ? sanitize_text_field($_GET['date_from']) : '';
$active_date_to = isset($_GET['date_to']) ? sanitize_text_field($_GET['date_to']) : '';
$active_cat_id = isset($_GET['filter_cat']) ? intval($_GET['filter_cat']) : 0;
$filter_autres = isset($_GET['filter_autres']) ? 1 : 0;
$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 = 0;
if ($active_cat_id) {
$active_cat_obj = get_category($active_cat_id);
$active_rubrique_id = ($active_cat_obj && $active_cat_obj->parent)
? $active_cat_obj->parent
: $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',
'lang' => '',
'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]);
$filter_parents = [];
foreach ($all_cats as $cat) {
if ($cat->parent == 0) {
$params = array_filter(array_merge($base_filter_params, ['filter_cat' => $cat->term_id]));
$filter_parents[] = [
'id' => $cat->term_id,
'name' => thalim_cat_name($cat),
'slug' => $cat->slug,
'link' => add_query_arg($params, $search_base),
];
}
}
$context['filter_parents'] = $filter_parents;
$filter_categories = [];
if ($active_rubrique_id) {
foreach ($all_cats as $cat) {
if ($cat->parent == $active_rubrique_id) {
$params = array_filter(array_merge($base_filter_params, ['filter_cat' => $cat->term_id]));
$filter_categories[] = [
'id' => $cat->term_id,
'name' => thalim_cat_name($cat),
'slug' => $cat->slug,
'link' => add_query_arg($params, $search_base),
];
}
}
}
// Add "Autres" entry if active rubrique has posts directly assigned to it
if ($active_rubrique_id && !empty($filter_categories)) {
$lang = thalim_current_language();
$direct_check = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => true,
'lang' => '',
'tax_query' => [[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [$active_rubrique_id],
'include_children' => false,
]],
]);
if ($direct_check->have_posts()) {
$params = array_filter(array_merge($base_filter_params, ['filter_cat' => $active_rubrique_id, 'filter_autres' => 1]));
$filter_categories[] = [
'id' => 'autres',
'name' => $lang === 'en' ? 'Other' : 'Autres',
'slug' => 'autres',
'link' => 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 = [ 600, 598 ]; // "À ranger", "Archive"
$user_query = new WP_User_Query([
'search' => '*' . $search_query . '*',
'search_columns' => ['display_name'],
'number' => 6,
'orderby' => 'display_name',
'order' => 'ASC',
'meta_query' => [
[
'key' => 'role_1',
'value' => $excluded_role_ids,
'compare' => 'NOT IN',
],
],
]);
$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);