<?php
/**
* This plugin depends on XThreads.
* Display the latest YouTube videos from XThreads YouTube Video Gallery on Profile
*
* XThreads official release thread:
*
*		http://mybbhacks.zingaburga.com/showthread.php?tid=288
*		Coded by: Yumi/ZiNgA BuRgA
*		@ http://mybbhacks.zingaburga.com
*/

if(!defined('IN_MYBB')){
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

function xtlatest_videos_on_profile_info(){
	return array(
		'name'			=> 'Latest XThreads YouTube Videos On Profile',
		'description'	=> 'Display the latest YouTube videos from XThreads YouTube Video Gallery on Profile',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function xtlatest_videos_on_profile_activate(){
	global $db;
	$template = array(
		'title'		=> 'xtlatest_videos_on_profile',
		'template'	=> $db->escape_string('<br class="clear" />
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead" colspan="2">
			<div class="float_right">
				[<a href="{$GLOBALS[\'forumurl_q\']}filterxt_uid={$memprofile[\'uid\']}">View All</a>]
			</div>
			<strong>Latest Videos</strong>
		</td>
	</tr>
	{$xtlatest_videos_on_profile_video}
</table>'),
		'sid'		=> -1
	);
	$db->insert_query('templates',$template);
	$template = array(
		'title'		=> 'xtlatest_videos_on_profile_video',
		'template'	=> $db->escape_string('<tr>
			<td class="{$altbg}" style="width:1%">
				<a href="{$ytvgal[\'threadlink\']}"><img src="http://img.youtube.com/vi/{$ytvideo[\'ytvgalid\']}/default.jpg" alt="{$ytvgal[\'subject\']}" title="{$ytvgal[\'subject\']}" style="margin: 3px; padding: 3px; border: 1px solid #ADCBE7;" /></a>
			</td>
			<td class="{$altbg}">
				<div><strong><a href="{$ytvgal[\'threadlink\']}">{$ytvgal[\'subject\']}</a></strong></div>
				<div class="smalltext">
					In Category: <a href="{$GLOBALS[\'forumurl_q\']}filtertf_ytvgalcat={$ytvideo[\'ytvgalcat\']}">{$ytvideo[\'ytvgalcat\']}</a>
				</div>
				<div class="smalltext">
					<em>{$ytvgal[\'threaddate\']}, {$ytvgal[\'threadtime\']}</em>
				</div>
				<div class="post_body">{$ytvgal[\'postpreview\']}</div>
				<div class="float_right smalltext">
					{$ytvgal[\'views\']} viewed, {$ytvgal[\'replies\']} comments
				</div>
			</td>
		</tr>'),
		'sid'		=> -1
	);
	$db->insert_query('templates',$template);
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile','#'.preg_quote('{$signature}').'#','{\$signature}{\$xtlatest_videos_on_profile}');
}

function xtlatest_videos_on_profile_deactivate(){
	$GLOBALS['db']->delete_query('templates','title IN("xtlatest_videos_on_profile","xtlatest_videos_on_profile_video")');
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile','#'.preg_quote('{$xtlatest_videos_on_profile}').'#','',0);
}

$plugins->add_hook('global_start','xtlatest_videos_on_profile_tcache');
function xtlatest_videos_on_profile_tcache(){
	if(THIS_SCRIPT == 'member.php'){
		if(isset($GLOBALS['templatelist'])){
			$GLOBALS['templatelist'] .= ',xtlatest_videos_on_profile,xtlatest_videos_on_profile_video';
		}
	}
}

$plugins->add_hook('member_profile_end','xtlatest_videos_on_profile_run');
function xtlatest_videos_on_profile_run(){
	global $mybb,$db,$memprofile;
	$xt_video_fid = 14;// YouTube Video Gallery forum id
	$xt_video_limit = 5;// Max num of videos to be displayed on profile
	$threadfield_cache = xthreads_gettfcache((int)$xt_video_fid);
	if(!empty($threadfield_cache)){
		$xtfields = '';
		foreach($threadfield_cache as &$tf){
			$xtfields .= ',tf.'.$tf['field'].' AS xthreads_'.$tf['field'];
		}
	}
	$query = $db->query('
		SELECT t.*'.$xtfields.'
		FROM '.TABLE_PREFIX.'threads t
		LEFT JOIN '.TABLE_PREFIX.'threadfields_data tf ON (tf.tid=t.tid)
		WHERE t.fid='.(int)$xt_video_fid.' AND t.uid='.(int)$memprofile['uid'].' AND t.visible=1 AND t.closed NOT LIKE "moved|%"
		ORDER BY t.dateline DESC
		LIMIT 0,'.(int)$xt_video_limit.'
	');
	if($db->num_rows($query)){
		$ytvgals = array();
		while($ytvgal = $db->fetch_array($query)){
			$ytvgals[$ytvgal['tid']] = $ytvgal;
		}
		if(!empty($ytvgals)){
			global $ytvideo,$theme,$xtlatest_videos_on_profile,$parser,$templates;
			$xtlatest_videos_on_profile_video = '';
			$tids = implode(',',array_keys($ytvgals));
			$altbg = alt_trow();
			if(!is_object($parser)){
				require_once MYBB_ROOT.'inc/class_parser.php';
				$parser = new postParser;
			}
			foreach($ytvgals as $ytvgal){
				$ytvgal['threaddate'] = my_date($mybb->settings['dateformat'],$ytvgal['dateline']);
				$ytvgal['threadtime'] = my_date($mybb->settings['timeformat'],$ytvgal['dateline']);
				$ytvgal['replies'] = my_number_format($ytvgal['replies']);
				$ytvgal['views'] = my_number_format($ytvgal['views']);
				$ytvgal['subject'] = htmlspecialchars_uni($parser->parse_badwords($ytvgal['subject']));
				$ytvgal['threadlink'] = get_thread_link((int)$ytvgal['tid']);
				if($ytvgal['postpreview']){
					$ytvgal['postpreview'] = htmlspecialchars_uni($parser->parse_badwords($ytvgal['postpreview']));
				}
				$ytvgal['forumlink'] = get_thread_link((int)$ytvgal['fid']);
				xthreads_set_threadforum_urlvars('thread',$ytvgal['tid']);
				xthreads_set_threadforum_urlvars('forum',$ytvgal['fid']);
				$ytvideo = array();
				foreach($threadfield_cache as $k => &$v) {
					$ytvideo[$k] =& $ytvgal['xthreads_'.$k];
					xthreads_sanitize_disp($ytvideo[$k],$v);
				}
				eval('$xtlatest_videos_on_profile_video .= "'.$GLOBALS['templates']->get('xtlatest_videos_on_profile_video').'";');
				$altbg = alt_trow();
			}
			eval('$xtlatest_videos_on_profile = "'.$GLOBALS['templates']->get('xtlatest_videos_on_profile').'";');
		}
	}
}
?>