<?php
//Goughy000 -> GOUGHY_CALENDAR_THREADS.php
if(!defined('IN_MYBB'))
{
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function goughy_calendar_threads_info(){
    return array(
        'name'	        => 'Threads On Calendar',
        'description'   => 'Lists threads on calendar',
        'website'	    => 'http://www.byteforums.com/',
        'author'	    => 'Goughy000',
        'authorsite'    => 'http://www.goughy000.com/',
        'version'	    => '1.0',
		'guid'			=> 'a8bd220dd8c01acd028bd360ed2f0f6f'
    );
}

$plugins->add_hook('calendar_dayview_start', 'goughy_calendar_threads_dayviewlist');
function goughy_calendar_threads_dayviewlist(){
	global $events, $day, $month, $year, $db, $mybb, $cache, $theme, $parser;
	$timestamp_from = strtotime($year."-".$month."-".$day." 00:00:01");
	$timestamp_to = strtotime($year."-".$month."-".$day." 23:59:59");
	$cds_unviewable = get_unviewable_forums(true);
	if($cds_unviewable){
		$cds_unviewwhere = ' AND t.fid NOT IN ('.$cds_unviewable.')';
	}
	$altbg = alt_trow();
	$cds_icon_cache = $cache->read('posticons');
	$query = $db->query('
		SELECT t.*, f.name, u.usergroup, u.displaygroup
		FROM '.TABLE_PREFIX.'threads t
		LEFT JOIN '.TABLE_PREFIX.'forums f ON (f.fid=t.fid)
		LEFT JOIN '.TABLE_PREFIX.'users u ON (u.uid=t.uid)
		WHERE t.visible=1 AND t.dateline > '.$timestamp_from.'  AND t.dateline < '.$timestamp_to.''.$cds_unviewwhere.'
		ORDER BY t.dateline DESC
	');
	$threaddisplay .= '<br />
<table border="0" cellspacing="'.$theme['borderwidth'].'" cellpadding="'.$theme['tablespace'].'" class="tborder">
	<tr>
		<td class="thead" colspan="6"><strong>Threads Posted</strong></td>
	</tr>';
	$threaddisplay .= '<tr class="tcat">
	<td colspan="2"><strong>Title</strong></td>
	<td><strong>Author</strong></td>
	<td><strong>Forum</strong></td>
	<td><strong>Replies</strong></td>
	<td><strong>Views</strong></td>
</tr>';
	if($db->num_rows($query) > 0){
		while($threaddetails = $db->fetch_array($query)){
			$threaddetails['name'] = preg_replace('#&(?!\#[0-9]+;)#si', '&amp;', $threaddetails['name']);
			if($threaddetails['icon'] > 0 && $cds_icon_cache[$threaddetails['icon']]){
				$icon = $cds_icon_cache[$threaddetails['icon']];
				$icon = '<img src="'.$icon['path'].'" alt="" />';
			}else{
				$icon = '';
			}
			$threaddisplay .= '<tr class="'.$altbg.'">
	<td width="1">'.$icon.'</td>
	<td><a href="'.get_thread_link($threaddetails['tid']).'">'.htmlspecialchars_uni($parser->parse_badwords($threaddetails['subject'])).'</a></td>
	<td>'.build_profile_link(format_name(htmlspecialchars_uni($threaddetails['username']), $threaddetails['usergroup'], $threaddetails['displaygroup']), $threaddetails['uid']).'</td>
	<td><a href="'.get_forum_link($threaddetails['fid']).'">'.$threaddetails['name'].'</a></td>
	<td><a href="javascript:MyBB.whoPosted('.intval($threaddetails['tid']).');">'.my_number_format($threaddetails['replies']).'</a></td>
	<td>'.my_number_format($threaddetails['views']).'</td>
</tr>';
			$altbg = alt_trow();
		}
	}else{
		$threaddisplay .= '<tr><td class="trow1" colspan="6">No Threads on this day</td></tr>';
	}
	$threaddisplay .= '</table>';
	$events .= $threaddisplay;
}
?>