UserTitles.php 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
*/

namespace Q2A\Controllers\Admin;

use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Middleware\Auth\MinimumUserLevel;

/**
 * Controller for admin page for editing custom user titles.
 */
class UserTitles extends BaseController
{
	public function __construct(DbConnection $db)
	{
		require_once QA_INCLUDE_DIR . 'app/admin.php';
		require_once QA_INCLUDE_DIR . 'db/selects.php';

		parent::__construct($db);

		$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN));
	}

	public function index()
	{
		// 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');

		$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;


		// Process saving an old or new user title

		$securityexpired = false;

61
		if (qa_clicked('docancel')) {
62
			qa_redirect('admin/users');
63
		} elseif (qa_clicked('dosavetitle')) {
64 65
			require_once QA_INCLUDE_DIR . 'util/string.php';

66
			if (!qa_check_form_security_code('admin/usertitles', qa_post_text('code'))) {
67
				$securityexpired = true;
68
			} else {
69 70 71 72 73 74 75 76 77 78 79 80 81
				if (qa_post_text('dodelete')) {
					unset($pointstitle[$oldpoints]);
				} else {
					$intitle = qa_post_text('title');
					$inpoints = qa_post_text('points');

					$errors = array();

					// Verify the title and points are legitimate

					if (!strlen($intitle))
						$errors['title'] = qa_lang('main/field_required');

82
					if (!is_numeric($inpoints)) {
83
						$errors['points'] = qa_lang('main/field_required');
84
					} else {
85 86 87 88 89 90 91 92 93 94 95 96 97 98
						$inpoints = (int)$inpoints;

						if (isset($pointstitle[$inpoints]) && ((!strlen(@$oldpoints)) || ($inpoints != $oldpoints)))
							$errors['points'] = qa_lang('admin/title_already_used');
					}

					// Perform appropriate action

					if (isset($pointstitle[$oldpoints])) { // changing existing user title
						$newpoints = isset($errors['points']) ? $oldpoints : $inpoints;
						$newtitle = isset($errors['title']) ? $pointstitle[$oldpoints] : $intitle;

						unset($pointstitle[$oldpoints]);
						$pointstitle[$newpoints] = $newtitle;
99
					} elseif (empty($errors)) { // creating a new user title
100
						$pointstitle[$inpoints] = $intitle;
101
					}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
				}

				// Save the new option value

				krsort($pointstitle, SORT_NUMERIC);

				$option = '';
				foreach ($pointstitle as $points => $title)
					$option .= (strlen($option) ? ',' : '') . $points . ' ' . $title;

				qa_set_option('points_to_titles', $option);

				if (empty($errors))
					qa_redirect('admin/users');
			}
		}


		// Prepare content for theme

		$qa_content = qa_content_prepare();

		$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();

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

			'style' => 'tall',

			'fields' => array(
				'title' => array(
					'tags' => 'name="title" id="title"',
					'label' => qa_lang_html('admin/user_title'),
					'value' => qa_html(isset($intitle) ? $intitle : @$pointstitle[$oldpoints]),
					'error' => qa_html(@$errors['title']),
				),

				'delete' => array(
					'tags' => 'name="dodelete" id="dodelete"',
					'label' => qa_lang_html('admin/delete_title'),
					'value' => 0,
					'type' => 'checkbox',
				),

				'points' => array(
					'id' => 'points_display',
					'tags' => 'name="points"',
					'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')),
				),

				'cancel' => array(
					'tags' => 'name="docancel"',
					'label' => qa_lang_html('main/cancel_button'),
				),
			),

			'hidden' => array(
				'dosavetitle' => '1', // for IE
				'edit' => @$oldpoints,
				'code' => qa_get_form_security_code('admin/usertitles'),
			),
		);

		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();


		return $qa_content;
	}
}