qa-page-admin-points.php 5.36 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-points.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	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-db-recalc.php';
	require_once QA_INCLUDE_DIR.'qa-db-points.php';
	require_once QA_INCLUDE_DIR.'qa-app-options.php';
	require_once QA_INCLUDE_DIR.'qa-app-admin.php';
	require_once QA_INCLUDE_DIR.'qa-util-sort.php';
Scott Vivian committed
37 38


Gideon Greenspan committed
39 40 41 42 43 44 45
//	Check admin privileges

	if (!qa_admin_check_privileges($qa_content))
		return $qa_content;


//	Process user actions
Scott Vivian committed
46

Gideon Greenspan committed
47 48
	$securityexpired=false;
	$recalculate=false;
Gideon Greenspan committed
49 50 51 52
	$optionnames=qa_db_points_option_names();

	if (qa_clicked('doshowdefaults')) {
		$options=array();
Scott Vivian committed
53

Gideon Greenspan committed
54 55
		foreach ($optionnames as $optionname)
			$options[$optionname]=qa_default_option($optionname);
Scott Vivian committed
56

Gideon Greenspan committed
57 58 59 60 61
	} else {
		if (qa_clicked('docancel'))
			;

		elseif (qa_clicked('dosaverecalc')) {
Gideon Greenspan committed
62 63
			if (!qa_check_form_security_code('admin/points', qa_post_text('code')))
				$securityexpired=true;
Scott Vivian committed
64

Gideon Greenspan committed
65 66 67
			else {
				foreach ($optionnames as $optionname)
					qa_set_option($optionname, (int)qa_post_text('option_'.$optionname));
Scott Vivian committed
68

Gideon Greenspan committed
69 70 71 72 73
				if (!qa_post_text('has_js'))
					qa_redirect('admin/recalc', array('dorecalcpoints' => 1));
				else
					$recalculate=true;
			}
Gideon Greenspan committed
74
		}
Scott Vivian committed
75

Gideon Greenspan committed
76 77
		$options=qa_get_options($optionnames);
	}
Scott Vivian committed
78 79


Gideon Greenspan committed
80 81 82 83 84
//	Prepare content for theme

	$qa_content=qa_content_prepare();

	$qa_content['title']=qa_lang_html('admin/admin_title').' - '.qa_lang_html('admin/points_title');
Gideon Greenspan committed
85
	$qa_content['error']=$securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Gideon Greenspan committed
86 87

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

Gideon Greenspan committed
90
		'style' => 'wide',
Scott Vivian committed
91

Gideon Greenspan committed
92 93
		'buttons' => array(
			'saverecalc' => array(
Gideon Greenspan committed
94
				'tags' => 'id="dosaverecalc"',
Gideon Greenspan committed
95 96 97
				'label' => qa_lang_html('admin/save_recalc_button'),
			),
		),
Scott Vivian committed
98

Gideon Greenspan committed
99 100 101
		'hidden' => array(
			'dosaverecalc' => '1',
			'has_js' => '0',
Gideon Greenspan committed
102
			'code' => qa_get_form_security_code('admin/points'),
Gideon Greenspan committed
103 104 105
		),
	);

Scott Vivian committed
106

Gideon Greenspan committed
107 108
	if (qa_clicked('doshowdefaults')) {
		$qa_content['form']['ok']=qa_lang_html('admin/points_defaults_shown');
Scott Vivian committed
109

Gideon Greenspan committed
110
		$qa_content['form']['buttons']['cancel']=array(
Gideon Greenspan committed
111
			'tags' => 'name="docancel"',
Gideon Greenspan committed
112 113 114 115
			'label' => qa_lang_html('main/cancel_button'),
		);

	} else {
Gideon Greenspan committed
116
		if ($recalculate) {
Gideon Greenspan committed
117
			$qa_content['form']['ok']='<span id="recalc_ok"></span>';
Gideon Greenspan committed
118
			$qa_content['form']['hidden']['code_recalc']=qa_get_form_security_code('admin/recalc');
Scott Vivian committed
119

Gideon Greenspan committed
120 121
			$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 Vivian committed
122

Gideon Greenspan committed
123
			$qa_content['script_onloads'][]=array(
Gideon Greenspan committed
124
				"qa_recalc_click('dorecalcpoints', document.getElementById('dosaverecalc'), null, 'recalc_ok');"
Gideon Greenspan committed
125 126
			);
		}
Scott Vivian committed
127

Gideon Greenspan committed
128
		$qa_content['form']['buttons']['showdefaults']=array(
Gideon Greenspan committed
129
			'tags' => 'name="doshowdefaults"',
Gideon Greenspan committed
130 131 132 133
			'label' => qa_lang_html('admin/show_defaults_button'),
		);
	}

Scott Vivian committed
134

Gideon Greenspan committed
135 136 137
	foreach ($optionnames as $optionname) {
		$optionfield=array(
			'label' => qa_lang_html('options/'.$optionname),
Gideon Greenspan committed
138
			'tags' => 'name="option_'.$optionname.'"',
Gideon Greenspan committed
139 140 141 142
			'value' => qa_html($options[$optionname]),
			'type' => 'number',
			'note' => qa_lang_html('admin/points'),
		);
Scott Vivian committed
143

Gideon Greenspan committed
144 145 146 147 148
		switch ($optionname) {
			case 'points_multiple':
				$prefix='&#215;';
				unset($optionfield['note']);
				break;
Scott Vivian committed
149

Gideon Greenspan committed
150 151 152 153 154 155
			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;
Scott Vivian committed
156

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

Gideon Greenspan committed
164 165 166
			case 'points_base':
				$prefix='+';
				break;
Scott Vivian committed
167

Gideon Greenspan committed
168
			default:
Gideon Greenspan committed
169
				$prefix='<span style="visibility:hidden;">+</span>'; // for even alignment
Gideon Greenspan committed
170 171
				break;
		}
Scott Vivian committed
172

Gideon Greenspan committed
173
		$optionfield['prefix']='<span style="width:1em; display:inline-block; display:-moz-inline-stack;">'.$prefix.'</span>';
Scott Vivian committed
174

Gideon Greenspan committed
175 176
		$qa_content['form']['fields'][$optionname]=$optionfield;
	}
Scott Vivian committed
177

Gideon Greenspan committed
178 179 180
	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 Vivian committed
181 182


Gideon Greenspan committed
183 184
	$qa_content['navigation']['sub']=qa_admin_sub_navigation();

Scott Vivian committed
185

Gideon Greenspan committed
186 187 188 189 190 191
	return $qa_content;


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