qa-page-admin-userfields.php 7.98 KB
Newer Older
Gideon Greenspan committed
1
<?php
Scott Vivian committed
2

Gideon Greenspan committed
3 4 5 6 7
/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-page-admin-userfields.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-app-admin.php';
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';

Scott Vivian committed
35

Gideon Greenspan committed
36 37 38 39 40
//	Get current list of user fields and determine the state of this admin page

	$fieldid=qa_post_text('edit');
	if (!isset($fieldid))
		$fieldid=qa_get('edit');
Scott Vivian committed
41

Gideon Greenspan committed
42 43 44 45 46 47
	$userfields=qa_db_select_with_pending(qa_db_userfields_selectspec());

	$editfield=null;
	foreach ($userfields as $userfield)
		if ($userfield['fieldid']==$fieldid)
			$editfield=$userfield;
Scott Vivian committed
48

Gideon Greenspan committed
49 50 51 52 53

//	Check admin privileges (do late to allow one DB query)

	if (!qa_admin_check_privileges($qa_content))
		return $qa_content;
Scott Vivian committed
54 55


Gideon Greenspan committed
56
//	Process saving an old or new user field
Scott Vivian committed
57

Gideon Greenspan committed
58
	$securityexpired=false;
Scott Vivian committed
59

Gideon Greenspan committed
60 61 62 63 64 65
	if (qa_clicked('docancel'))
		qa_redirect('admin/users');

	elseif (qa_clicked('dosavefield')) {
		require_once QA_INCLUDE_DIR.'qa-db-admin.php';
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
66

Gideon Greenspan committed
67 68
		if (!qa_check_form_security_code('admin/userfields', qa_post_text('code')))
			$securityexpired=true;
Scott Vivian committed
69

Gideon Greenspan committed
70 71 72 73
		else {
			if (qa_post_text('dodelete')) {
				qa_db_userfield_delete($editfield['fieldid']);
				qa_redirect('admin/users');
Scott Vivian committed
74

Gideon Greenspan committed
75 76 77 78 79 80 81
			} 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 Vivian committed
82

Gideon Greenspan committed
83
				$errors=array();
Scott Vivian committed
84

Gideon Greenspan committed
85
			//	Verify the name is legitimate
Scott Vivian committed
86

Gideon Greenspan committed
87 88
				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 Vivian committed
89

Gideon Greenspan committed
90
			//	Perform appropriate database action
Scott Vivian committed
91

Gideon Greenspan committed
92 93 94
				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 Vivian committed
95

Gideon Greenspan committed
96
					if (empty($errors))
Gideon Greenspan committed
97
						qa_redirect('admin/users');
Scott Vivian committed
98

Gideon Greenspan committed
99 100 101 102 103 104
					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 Vivian committed
105

Gideon Greenspan committed
106
				} elseif (empty($errors)) { // creating a new user field
Scott Vivian committed
107

Gideon Greenspan committed
108 109 110 111
					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 Vivian committed
112 113

						foreach ($userfields as $userfield)
Gideon Greenspan committed
114 115
							if (qa_strtolower(trim($newtag)) == qa_strtolower(trim($userfield['title'])))
								$uniquetag=false;
Scott Vivian committed
116

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

Gideon Greenspan committed
124
					qa_fatal_error('Could not create a unique database tag');
Gideon Greenspan committed
125 126 127 128
				}
			}
		}
	}
Scott Vivian committed
129 130


Gideon Greenspan committed
131
//	Prepare content for theme
Scott Vivian committed
132

Gideon Greenspan committed
133 134 135
	$qa_content=qa_content_prepare();

	$qa_content['title']=qa_lang_html('admin/admin_title').' - '.qa_lang_html('admin/users_title');
Gideon Greenspan committed
136
	$qa_content['error']=$securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Gideon Greenspan committed
137 138 139 140

	$positionoptions=array();
	$previous=null;
	$passedself=false;
Scott Vivian committed
141

Gideon Greenspan committed
142 143 144 145 146
	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 Vivian committed
147

Gideon Greenspan committed
148
		$positionoptions[$userfield['position']]=$positionhtml;
Scott Vivian committed
149

Gideon Greenspan committed
150 151 152 153 154
		if ($userfield['fieldid']==@$editfield['fieldid'])
			$passedself=true;

		$previous=$userfield;
	}
Scott Vivian committed
155

Gideon Greenspan committed
156 157 158 159 160 161
	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;
	}
Scott Vivian committed
162

Gideon Greenspan committed
163 164 165 166 167
	$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 Vivian committed
168

Gideon Greenspan committed
169 170 171
	$permitoptions=qa_admin_permit_options(QA_PERMIT_ALL, QA_PERMIT_ADMINS, false, false);
	$permitvalue=@$permitoptions[isset($inpermit) ? $inpermit : $editfield['permit']];

Gideon Greenspan committed
172
	$qa_content['form']=array(
Gideon Greenspan committed
173
		'tags' => 'method="post" action="'.qa_path_html(qa_request()).'"',
Scott Vivian committed
174

Gideon Greenspan committed
175
		'style' => 'tall',
Scott Vivian committed
176

Gideon Greenspan committed
177 178
		'fields' => array(
			'name' => array(
Gideon Greenspan committed
179
				'tags' => 'name="name" id="name"',
Gideon Greenspan committed
180 181 182 183
				'label' => qa_lang_html('admin/field_name'),
				'value' => qa_html(isset($inname) ? $inname : qa_user_userfield_label($editfield)),
				'error' => qa_html(@$errors['name']),
			),
Scott Vivian committed
184

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

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

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

Gideon Greenspan committed
210 211
			'position' => array(
				'id' => 'position_display',
Gideon Greenspan committed
212
				'tags' => 'name="position"',
Gideon Greenspan committed
213 214 215 216
				'label' => qa_lang_html('admin/position'),
				'type' => 'select',
				'options' => $positionoptions,
				'value' => $positionvalue,
Scott Vivian committed
217
			),
Gideon Greenspan committed
218 219 220

			'onregister' => array(
				'id' => 'register_display',
Gideon Greenspan committed
221
				'tags' => 'name="onregister"',
Gideon Greenspan committed
222 223 224
				'label' => qa_lang_html('admin/show_on_register_form'),
				'type' => 'checkbox',
				'value' => isset($inonregister) ? $inonregister : (@$editfield['flags']&QA_FIELD_FLAGS_ON_REGISTER),
Gideon Greenspan committed
225 226 227 228 229 230 231
			),
		),

		'buttons' => array(
			'save' => array(
				'label' => qa_lang_html(isset($editfield['fieldid']) ? 'main/save_button' : ('admin/add_field_button')),
			),
Scott Vivian committed
232

Gideon Greenspan committed
233
			'cancel' => array(
Gideon Greenspan committed
234
				'tags' => 'name="docancel"',
Gideon Greenspan committed
235 236 237
				'label' => qa_lang_html('main/cancel_button'),
			),
		),
Scott Vivian committed
238

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

Gideon Greenspan committed
246 247
	if (isset($editfield['fieldid']))
		qa_set_display_rules($qa_content, array(
Gideon Greenspan committed
248
			'type_display' => '!dodelete',
Gideon Greenspan committed
249
			'position_display' => '!dodelete',
Gideon Greenspan committed
250
			'register_display' => '!dodelete',
Gideon Greenspan committed
251
			'permit_display' => '!dodelete',
Gideon Greenspan committed
252 253 254
		));
	else
		unset($qa_content['form']['fields']['delete']);
Scott Vivian committed
255

Gideon Greenspan committed
256 257 258 259
	$qa_content['focusid']='name';

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

Scott Vivian committed
260

Gideon Greenspan committed
261 262 263 264 265 266
	return $qa_content;


/*
	Omit PHP closing tag to help avoid accidental output
*/