<?php
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ******************************************************************** >>
<< * ---------------------------------------------------------------- * >>
<< * Copyright ©2011-2012 Ozzy47                                      * >>
<< * All Rights Reserved. 											  * >>
<< * This file may not be redistributed in whole or significant part. * >>
<< * ---------------------------------------------------------------- * >>
<< * You are not allowed to use this on your server unless the files  * >>
<< * you downloaded were done so with permission.					  * >>
<< * ---------------------------------------------------------------- * >>
<< ******************************************************************** >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

//Check to see if the entire system is active in General Settings
if (!$vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
{
	eval(standard_error(fetch_error('dbtech_vbsuper_pm_warning_disabled')));
}
		
// ######################### REQUIRE BACK-END ############################
require_once(DIR . '/includes/functions_user.php');

// #################### DEFINE IMPORTANT CONSTANTS #######################
$user_id = $vbulletin->userinfo['userid'];

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

	if (($_REQUEST['action'] == 'get_warning') AND !is_member_of($vbulletin->userinfo, explode(',', $vbulletin->options['dbtech_vbsuper_pm_warning_msg_allowed'])))
	{
		eval(standard_error($vbulletin->options['dbtech_vbsuper_pm_cantuse_warning']));
	}

// ############################### start display options ###############################
	if (!$vbulletin->options['dbtech_vbsuper_pm_warning_msg_active'])
	{
		eval(standard_error(fetch_error('dbtech_vbsuper_pm_warning_msg_disabled')));
	}
	else
	{	
		if ($_REQUEST['action'] == 'get_warning')
		{
			// Navigation bits
			$navbits[''] = $vbphrase['dbtech_vbsuper_pm_warning_nav_link'];
							
				//Get users warning message 
    			$pm_warning_msg = $db->query_first("
				SELECT warning, warning_active
				FROM " . TABLE_PREFIX . "dbtech_vbsuper_pm_warning
				WHERE userid = '" . $user_id . "'
			");

			$pm_warning_msg['warning'] = iif(empty($pm_warning_msg['warning']), $vbphrase['dbtech_vbsuper_pm_warning_default'], $pm_warning_msg['warning']);
			$pm_warning_msg['warning_active'] = iif(empty($pm_warning_msg['warning_active']), '0', $pm_warning_msg['warning_active']);

			$page_templater = vB_Template::create('dbtech_vbsuper_pm_warning_options');
			$page_templater->register('pm_warning_msg', $pm_warning_msg);
			$page_templater->register('pm_warning_msgs', $pm_warning_msgs);
			$template_hook['usercp_options_other'] .= $page_templater->render();		
				
		}		

// ############################### start save options ##################################
	if ($_POST['action'] == 'save_warning')
	{	
		$vbulletin->input->clean_array_gpc('p', array(
			'pm_warning_msg' => TYPE_ARRAY_NOHTML
			
		));			

		if (count($vbulletin->GPC['pm_warning_msg']))
		{
			// Make sure auto responder is active
				$vbulletin->GPC['pm_warning_msg']['warning_active'] = iif(
				in_array($vbulletin->GPC['pm_warning_msg']['warning_active'], array('0', '1')),
				$vbulletin->GPC['pm_warning_msg']['warning_active'],
				'0'
			);

			// Default message if not entered
			if (empty($vbulletin->GPC['pm_warning_msg']['warning']) OR $vbulletin->GPC['pm_warning_msg']['warning'] == $vbphrase['dbtech_vbsuper_pm_warning_default'])
			{
				$vbulletin->GPC['pm_warning_msg']['warning'] = ' ';
			}							
			
			$db->query_write("
				REPLACE INTO " . TABLE_PREFIX . "dbtech_vbsuper_pm_warning
					(userid, warning, warning_active)
				VALUES
					(
						'" . intval($vbulletin->userinfo['userid']) . "',
						'" . $db->escape_string($vbulletin->GPC['pm_warning_msg']['warning']) . "',
						'" . intval($vbulletin->GPC['pm_warning_msg']['warning_active']) . "'
					)
			");				
						
		}

		$vbulletin->url = 'vbsuper_pm.php?' . $vbulletin->session->vars['sessionurl'] . 'do=warning&action=get_warning';
		print_standard_redirect(array('redirect_dbtech_vbsuper_pm_updatethanks_warning_msg',$vbulletin->userinfo['username']), true, true);
	}

	$navbar = render_navbar_template(construct_navbits($navbits));	
	construct_usercp_nav('dbtech_vbsuper_pm_' . $_REQUEST['action']);

	$templater = vB_Template::create('USERCP_SHELL');
		$templater->register_page_templates();
		$templater->register('cpnav', $cpnav);
		if (method_exists($page_templater, 'render'))
		{
			// Only run this if there's anything to render
			$templater->register('HTML', $page_templater->render());
		}
		$templater->register('clientscripts', $clientscripts);
		$templater->register('navbar', $navbar);
		$templater->register('navclass', $navclass);
		$templater->register('onload', $onload);
		$templater->register('pagetitle', $pagetitle);
		$templater->register('template_hook', $template_hook);
	print_output($templater->render());
	}		

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ********************************************************************* >>
<< * Created: 09:30, Fri June 8th 2012                                 * >>
<< * VER: 1.0.0                                                        * >>
<< ********************************************************************* >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
?>