<?php
if(!defined('IN_MYBB')) die();

function mnxt_filter_non_sticky_info(){
	return array(
		'name'			=> 'Filter Sticky or Non Sticky Threads',
		'description'	=> '<strong style="color: red"><em><a href="http://mybbhacks.zingaburga.com/showthread.php?tid=288">XThreads</a> is required for this plugin</em></strong><br />Display only sticky or non sticky threads on forumdisplay. Accessible by using <span style="font-family:courier">&sticky=0</span> or <span style="font-family:courier">&sticky=1</span> at the end of the URL',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'My Nie',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

$plugins->add_hook('forumdisplay_start','mnxt_filter_non_sticky',99);
function mnxt_filter_non_sticky(){
	if(!in_array($GLOBALS['mybb']->input['sticky'],array('0','1'))) return;
	$sticky = (int)$GLOBALS['mybb']->input['sticky'];
	control_object($GLOBALS['db'],'
		function simple_select($table, $fields="*", $conditions="", $options=array()){
			static $done_p = false;
			if(!$done_p){
				$done_p = true;
				if($table == \'threads\' && ($fields == \'COUNT(tid) AS threads\' || $fields == \'COUNT(tid) AS threads, 0 AS unapprovedthreads, 0 AS deletedthreads\')){
					$conditions .= \' AND sticky='.$sticky.'\';
				}elseif($table == \'threads t LEFT JOIN '.TABLE_PREFIX.'threadfields_data tfd ON t.tid=tfd.tid\' && ($fields == \'COUNT(t.tid) AS threads, 0 AS unapprovedthreads, 0 AS deletedthreads\' || $fields == \'COUNT(t.tid) AS threads\')){
					$conditions .= \' AND t.sticky='.$sticky.'\';
				}elseif($table == "forums" && $fields == "threads, unapprovedthreads, deletedthreads"){
					$table = "threads";
					$fields = "COUNT(tid) AS threads, 0 AS unapprovedthreads, 0 AS deletedthreads";
					$conditions .= " $GLOBALS[visibleonly] $GLOBALS[useronly] $GLOBALS[datecutsql] $GLOBALS[prefixsql] AND sticky='.$sticky.'";
				}
			}
			return parent::simple_select($table, $fields, $conditions, $options);
		}
	');
	control_object($GLOBALS['templates'],'
		function get($title, $eslashes=1, $htmlcomments=1){
			if($title == \'forumdisplay_orderarrow\'){
				$GLOBALS[\'sorturl\'] .= \'&amp;sticky='.$sticky.'\';
			}
			return parent::get($title, $eslashes, $htmlcomments);
		}
	');
	control_object($GLOBALS['templates'],'
		function get($title, $eslashes=1, $htmlcomments=1){
			if($title == \'forumdisplay_threadlist\'){
				$GLOBALS[\'prefixselect\'] .= \'<input type="hidden" name="sticky" value="'.$sticky.'" />\';
			}
			return parent::get($title, $eslashes, $htmlcomments);
		}
	');
	control_object($GLOBALS['templates'],'
		function get($title, $eslashes=1, $htmlcomments=1){
			if($title == \'forumdisplay_searchforum_inline\'){
				$GLOBALS[\'xthreads_forum_filter_form\'] .= \'<input type="hidden" name="sticky" value="'.$sticky.'" />\';
			}
			return parent::get($title, $eslashes, $htmlcomments);
		}
	');
	global $sticky_input;
	$sticky_input = '&amp;sticky='.$sticky;
	$tpl_array = array('multipage_end','multipage_nextpage','multipage_page','multipage_prevpage','multipage_start');
	foreach($tpl_array as &$tpl) {
		$GLOBALS['templates']->cache[$tpl] = str_replace('{$page_url}','{$page_url}{$GLOBALS[\'sticky_input\']}',$GLOBALS['templates']->cache[$tpl]);
	}
}

$plugins->add_hook('forumdisplay_get_threads','mnxt_filter_non_sticky_threads',99);
function mnxt_filter_non_sticky_threads(){
	if(!in_array($GLOBALS['mybb']->input['sticky'],array('0','1'))) return;
	$sticky = (int)$GLOBALS['mybb']->input['sticky'];
	control_object($GLOBALS['db'],'
		function query($string, $hide_errors=0, $write_query=0) {
			static $done=false;
			if(!$done && !$write_query && strpos($string, \'t.username AS threadusername, u.username\')) {
				$done = true;
				$string = strtr($string, array(
					\'ORDER BY t.sticky DESC\' => \' AND t.sticky='.$sticky.' ORDER BY t.sticky DESC\'
				));
			}
			return parent::query($string, $hide_errors, $write_query);
		}
	');
}
