<?php

function bw_course_schedule_request ($request) {

	$meta = $request->get_param('meta');
	if (empty($meta) || !is_array($meta)) $meta = false;

	$tax = $request->get_param('tax');
	if (empty($tax) || !is_array($tax)) $tax = false;

	$posts_per_page = $request->get_param('posts_per_page');
	if (empty($posts_per_page)) $posts_per_page = 12;

	$paged = $request->get_param('paged');
	if (empty($paged)) $paged = 1;

	return resource_schedule($meta, $tax, $posts_per_page, $paged);
}

function resource_schedule ($meta = false, $tax = false, $posts_per_page = 20, $paged = 1) {
	$args = array(
		'post_type'      => 'tribe_events',
		'posts_per_page' => $posts_per_page,
		'paged'          => $paged,
		'tax_query'      => array(
			'relation' => 'AND'
		),
		'meta_query'     => array(
			'relation' => 'AND',
			'_EventStartDate_clause' => array(
				'key'     => '_EventStartDate',
				'value'   =>  date('Y-m-d 00:00:00', strtotime('-2 days', time())),
				'compare' => '>'
			)
		),
		'orderby'        => array(
			'title'                  => 'ASC',
			'_EventStartDate_clause' => 'ASC'
		)
	);
	if ($meta) {
		foreach ($meta as $key => $meta_query) {
			if (isset($meta_query['value'])) {
				$args['meta_query']["{$key}_clause"] = array(
					'key'     => $key,
					'value'   => $meta_query['value'],
					'compare' => isset($meta_query['compare']) ? $meta_query['compare'] : '=' 
				);
			}
		}
	}
	if ($tax) {
		foreach ($tax as $taxonomy => $tax_query) {
			if (isset($tax_query['terms'])) {
				$args['tax_query'][] = array(
					'taxonomy' => $taxonomy,
					'terms'    => $tax_query['terms'],
					'field'    => isset($tax_query['field']) ? $tax_query['field'] : 'slug',
					'operator'  => isset($tax_query['compare']) ? $tax_query['compare'] : 'IN' 
				);
			}
		}
	}
	$data = new stdClass();
	$data->posts = array();
	$results = new WP_Query($args);
	$data->found_posts = $results->found_posts;

	while ($results->have_posts()) {
		$results->the_post();

		$data->posts[] = $post = new stdClass();

		$post->title = get_the_title();
		$post->link = get_permalink();

		$post->meta = get_fields(get_the_ID());
		if (!is_array($post->meta)) $post->meta = array();
		$meta_raw = get_post_meta(get_the_ID());

		foreach ($meta_raw as $key => $values) {
			if (!is_array($values) || isset($post->meta[$key])) continue;

			$post->meta[$key] = reset($values);
		}

		$post->tax = array();
		foreach (get_object_taxonomies('tribe_events') as $taxonomy) {
			$post->tax[$taxonomy] = wp_get_object_terms(get_the_ID(), $taxonomy);
		}
	}
	wp_reset_postdata();
	return $data;
}
function valid_courses ($terms) {
	global $wpdb;
	$is_valid = array();

	$args = array(
		'post_type'      => 'tribe_events',
		'posts_per_page' => -1,
		'tax_query'      => array(
			'relation' => 'AND',
			array(
					'taxonomy' => 'program',
					'terms'    => $terms,
					'field'    => 'term_id',
					'operator'  => 'NOT IN' 
			)
		),
		'meta_query'     => array(
			'relation' => 'AND',
			'_EventStartDate_clause' => array(
				'key'     => '_EventStartDate',
				'value'   =>  date('Y-m-d 00:00:00', strtotime('-2 days', time())),
				'compare' => '>'
			)
		)
	);
	$results = new WP_Query($args);
	while ($results->have_posts()) {
		$results->the_post();
		$content_block = get_field('content_block', get_the_ID());
		if ( $content_block ) {
			if (!is_array( $content_block )) {
				if (is_object($content_block)) {
					$is_valid[$content_block->ID] = true;
				} else {
					$is_valid[$content_block] = true;
				}
			} else {
				foreach ($content_block as $cb) {
					if (is_object($cb)) {
						$is_valid[$cb->ID] = true;
					} else {
						$is_valid[$cb] = true;
					}
				}
			}
		}
	}
	wp_reset_postdata();

	$is_valid['query'] = $results;

	return $is_valid;
}
