admin-usertitles.php 4.69 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for admin page for editing custom user titles


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
29 30


Scott committed
31
// Get current list of user titles and determine the state of this admin page
Scott committed
32

Scott committed
33 34 35
$oldpoints = qa_post_text('edit');
if (!isset($oldpoints))
	$oldpoints = qa_get('edit');
Scott committed
36

Scott committed
37
$pointstitle = qa_get_points_to_titles();
Scott committed
38 39


Scott committed
40
// Check admin privileges (do late to allow one DB query)
Scott committed
41

Scott committed
42 43
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
44 45


Scott committed
46
// Process saving an old or new user title
Scott committed
47

Scott committed
48
$securityexpired = false;
Scott committed
49

Scott committed
50 51
if (qa_clicked('docancel'))
	qa_redirect('admin/users');
Scott committed
52

Scott committed
53 54
elseif (qa_clicked('dosavetitle')) {
	require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
55

Scott committed
56 57
	if (!qa_check_form_security_code('admin/usertitles', qa_post_text('code')))
		$securityexpired = true;
Scott committed
58

Scott committed
59 60 61
	else {
		if (qa_post_text('dodelete')) {
			unset($pointstitle[$oldpoints]);
Scott committed
62

Scott committed
63 64 65
		} else {
			$intitle = qa_post_text('title');
			$inpoints = qa_post_text('points');
Scott committed
66

Scott committed
67
			$errors = array();
Scott committed
68

Scott committed
69
			// Verify the title and points are legitimate
Scott committed
70

Scott committed
71 72
			if (!strlen($intitle))
				$errors['title'] = qa_lang('main/field_required');
Scott committed
73

Scott committed
74 75 76 77
			if (!is_numeric($inpoints))
				$errors['points'] = qa_lang('main/field_required');
			else {
				$inpoints = (int)$inpoints;
Scott committed
78

Scott committed
79 80 81
				if (isset($pointstitle[$inpoints]) && ((!strlen(@$oldpoints)) || ($inpoints != $oldpoints)))
					$errors['points'] = qa_lang('admin/title_already_used');
			}
Scott committed
82

Scott committed
83
			// Perform appropriate action
Scott committed
84

Scott committed
85 86 87
			if (isset($pointstitle[$oldpoints])) { // changing existing user title
				$newpoints = isset($errors['points']) ? $oldpoints : $inpoints;
				$newtitle = isset($errors['title']) ? $pointstitle[$oldpoints] : $intitle;
Scott committed
88

Scott committed
89 90
				unset($pointstitle[$oldpoints]);
				$pointstitle[$newpoints] = $newtitle;
Scott committed
91

Scott committed
92 93 94
			} elseif (empty($errors)) // creating a new user title
				$pointstitle[$inpoints] = $intitle;
		}
Scott committed
95

Scott committed
96
		// Save the new option value
Scott committed
97

Scott committed
98
		krsort($pointstitle, SORT_NUMERIC);
Scott committed
99

Scott committed
100 101 102
		$option = '';
		foreach ($pointstitle as $points => $title)
			$option .= (strlen($option) ? ',' : '') . $points . ' ' . $title;
Scott committed
103

Scott committed
104
		qa_set_option('points_to_titles', $option);
Scott committed
105

Scott committed
106 107
		if (empty($errors))
			qa_redirect('admin/users');
Scott committed
108
	}
Scott committed
109
}
Scott committed
110 111


Scott committed
112
// Prepare content for theme
Scott committed
113

Scott committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
$qa_content = qa_content_prepare();

$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/users_title');
$qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();

$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',

	'style' => 'tall',

	'fields' => array(
		'title' => array(
			'tags' => 'name="title" id="title"',
			'label' => qa_lang_html('admin/user_title'),
			'value' => qa_html(isset($intitle) ? $intitle : @$pointstitle[$oldpoints]),
			'error' => qa_html(@$errors['title']),
Scott committed
130 131
		),

Scott committed
132 133 134 135 136 137
		'delete' => array(
			'tags' => 'name="dodelete" id="dodelete"',
			'label' => qa_lang_html('admin/delete_title'),
			'value' => 0,
			'type' => 'checkbox',
		),
Scott committed
138

Scott committed
139 140 141 142 143 144 145
		'points' => array(
			'id' => 'points_display',
			'tags' => 'name="points"',
			'label' => qa_lang_html('admin/points_required'),
			'type' => 'number',
			'value' => qa_html(isset($inpoints) ? $inpoints : @$oldpoints),
			'error' => qa_html(@$errors['points']),
Scott committed
146
		),
Scott committed
147
	),
Scott committed
148

Scott committed
149 150 151
	'buttons' => array(
		'save' => array(
			'label' => qa_lang_html(isset($pointstitle[$oldpoints]) ? 'main/save_button' : ('admin/add_title_button')),
Scott committed
152 153
		),

Scott committed
154 155 156 157 158
		'cancel' => array(
			'tags' => 'name="docancel"',
			'label' => qa_lang_html('main/cancel_button'),
		),
	),
Scott committed
159

Scott committed
160 161 162 163 164 165
	'hidden' => array(
		'dosavetitle' => '1', // for IE
		'edit' => @$oldpoints,
		'code' => qa_get_form_security_code('admin/usertitles'),
	),
);
Scott committed
166

Scott committed
167 168 169 170 171 172 173
if (isset($pointstitle[$oldpoints])) {
	qa_set_display_rules($qa_content, array(
		'points_display' => '!dodelete',
	));
} else {
	unset($qa_content['form']['fields']['delete']);
}
Scott committed
174

Scott committed
175
$qa_content['focusid'] = 'title';
Scott committed
176

Scott committed
177
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
178 179


Scott committed
180
return $qa_content;