Blame Identity/Webenv/phpBB/3.0.4/includes/acp/acp_reasons.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_reasons.php 8479 2008-03-29 00:22:48Z naderman $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* @package acp
ef5584
*/
ef5584
class acp_reasons
ef5584
{
ef5584
	var $u_action;
ef5584
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		$user->add_lang(array('mcp', 'acp/posting'));
ef5584
ef5584
		// Set up general vars
ef5584
		$action = request_var('action', '');
ef5584
		$submit = (isset($_POST['submit'])) ? true : false;
ef5584
		$reason_id = request_var('id', 0);
ef5584
ef5584
		$this->tpl_name = 'acp_reasons';
ef5584
		$this->page_title = 'ACP_REASONS';
ef5584
ef5584
		$form_name = 'acp_reason';
ef5584
		add_form_key('acp_reason');
ef5584
ef5584
		$error = array();
ef5584
ef5584
		switch ($action)
ef5584
		{
ef5584
			case 'add':
ef5584
			case 'edit':
ef5584
ef5584
				$reason_row = array(
ef5584
					'reason_title'			=> utf8_normalize_nfc(request_var('reason_title', '', true)),
ef5584
					'reason_description'	=> utf8_normalize_nfc(request_var('reason_description', '', true)),
ef5584
				);
ef5584
ef5584
				if ($submit)
ef5584
				{
ef5584
					if (!check_form_key($form_name))
ef5584
					{
ef5584
						$error[] = $user->lang['FORM_INVALID'];
ef5584
					}
ef5584
					// Reason specified?
ef5584
					if (!$reason_row['reason_title'] || !$reason_row['reason_description'])
ef5584
					{
ef5584
						$error[] = $user->lang['NO_REASON_INFO'];
ef5584
					}
ef5584
ef5584
					$check_double = ($action == 'add') ? true : false;
ef5584
ef5584
					if ($action == 'edit')
ef5584
					{
ef5584
						$sql = 'SELECT reason_title
ef5584
							FROM ' . REPORTS_REASONS_TABLE . "
ef5584
							WHERE reason_id = $reason_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if (strtolower($row['reason_title']) == 'other' || strtolower($reason_row['reason_title']) == 'other')
ef5584
						{
ef5584
							$reason_row['reason_title'] = 'other';
ef5584
						}
ef5584
ef5584
						if ($row['reason_title'] != $reason_row['reason_title'])
ef5584
						{
ef5584
							$check_double = true;
ef5584
						}
ef5584
					}
ef5584
ef5584
					// Check for same reason if adding it...
ef5584
					if ($check_double)
ef5584
					{
ef5584
						$sql = 'SELECT reason_id
ef5584
							FROM ' . REPORTS_REASONS_TABLE . "
ef5584
							WHERE reason_title = '" . $db->sql_escape($reason_row['reason_title']) . "'";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if ($row || ($action == 'add' && strtolower($reason_row['reason_title']) == 'other'))
ef5584
						{
ef5584
							$error[] = $user->lang['REASON_ALREADY_EXIST'];
ef5584
						}
ef5584
					}
ef5584
ef5584
					if (!sizeof($error))
ef5584
					{
ef5584
						// New reason?
ef5584
						if ($action == 'add')
ef5584
						{
ef5584
							// Get new order...
ef5584
							$sql = 'SELECT MAX(reason_order) as max_reason_order
ef5584
								FROM ' . REPORTS_REASONS_TABLE;
ef5584
							$result = $db->sql_query($sql);
ef5584
							$max_order = (int) $db->sql_fetchfield('max_reason_order');
ef5584
							$db->sql_freeresult($result);
ef5584
							
ef5584
							$sql_ary = array(
ef5584
								'reason_title'			=> (string) $reason_row['reason_title'],
ef5584
								'reason_description'	=> (string) $reason_row['reason_description'],
ef5584
								'reason_order'			=> $max_order + 1
ef5584
							);
ef5584
ef5584
							$db->sql_query('INSERT INTO ' . REPORTS_REASONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
ef5584
ef5584
							$log = 'ADDED';
ef5584
						}
ef5584
						else if ($reason_id)
ef5584
						{
ef5584
							$sql_ary = array(
ef5584
								'reason_title'			=> (string) $reason_row['reason_title'],
ef5584
								'reason_description'	=> (string) $reason_row['reason_description'],
ef5584
							);
ef5584
ef5584
							$db->sql_query('UPDATE ' . REPORTS_REASONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
ef5584
								WHERE reason_id = ' . $reason_id);
ef5584
ef5584
							$log = 'UPDATED';
ef5584
						}
ef5584
ef5584
						add_log('admin', 'LOG_REASON_' . $log, $reason_row['reason_title']);
ef5584
						trigger_error($user->lang['REASON_' . $log] . adm_back_link($this->u_action));
ef5584
					}
ef5584
				}
ef5584
				else if ($reason_id)
ef5584
				{
ef5584
					$sql = 'SELECT *
ef5584
						FROM ' . REPORTS_REASONS_TABLE . '
ef5584
						WHERE reason_id = ' . $reason_id;
ef5584
					$result = $db->sql_query($sql);
ef5584
					$reason_row = $db->sql_fetchrow($result);
ef5584
					$db->sql_freeresult($result);
ef5584
ef5584
					if (!$reason_row)
ef5584
					{
ef5584
						trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
				}
ef5584
ef5584
				$l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
ef5584
ef5584
				$translated = false;
ef5584
ef5584
				// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
ef5584
				if (isset($user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])]))
ef5584
				{
ef5584
					$translated = true;
ef5584
				}
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'L_TITLE'		=> $user->lang['REASON_' . $l_title],
ef5584
					'U_ACTION'		=> $this->u_action . "&id=$reason_id&action=$action",
ef5584
					'U_BACK'		=> $this->u_action,
ef5584
					'ERROR_MSG'		=> (sizeof($error)) ? implode('
', $error) : '',
ef5584
					
ef5584
					'REASON_TITLE'			=> $reason_row['reason_title'],
ef5584
					'REASON_DESCRIPTION'	=> $reason_row['reason_description'],
ef5584
ef5584
					'TRANSLATED_TITLE'		=> ($translated) ? $user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])] : '',
ef5584
					'TRANSLATED_DESCRIPTION'=> ($translated) ? $user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])] : '',
ef5584
ef5584
					'S_AVAILABLE_TITLES'	=> implode(', ', array_map('htmlspecialchars', array_keys($user->lang['report_reasons']['TITLE']))),
ef5584
					'S_EDIT_REASON'			=> true,
ef5584
					'S_TRANSLATED'			=> $translated,
ef5584
					'S_ERROR'				=> (sizeof($error)) ? true : false,
ef5584
					)
ef5584
				);
ef5584
ef5584
				return;
ef5584
			break;
ef5584
ef5584
			case 'delete':
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . REPORTS_REASONS_TABLE . '
ef5584
					WHERE reason_id = ' . $reason_id;
ef5584
				$result = $db->sql_query($sql);
ef5584
				$reason_row = $db->sql_fetchrow($result);
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				if (!$reason_row)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				if (strtolower($reason_row['reason_title']) == 'other')
ef5584
				{
ef5584
					trigger_error($user->lang['NO_REMOVE_DEFAULT_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				// Let the deletion be confirmed...
ef5584
				if (confirm_box(true))
ef5584
				{
ef5584
					$sql = 'SELECT reason_id
ef5584
						FROM ' . REPORTS_REASONS_TABLE . "
ef5584
						WHERE LOWER(reason_title) = 'other'";
ef5584
					$result = $db->sql_query($sql);
ef5584
					$other_reason_id = (int) $db->sql_fetchfield('reason_id');
ef5584
					$db->sql_freeresult($result);
ef5584
ef5584
					switch ($db->sql_layer)
ef5584
					{
ef5584
						// The ugly one!
ef5584
						case 'mysqli':
ef5584
						case 'mysql4':
ef5584
						case 'mysql':
ef5584
							// Change the reports using this reason to 'other'
ef5584
							$sql = 'UPDATE ' . REPORTS_TABLE . '
ef5584
								SET reason_id = ' . $other_reason_id . ", report_text = CONCAT('" . $db->sql_escape($reason_row['reason_description']) . "\n\n', report_text)
ef5584
								WHERE reason_id = $reason_id";
ef5584
						break;
ef5584
ef5584
						// Standard? What's that?
ef5584
						case 'mssql':
ef5584
						case 'mssql_odbc':
ef5584
							// Change the reports using this reason to 'other'
ef5584
							$sql = "DECLARE @ptrval binary(16)
ef5584
ef5584
									SELECT @ptrval = TEXTPTR(report_text)
ef5584
										FROM " . REPORTS_TABLE . "
ef5584
									WHERE reason_id = " . $reason_id . "
ef5584
ef5584
									UPDATETEXT " . REPORTS_TABLE . ".report_text @ptrval 0 0 '" . $db->sql_escape($reason_row['reason_description']) . "\n\n'
ef5584
ef5584
									UPDATE " . REPORTS_TABLE . '
ef5584
										SET reason_id = ' . $other_reason_id . "
ef5584
									WHERE reason_id = $reason_id";
ef5584
						break;
ef5584
ef5584
						// Teh standard
ef5584
						case 'postgres':
ef5584
						case 'oracle':
ef5584
						case 'firebird':
ef5584
						case 'sqlite':
ef5584
							// Change the reports using this reason to 'other'
ef5584
							$sql = 'UPDATE ' . REPORTS_TABLE . '
ef5584
								SET reason_id = ' . $other_reason_id . ", report_text = '" . $db->sql_escape($reason_row['reason_description']) . "\n\n' || report_text
ef5584
								WHERE reason_id = $reason_id";
ef5584
						break;
ef5584
					}
ef5584
					$db->sql_query($sql);
ef5584
ef5584
					$db->sql_query('DELETE FROM ' . REPORTS_REASONS_TABLE . ' WHERE reason_id = ' . $reason_id);
ef5584
ef5584
					add_log('admin', 'LOG_REASON_REMOVED', $reason_row['reason_title']);
ef5584
					trigger_error($user->lang['REASON_REMOVED'] . adm_back_link($this->u_action));
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
ef5584
						'i'			=> $id,
ef5584
						'mode'		=> $mode,
ef5584
						'action'	=> $action,
ef5584
						'id'		=> $reason_id))
ef5584
					);
ef5584
				}
ef5584
ef5584
			break;
ef5584
ef5584
			case 'move_up':
ef5584
			case 'move_down':
ef5584
ef5584
				$order = request_var('order', 0);
ef5584
				$order_total = $order * 2 + (($action == 'move_up') ? -1 : 1);
ef5584
ef5584
				$sql = 'UPDATE ' . REPORTS_REASONS_TABLE . '
ef5584
					SET reason_order = ' . $order_total . ' - reason_order
ef5584
					WHERE reason_order IN (' . $order . ', ' . (($action == 'move_up') ? $order - 1 : $order + 1) . ')';
ef5584
				$db->sql_query($sql);
ef5584
ef5584
			break;
ef5584
		}
ef5584
ef5584
		// By default, check that order is valid and fix it if necessary
ef5584
		$sql = 'SELECT reason_id, reason_order
ef5584
			FROM ' . REPORTS_REASONS_TABLE . '
ef5584
			ORDER BY reason_order';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$order = 0;
ef5584
			do
ef5584
			{
ef5584
				++$order;
ef5584
				
ef5584
				if ($row['reason_order'] != $order)
ef5584
				{
ef5584
					$sql = 'UPDATE ' . REPORTS_REASONS_TABLE . "
ef5584
						SET reason_order = $order
ef5584
						WHERE reason_id = {$row['reason_id']}";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
			}
ef5584
			while ($row = $db->sql_fetchrow($result));
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'U_ACTION'			=> $this->u_action,
ef5584
			)
ef5584
		);
ef5584
ef5584
		// Reason count
ef5584
		$sql = 'SELECT reason_id, COUNT(reason_id) AS reason_count
ef5584
			FROM ' . REPORTS_TABLE . '
ef5584
			GROUP BY reason_id';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$reason_count = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$reason_count[$row['reason_id']] = $row['reason_count'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$sql = 'SELECT *
ef5584
			FROM ' . REPORTS_REASONS_TABLE . '
ef5584
			ORDER BY reason_order ASC';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$translated = false;
ef5584
			$other_reason = ($row['reason_title'] == 'other') ? true : false;
ef5584
ef5584
			// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
ef5584
			if (isset($user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
ef5584
			{
ef5584
				$row['reason_description'] = $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])];
ef5584
				$row['reason_title'] = $user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])];
ef5584
ef5584
				$translated = true;
ef5584
			}
ef5584
ef5584
			$template->assign_block_vars('reasons', array(
ef5584
				'REASON_TITLE'			=> $row['reason_title'],
ef5584
				'REASON_DESCRIPTION'	=> $row['reason_description'],
ef5584
				'REASON_COUNT'			=> (isset($reason_count[$row['reason_id']])) ? $reason_count[$row['reason_id']] : 0,
ef5584
ef5584
				'S_TRANSLATED'		=> $translated,
ef5584
				'S_OTHER_REASON'	=> $other_reason,
ef5584
ef5584
				'U_EDIT'		=> $this->u_action . '&action=edit&id=' . $row['reason_id'],
ef5584
				'U_DELETE'		=> (!$other_reason) ? $this->u_action . '&action=delete&id=' . $row['reason_id'] : '',
ef5584
				'U_MOVE_UP'		=> $this->u_action . '&action=move_up&order=' . $row['reason_order'],
ef5584
				'U_MOVE_DOWN'	=> $this->u_action . '&action=move_down&order=' . $row['reason_order'])
ef5584
			);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
}
ef5584
ef5584
?>