<?php
/**
* This plugin depends on XThreads.
* Displayiong latest videos uploaded by user on their 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 xt_lvop_info(){
	return array(
		'name'			=> 'Latest Videos On Profile',
		'description'	=> 'Displaying latest videos uploaded by users on their profile',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function xt_lvop_activate(){
	global $db;
	$db->insert_query('templates', array(
		'title'		=> 'xt_lvop_latest_videos',
		'template'	=> $db->escape_string('<br class="clear" />
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead" colspan="2">
			<strong>Latest Videos</strong>
		</td>
	</tr>
	{$xt_lvop_latest_videos_video}
</table>'),
		'sid'		=> -1
	));
	$db->insert_query('templates', array(
		'title'		=> 'xt_lvop_latest_videos_video',
		'template'	=> $db->escape_string('<tr>
	<td class="{$altbg}" style="width:1%;text-align:center">
		{$GLOBALS[\'threadfields\'][\'xtmv_thumb\'][\'value\']}
	</td>
	<td class="{$altbg}">
		<div><strong><a href="{$thread[\'threadlink\']}">{$thread[\'subject\']}</a></strong></div>
		<div class="smalltext">
			<em>{$thread[\'threaddate\']}, {$thread[\'threadtime\']}</em>
		</div>
		<div class="post_body">{$thread[\'postpreview\']}</div>
		<div class="float_right smalltext">
			{$thread[\'views\']} viewed, {$thread[\'replies\']} comments
		</div>
	</td>
</tr>'),
		'sid'		=> -1
	));
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile','#'.preg_quote('{$signature}').'#','{\$signature}{\$xt_lvop_latest_videos}');
}

function xt_lvop_deactivate(){
	$GLOBALS['db']->delete_query('templates','title IN("xt_lvop_latest_videos","xt_lvop_latest_videos_video")');
	require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('member_profile','#'.preg_quote('{$xt_lvop_latest_videos}').'#','',0);
}

$plugins->add_hook('global_start','xt_lvop_tcache');
function xt_lvop_tcache(){
	if(THIS_SCRIPT == 'member.php'){
		if(isset($GLOBALS['templatelist'])){
			$GLOBALS['templatelist'] .= ',xt_lvop_latest_videos,xt_lvop_latest_videos_video';
		}
	}
}

$plugins->add_hook('member_profile_end','xt_lvop_run');
function xt_lvop_run(){
	global $memprofile,$mybb,$db;
	$xt_video_fid = 2;// XThreads Video Gallery forum id
	$xt_video_limit = 5;// Max num of latest videos displayed on profile
	$threadfield_cache = xthreads_gettfcache(intval($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='.intval($xt_video_fid).' AND t.uid='.intval($memprofile['uid']).' AND t.visible=1 AND t.closed NOT LIKE "moved|%"
		ORDER BY t.dateline DESC
		LIMIT 0,'.intval($xt_video_limit)
	);
	if($db->num_rows($query)){
		$altbg = alt_trow();
		$threadcache = array();
		while($thread = $db->fetch_array($query)){
			$threadcache[$thread['tid']] = $thread;
		}
		if(!empty($threadcache)){
			global $threadfields,$xt_lvop_latest_videos,$theme,$parser;
			$xt_lvop_latest_videos_video = '';
			$tids = implode(',',array_keys($threadcache));
			if(!is_object($parser)){
				require_once MYBB_ROOT.'inc/class_parser.php';
				$parser = new postParser;
			}
			foreach($threadcache as $thread){
				$thread['threaddate'] = my_date($mybb->settings['dateformat'],$thread['dateline']);
				$thread['threadtime'] = my_date($mybb->settings['timeformat'],$thread['dateline']);
				$thread['replies'] = my_number_format($thread['replies']);
				$thread['views'] = my_number_format($thread['views']);
				$thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
				$thread['threadlink'] = get_thread_link(intval($thread['tid']));
				if($thread['postpreview']){
					$thread['postpreview'] = htmlspecialchars_uni($parser->parse_badwords($thread['postpreview']));
				}
				xthreads_set_threadforum_urlvars('thread',$thread['tid']);
				$threadfields = array();
				foreach($threadfield_cache as $k => &$v) {
					xthreads_get_xta_cache($v,$tids);
					$threadfields[$k] =& $thread['xthreads_'.$k];
					xthreads_sanitize_disp($threadfields[$k],$v);
				}
				eval('$xt_lvop_latest_videos_video .= "'.$GLOBALS['templates']->get('xt_lvop_latest_videos_video').'";');
				$altbg = alt_trow();
			}
			eval('$xt_lvop_latest_videos = "'.$GLOBALS['templates']->get('xt_lvop_latest_videos').'";');
		}
	}
}
?>