includes/acp/acp_reasons.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_reasons.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,375 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @package acp
    1.24 +*/
    1.25 +class acp_reasons
    1.26 +{
    1.27 +	var $u_action;
    1.28 +
    1.29 +	function main($id, $mode)
    1.30 +	{
    1.31 +		global $db, $user, $auth, $template, $cache;
    1.32 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    1.33 +
    1.34 +		$user->add_lang(array('mcp', 'acp/posting'));
    1.35 +
    1.36 +		// Set up general vars
    1.37 +		$action = request_var('action', '');
    1.38 +		$submit = (isset($_POST['submit'])) ? true : false;
    1.39 +		$reason_id = request_var('id', 0);
    1.40 +
    1.41 +		$this->tpl_name = 'acp_reasons';
    1.42 +		$this->page_title = 'ACP_REASONS';
    1.43 +
    1.44 +		$form_name = 'acp_reason';
    1.45 +		add_form_key('acp_reason');
    1.46 +
    1.47 +		$error = array();
    1.48 +
    1.49 +		switch ($action)
    1.50 +		{
    1.51 +			case 'add':
    1.52 +			case 'edit':
    1.53 +
    1.54 +				$reason_row = array(
    1.55 +					'reason_title'			=> utf8_normalize_nfc(request_var('reason_title', '', true)),
    1.56 +					'reason_description'	=> utf8_normalize_nfc(request_var('reason_description', '', true)),
    1.57 +				);
    1.58 +
    1.59 +				if ($submit)
    1.60 +				{
    1.61 +					if (!check_form_key($form_name))
    1.62 +					{
    1.63 +						$error[] = $user->lang['FORM_INVALID'];
    1.64 +					}
    1.65 +					// Reason specified?
    1.66 +					if (!$reason_row['reason_title'] || !$reason_row['reason_description'])
    1.67 +					{
    1.68 +						$error[] = $user->lang['NO_REASON_INFO'];
    1.69 +					}
    1.70 +
    1.71 +					$check_double = ($action == 'add') ? true : false;
    1.72 +
    1.73 +					if ($action == 'edit')
    1.74 +					{
    1.75 +						$sql = 'SELECT reason_title
    1.76 +							FROM ' . REPORTS_REASONS_TABLE . "
    1.77 +							WHERE reason_id = $reason_id";
    1.78 +						$result = $db->sql_query($sql);
    1.79 +						$row = $db->sql_fetchrow($result);
    1.80 +						$db->sql_freeresult($result);
    1.81 +
    1.82 +						if (strtolower($row['reason_title']) == 'other' || strtolower($reason_row['reason_title']) == 'other')
    1.83 +						{
    1.84 +							$reason_row['reason_title'] = 'other';
    1.85 +						}
    1.86 +
    1.87 +						if ($row['reason_title'] != $reason_row['reason_title'])
    1.88 +						{
    1.89 +							$check_double = true;
    1.90 +						}
    1.91 +					}
    1.92 +
    1.93 +					// Check for same reason if adding it...
    1.94 +					if ($check_double)
    1.95 +					{
    1.96 +						$sql = 'SELECT reason_id
    1.97 +							FROM ' . REPORTS_REASONS_TABLE . "
    1.98 +							WHERE reason_title = '" . $db->sql_escape($reason_row['reason_title']) . "'";
    1.99 +						$result = $db->sql_query($sql);
   1.100 +						$row = $db->sql_fetchrow($result);
   1.101 +						$db->sql_freeresult($result);
   1.102 +
   1.103 +						if ($row || ($action == 'add' && strtolower($reason_row['reason_title']) == 'other'))
   1.104 +						{
   1.105 +							$error[] = $user->lang['REASON_ALREADY_EXIST'];
   1.106 +						}
   1.107 +					}
   1.108 +
   1.109 +					if (!sizeof($error))
   1.110 +					{
   1.111 +						// New reason?
   1.112 +						if ($action == 'add')
   1.113 +						{
   1.114 +							// Get new order...
   1.115 +							$sql = 'SELECT MAX(reason_order) as max_reason_order
   1.116 +								FROM ' . REPORTS_REASONS_TABLE;
   1.117 +							$result = $db->sql_query($sql);
   1.118 +							$max_order = (int) $db->sql_fetchfield('max_reason_order');
   1.119 +							$db->sql_freeresult($result);
   1.120 +							
   1.121 +							$sql_ary = array(
   1.122 +								'reason_title'			=> (string) $reason_row['reason_title'],
   1.123 +								'reason_description'	=> (string) $reason_row['reason_description'],
   1.124 +								'reason_order'			=> $max_order + 1
   1.125 +							);
   1.126 +
   1.127 +							$db->sql_query('INSERT INTO ' . REPORTS_REASONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
   1.128 +
   1.129 +							$log = 'ADDED';
   1.130 +						}
   1.131 +						else if ($reason_id)
   1.132 +						{
   1.133 +							$sql_ary = array(
   1.134 +								'reason_title'			=> (string) $reason_row['reason_title'],
   1.135 +								'reason_description'	=> (string) $reason_row['reason_description'],
   1.136 +							);
   1.137 +
   1.138 +							$db->sql_query('UPDATE ' . REPORTS_REASONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
   1.139 +								WHERE reason_id = ' . $reason_id);
   1.140 +
   1.141 +							$log = 'UPDATED';
   1.142 +						}
   1.143 +
   1.144 +						add_log('admin', 'LOG_REASON_' . $log, $reason_row['reason_title']);
   1.145 +						trigger_error($user->lang['REASON_' . $log] . adm_back_link($this->u_action));
   1.146 +					}
   1.147 +				}
   1.148 +				else if ($reason_id)
   1.149 +				{
   1.150 +					$sql = 'SELECT *
   1.151 +						FROM ' . REPORTS_REASONS_TABLE . '
   1.152 +						WHERE reason_id = ' . $reason_id;
   1.153 +					$result = $db->sql_query($sql);
   1.154 +					$reason_row = $db->sql_fetchrow($result);
   1.155 +					$db->sql_freeresult($result);
   1.156 +
   1.157 +					if (!$reason_row)
   1.158 +					{
   1.159 +						trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.160 +					}
   1.161 +				}
   1.162 +
   1.163 +				$l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
   1.164 +
   1.165 +				$translated = false;
   1.166 +
   1.167 +				// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
   1.168 +				if (isset($user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])]))
   1.169 +				{
   1.170 +					$translated = true;
   1.171 +				}
   1.172 +
   1.173 +				$template->assign_vars(array(
   1.174 +					'L_TITLE'		=> $user->lang['REASON_' . $l_title],
   1.175 +					'U_ACTION'		=> $this->u_action . "&amp;id=$reason_id&amp;action=$action",
   1.176 +					'U_BACK'		=> $this->u_action,
   1.177 +					'ERROR_MSG'		=> (sizeof($error)) ? implode('<br />', $error) : '',
   1.178 +					
   1.179 +					'REASON_TITLE'			=> $reason_row['reason_title'],
   1.180 +					'REASON_DESCRIPTION'	=> $reason_row['reason_description'],
   1.181 +
   1.182 +					'TRANSLATED_TITLE'		=> ($translated) ? $user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])] : '',
   1.183 +					'TRANSLATED_DESCRIPTION'=> ($translated) ? $user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])] : '',
   1.184 +
   1.185 +					'S_AVAILABLE_TITLES'	=> implode(', ', array_map('htmlspecialchars', array_keys($user->lang['report_reasons']['TITLE']))),
   1.186 +					'S_EDIT_REASON'			=> true,
   1.187 +					'S_TRANSLATED'			=> $translated,
   1.188 +					'S_ERROR'				=> (sizeof($error)) ? true : false,
   1.189 +					)
   1.190 +				);
   1.191 +
   1.192 +				return;
   1.193 +			break;
   1.194 +
   1.195 +			case 'delete':
   1.196 +
   1.197 +				$sql = 'SELECT *
   1.198 +					FROM ' . REPORTS_REASONS_TABLE . '
   1.199 +					WHERE reason_id = ' . $reason_id;
   1.200 +				$result = $db->sql_query($sql);
   1.201 +				$reason_row = $db->sql_fetchrow($result);
   1.202 +				$db->sql_freeresult($result);
   1.203 +
   1.204 +				if (!$reason_row)
   1.205 +				{
   1.206 +					trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.207 +				}
   1.208 +
   1.209 +				if (strtolower($reason_row['reason_title']) == 'other')
   1.210 +				{
   1.211 +					trigger_error($user->lang['NO_REMOVE_DEFAULT_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.212 +				}
   1.213 +
   1.214 +				// Let the deletion be confirmed...
   1.215 +				if (confirm_box(true))
   1.216 +				{
   1.217 +					$sql = 'SELECT reason_id
   1.218 +						FROM ' . REPORTS_REASONS_TABLE . "
   1.219 +						WHERE LOWER(reason_title) = 'other'";
   1.220 +					$result = $db->sql_query($sql);
   1.221 +					$other_reason_id = (int) $db->sql_fetchfield('reason_id');
   1.222 +					$db->sql_freeresult($result);
   1.223 +
   1.224 +					switch ($db->sql_layer)
   1.225 +					{
   1.226 +						// The ugly one!
   1.227 +						case 'mysqli':
   1.228 +						case 'mysql4':
   1.229 +						case 'mysql':
   1.230 +							// Change the reports using this reason to 'other'
   1.231 +							$sql = 'UPDATE ' . REPORTS_TABLE . '
   1.232 +								SET reason_id = ' . $other_reason_id . ", report_text = CONCAT('" . $db->sql_escape($reason_row['reason_description']) . "\n\n', report_text)
   1.233 +								WHERE reason_id = $reason_id";
   1.234 +						break;
   1.235 +
   1.236 +						// Standard? What's that?
   1.237 +						case 'mssql':
   1.238 +						case 'mssql_odbc':
   1.239 +						case 'mssqlnative':
   1.240 +							// Change the reports using this reason to 'other'
   1.241 +							$sql = "DECLARE @ptrval binary(16)
   1.242 +
   1.243 +									SELECT @ptrval = TEXTPTR(report_text)
   1.244 +										FROM " . REPORTS_TABLE . "
   1.245 +									WHERE reason_id = " . $reason_id . "
   1.246 +
   1.247 +									UPDATETEXT " . REPORTS_TABLE . ".report_text @ptrval 0 0 '" . $db->sql_escape($reason_row['reason_description']) . "\n\n'
   1.248 +
   1.249 +									UPDATE " . REPORTS_TABLE . '
   1.250 +										SET reason_id = ' . $other_reason_id . "
   1.251 +									WHERE reason_id = $reason_id";
   1.252 +						break;
   1.253 +
   1.254 +						// Teh standard
   1.255 +						case 'postgres':
   1.256 +						case 'oracle':
   1.257 +						case 'firebird':
   1.258 +						case 'sqlite':
   1.259 +							// Change the reports using this reason to 'other'
   1.260 +							$sql = 'UPDATE ' . REPORTS_TABLE . '
   1.261 +								SET reason_id = ' . $other_reason_id . ", report_text = '" . $db->sql_escape($reason_row['reason_description']) . "\n\n' || report_text
   1.262 +								WHERE reason_id = $reason_id";
   1.263 +						break;
   1.264 +					}
   1.265 +					$db->sql_query($sql);
   1.266 +
   1.267 +					$db->sql_query('DELETE FROM ' . REPORTS_REASONS_TABLE . ' WHERE reason_id = ' . $reason_id);
   1.268 +
   1.269 +					add_log('admin', 'LOG_REASON_REMOVED', $reason_row['reason_title']);
   1.270 +					trigger_error($user->lang['REASON_REMOVED'] . adm_back_link($this->u_action));
   1.271 +				}
   1.272 +				else
   1.273 +				{
   1.274 +					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   1.275 +						'i'			=> $id,
   1.276 +						'mode'		=> $mode,
   1.277 +						'action'	=> $action,
   1.278 +						'id'		=> $reason_id))
   1.279 +					);
   1.280 +				}
   1.281 +
   1.282 +			break;
   1.283 +
   1.284 +			case 'move_up':
   1.285 +			case 'move_down':
   1.286 +
   1.287 +				$order = request_var('order', 0);
   1.288 +				$order_total = $order * 2 + (($action == 'move_up') ? -1 : 1);
   1.289 +
   1.290 +				$sql = 'UPDATE ' . REPORTS_REASONS_TABLE . '
   1.291 +					SET reason_order = ' . $order_total . ' - reason_order
   1.292 +					WHERE reason_order IN (' . $order . ', ' . (($action == 'move_up') ? $order - 1 : $order + 1) . ')';
   1.293 +				$db->sql_query($sql);
   1.294 +
   1.295 +			break;
   1.296 +		}
   1.297 +
   1.298 +		// By default, check that order is valid and fix it if necessary
   1.299 +		$sql = 'SELECT reason_id, reason_order
   1.300 +			FROM ' . REPORTS_REASONS_TABLE . '
   1.301 +			ORDER BY reason_order';
   1.302 +		$result = $db->sql_query($sql);
   1.303 +
   1.304 +		if ($row = $db->sql_fetchrow($result))
   1.305 +		{
   1.306 +			$order = 0;
   1.307 +			do
   1.308 +			{
   1.309 +				++$order;
   1.310 +				
   1.311 +				if ($row['reason_order'] != $order)
   1.312 +				{
   1.313 +					$sql = 'UPDATE ' . REPORTS_REASONS_TABLE . "
   1.314 +						SET reason_order = $order
   1.315 +						WHERE reason_id = {$row['reason_id']}";
   1.316 +					$db->sql_query($sql);
   1.317 +				}
   1.318 +			}
   1.319 +			while ($row = $db->sql_fetchrow($result));
   1.320 +		}
   1.321 +		$db->sql_freeresult($result);
   1.322 +
   1.323 +		$template->assign_vars(array(
   1.324 +			'U_ACTION'			=> $this->u_action,
   1.325 +			)
   1.326 +		);
   1.327 +
   1.328 +		// Reason count
   1.329 +		$sql = 'SELECT reason_id, COUNT(reason_id) AS reason_count
   1.330 +			FROM ' . REPORTS_TABLE . '
   1.331 +			GROUP BY reason_id';
   1.332 +		$result = $db->sql_query($sql);
   1.333 +
   1.334 +		$reason_count = array();
   1.335 +		while ($row = $db->sql_fetchrow($result))
   1.336 +		{
   1.337 +			$reason_count[$row['reason_id']] = $row['reason_count'];
   1.338 +		}
   1.339 +		$db->sql_freeresult($result);
   1.340 +
   1.341 +		$sql = 'SELECT *
   1.342 +			FROM ' . REPORTS_REASONS_TABLE . '
   1.343 +			ORDER BY reason_order ASC';
   1.344 +		$result = $db->sql_query($sql);
   1.345 +
   1.346 +		while ($row = $db->sql_fetchrow($result))
   1.347 +		{
   1.348 +			$translated = false;
   1.349 +			$other_reason = ($row['reason_title'] == 'other') ? true : false;
   1.350 +
   1.351 +			// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
   1.352 +			if (isset($user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
   1.353 +			{
   1.354 +				$row['reason_description'] = $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])];
   1.355 +				$row['reason_title'] = $user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])];
   1.356 +
   1.357 +				$translated = true;
   1.358 +			}
   1.359 +
   1.360 +			$template->assign_block_vars('reasons', array(
   1.361 +				'REASON_TITLE'			=> $row['reason_title'],
   1.362 +				'REASON_DESCRIPTION'	=> $row['reason_description'],
   1.363 +				'REASON_COUNT'			=> (isset($reason_count[$row['reason_id']])) ? $reason_count[$row['reason_id']] : 0,
   1.364 +
   1.365 +				'S_TRANSLATED'		=> $translated,
   1.366 +				'S_OTHER_REASON'	=> $other_reason,
   1.367 +
   1.368 +				'U_EDIT'		=> $this->u_action . '&amp;action=edit&amp;id=' . $row['reason_id'],
   1.369 +				'U_DELETE'		=> (!$other_reason) ? $this->u_action . '&amp;action=delete&amp;id=' . $row['reason_id'] : '',
   1.370 +				'U_MOVE_UP'		=> $this->u_action . '&amp;action=move_up&amp;order=' . $row['reason_order'],
   1.371 +				'U_MOVE_DOWN'	=> $this->u_action . '&amp;action=move_down&amp;order=' . $row['reason_order'])
   1.372 +			);
   1.373 +		}
   1.374 +		$db->sql_freeresult($result);
   1.375 +	}
   1.376 +}
   1.377 +
   1.378 +?>
   1.379 \ No newline at end of file