admin-points.php 5.03 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-points.php
	Description: Controller for admin page for settings about user points


	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 30 31 32
require_once QA_INCLUDE_DIR . 'db/recalc.php';
require_once QA_INCLUDE_DIR . 'db/points.php';
require_once QA_INCLUDE_DIR . 'app/options.php';
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
33 34


Scott committed
35
// Check admin privileges
Scott committed
36

Scott committed
37 38
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
39 40


Scott committed
41
// Process user actions
Scott committed
42

Scott committed
43 44 45
$securityexpired = false;
$recalculate = false;
$optionnames = qa_db_points_option_names();
Scott committed
46

Scott committed
47 48
if (qa_clicked('doshowdefaults')) {
	$options = array();
Scott committed
49

Scott committed
50 51
	foreach ($optionnames as $optionname)
		$options[$optionname] = qa_default_option($optionname);
Scott committed
52

Scott committed
53 54 55
} else {
	if (qa_clicked('docancel'))
		;
Scott committed
56

Scott committed
57 58 59
	elseif (qa_clicked('dosaverecalc')) {
		if (!qa_check_form_security_code('admin/points', qa_post_text('code')))
			$securityexpired = true;
Scott committed
60

Scott committed
61 62 63
		else {
			foreach ($optionnames as $optionname)
				qa_set_option($optionname, (int)qa_post_text('option_' . $optionname));
Scott committed
64

Scott committed
65 66 67 68
			if (!qa_post_text('has_js'))
				qa_redirect('admin/recalc', array('dorecalcpoints' => 1));
			else
				$recalculate = true;
Scott committed
69 70 71
		}
	}

Scott committed
72 73 74
	$options = qa_get_options($optionnames);
}

Scott committed
75

Scott committed
76
// Prepare content for theme
Scott committed
77

Scott committed
78
$qa_content = qa_content_prepare();
Scott committed
79

Scott committed
80 81
$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/points_title');
$qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Scott committed
82

Scott committed
83 84
$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '" name="points_form" onsubmit="document.forms.points_form.has_js.value=1; return true;"',
Scott committed
85

Scott committed
86
	'style' => 'wide',
Scott committed
87

Scott committed
88 89 90 91
	'buttons' => array(
		'saverecalc' => array(
			'tags' => 'id="dosaverecalc"',
			'label' => qa_lang_html('admin/save_recalc_button'),
Scott committed
92
		),
Scott committed
93
	),
Scott committed
94

Scott committed
95 96 97 98 99 100
	'hidden' => array(
		'dosaverecalc' => '1',
		'has_js' => '0',
		'code' => qa_get_form_security_code('admin/points'),
	),
);
Scott committed
101 102


Scott committed
103 104
if (qa_clicked('doshowdefaults')) {
	$qa_content['form']['ok'] = qa_lang_html('admin/points_defaults_shown');
Scott committed
105

Scott committed
106 107 108 109
	$qa_content['form']['buttons']['cancel'] = array(
		'tags' => 'name="docancel"',
		'label' => qa_lang_html('main/cancel_button'),
	);
Scott committed
110

Scott committed
111 112 113 114
} else {
	if ($recalculate) {
		$qa_content['form']['ok'] = '<span id="recalc_ok"></span>';
		$qa_content['form']['hidden']['code_recalc'] = qa_get_form_security_code('admin/recalc');
Scott committed
115

Scott committed
116 117
		$qa_content['script_rel'][] = 'qa-content/qa-admin.js?' . QA_VERSION;
		$qa_content['script_var']['qa_warning_recalc'] = qa_lang('admin/stop_recalc_warning');
Scott committed
118

Scott committed
119 120
		$qa_content['script_onloads'][] = array(
			"qa_recalc_click('dorecalcpoints', document.getElementById('dosaverecalc'), null, 'recalc_ok');"
Scott committed
121 122 123
		);
	}

Scott committed
124 125 126 127 128
	$qa_content['form']['buttons']['showdefaults'] = array(
		'tags' => 'name="doshowdefaults"',
		'label' => qa_lang_html('admin/show_defaults_button'),
	);
}
Scott committed
129 130


Scott committed
131 132 133 134 135 136 137 138
foreach ($optionnames as $optionname) {
	$optionfield = array(
		'label' => qa_lang_html('options/' . $optionname),
		'tags' => 'name="option_' . $optionname . '"',
		'value' => qa_html($options[$optionname]),
		'type' => 'number',
		'note' => qa_lang_html('admin/points'),
	);
Scott committed
139

Scott committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	switch ($optionname) {
		case 'points_multiple':
			$prefix = '&#215;';
			unset($optionfield['note']);
			break;

		case 'points_per_q_voted_up':
		case 'points_per_a_voted_up':
		case 'points_q_voted_max_gain':
		case 'points_a_voted_max_gain':
			$prefix = '+';
			break;

		case 'points_per_q_voted_down':
		case 'points_per_a_voted_down':
		case 'points_q_voted_max_loss':
		case 'points_a_voted_max_loss':
			$prefix = '&ndash;';
			break;

		case 'points_base':
			$prefix = '+';
			break;

		default:
			$prefix = '<span style="visibility:hidden;">+</span>'; // for even alignment
			break;
Scott committed
167 168
	}

Scott committed
169
	$optionfield['prefix'] = '<span style="width:1em; display:inline-block; display:-moz-inline-stack;">' . $prefix . '</span>';
Scott committed
170

Scott committed
171 172
	$qa_content['form']['fields'][$optionname] = $optionfield;
}
Scott committed
173

Scott committed
174 175 176
qa_array_insert($qa_content['form']['fields'], 'points_post_a', array('blank0' => array('type' => 'blank')));
qa_array_insert($qa_content['form']['fields'], 'points_vote_up_q', array('blank1' => array('type' => 'blank')));
qa_array_insert($qa_content['form']['fields'], 'points_multiple', array('blank2' => array('type' => 'blank')));
Scott committed
177 178


Scott committed
179
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
180 181


Scott committed
182
return $qa_content;