<?php

define('LATESTSUBSCRIBE_ADD_INDEX', 1);

if(!defined('IN_MYBB'))
	die('This file cannot be accessed directly.');

$plugins->add_hook('global_start', 'latestsubscribe_lang');
$plugins->add_hook('search_do_search_process', 'latestsubscribe_getnew');

function latestsubscribe_info()
{
	return array(
		'name'			=> 'View New Subscribed Posts',
		'description'	=> 'Adds ability to filter the Vew New Posts and View Today\'s Posts by subscribed threads.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.1',
		'compatibility'	=> '14*,15*,16*,17*,18*',
		'guid'			=> ''
	);
}


function latestsubscribe_activate()
{
	global $db;
	require MYBB_ROOT.'inc/adminfunctions_templates.php';
	find_replace_templatesets('header_welcomeblock_member', '~'.preg_quote('<a href="{$mybb->settings[\'bburl\']}/search.php?action=getnew">{$lang->welcome_newposts}</a> | ').'~i', '$0<a href="{$mybb->settings[\'bburl\']}/search.php?action=getnew&amp;subscriptionsonly=1">{$lang->welcome_newposts_subscribed}</a> | ');
	find_replace_templatesets('header_welcomeblock_member', '~'.preg_quote('<li><a href="{$mybb->settings[\'bburl\']}/search.php?action=getnew">{$lang->welcome_newposts}</a></li>').'~i', '$0<li><a href="{$mybb->settings[\'bburl\']}/search.php?action=getnew&amp;subscriptionsonly=1">{$lang->welcome_newposts_subscribed}</a></li>');
	
	
	if(defined('LATESTSUBSCRIBE_ADD_INDEX') && LATESTSUBSCRIBE_ADD_INDEX) {
		// add a unique key for the table, for quicker lookup
		// first, as MyBB doesn't do this (it _should_), check and remove duplicates in the table
		$db->write_query('LOCK TABLES '.$db->table_prefix.'threadsubscriptions LOW_PRIORITY WRITE', true);
		$query = $db->simple_select('threadsubscriptions', 'GROUP_CONCAT(sid) AS sids', '1=1 GROUP BY tid,uid HAVING COUNT(sid) > 1');
		$delete_sids = '';
		while($dupe = $db->fetch_array($query)) {
			// remove the first sid
			$sids = substr($dupe['sids'], strpos($dupe['sids'], ','));
			$delete_sids .= $sids;
		}
		if($delete_sids) // delete duplicates
			$db->delete_query('threadsubscriptions', 'sid IN ('.substr($delete_sids, 1).')');
		// now add key
		$db->write_query('ALTER TABLE '.$db->table_prefix.'threadsubscriptions ADD UNIQUE KEY tiduid (tid,uid)');
		$db->write_query('UNLOCK TABLES', true);
	}
}

function latestsubscribe_deactivate()
{
	global $db;
	require MYBB_ROOT.'inc/adminfunctions_templates.php';
	find_replace_templatesets('header_welcomeblock_member', '~(?:\<li\>)?'.preg_quote('<a href="{$mybb->settings[\'bburl\']}/search.php?action=getnew&amp;subscriptionsonly=1">{$lang->welcome_newposts_subscribed}</a>').'(?:\</li\>| \| )~i', '', 0);
	
	if(defined('LATESTSUBSCRIBE_ADD_INDEX') && LATESTSUBSCRIBE_ADD_INDEX) {
		$db->write_query('ALTER TABLE '.$db->table_prefix.'threadsubscriptions DROP KEY tiduid', true);
	}
}

function latestsubscribe_lang() {
	global $lang;
	isset($lang->welcome_newposts_subscribed) or $lang->welcome_newposts_subscribed = 'View New Subscribed Posts';
}

function latestsubscribe_getnew() {
	global $mybb;
	if($mybb->input['action'] != 'getnew' && $mybb->input['action'] != 'getdaily') return;
	if(!$mybb->input['subscriptionsonly'] || !$mybb->user['uid']) return;
	
	global $where_sql, $searcharray, $db;
	//$where_sql .= ' AND EXISTS (SELECT sid FROM '.$db->table_prefix.'threadsubscriptions WHERE uid='.$mybb->user['uid'].' AND tid=t.tid LIMIT 1)';
	$where_sql .= ' AND tid IN (SELECT tid FROM '.$db->table_prefix.'threadsubscriptions WHERE uid='.$mybb->user['uid'].')';
	
	$searcharray['querycache'] = $db->escape_string($where_sql);
}

?>