Files
thalim-theme/index.php

255 lines
9.7 KiB
PHP
Executable File

<?php
$context = Timber::context();
// Helper: split posts into active-pinned / normal and merge (pinned first, max_normal non-pinned)
$sort_with_pinned = function ($posts, string $pin_field, int $max_normal = PHP_INT_MAX) {
$today = date('Y-m-d');
$pinned = [];
$normal = [];
foreach ($posts as $post) {
$epingle = get_post_meta($post->ID, $pin_field, true);
$fin = get_post_meta($post->ID, 'date_de_fin_depinglage', true);
$active = $epingle == '1' && (empty($fin) || $fin === '0000-00-00' || $fin >= $today);
if ($active) { $pinned[] = $post; } else { $normal[] = $post; }
}
return array_merge($pinned, array_slice($normal, 0, $max_normal));
};
// --- Nombre d'items des diaporamas (depuis la page Le Laboratoire) ---
$labo_page = get_page_by_path('le-laboratoire');
$max_swiper = $labo_page ? intval(get_post_meta($labo_page->ID, 'nombres_ditems_des_diaporamas', true)) : 0;
if ($max_swiper < 1) $max_swiper = 10;
// --- Annonces diaporama ---
$annonces_raw = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => [[
'key' => 'afficher_dans_le_diaporama_dannonces_page_daccueil',
'value' => '1',
]],
'orderby' => 'date',
'order' => 'DESC',
'thalim_event_date_order' => true,
]);
$context['annonces'] = $sort_with_pinned($annonces_raw, 'epingler_dans_le_diaporama_dannonces', $max_swiper);
$context['annonces_cards'] = thalim_get_cards_data($annonces_raw);
// --- Publications et productions diaporama ---
$publications_raw = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 30,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [ thalim_cat_id('publications') ],
'operator' => 'IN',
'include_children' => true,
],
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [ thalim_cat_id('articles') ],
'operator' => 'NOT IN',
],
],
'meta_query' => [
'relation' => 'OR',
[
'key' => 'type_autre',
'value' => "Chapitre d'ouvrage",
'compare' => '!=',
],
[
'key' => 'type_autre',
'compare' => 'NOT EXISTS',
],
],
]);
$context['publications'] = $sort_with_pinned($publications_raw, 'epingler_dans_le_diaporama_des_publications_et_productions', $max_swiper);
$context['publications_cards'] = thalim_get_cards_data($publications_raw);
$context['publications_link'] = thalim_en_url( get_category_link( thalim_cat_id('publications') ) );
// Page « annonces » résolue par slug dans add_to_context() (annonces_url),
// au lieu d'un get_permalink(29100) codé en dur (post potentiellement absent).
$context['annonces_link'] = $context['annonces_url'];
// --- Message du laboratoire ---
$messages_labo = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => thalim_cat_id('message-labo'),
'orderby' => 'date',
'order' => 'DESC',
]);
$context['messages_labo'] = $messages_labo ?: [];
$context['message_labo_link'] = thalim_en_url( get_category_link( thalim_cat_id('message-labo') ) );
// --- Agenda (médiation scientifique + séances de séminaire à venir) ---
$agenda_lang = thalim_current_language();
// Catégories « agenda » résolues par slug (médiation + newsletter/gazette historiques)
$mediation_cat_ids = array_values( array_filter( [
thalim_cat_id('mediation'),
thalim_cat_id('evenements-culturels'),
thalim_cat_id('medias'),
thalim_cat_id('newsletter'),
thalim_cat_id('gazette'),
thalim_cat_id('podcast'),
thalim_cat_id('captations'),
] ) );
$months_fr = ['jan.', 'fév.', 'mars', 'avr.', 'mai', 'juin', 'juil.', 'août', 'sept.', 'oct.', 'nov.', 'déc.'];
$months_en = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
$agenda_type_fields = [
'type_colloque_journee_d_etude', 'type_soutenance', 'type_evenement_culturel',
'type_media', 'type_captation', 'type_revue_collection', 'type_autre',
];
$now_str = date('Y-m-d H:i:s');
$make_agenda_item = function ($post, $raw_date, $type_label, $lieu, $link) use ($agenda_lang, $months_fr, $months_en) {
$ts = strtotime($raw_date);
if (!$ts) return null;
$month_idx = intval(date('n', $ts)) - 1;
return [
'post' => $post,
'ts' => $ts,
'day' => intval(date('j', $ts)),
'month' => $agenda_lang === 'en' ? $months_en[$month_idx] : $months_fr[$month_idx],
'type_label' => $type_label,
'lieu' => $lieu,
'link' => $link,
];
};
$agenda_items = [];
// 1. Upcoming médiation scientifique posts
$mediation_upcoming = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 8,
'category__in' => $mediation_cat_ids,
'orderby' => ['date_clause' => 'ASC'],
'meta_query' => [
'date_clause' => [
'key' => 'date_de_debut',
'value' => $now_str,
'compare' => '>=',
'type' => 'DATETIME',
],
],
]);
foreach ($mediation_upcoming as $mpost) {
$raw_date = get_post_meta($mpost->ID, 'date_de_debut', true);
if (!$raw_date) continue;
$type_label = '';
foreach ($agenda_type_fields as $field) {
$val = get_post_meta($mpost->ID, $field, true);
if ($val) { $type_label = $val; break; }
}
if (!$type_label) {
foreach (get_the_category($mpost->ID) as $cat) {
if (in_array($cat->term_id, $mediation_cat_ids)) { $type_label = thalim_cat_name($cat); break; }
}
}
$item = $make_agenda_item($mpost, $raw_date, $type_label, get_post_meta($mpost->ID, 'lieu', true) ?: '', get_permalink($mpost->ID));
if ($item) $agenda_items[] = $item;
}
// 2. Upcoming séances de séminaire
$seances_upcoming = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 8,
'category__in' => [ thalim_cat_id('seance') ],
'orderby' => ['date_clause' => 'ASC'],
'meta_query' => [
'date_clause' => [
'key' => 'date_de_debut',
'value' => $now_str,
'compare' => '>=',
'type' => 'DATETIME',
],
],
]);
foreach ($seances_upcoming as $seance) {
$raw_date = get_post_meta($seance->ID, 'date_de_debut', true);
if (!$raw_date) continue;
$link = thalim_get_seance_link($seance->ID);
$label = $agenda_lang === 'en' ? 'Seminar session' : 'Séance de séminaire';
$item = $make_agenda_item($seance, $raw_date, $label, get_post_meta($seance->ID, 'lieu', true) ?: '', $link);
if ($item) $agenda_items[] = $item;
}
// Sort merged list by date, keep 5 soonest
usort($agenda_items, fn($a, $b) => $a['ts'] <=> $b['ts']);
$agenda_items = array_slice($agenda_items, 0, 5);
// Fallback: if no upcoming events, show 5 most recent mediation events
if (empty($agenda_items)) {
$fallback = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 5,
'category__in' => $mediation_cat_ids,
'orderby' => ['date_clause' => 'DESC'],
'meta_query' => [
'date_clause' => ['key' => 'date_de_debut', 'type' => 'DATETIME'],
],
]);
foreach ($fallback as $fpost) {
$raw_date = get_post_meta($fpost->ID, 'date_de_debut', true);
if (!$raw_date) continue;
$type_label = '';
foreach ($agenda_type_fields as $field) {
$val = get_post_meta($fpost->ID, $field, true);
if ($val) { $type_label = $val; break; }
}
if (!$type_label) {
foreach (get_the_category($fpost->ID) as $cat) {
if (in_array($cat->term_id, $mediation_cat_ids)) { $type_label = thalim_cat_name($cat); break; }
}
}
$item = $make_agenda_item($fpost, $raw_date, $type_label, get_post_meta($fpost->ID, 'lieu', true) ?: '', get_permalink($fpost->ID));
if ($item) $agenda_items[] = $item;
}
}
$context['agenda_items'] = $agenda_items;
$context['manifestations_link'] = thalim_en_url( add_query_arg( 'view', 'agenda', get_category_link( thalim_cat_id('manifestations') ) ) );
// --- Quick links ---
$newsletter_cat = get_category_by_slug('newsletter');
$newsletter_url = '';
if ($newsletter_cat) {
$nl_posts = get_posts([
'post_type' => 'post',
'posts_per_page' => 1,
'category__in' => [ $newsletter_cat->term_id ],
'include_children' => false,
'orderby' => 'date',
'order' => 'DESC',
'suppress_filters' => true,
]);
if ( ! empty( $nl_posts ) ) {
$newsletter_url = thalim_en_url( get_permalink( $nl_posts[0]->ID ) );
}
}
if ( ! $newsletter_url ) {
$newsletter_url = thalim_en_url(
$newsletter_cat ? get_category_link( $newsletter_cat->term_id ) : home_url( '/category/le-laboratoire/newsletter/' )
);
}
$context['quick_links'] = [
'agenda' => thalim_en_url(add_query_arg('view', 'agenda', get_category_link( thalim_cat_id('manifestations') ))),
'contacts' => thalim_en_url(home_url('/contacts/')),
'newsletter' => $newsletter_url,
];
// --- Tags (étiquettes) pour le nuage de mots-clés ---
$context['has_tags'] = !empty(get_terms([
'taxonomy' => 'post_tag',
'hide_empty' => true,
'number' => 1,
]));
Timber::render('index.twig', $context);