<?php

/**
 * Returns the Tax and Business helpsheets
 *
 * This function returns the Tax and Business helpsheets
 * The function checks if _GET['hs'] exists. If it does,
 * the particular helpsheet content is retrieved from
 * the database. If _GET['hs'] doesn't exist, the
 * function returns a list of all available helpsheets.
 *
 * @function add_helpsheet()
 *
 * @uses iris_db::get_results()
 *
 * @param (array) $var[ (string) category ]
 *
 * @return (string) Either the list of all helpsheets, or the content for the selected helpsheet
 *
 * @since 1.1.4 
 *
 */
function add_helpsheet( $var ) {
	
	global $iris_db;
	
	$var = shortcode_atts( array( 'category' => '' ), $var, 'helpsheet' );
	$content = isset( $_GET['hs'] ) ? $_GET['hs'] : '';
	
	if ( $content ) {
		
		$helpsheets_array = $iris_db->get_results( "SELECT * FROM t_helpsheets WHERE id='$content'" );
		$content = '';
		
		foreach ( $helpsheets_array as $helpsheet ) {
			
			$content .= '<a href="' . get_permalink() . '" class="call-to-action">Back to Helpsheets</a><br /><br />';
			$content .= '<h2>' . $helpsheet->hsTitle . '</h2>';
			$content .= wpautop( $helpsheet->hsFullText );
		}
		return $content;
		
	} else {
		
		$category_array = $iris_db->get_results( "SELECT sub_category FROM t_helpsheets WHERE category='{$var['category']}' GROUP BY sub_category" );
		$list = '<ul class="accordian">';
		
		foreach( $category_array as $category ) {
			
		$helpsheets_array = $iris_db->get_results( "SELECT * FROM t_helpsheets WHERE sub_category='{$category->sub_category}'" );
			$list .= '<li data-type="accordian"><h2>' . $category->sub_category . '</h2>';
			$list .= '<ul>';
			
			foreach ( $helpsheets_array as $helpsheet ) {
				
				$list .= '<li><a href="' . get_permalink() . '?hs=' . $helpsheet->id . '">' . $helpsheet->hsTitle . '</a></li>';
			}
			$list .= '</ul>';
			$list .= '</li>';
		}
		$list .= '</ul>';
		return $list;	
	}
}
add_shortcode( 'helpsheet', 'add_helpsheet' );