admin-userfields.php 7.59 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-page-admin-userfields.php
	Description: Controller for admin page for editing custom user fields


	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
23 24 25 26
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
Scott committed
27

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


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

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

Scott committed
38
$userfields = qa_db_select_with_pending(qa_db_userfields_selectspec());
Scott committed
39

Scott committed
40 41 42 43 44
$editfield = null;
foreach ($userfields as $userfield) {
	if ($userfield['fieldid'] == $fieldid)
		$editfield = $userfield;
}
Scott committed
45 46


Scott committed
47
// Check admin privileges (do late to allow one DB query)
Scott committed
48

Scott committed
49 50
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
51 52


Scott committed
53
// Process saving an old or new user field
Scott committed
54

Scott committed
55
$securityexpired = false;
Scott committed
56

Scott committed
57 58
if (qa_clicked('docancel'))
	qa_redirect('admin/users');
Scott committed
59

Scott committed
60 61 62
elseif (qa_clicked('dosavefield')) {
	require_once QA_INCLUDE_DIR . 'db/admin.php';
	require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
63

Scott committed
64 65
	if (!qa_check_form_security_code('admin/userfields', qa_post_text('code')))
		$securityexpired = true;
Scott committed
66

Scott committed
67 68 69 70
	else {
		if (qa_post_text('dodelete')) {
			qa_db_userfield_delete($editfield['fieldid']);
			qa_redirect('admin/users');
Scott committed
71

Scott committed
72 73 74 75 76 77 78
		} else {
			$inname = qa_post_text('name');
			$intype = qa_post_text('type');
			$inonregister = (int)qa_post_text('onregister');
			$inflags = $intype | ($inonregister ? QA_FIELD_FLAGS_ON_REGISTER : 0);
			$inposition = qa_post_text('position');
			$inpermit = (int)qa_post_text('permit');
Scott committed
79

Scott committed
80
			$errors = array();
Scott committed
81

Scott committed
82
			// Verify the name is legitimate
Scott committed
83

Scott committed
84 85
			if (qa_strlen($inname) > QA_DB_MAX_PROFILE_TITLE_LENGTH)
				$errors['name'] = qa_lang_sub('main/max_length_x', QA_DB_MAX_PROFILE_TITLE_LENGTH);
Scott committed
86

Scott committed
87
			// Perform appropriate database action
Scott committed
88

Scott committed
89 90 91
			if (isset($editfield['fieldid'])) { // changing existing user field
				qa_db_userfield_set_fields($editfield['fieldid'], isset($errors['name']) ? $editfield['content'] : $inname, $inflags, $inpermit);
				qa_db_userfield_move($editfield['fieldid'], $inposition);
Scott committed
92

Scott committed
93 94
				if (empty($errors))
					qa_redirect('admin/users');
Scott committed
95

Scott committed
96 97 98 99 100 101
				else {
					$userfields = qa_db_select_with_pending(qa_db_userfields_selectspec()); // reload after changes
					foreach ($userfields as $userfield)
						if ($userfield['fieldid'] == $editfield['fieldid'])
							$editfield = $userfield;
				}
Scott committed
102

Scott committed
103
			} elseif (empty($errors)) { // creating a new user field
Scott committed
104

Scott committed
105 106 107 108
				for ($attempt = 0; $attempt < 1000; $attempt++) {
					$suffix = $attempt ? ('-' . (1 + $attempt)) : '';
					$newtag = qa_substr(implode('-', qa_string_to_words($inname)), 0, QA_DB_MAX_PROFILE_TITLE_LENGTH - strlen($suffix)) . $suffix;
					$uniquetag = true;
Scott committed
109

Scott committed
110 111 112
					foreach ($userfields as $userfield) {
						if (qa_strtolower(trim($newtag)) == qa_strtolower(trim($userfield['title'])))
							$uniquetag = false;
Scott committed
113 114
					}

Scott committed
115 116 117 118 119
					if ($uniquetag) {
						$fieldid = qa_db_userfield_create($newtag, $inname, $inflags, $inpermit);
						qa_db_userfield_move($fieldid, $inposition);
						qa_redirect('admin/users');
					}
Scott committed
120
				}
Scott committed
121 122

				qa_fatal_error('Could not create a unique database tag');
Scott committed
123 124 125
			}
		}
	}
Scott committed
126
}
Scott committed
127 128


Scott committed
129
// Prepare content for theme
Scott committed
130

Scott committed
131
$qa_content = qa_content_prepare();
Scott committed
132

Scott committed
133 134
$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();
Scott committed
135

Scott committed
136 137 138
$positionoptions = array();
$previous = null;
$passedself = false;
Scott committed
139

Scott committed
140 141 142 143 144
foreach ($userfields as $userfield) {
	if (isset($previous))
		$positionhtml = qa_lang_html_sub('admin/after_x', qa_html(qa_user_userfield_label($passedself ? $userfield : $previous)));
	else
		$positionhtml = qa_lang_html('admin/first');
Scott committed
145

Scott committed
146
	$positionoptions[$userfield['position']] = $positionhtml;
Scott committed
147

Scott committed
148 149
	if ($userfield['fieldid'] == @$editfield['fieldid'])
		$passedself = true;
Scott committed
150

Scott committed
151 152
	$previous = $userfield;
}
Scott committed
153

Scott committed
154 155 156 157 158 159 160 161 162 163 164 165
if (isset($editfield['position']))
	$positionvalue = $positionoptions[$editfield['position']];
else {
	$positionvalue = isset($previous) ? qa_lang_html_sub('admin/after_x', qa_html(qa_user_userfield_label($previous))) : qa_lang_html('admin/first');
	$positionoptions[1 + @max(array_keys($positionoptions))] = $positionvalue;
}

$typeoptions = array(
	0 => qa_lang_html('admin/field_single_line'),
	QA_FIELD_FLAGS_MULTI_LINE => qa_lang_html('admin/field_multi_line'),
	QA_FIELD_FLAGS_LINK_URL => qa_lang_html('admin/field_link_url'),
);
Scott committed
166

Scott committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
$permitoptions = qa_admin_permit_options(QA_PERMIT_ALL, QA_PERMIT_ADMINS, false, false);
$permitvalue = @$permitoptions[isset($inpermit) ? $inpermit : $editfield['permit']];

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

	'style' => 'tall',

	'fields' => array(
		'name' => array(
			'tags' => 'name="name" id="name"',
			'label' => qa_lang_html('admin/field_name'),
			'value' => qa_html(isset($inname) ? $inname : qa_user_userfield_label($editfield)),
			'error' => qa_html(@$errors['name']),
Scott committed
181 182
		),

Scott committed
183 184 185 186 187 188
		'delete' => array(
			'tags' => 'name="dodelete" id="dodelete"',
			'label' => qa_lang_html('admin/delete_field'),
			'value' => 0,
			'type' => 'checkbox',
		),
Scott committed
189

Scott committed
190 191 192 193 194 195 196
		'type' => array(
			'id' => 'type_display',
			'tags' => 'name="type"',
			'label' => qa_lang_html('admin/field_type'),
			'type' => 'select',
			'options' => $typeoptions,
			'value' => @$typeoptions[isset($intype) ? $intype : (@$editfield['flags'] & (QA_FIELD_FLAGS_MULTI_LINE | QA_FIELD_FLAGS_LINK_URL))],
Scott committed
197 198
		),

Scott committed
199 200 201 202 203 204 205
		'permit' => array(
			'id' => 'permit_display',
			'tags' => 'name="permit"',
			'label' => qa_lang_html('admin/permit_to_view'),
			'type' => 'select',
			'options' => $permitoptions,
			'value' => $permitvalue,
Scott committed
206 207
		),

Scott committed
208 209 210 211 212 213 214 215
		'position' => array(
			'id' => 'position_display',
			'tags' => 'name="position"',
			'label' => qa_lang_html('admin/position'),
			'type' => 'select',
			'options' => $positionoptions,
			'value' => $positionvalue,
		),
Scott committed
216

Scott committed
217 218 219 220 221 222 223 224
		'onregister' => array(
			'id' => 'register_display',
			'tags' => 'name="onregister"',
			'label' => qa_lang_html('admin/show_on_register_form'),
			'type' => 'checkbox',
			'value' => isset($inonregister) ? $inonregister : (@$editfield['flags'] & QA_FIELD_FLAGS_ON_REGISTER),
		),
	),
Scott committed
225

Scott committed
226 227 228 229
	'buttons' => array(
		'save' => array(
			'label' => qa_lang_html(isset($editfield['fieldid']) ? 'main/save_button' : ('admin/add_field_button')),
		),
Scott committed
230

Scott committed
231 232 233 234 235
		'cancel' => array(
			'tags' => 'name="docancel"',
			'label' => qa_lang_html('main/cancel_button'),
		),
	),
Scott committed
236

Scott committed
237 238 239 240 241 242
	'hidden' => array(
		'dosavefield' => '1', // for IE
		'edit' => @$editfield['fieldid'],
		'code' => qa_get_form_security_code('admin/userfields'),
	),
);
Scott committed
243

Scott committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
if (isset($editfield['fieldid'])) {
	qa_set_display_rules($qa_content, array(
		'type_display' => '!dodelete',
		'position_display' => '!dodelete',
		'register_display' => '!dodelete',
		'permit_display' => '!dodelete',
	));
} else {
	unset($qa_content['form']['fields']['delete']);
}

$qa_content['focusid'] = 'name';

$qa_content['navigation']['sub'] = qa_admin_sub_navigation();


return $qa_content;