<?php /* Template Name: Page — Actualités */ ?>

<?php

    while ( have_posts() ) : the_post();
        $pageData['title'] = get_the_title();
        $pageData['content'] = apply_filters('the_content', get_the_content());
        
        $pageData['intro_cover'] = getImageUrl(get_field('intro_cover'));
        $pageData['intro_cover_object'] = get_field('intro_cover');
        $pageData['intro_title'] = get_field('intro_title');
        $pageData['intro_quote'] = get_field('intro_quote');


        $pageData['categories'] = get_terms('category', array(
            'hide_empty' => true,
            'orderby' => 'name',
            'order' => 'ASC' 
        ));

        $options = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'orderby' => 'publishDate',
            'meta_key' => 'publishDate',
            'order' => 'desc',
            'post_status' => 'publish'
        );

        if (!empty($_GET['c'])) {
            $options['category_name'] = $_GET['c'];
        }
        
        $articlesQuery = new WP_Query($options);
        
        $pageData['news_list'] = [];
        $pageData['highlightedNews'] = null;

        foreach ($articlesQuery->posts as $news) {
            if (get_field('publishDate', $news->ID)) {
                if (!$pageData['highlightedNews']) {
                    $pageData['highlightedNews'] = $news;
                }
                $year = DateTime::createFromFormat('d/m/Y', get_field('publishDate', $news->ID))->format('Y');
                $pageData['news_list'][$year][] = $news;
            }
        }

    endwhile;

    get_header();
    include('layouts/actualites.php');
    get_footer();

?>