<?php
define('DB_DRIVER', 'mysql');
require_once( 'wp-config.php' );
require_once( 'wp-load.php' );
require_once('wp-admin/includes/file.php' );
require_once( 'wp-admin/includes/media.php');
require_once( 'wp-admin/includes/file.php');
require_once('wp-admin/includes/image.php');
require_once ('wp-admin/includes/post.php');
require_once ('wp-content/plugins/event-organiser/includes/event.php');
global $wpdb; 
// basic options for PDO 
$dboptions = array(
    PDO::ATTR_PERSISTENT => FALSE,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

//connect with the server
try {
   // $DB = new PDO(DB_DRIVER . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD, $dboptions);
} catch (Exception $ex) {
   // echo($ex->getMessage());
   // die;
}
//print_r($DB);
//get category :https://api.uniqueschoolapp.ie/api/calendarcategories?school_id=28

//https://api.uniqueschoolapp.ie/feeds/calendar?idschool=28&sort_order=asc&limit=10000


$url = 'https://api.uniqueschoolapp.ie/feeds/calendar?idschool=218&sort_order=asc&limit=1000';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
//var_dump(json_decode($result, true));

	$events = json_decode($result);
	echo "<pre>";
					print_r($events);
					echo "</pre>";
	$args = array(
        'post_type'      => 'event',
        'posts_per_page' => -1
    );
     $posts = get_posts( $args );
    foreach ( $posts as $post ) {
		  $my_post = array(
		  'ID'           => $post->ID,
		  'post_status'    => 'draft',
		);
     // wp_update_post( $my_post );
    }
    
    foreach ($events->data->calendar AS $event) {
		//$gallery_data == array();
		$post_array = $event_keys = array();
		$found_post = false;
					
		$found_post = post_exists( $event->title,'',$event->created_date,'event');
        if(!$found_post) {
			
			$post_array = array(
					'post_author' => 1,
					'post_content' => $event->description,  // content of the article,
					'post_title' => $event->title,
					'post_date' => $event->created_date,
					'post_status' => 'publish',   // i just published the post directly, but you can change the status as of your need
					'post_type' => 'event'  // if any custom post type, you can use it here
				);
			//echo "==";
			echo $starttime = ($event->start_time!='')?trim($event->start_time):'00:00:00';
			//echo "==";
			echo $endtime = ($event->end_time!='')?trim($event->end_time):'23:00:00';
			//echo "==";
			$start	= new DateTime( $event->start_date.' '.$starttime);
			$end 	= new DateTime( $event->end_date.' '.$endtime );		
			
			$event_data = array(
				'schedule' => 'once',
				'all_day' => '',
				'start' => $start,
				'end' => $end,
				);
			$new_cat_id = '';
			echo "<br/>category data :";
			echo $new_category_name = ($event->category->name)?$event->category->name:''; 
			echo "==data end<br/>";
			$category_color= ($event->color!='')?$event->color:'0D3216'; 
			if(!empty($new_category_name)){
				if( !get_term_by( 'name', $new_category_name, 'event-category' ) ){
					$new_cat = wp_insert_term( $new_category_name, 'event-category', array('colour' => $category_color ) );
					echo '<pre>';
					print_r($new_cat);
					echo '</pre>';
			
					if( !is_wp_error( $new_cat ) && $new_cat ){
						$new_cat_id = $new_cat['term_id'];
						$re = update_option( "eo-event-category_{$new_cat_id}", array( 'colour' => $category_color ) );
					}
				}
			}
			$cat = get_term_by( 'name', $new_category_name, 'event-category' );
			if( $cat )
				$post_array['tax_input']['event-category'][] =  intval( $cat->term_id );
							
			echo '<pre>';
			print_r($event_data);
			echo '</pre>';
			$event_post_id = eo_insert_event($post_array,$event_data);
			echo "==event id ==".$event_post_id . "==<br/>";
		}
		else
		{
			 wp_update_post(array(
				'ID'    =>  $found_post,
				'post_status'   =>  'publish'
				));
				echo "event already exixts<br/>";
		}
}
$args = array(
        'post_type'      => 'event',
        'post_status'    => 'draft',
        'posts_per_page' => -1
    );
     $posts = get_posts( $args );
    foreach ( $posts as $post ) {
		  $my_post = array(
		  'ID'           => $post->ID,
		  'post_status'    => 'trash',
		);
     // wp_update_post( $my_post );
    }
    
