<?php
/**
*
* @package acp
* @version $Id acp_models.php 2009-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;
}

/**
* @package acp
*/
class acp_models
{
	var $u_action;

	function main($id, $mode)
	{
		global $db, $user, $auth, $template;
		global $phpbb_root_path, $phpEx;
		
		include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
		$user->add_lang(array('posting', 'mods/info_acp_models'));

		// Set up general vars
		$action = request_var('action', '');
		$submit = (isset($_POST['submit'])) ? true : false;
		$model_id = request_var('id', 0);
		
		$this->tpl_name = 'acp_models';
		$this->page_title = 'ACP_MODELS';

		$form_name = 'acp_models';
		add_form_key('acp_models');
		
		// Check if we have permission to manage post models
		if (!$auth->acl_get('a_models'))
		{
			trigger_error($user->lang['NO_PERMISSIONS'], E_USER_WARNING);
		}

		$error = array();
		switch ($action)
		{
			case 'add':
			case 'edit':
			
				generate_smilies('inline', '',1);
				$model_row = array(
					'model_title'			=> utf8_normalize_nfc(request_var('model_title', '', true)),
					'model_content'			=> utf8_normalize_nfc(request_var('model_content', '', true)),
					'model_auth'			=> request_var('model_auth', 0),
					'model_pm'				=> request_var('model_pm', 0),
					'model_lang'			=> request_var('model_lang', '', true),
				);

				if ($submit)
				{
					if (!check_form_key($form_name))
					{
						$error[] = $user->lang['FORM_INVALID'];
					}
					// Model specified?
					if (!$model_row['model_title'] || !$model_row['model_content'] || !$model_row['model_lang'])
					{
						$error[] = $user->lang['NO_MODEL_INFO'];
					}

					$check_double = ($action == 'add') ? true : false;

					if ($action == 'edit')
					{
						$sql = 'SELECT model_title
							FROM ' . MODELS_TABLE . "
							WHERE model_id = " . (int) $model_id;
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						$db->sql_freeresult($result);

						if ($row['model_title'] != $model_row['model_title'])
						{
							$check_double = true;
						}
					}

					// Check for same model if adding it...
					if ($check_double)
					{
						$sql = 'SELECT model_id
							FROM ' . MODELS_TABLE . "
							WHERE model_title = '" . $db->sql_escape($model_row['model_title']) . "'";
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						$db->sql_freeresult($result);

						if ($row)
						{
							$error[] = $user->lang['MODEL_ALREADY_EXIST'];
						}
					}

					if (!sizeof($error))
					{
						// New model?
						if ($action == 'add')
						{
							$sql_ary = array(
								'model_title'		=> (string) $model_row['model_title'],
								'model_content'		=> (string) $model_row['model_content'],
								'model_auth'		=> (int) $model_row['model_auth'],
								'model_pm'			=> (int) $model_row['model_pm'],
								'model_lang'		=> (string) $model_row['model_lang'],
							);

							$db->sql_query('INSERT INTO ' . MODELS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));

							$log = 'ADDED';
						}
						else if ($model_id)
						{
							$sql_ary = array(
								'model_title'		=> (string) $model_row['model_title'],
								'model_content'		=> (string) $model_row['model_content'],
								'model_auth'		=> (int) $model_row['model_auth'],
								'model_pm'			=> (int) $model_row['model_pm'],
								'model_lang'		=> (string) $model_row['model_lang'],
							);

							$db->sql_query('UPDATE ' . MODELS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
								WHERE model_id = ' . (int) $model_id);

							$log = 'UPDATED';
						}

						add_log('admin', 'LOG_MODEL_' . $log, $model_row['model_title']);
						trigger_error($user->lang['MODEL_' . $log] . adm_back_link($this->u_action));
					}
				}
				else if ($model_id)
				{
					$sql = 'SELECT *
						FROM ' . MODELS_TABLE . '
						WHERE model_id = ' . (int) $model_id;
					$result = $db->sql_query($sql);
					$model_row = $db->sql_fetchrow($result);
					$db->sql_freeresult($result);

					if (!$model_row)
					{
						trigger_error($user->lang['NO_MODEL'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
				}

				$l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
				$default_language = ($action == 'edit') ? $model_row['model_lang'] : $user->data['user_lang'];
				
				$s_selected_users = $s_selected_mod = $s_selected_admin = $s_selected_founders = '';
				
				if ($model_row['model_auth'] == MODEL_USERS)
				{
					$s_selected_users = 'selected="selected"';
				}
				else if ($model_row['model_auth'] == MODEL_MODERATORS)
				{
					$s_selected_mod = 'selected="selected"';
				}
				else if ($model_row['model_auth'] == MODEL_ADMINISTRATORS)
				{
					$s_selected_admin = 'selected="selected"';
				}
				else if ($model_row['model_auth'] == MODEL_FOUNDERS)
				{
					$s_selected_founders = 'selected="selected"';
				}

				$template->assign_vars(array(
					'L_TITLE'		=> $user->lang['MODEL_' . $l_title],
					'U_ACTION'		=> $this->u_action . "&amp;id=$model_id&amp;action=$action",
					'U_BACK'		=> $this->u_action,
					'ERROR_MSG'		=> (sizeof($error)) ? implode('<br />', $error) : '',
					
					'MODEL_TITLE'			=> $model_row['model_title'],
					'MODEL_CONTENT'			=> $model_row['model_content'],
					'MODEL_PM'				=> $model_row['model_pm'],
					'S_MODEL_LANG'			=> language_select($default_language),
					'S_SELECTED_USERS'		=> $s_selected_users,
					'S_SELECTED_MOD'		=> $s_selected_mod,
					'S_SELECTED_ADMIN'		=> $s_selected_admin,
					'S_SELECTED_FOUNDERS'	=> $s_selected_founders,

					'S_EDIT_MODEL'			=> true,
					'S_ERROR'				=> (sizeof($error)) ? true : false,
					)
				);

				return;
			break;

			case 'delete':

				$sql = 'SELECT *
					FROM ' . MODELS_TABLE . '
					WHERE model_id = ' . (int) $model_id;
				$result = $db->sql_query($sql);
				$model_row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);

				if (!$model_row)
				{
					trigger_error($user->lang['NO_MODEL'] . adm_back_link($this->u_action), E_USER_WARNING);
				}

				// Let the deletion be confirmed...
				if (confirm_box(true))
				{
					$db->sql_query('DELETE FROM ' . MODELS_TABLE . ' WHERE model_id = ' . (int) $model_id);

					add_log('admin', 'LOG_MODEL_REMOVED', $model_row['model_title']);
					trigger_error($user->lang['MODEL_REMOVED'] . adm_back_link($this->u_action));
				}
				else
				{
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
						'i'			=> $id,
						'mode'		=> $mode,
						'action'	=> $action,
						'id'		=> $model_id))
					);
				}

			break;
		}
		
		$sql = "SELECT lang_iso
			FROM " . LANG_TABLE . "
			ORDER BY lang_english_name";
		$result = $db->sql_query($sql);
		
		$language = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$language[] = $row['lang_iso'];
		}
		$db->sql_freeresult($result);
		
		$sql = "SELECT model_id, model_title, model_content, model_auth, model_lang
			FROM " . MODELS_TABLE . "
			ORDER BY model_title ASC";
		$result = $db->sql_query($sql);
		
		while ($row = $db->sql_fetchrow($result))
		{
			$model_auth = array($user->lang['USERS'], $user->lang['MODERATORS'], $user->lang['ADMINISTRATORS'], $user->lang['FOUNDERS']);
			$no_exist = (in_array($row['model_lang'], $language)) ? false : true;
			
			$template->assign_block_vars('models', array(
				'MODEL_TITLE'	=> $row['model_title'],
				'MODEL_AUTH'	=> $model_auth[$row['model_auth']],
				'MODEL_LANG'	=> $row['model_lang'],
				'MODEL_EXIST'	=> ($no_exist == true) ? $user->lang['NO_EXIST'] : '',
				
				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['model_id'],
				'U_DELETE'		=> $this->u_action . '&amp;action=delete&amp;id=' . $row['model_id'],)
			);
		}
		$db->sql_freeresult($result);
	}
}

?>