<?php /* Template Name: Page — Publications */ ?>

<?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['quote'] = get_field('quote');
        $pageData['quote_link'] = get_field('quote_link');
        $gallery = get_field('gallery');
        shuffle($gallery);
        if (count($gallery) > 6) {
            $pageData['gallery'] = array_slice($gallery, 0, 6);
        } else {
            $pageData['gallery'] = $gallery;
        }

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

        $publicationsQuery = new WP_Query($options);
        $pageData['publications'] = $publicationsQuery->posts;
        $years = [];

        foreach ($publicationsQuery->posts as $publication) {
            $year = getYearFromDate(get_field('publishDate', $publication->ID));
            $years[] = $year;
        }

        $pageData['years'] = array_unique($years);

    endwhile;

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

?>