qa-page-admin-usertitles.php 5.05 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-usertitles.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
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 titles and determine the state of this admin page

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

Gideon Greenspan committed
42 43 44 45 46 47 48
	$pointstitle=qa_get_points_to_titles();


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

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


Gideon Greenspan committed
51 52
//	Process saving an old or new user title

Gideon Greenspan committed
53
	$securityexpired=false;
Scott Vivian committed
54

Gideon Greenspan committed
55 56 57 58 59
	if (qa_clicked('docancel'))
		qa_redirect('admin/users');

	elseif (qa_clicked('dosavetitle')) {
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
60

Gideon Greenspan committed
61 62
		if (!qa_check_form_security_code('admin/usertitles', qa_post_text('code')))
			$securityexpired=true;
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66
		else {
			if (qa_post_text('dodelete')) {
				unset($pointstitle[$oldpoints]);
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70
			} else {
				$intitle=qa_post_text('title');
				$inpoints=qa_post_text('points');
Scott Vivian committed
71

Gideon Greenspan committed
72
				$errors=array();
Scott Vivian committed
73

Gideon Greenspan committed
74
			//	Verify the title and points are legitimate
Scott Vivian committed
75

Gideon Greenspan committed
76 77
				if (!strlen($intitle))
					$errors['title']=qa_lang('main/field_required');
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81 82
				if (!is_numeric($inpoints))
					$errors['points']=qa_lang('main/field_required');
				else {
					$inpoints=(int)$inpoints;
Scott Vivian committed
83

Gideon Greenspan committed
84 85 86
					if (isset($pointstitle[$inpoints]) && ((!strlen(@$oldpoints)) || ($inpoints!=$oldpoints)) )
						$errors['points']=qa_lang('admin/title_already_used');
				}
Scott Vivian committed
87

Gideon Greenspan committed
88
			//	Perform appropriate action
Scott Vivian committed
89

Gideon Greenspan committed
90 91 92
				if (isset($pointstitle[$oldpoints])) { // changing existing user title
					$newpoints=isset($errors['points']) ? $oldpoints : $inpoints;
					$newtitle=isset($errors['title']) ? $pointstitle[$oldpoints] : $intitle;
Scott Vivian committed
93

Gideon Greenspan committed
94 95
					unset($pointstitle[$oldpoints]);
					$pointstitle[$newpoints]=$newtitle;
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99
				} elseif (empty($errors)) // creating a new user title
					$pointstitle[$inpoints]=$intitle;
			}
Scott Vivian committed
100

Gideon Greenspan committed
101
		//	Save the new option value
Scott Vivian committed
102

Gideon Greenspan committed
103
			krsort($pointstitle, SORT_NUMERIC);
Scott Vivian committed
104

Gideon Greenspan committed
105 106 107
			$option='';
			foreach ($pointstitle as $points => $title)
				$option.=(strlen($option) ? ',' : '').$points.' '.$title;
Scott Vivian committed
108 109 110

			qa_set_option('points_to_titles', $option);

Gideon Greenspan committed
111 112 113
			if (empty($errors))
				qa_redirect('admin/users');
		}
Gideon Greenspan committed
114
	}
Scott Vivian committed
115 116


Gideon Greenspan committed
117
//	Prepare content for theme
Scott Vivian committed
118

Gideon Greenspan committed
119 120
	$qa_content=qa_content_prepare();

Scott Vivian committed
121
	$qa_content['title']=qa_lang_html('admin/admin_title').' - '.qa_lang_html('admin/users_title');
Gideon Greenspan committed
122
	$qa_content['error']=$securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Gideon Greenspan committed
123 124

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

Gideon Greenspan committed
127
		'style' => 'tall',
Scott Vivian committed
128

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

Gideon Greenspan committed
137
			'delete' => array(
Gideon Greenspan committed
138
				'tags' => 'name="dodelete" id="dodelete"',
Gideon Greenspan committed
139 140 141 142
				'label' => qa_lang_html('admin/delete_title'),
				'value' => 0,
				'type' => 'checkbox',
			),
Scott Vivian committed
143

Gideon Greenspan committed
144 145
			'points' => array(
				'id' => 'points_display',
Gideon Greenspan committed
146
				'tags' => 'name="points"',
Gideon Greenspan committed
147 148 149 150 151 152 153 154 155 156 157
				'label' => qa_lang_html('admin/points_required'),
				'type' => 'number',
				'value' => qa_html(isset($inpoints) ? $inpoints : @$oldpoints),
				'error' => qa_html(@$errors['points']),
			),
		),

		'buttons' => array(
			'save' => array(
				'label' => qa_lang_html(isset($pointstitle[$oldpoints]) ? 'main/save_button' : ('admin/add_title_button')),
			),
Scott Vivian committed
158

Gideon Greenspan committed
159
			'cancel' => array(
Gideon Greenspan committed
160
				'tags' => 'name="docancel"',
Gideon Greenspan committed
161 162 163
				'label' => qa_lang_html('main/cancel_button'),
			),
		),
Scott Vivian committed
164

Gideon Greenspan committed
165 166 167
		'hidden' => array(
			'dosavetitle' => '1', // for IE
			'edit' => @$oldpoints,
Gideon Greenspan committed
168
			'code' => qa_get_form_security_code('admin/usertitles'),
Gideon Greenspan committed
169 170
		),
	);
Scott Vivian committed
171

Gideon Greenspan committed
172 173 174 175 176 177 178 179 180 181 182
	if (isset($pointstitle[$oldpoints]))
		qa_set_display_rules($qa_content, array(
			'points_display' => '!dodelete',
		));
	else
		unset($qa_content['form']['fields']['delete']);

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

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

Scott Vivian committed
183

Gideon Greenspan committed
184 185 186 187 188 189
	return $qa_content;


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