<?php
/**
*
*
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

class q2nt
{
	/**
	* Are we requesting Q2NT?
	*/
	private static $req_q2nt = false;

	/**
	* Initialise the MOD
	*/
	public static function init()
	{
		// This is a bit nasty, we check for the a2nt value in the "mode" request variable
		// if it is set we internally track it and change this value to "quote" inorder to
		// keep the posting page work correctly
		if ('quote_nt' == ($mode = request_var('mode', '')))
		{
			self::$req_q2nt = true;
			$_REQUEST['mode'] = 'quote';
		}
	}

	/**
	* We need to modify some information from the post request inorder to achieve
	* the result we are looking for.
	*
	* @param	String	$mode	The mode variable that must be updated
	* @param	Mixed	$data	The array with post data
	*/
	public static function alter_post_request(&$mode, &$data)
	{
		// MOD not used?
		if (self::$req_q2nt == false)
		{
			return;
		}

		// Do we want to create a post in a given forum?
		$target_forum = request_var('q2nt_new_forum', 0);
		if ($target_forum != 0)
		{
			// Does this forum exist?
			global $auth, $db;

			$sql = 'SELECT forum_name
				FROM ' . FORUMS_TABLE . '
				WHERE forum_id = ' . $target_forum;
			$result	= $db->sql_query($sql);
			$fn		= $db->sql_fetchfield('forum_name', false, $result);
			$db->sql_freeresult($result);

			// Forum doesn't exists or user isn't allowed to start new topics there
			if (!$fn || $auth->acl_get('!f_post', $target_forum))
			{
				// Just reply with a quote to the actual topic
				return;
			}

			// Change the mode
			$mode = 'post';

			// Set the target forum id
			$data['forum_id'] = $target_forum;
		}
	}

	/**
	* Add some stuff to the posting page. As well alter the $s_action variable to
	* include our token if required
	*
	* @param	String	$s_action	The $s_action variable from posting.php
	* @param	String	$mode		The post mode
	* @param	int		$fid		The forum id
	*/
	public static function alter_posting_page(&$s_action, $mode, $fid = 0)
	{
		if (self::$req_q2nt == false)
		{
			return;
		}

		// Modify $s_action
		$s_action = str_replace('mode=' . $mode, 'mode=quote_nt', $s_action);

		// Assign the forum dropdown box to the templates
		if (!function_exists('make_forum_select'))
		{
			global $phpbb_root_path, $phpEx;
			include ($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
		}

		$forum_options = make_forum_select($fid, false, false, false, false, true);

		global $template;
		$template->assign_var('S_Q2NT_NEW_FORUM_OPTIONS', $forum_options);
	}
}
?>