<?php
/**
*
* @author Mickaël Salfati (Elglobo) http://www.phpbb-services.com
*
* @version $Id functions_models.php 2010-11-03 Elglobo $
* @copyright (c) 2007 phpBB-Services
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

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

/**
* Generate the models selection
*/
function generate_post_models($forum_id = 0, $error = '', $refresh = '', $preview = '')
{
	global $db, $user, $auth, $template;
	
	$user->add_lang('mods/post_models');
	$selected_language = basename(request_var('selected_language', $user->data['user_lang']));
		
	$s_post_model = $s_language = $button = '';
	$total_models = $total_language = 0;
	
	//Are we in private messaging ?
	$s_auth_model = false;
	if ( !$forum_id || empty($forum_id) )
	{
		$s_auth_model = ($auth->acl_get('u_pm_models')) ? true : false;
		$sql_where_auth = 'm.model_pm = 1';
	}
	else
	{
		
		$s_auth_model = ($auth->acl_get('f_models', $forum_id)) ? true : false;
		$sql_where_auth = 'm.model_pm >= 0';
	}
		
	//I have auth permission to use models
	if ($s_auth_model)
	{
		//Can I use post models founder/admin/mod ?
		if ($user->data['user_type'] == USER_FOUNDER)
		{
			$sql_where_auth .= ' AND m.model_auth <= ' . MODEL_FOUNDERS;
		}
		else if ($auth->acl_get('a_'))
		{
			$sql_where_auth .= ' AND m.model_auth <= ' . MODEL_ADMINISTRATORS;
		}
		else if ($auth->acl_get('m_'))
		{
			$sql_where_auth .= ' AND m.model_auth <= ' . MODEL_MODERATORS;
		}		
		else
		{
			$sql_where_auth .= ' AND m.model_auth = ' . MODEL_USERS;
		}
		
		$postrow = array();
		
		$sql = "SELECT m.model_title, m.model_content, m.model_lang, l.lang_local_name
		   	FROM " . MODELS_TABLE . " m
			LEFT JOIN  " . LANG_TABLE ." l ON (m.model_lang = l.lang_iso)
			WHERE " . $sql_where_auth . "
			AND m.model_lang = l.lang_iso
			ORDER BY model_title, model_lang";
		$result = $db->sql_query($sql);
		
		while ($row = $db->sql_fetchrow($result))
		{
			$postrow[] = $row;
		}
		$db->sql_freeresult($result);
		$total_models = sizeof($postrow);
		
		$s_post_model = '<select id="post_model" name="post_model">';
		
		if ($total_models)
		{	
			//Check if models exist in default language of user
			$default_language = false;
			foreach ($postrow as $row)
			{
				foreach($row as $cell)
				{
					if ($cell == $user->data['user_lang'] && ($default_language == false)) 
					{
						$default_language = true;
					}
				}
			} 
			
			$selected_language = ($default_language) ? $selected_language : $postrow[0]['model_lang'];
			
			$s_post_model .= '<option value="">' . $user->lang['SELECT_MODEL'] . '</option>';
			$s_language .= '<select id="selected_language" name="selected_language" onchange="refresh(\''.$forum_id.'\', this.value);">';
			for($i = 0; $i < $total_models; $i++)
			{
				//Don't add a language in duplicate
				if ( !preg_match('#<option value="' . $postrow[$i]['model_lang'] . '"#', $s_language) )
				{
					$selected = ($postrow[$i]['model_lang'] == $selected_language) ? ' selected="selected"' : '';
					$s_language .= '<option value="' . $postrow[$i]['model_lang'] . '"' . $selected . '>' . $postrow[$i]['lang_local_name'] . '</option>';
					$total_language++;
				}
				
				if ($postrow[$i]['model_lang'] == $selected_language)
				{
					$value = $postrow[$i]['model_content'];
					$s_post_model .= '<option value="' . $value . '">&nbsp;-&nbsp;' . $postrow[$i]['model_title'] . '</option>';
				}
			}
			$s_language .= '</select>';
		}
		$s_post_model .= '</select>';
		$button = '<input type="button" class="button2" name="insert_post_model" value="'. $user->lang['INSERT'] .'" style="width: 70px" onclick="insert_text(post_model.value)" />';
	}
	
	//Output Ajax if language is changed in s_language
	if (isset($_POST['selected_language']) && !$error && !$refresh && !$preview)
	{
		echo $s_language . '&nbsp;' .$s_post_model .'&nbsp;' .$button;
	}
	
	$template->assign_vars(array(
		'BUTTON'					=> $button,
		'S_AUTH_MODEL'				=> $s_auth_model,
		'S_LANGUAGE'				=> $s_language,
		'S_POST_MODEL'				=> $s_post_model,
		'S_TOTAL_MODEL'				=> $total_models,
		'S_TOTAL_LANGUAGE'			=> ($total_language > 1) ? true : false
	));
	
	return;
}

?>