Blame Extras/phpBB/3.0.4/includes/acp/acp_reasons.php

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