<?php /* Template Name: IMPORT NEWS */ ?>

<?php

    require_once('../db/news.php');

    function __update_post_meta( $post_id, $field_name, $value = '' )
    {
        if ( empty( $value ) OR ! $value )
        {
            delete_post_meta( $post_id, $field_name );
        }
        elseif ( ! get_post_meta( $post_id, $field_name ) )
        {
            add_post_meta( $post_id, $field_name, $value );
        }
        else
        {
            update_post_meta( $post_id, $field_name, $value );
        }
    }

    // die('IMPORT ------------------');
    
    // dump($news);

    // DATAS NEWS ----------------------------

    // $newsData = array();
    // $newsData['title'] = get_the_title($news->ID);
    // $newsData['publishDate'] = get_field('publishDate', $news->ID);
    // $newsData['intro'] = get_field('intro', $news->ID);
    // $newsData['file'] = get_field('file', $news->ID);
    // $newsData['videoLink'] = get_field('videoLink', $news->ID);
    // $c = wp_get_post_terms($news->ID, 'category');
    // $newsData['category'] = count($c) > 0 ? $c[0] : null;
    // $newsData['categoryLetter'] = substr($newsData['category']->name, 0, 1);



    // EXEMPLE ----------------------------
    /*

    $my_post = array(
        'post_title' => $_SESSION['booking-form-title'],
        'post_date' => $_SESSION['cal_startdate'],
        'post_content' => 'This is my post.',
        'post_status' => 'publish',
        'post_type' => 'booking',
    );
    $insertedID = wp_insert_post( $my_post );
    
    __update_post_meta( $insertedID, 'my-custom-field', 'my_custom_field_value' );
    wp_set_post_terms($insertedID, 15/17, 'category');


    // CATEGORIES ----------------------------
    // ID Article : 15
    // ID Vidéo : 17
    
    */

    die("BEFORE INSERT");

    // foreach ($news as $n) {

    //     $d = substr($n['date'],0,10);
    //     $dExpl = explode('-',$d);
    //     $date = $dExpl[2] . '/' . $dExpl[1] . '/' .  $dExpl[0];

    //     $video = null;
    //     $category = 15;

    //     if ($n['vimeo']) {
    //         $video = 'https://vimeo.com/' . $n['vimeo'];
    //         $category = 17;
    //     }

    //     $insertData = array(
    //         'post_title' => $n['title'],
    //         'post_date' => $n['created_at'],
    //         'post_content' => null,
    //         'post_status' => 'publish',
    //         'post_type' => 'post',
    //     );
    //     $insertedID = wp_insert_post( $insertData );
    //     __update_post_meta( $insertedID, 'old_id', $n['id'] );
    //     __update_post_meta( $insertedID, 'old_title', $n['title'] );
    //     __update_post_meta( $insertedID, 'old_text', $n['text'] );
    //     __update_post_meta( $insertedID, 'old_date', $n['date'] );
    //     __update_post_meta( $insertedID, 'old_created', $n['created_at'] );

    //     if ($n['pdf']) {
    //         __update_post_meta( $insertedID, 'old_pdf', $n['pdf'] );
    //         __update_post_meta( $insertedID, 'old_rewrite', 'https://www.pradel.law/default/assets/pdf/' . $n['id'] . '/' . $n['pdf'] );
    //     }
        
    //     __update_post_meta( $insertedID, 'publishDate', $n['date'] );
    //     __update_post_meta( $insertedID, 'intro', $n['text'] );

    //     if ($video) {
    //         __update_post_meta( $insertedID, 'old_vimeo', $n['vimeo'] );
    //         __update_post_meta( $insertedID, 'videoLink', $video );
    //     }

    //     wp_set_post_terms($insertedID, $category, 'category');

    //     echo $insertedID . "-" . $n['title'] . "<br/><br/>";

    //     // die();
    // }


    die('------------------ IMPORT');

?>