<?php

/**
 * Retrieves the Tax Calendar events
 *
 * This function creates an array of
 * events taken from the database.
 *
 * @function get_events()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (array) array of events with dates
 *
 * @since 1.0.0 
 *
 */
function get_events() {
	
	global $iris_db;
	
	$events = $iris_db->get_results( 'SELECT * FROM tax_calendar' );
	
	$events_array =[];
	
	foreach ( $events as $row ) {	
	
		$events_array[$row->date] = $row->details;
	}
	return $events_array;
}



/**
 * Build the Tax Calendar
 *
 * This function builds the Tax Calendar.
 * It gets the array of events and merges
 * them with an array of all dates in one
 * month. Finally the function adds all
 * of the dates into an HTML <table>.
 *
 * @function build_calendar()
 *
 * @uses get_events()
 *
 * @param (string) $month, (string) $year
 *
 * @return (string) A month in an HTML <table>
 *
 * @since 1.0.0 
 *
 */
function build_calendar( $month, $year ) {
	
	$events_array = get_events();
	$daysOfWeek = array( 'S', 'M', 'T', 'W', 'T', 'F', 'S' );
	$firstDayOfMonth = mktime( 0, 0, 0, $month, 1, $year );
	$numberDays = date( 't', $firstDayOfMonth );
	$dateComponents = getdate( $firstDayOfMonth );
	$monthName = $dateComponents['month'];
	$dayOfWeek = $dateComponents['wday'];
	$calendar = '<table class="calendar">';
	$calendar .= '<caption>' . $monthName . ' ' . $year . '</caption>';
	$calendar .= '<tr>';
	
	foreach ( $daysOfWeek as $day ) {
		
		$calendar .= '<th class="header">' . $day . '</th>';
	}
	$currentDay = 1;
	$calendar .= '</tr><tr>';
	
	if ( $dayOfWeek > 0 ) { 
	
		$calendar .= '<td colspan="' . $dayOfWeek . '">&nbsp;</td>'; 
	}
	$month = str_pad( $month, 2, '0', STR_PAD_LEFT );
	
	while ( $currentDay <= $numberDays ) {
		
		if ( $dayOfWeek == 7 ) {
			
			$dayOfWeek = 0;
			$calendar .= '</tr><tr>';
		}
		$currentDayRel = str_pad( $currentDay, 2, '0', STR_PAD_LEFT );
		if($currentDay==1){$attach='st ';}
		else if($currentDay==2){$attach='nd ';}
		else if($currentDay==3){$attach='rd ';}
		else{$attach='th ';}
		$date = $year . '-' . $month . '-' . $currentDayRel;
		$todayDate = date( 'Y-m-d', time() );
		$highlight = ( array_key_exists( $date, $events_array ) ) ? ' highlight' : '';
		$today = ( $date == $todayDate ) ? ' today' : '';
		$details = ( array_key_exists( $date, $events_array ) ) ? '<div class="popup-info"><p><strong>' . $currentDay . $attach . $monthName . '</strong><button id="calendar-close">&times;</button></p><p>' . $events_array[$date] . '</p><a href="/contact/" class="call-to-action">Contact Us</a></div>' : '';
		$calendar .= '<td class="day' . $highlight.$today . '">' . $currentDay.$details . '</td>';
		$currentDay++;
		$dayOfWeek++;
	}
	if ( $dayOfWeek != 7 ) {
	
		$remainingDays = 7 - $dayOfWeek;
		$calendar .= '<td colspan="' . $remainingDays . '">&nbsp;</td>'; 
	}
	$calendar .= '</tr>';
	$calendar .= '</table>';
	return $calendar;
}



/**
 * Returns the completed tax calendar
 *
 * This function loops through the year
 * from April to March (UK tax year)
 * getting a table of events for each
 * month. JavaScript then formats the
 * table nicely.
 *
 * @function add_tax_calendar()
 *
 * @uses build_calendar()
 *
 * @param N/A
 *
 * @return (string) Complete Tax calendar
 *
 * @since 1.0.0 
 *
 */
function add_tax_calendar() {
	
	$thismonth = date( 'n' );	
	$year = ( $thismonth < 5 ) ? date( 'Y', strtotime( '-1 year' ) ) : date( 'Y' );
	$month = 4;
	$tax_calendar = '<div id="tax-calendar" data-start-month="' . $thismonth . '">';
	$tax_calendar .= '<button id="calendar-prev">&#9666;</button>';
	$tax_calendar .= '<button id="calendar-next">&#9656;</button>';
	$tax_calendar .= '<div id="tax-calendar-container">';
	$tax_calendar .= '<ul id="tax-calendar-inner">';
	
	for ( $x = 0; $x <= 12; $x++ ) {
		
		$tax_calendar .= '<li>';
		$tax_calendar .= build_calendar( $month, $year );
		$tax_calendar .= '</li>';
		
		if ( $month == 12 ) {
			
			$month = 1;
			$year++;
		}else {
			
			$month++;
			$year;
		}
	}
	$tax_calendar .= '</ul>';
	$tax_calendar .= '</div>';
	$tax_calendar .= '</div>';
	return $tax_calendar;
}
add_shortcode( 'tax_calendar', 'add_tax_calendar' );