admin-categories.php 20.2 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for admin page for editing categories


	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
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28 29 30
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'db/admin.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
31 32


Scott committed
33
// Get relevant list of categories
Scott committed
34

Scott committed
35 36 37 38 39
$editcategoryid = qa_post_text('edit');
if (!isset($editcategoryid))
	$editcategoryid = qa_get('edit');
if (!isset($editcategoryid))
	$editcategoryid = qa_get('addsub');
Scott committed
40

Scott committed
41
$categories = qa_db_select_with_pending(qa_db_category_nav_selectspec($editcategoryid, true, false, true));
Scott committed
42 43


Scott committed
44
// Check admin privileges (do late to allow one DB query)
Scott committed
45

Scott committed
46 47
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
48 49


Scott committed
50
// Work out the appropriate state for the page
Scott committed
51

Scott committed
52
$editcategory = @$categories[$editcategoryid];
Scott committed
53

Scott committed
54 55 56 57
if (isset($editcategory)) {
	$parentid = qa_get('addsub');
	if (isset($parentid))
		$editcategory = array('parentid' => $parentid);
Scott committed
58

Scott committed
59 60 61
} else {
	if (qa_clicked('doaddcategory'))
		$editcategory = array();
Scott committed
62

Scott committed
63 64 65
	elseif (qa_clicked('dosavecategory')) {
		$parentid = qa_post_text('parent');
		$editcategory = array('parentid' => strlen($parentid) ? $parentid : null);
Scott committed
66
	}
Scott committed
67
}
Scott committed
68

Scott committed
69
$setmissing = qa_post_text('missing') || qa_get('missing');
Scott committed
70

Scott committed
71
$setparent = !$setmissing && (qa_post_text('setparent') || qa_get('setparent')) && isset($editcategory['categoryid']);
Scott committed
72

Scott committed
73 74 75 76 77
$hassubcategory = false;
foreach ($categories as $category) {
	if (!strcmp($category['parentid'], $editcategoryid))
		$hassubcategory = true;
}
Scott committed
78 79


Scott committed
80
// Process saving options
Scott committed
81

Scott committed
82 83
$savedoptions = false;
$securityexpired = false;
Scott committed
84

Scott committed
85 86 87
if (qa_clicked('dosaveoptions')) {
	if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
		$securityexpired = true;
Scott committed
88

Scott committed
89 90 91 92
	else {
		qa_set_option('allow_no_category', (int)qa_post_text('option_allow_no_category'));
		qa_set_option('allow_no_sub_category', (int)qa_post_text('option_allow_no_sub_category'));
		$savedoptions = true;
Scott committed
93
	}
Scott committed
94
}
Scott committed
95 96


Scott committed
97
// Process saving an old or new category
Scott committed
98

Scott committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
if (qa_clicked('docancel')) {
	if ($setmissing || $setparent)
		qa_redirect(qa_request(), array('edit' => $editcategory['categoryid']));
	elseif (isset($editcategory['categoryid']))
		qa_redirect(qa_request());
	else
		qa_redirect(qa_request(), array('edit' => @$editcategory['parentid']));

} elseif (qa_clicked('dosetmissing')) {
	if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
		$securityexpired = true;

	else {
		$inreassign = qa_get_category_field_value('reassign');
		qa_db_category_reassign($editcategory['categoryid'], $inreassign);
		qa_redirect(qa_request(), array('recalc' => 1, 'edit' => $editcategory['categoryid']));
	}
Scott committed
116

Scott committed
117 118 119
} elseif (qa_clicked('dosavecategory')) {
	if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
		$securityexpired = true;
Scott committed
120

Scott committed
121 122 123
	elseif (qa_post_text('dodelete')) {
		if (!$hassubcategory) {
			$inreassign = qa_get_category_field_value('reassign');
Scott committed
124
			qa_db_category_reassign($editcategory['categoryid'], $inreassign);
Scott committed
125 126
			qa_db_category_delete($editcategory['categoryid']);
			qa_redirect(qa_request(), array('recalc' => 1, 'edit' => $editcategory['parentid']));
Scott committed
127 128
		}

Scott committed
129 130
	} else {
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
131

Scott committed
132 133 134 135 136
		$inname = qa_post_text('name');
		$incontent = qa_post_text('content');
		$inparentid = $setparent ? qa_get_category_field_value('parent') : $editcategory['parentid'];
		$inposition = qa_post_text('position');
		$errors = array();
Scott committed
137

Scott committed
138
		// Check the parent ID
Scott committed
139

Scott committed
140
		$incategories = qa_db_select_with_pending(qa_db_category_nav_selectspec($inparentid, true));
Scott committed
141

Scott committed
142
		// Verify the name is legitimate for that parent ID
Scott committed
143

Scott committed
144 145 146 147 148 149
		if (empty($inname))
			$errors['name'] = qa_lang('main/field_required');
		elseif (qa_strlen($inname) > QA_DB_MAX_CAT_PAGE_TITLE_LENGTH)
			$errors['name'] = qa_lang_sub('main/max_length_x', QA_DB_MAX_CAT_PAGE_TITLE_LENGTH);
		else {
			foreach ($incategories as $category) {
150
				if (!strcmp($category['parentid'], $inparentid) &&
Scott committed
151 152 153 154 155
					strcmp($category['categoryid'], @$editcategory['categoryid']) &&
					qa_strtolower($category['title']) == qa_strtolower($inname)
				) {
					$errors['name'] = qa_lang('admin/category_already_used');
				}
Scott committed
156
			}
Scott committed
157
		}
Scott committed
158

Scott committed
159
		// Verify the slug is legitimate for that parent ID
Scott committed
160

Scott committed
161 162 163 164 165 166 167
		for ($attempt = 0; $attempt < 100; $attempt++) {
			switch ($attempt) {
				case 0:
					$inslug = qa_post_text('slug');
					if (!isset($inslug))
						$inslug = implode('-', qa_string_to_words($inname));
					break;
Scott committed
168

Scott committed
169 170 171
				case 1:
					$inslug = qa_lang_sub('admin/category_default_slug', $inslug);
					break;
Scott committed
172

Scott committed
173 174
				default:
					$inslug = qa_lang_sub('admin/category_default_slug', $attempt - 1);
Scott committed
175 176 177
					break;
			}

Scott committed
178 179 180 181 182 183 184 185 186 187 188 189 190
			$matchcategoryid = qa_db_category_slug_to_id($inparentid, $inslug); // query against DB since MySQL ignores accents, etc...

			if (!isset($inparentid))
				$matchpage = qa_db_single_select(qa_db_page_full_selectspec($inslug, false));
			else
				$matchpage = null;

			if (empty($inslug))
				$errors['slug'] = qa_lang('main/field_required');
			elseif (qa_strlen($inslug) > QA_DB_MAX_CAT_PAGE_TAGS_LENGTH)
				$errors['slug'] = qa_lang_sub('main/max_length_x', QA_DB_MAX_CAT_PAGE_TAGS_LENGTH);
			elseif (preg_match('/[\\+\\/]/', $inslug))
				$errors['slug'] = qa_lang_sub('admin/slug_bad_chars', '+ /');
Scott committed
191
			elseif (!isset($inparentid) && qa_admin_is_slug_reserved($inslug)) // only top level is a problem
Scott committed
192 193 194 195 196 197 198 199 200 201 202 203
				$errors['slug'] = qa_lang('admin/slug_reserved');
			elseif (isset($matchcategoryid) && strcmp($matchcategoryid, @$editcategory['categoryid']))
				$errors['slug'] = qa_lang('admin/category_already_used');
			elseif (isset($matchpage))
				$errors['slug'] = qa_lang('admin/page_already_used');
			else
				unset($errors['slug']);

			if (isset($editcategory['categoryid']) || !isset($errors['slug'])) // don't try other options if editing existing category
				break;
		}

Scott committed
204
		// Perform appropriate database action
Scott committed
205

Scott committed
206 207 208
		if (empty($errors)) {
			if (isset($editcategory['categoryid'])) { // changing existing category
				qa_db_category_rename($editcategory['categoryid'], $inname, $inslug);
Scott committed
209

Scott committed
210
				$recalc = false;
Scott committed
211

Scott committed
212 213 214 215 216 217 218 219
				if ($setparent) {
					qa_db_category_set_parent($editcategory['categoryid'], $inparentid);
					$recalc = true;
				} else {
					qa_db_category_set_content($editcategory['categoryid'], $incontent);
					qa_db_category_set_position($editcategory['categoryid'], $inposition);
					$recalc = $hassubcategory && $inslug !== $editcategory['tags'];
				}
Scott committed
220

Scott committed
221
				qa_redirect(qa_request(), array('edit' => $editcategory['categoryid'], 'saved' => true, 'recalc' => (int)$recalc));
Scott committed
222

Scott committed
223 224
			} else { // creating a new one
				$categoryid = qa_db_category_create($inparentid, $inname, $inslug);
Scott committed
225

Scott committed
226
				qa_db_category_set_content($categoryid, $incontent);
Scott committed
227

Scott committed
228 229
				if (isset($inposition))
					qa_db_category_set_position($categoryid, $inposition);
Scott committed
230

Scott committed
231
				qa_redirect(qa_request(), array('edit' => $inparentid, 'added' => true));
Scott committed
232 233 234
			}
		}
	}
Scott committed
235
}
Scott committed
236 237


Scott committed
238
// Prepare content for theme
Scott committed
239

Scott committed
240
$qa_content = qa_content_prepare();
Scott committed
241

Scott committed
242 243
$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/categories_title');
$qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Scott committed
244

Scott committed
245 246 247
if ($setmissing) {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
248

Scott committed
249
		'style' => 'tall',
Scott committed
250

Scott committed
251 252 253 254 255 256
		'fields' => array(
			'reassign' => array(
				'label' => isset($editcategory)
					? qa_lang_html_sub('admin/category_no_sub_to', qa_html($editcategory['title']))
					: qa_lang_html('admin/category_none_to'),
				'loose' => true,
Scott committed
257
			),
Scott committed
258
		),
Scott committed
259

Scott committed
260 261 262 263
		'buttons' => array(
			'save' => array(
				'tags' => 'id="dosaveoptions"', // just used for qa_recalc_click()
				'label' => qa_lang_html('main/save_button'),
Scott committed
264 265
			),

Scott committed
266 267 268
			'cancel' => array(
				'tags' => 'name="docancel"',
				'label' => qa_lang_html('main/cancel_button'),
Scott committed
269
			),
Scott committed
270
		),
Scott committed
271

Scott committed
272 273 274 275 276 277 278
		'hidden' => array(
			'dosetmissing' => '1', // for IE
			'edit' => @$editcategory['categoryid'],
			'missing' => '1',
			'code' => qa_get_form_security_code('admin/categories'),
		),
	);
Scott committed
279

Scott committed
280 281
	qa_set_up_category_field($qa_content, $qa_content['form']['fields']['reassign'], 'reassign',
		$categories, @$editcategory['categoryid'], qa_opt('allow_no_category'), qa_opt('allow_no_sub_category'));
Scott committed
282 283


Scott committed
284 285 286
} elseif (isset($editcategory)) {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
287

Scott committed
288
		'style' => 'tall',
Scott committed
289

Scott committed
290
		'ok' => qa_get('saved') ? qa_lang_html('admin/category_saved') : (qa_get('added') ? qa_lang_html('admin/category_added') : null),
Scott committed
291

Scott committed
292 293 294 295 296 297 298 299
		'fields' => array(
			'name' => array(
				'id' => 'name_display',
				'tags' => 'name="name" id="name"',
				'label' => qa_lang_html(count($categories) ? 'admin/category_name' : 'admin/category_name_first'),
				'value' => qa_html(isset($inname) ? $inname : @$editcategory['title']),
				'error' => qa_html(@$errors['name']),
			),
Scott committed
300

Scott committed
301
			'questions' => array(),
Scott committed
302

Scott committed
303
			'delete' => array(),
Scott committed
304

Scott committed
305
			'reassign' => array(),
Scott committed
306

Scott committed
307 308 309 310 311 312
			'slug' => array(
				'id' => 'slug_display',
				'tags' => 'name="slug"',
				'label' => qa_lang_html('admin/category_slug'),
				'value' => qa_html(isset($inslug) ? $inslug : @$editcategory['tags']),
				'error' => qa_html(@$errors['slug']),
Scott committed
313 314
			),

Scott committed
315 316 317 318 319 320 321 322 323
			'content' => array(
				'id' => 'content_display',
				'tags' => 'name="content"',
				'label' => qa_lang_html('admin/category_description'),
				'value' => qa_html(isset($incontent) ? $incontent : @$editcategory['content']),
				'error' => qa_html(@$errors['content']),
				'rows' => 2,
			),
		),
Scott committed
324

Scott committed
325 326 327 328
		'buttons' => array(
			'save' => array(
				'tags' => 'id="dosaveoptions"', // just used for qa_recalc_click
				'label' => qa_lang_html(isset($editcategory['categoryid']) ? 'main/save_button' : 'admin/add_category_button'),
Scott committed
329 330
			),

Scott committed
331 332 333
			'cancel' => array(
				'tags' => 'name="docancel"',
				'label' => qa_lang_html('main/cancel_button'),
Scott committed
334
			),
Scott committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		),

		'hidden' => array(
			'dosavecategory' => '1', // for IE
			'edit' => @$editcategory['categoryid'],
			'parent' => @$editcategory['parentid'],
			'setparent' => (int)$setparent,
			'code' => qa_get_form_security_code('admin/categories'),
		),
	);


	if ($setparent) {
		unset($qa_content['form']['fields']['delete']);
		unset($qa_content['form']['fields']['reassign']);
		unset($qa_content['form']['fields']['questions']);
		unset($qa_content['form']['fields']['content']);

		$qa_content['form']['fields']['parent'] = array(
			'label' => qa_lang_html('admin/category_parent'),
Scott committed
355 356
		);

Scott committed
357 358 359 360 361
		$childdepth = qa_db_category_child_depth($editcategory['categoryid']);

		qa_set_up_category_field($qa_content, $qa_content['form']['fields']['parent'], 'parent',
			isset($incategories) ? $incategories : $categories, isset($inparentid) ? $inparentid : @$editcategory['parentid'],
			true, true, QA_CATEGORY_DEPTH - 1 - $childdepth, @$editcategory['categoryid']);
Scott committed
362

Scott committed
363 364 365 366 367 368 369
		$qa_content['form']['fields']['parent']['options'][''] = qa_lang_html('admin/category_top_level');

		@$qa_content['form']['fields']['parent']['note'] .= qa_lang_html_sub('admin/category_max_depth_x', QA_CATEGORY_DEPTH);

	} elseif (isset($editcategory['categoryid'])) { // existing category
		if ($hassubcategory) {
			$qa_content['form']['fields']['name']['note'] = qa_lang_html('admin/category_no_delete_subs');
Scott committed
370 371 372
			unset($qa_content['form']['fields']['delete']);
			unset($qa_content['form']['fields']['reassign']);

Scott committed
373 374 375 376 377 378 379 380
		} else {
			$qa_content['form']['fields']['delete'] = array(
				'tags' => 'name="dodelete" id="dodelete"',
				'label' =>
					'<span id="reassign_shown">' . qa_lang_html('admin/delete_category_reassign') . '</span>' .
					'<span id="reassign_hidden" style="display:none;">' . qa_lang_html('admin/delete_category') . '</span>',
				'value' => 0,
				'type' => 'checkbox',
Scott committed
381 382
			);

Scott committed
383 384 385 386
			$qa_content['form']['fields']['reassign'] = array(
				'id' => 'reassign_display',
				'tags' => 'name="reassign"',
			);
Scott committed
387

Scott committed
388 389 390
			qa_set_up_category_field($qa_content, $qa_content['form']['fields']['reassign'], 'reassign',
				$categories, $editcategory['parentid'], true, true, null, $editcategory['categoryid']);
		}
Scott committed
391

Scott committed
392 393 394 395 396 397 398 399 400
		$qa_content['form']['fields']['questions'] = array(
			'label' => qa_lang_html('admin/total_qs'),
			'type' => 'static',
			'value' => '<a href="' . qa_path_html('questions/' . qa_category_path_request($categories, $editcategory['categoryid'])) . '">' .
				($editcategory['qcount'] == 1
					? qa_lang_html_sub('main/1_question', '1', '1')
					: qa_lang_html_sub('main/x_questions', qa_format_number($editcategory['qcount']))
				) . '</a>',
		);
Scott committed
401

Scott committed
402 403
		if ($hassubcategory && !qa_opt('allow_no_sub_category')) {
			$nosubcount = qa_db_count_categoryid_qs($editcategory['categoryid']);
Scott committed
404

Scott committed
405 406 407 408 409 410 411 412 413
			if ($nosubcount) {
				$qa_content['form']['fields']['questions']['error'] =
					strtr(qa_lang_html('admin/category_no_sub_error'), array(
						'^q' => qa_format_number($nosubcount),
						'^1' => '<a href="' . qa_path_html(qa_request(), array('edit' => $editcategory['categoryid'], 'missing' => 1)) . '">',
						'^2' => '</a>',
					));
			}
		}
Scott committed
414

Scott committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		qa_set_display_rules($qa_content, array(
			'position_display' => '!dodelete',
			'slug_display' => '!dodelete',
			'content_display' => '!dodelete',
			'parent_display' => '!dodelete',
			'children_display' => '!dodelete',
			'reassign_display' => 'dodelete',
			'reassign_shown' => 'dodelete',
			'reassign_hidden' => '!dodelete',
		));

	} else { // new category
		unset($qa_content['form']['fields']['delete']);
		unset($qa_content['form']['fields']['reassign']);
		unset($qa_content['form']['fields']['slug']);
		unset($qa_content['form']['fields']['questions']);

		$qa_content['focusid'] = 'name';
	}
Scott committed
434

Scott committed
435 436
	if (!$setparent) {
		$pathhtml = qa_category_path_html($categories, @$editcategory['parentid']);
Scott committed
437

Scott committed
438 439 440 441
		if (count($categories)) {
			$qa_content['form']['fields']['parent'] = array(
				'id' => 'parent_display',
				'label' => qa_lang_html('admin/category_parent'),
Scott committed
442
				'type' => 'static',
Scott committed
443
				'value' => (strlen($pathhtml) ? $pathhtml : qa_lang_html('admin/category_top_level')),
Scott committed
444 445
			);

Scott committed
446 447 448
			$qa_content['form']['fields']['parent']['value'] =
				'<a href="' . qa_path_html(qa_request(), array('edit' => @$editcategory['parentid'])) . '">' .
				$qa_content['form']['fields']['parent']['value'] . '</a>';
Scott committed
449

Scott committed
450 451 452 453
			if (isset($editcategory['categoryid'])) {
				$qa_content['form']['fields']['parent']['value'] .= ' - ' .
					'<a href="' . qa_path_html(qa_request(), array('edit' => $editcategory['categoryid'], 'setparent' => 1)) .
					'" style="white-space: nowrap;">' . qa_lang_html('admin/category_move_parent') . '</a>';
Scott committed
454
			}
Scott committed
455
		}
Scott committed
456

Scott committed
457
		$positionoptions = array();
Scott committed
458

Scott committed
459 460
		$previous = null;
		$passedself = false;
Scott committed
461

Scott committed
462 463 464 465 466 467
		foreach ($categories as $key => $category) {
			if (!strcmp($category['parentid'], @$editcategory['parentid'])) {
				if (isset($previous))
					$positionhtml = qa_lang_html_sub('admin/after_x', qa_html($passedself ? $category['title'] : $previous['title']));
				else
					$positionhtml = qa_lang_html('admin/first');
Scott committed
468

Scott committed
469
				$positionoptions[$category['position']] = $positionhtml;
Scott committed
470

Scott committed
471 472
				if (!strcmp($category['categoryid'], @$editcategory['categoryid']))
					$passedself = true;
Scott committed
473

Scott committed
474
				$previous = $category;
Scott committed
475
			}
Scott committed
476
		}
Scott committed
477

Scott committed
478 479
		if (isset($editcategory['position']))
			$positionvalue = $positionoptions[$editcategory['position']];
Scott committed
480

Scott committed
481 482 483 484
		else {
			$positionvalue = isset($previous) ? qa_lang_html_sub('admin/after_x', qa_html($previous['title'])) : qa_lang_html('admin/first');
			$positionoptions[1 + @max(array_keys($positionoptions))] = $positionvalue;
		}
Scott committed
485

Scott committed
486 487 488 489 490 491 492 493
		$qa_content['form']['fields']['position'] = array(
			'id' => 'position_display',
			'tags' => 'name="position"',
			'label' => qa_lang_html('admin/position'),
			'type' => 'select',
			'options' => $positionoptions,
			'value' => $positionvalue,
		);
Scott committed
494

Scott committed
495 496
		if (isset($editcategory['categoryid'])) {
			$catdepth = count(qa_category_path($categories, $editcategory['categoryid']));
Scott committed
497

Scott committed
498 499
			if ($catdepth < QA_CATEGORY_DEPTH) {
				$childrenhtml = '';
Scott committed
500

Scott committed
501 502 503 504 505 506
				foreach ($categories as $category) {
					if (!strcmp($category['parentid'], $editcategory['categoryid'])) {
						$childrenhtml .= (strlen($childrenhtml) ? ', ' : '') .
							'<a href="' . qa_path_html(qa_request(), array('edit' => $category['categoryid'])) . '">' . qa_html($category['title']) . '</a>' .
							' (' . $category['qcount'] . ')';
					}
Scott committed
507 508
				}

Scott committed
509 510
				if (!strlen($childrenhtml))
					$childrenhtml = qa_lang_html('admin/category_no_subs');
Scott committed
511

Scott committed
512 513
				$childrenhtml .= ' - <a href="' . qa_path_html(qa_request(), array('addsub' => $editcategory['categoryid'])) .
					'" style="white-space: nowrap;"><b>' . qa_lang_html('admin/category_add_sub') . '</b></a>';
Scott committed
514

Scott committed
515 516 517 518 519 520 521 522
				$qa_content['form']['fields']['children'] = array(
					'id' => 'children_display',
					'label' => qa_lang_html('admin/category_subs'),
					'type' => 'static',
					'value' => $childrenhtml,
				);
			} else {
				$qa_content['form']['fields']['name']['note'] = qa_lang_html_sub('admin/category_no_add_subs_x', QA_CATEGORY_DEPTH);
Scott committed
523
			}
Scott committed
524

Scott committed
525
		}
Scott committed
526
	}
Scott committed
527

Scott committed
528 529 530
} else {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
531

Scott committed
532
		'ok' => $savedoptions ? qa_lang_html('admin/options_saved') : null,
Scott committed
533

Scott committed
534
		'style' => 'tall',
Scott committed
535

Scott committed
536 537 538 539
		'fields' => array(
			'intro' => array(
				'label' => qa_lang_html('admin/categories_introduction'),
				'type' => 'static',
Scott committed
540
			),
Scott committed
541
		),
Scott committed
542

Scott committed
543 544 545 546
		'buttons' => array(
			'save' => array(
				'tags' => 'name="dosaveoptions" id="dosaveoptions"',
				'label' => qa_lang_html('main/save_button'),
Scott committed
547 548
			),

Scott committed
549 550 551
			'add' => array(
				'tags' => 'name="doaddcategory"',
				'label' => qa_lang_html('admin/add_category_button'),
Scott committed
552
			),
Scott committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		),

		'hidden' => array(
			'code' => qa_get_form_security_code('admin/categories'),
		),
	);

	if (count($categories)) {
		unset($qa_content['form']['fields']['intro']);

		$navcategoryhtml = '';

		foreach ($categories as $category) {
			if (!isset($category['parentid'])) {
				$navcategoryhtml .=
					'<a href="' . qa_path_html('admin/categories', array('edit' => $category['categoryid'])) . '">' .
					qa_html($category['title']) .
					'</a> - ' .
					($category['qcount'] == 1
						? qa_lang_html_sub('main/1_question', '1', '1')
						: qa_lang_html_sub('main/x_questions', qa_format_number($category['qcount']))
					) . '<br/>';
			}
		}
Scott committed
577

Scott committed
578 579 580 581 582
		$qa_content['form']['fields']['nav'] = array(
			'label' => qa_lang_html('admin/top_level_categories'),
			'type' => 'static',
			'value' => $navcategoryhtml,
		);
Scott committed
583

Scott committed
584 585 586 587 588 589
		$qa_content['form']['fields']['allow_no_category'] = array(
			'label' => qa_lang_html('options/allow_no_category'),
			'tags' => 'name="option_allow_no_category"',
			'type' => 'checkbox',
			'value' => qa_opt('allow_no_category'),
		);
Scott committed
590

Scott committed
591 592
		if (!qa_opt('allow_no_category')) {
			$nocatcount = qa_db_count_categoryid_qs(null);
Scott committed
593

Scott committed
594 595 596 597 598 599 600
			if ($nocatcount) {
				$qa_content['form']['fields']['allow_no_category']['error'] =
					strtr(qa_lang_html('admin/category_none_error'), array(
						'^q' => qa_format_number($nocatcount),
						'^1' => '<a href="' . qa_path_html(qa_request(), array('missing' => 1)) . '">',
						'^2' => '</a>',
					));
Scott committed
601
			}
Scott committed
602
		}
Scott committed
603

Scott committed
604 605 606 607 608 609
		$qa_content['form']['fields']['allow_no_sub_category'] = array(
			'label' => qa_lang_html('options/allow_no_sub_category'),
			'tags' => 'name="option_allow_no_sub_category"',
			'type' => 'checkbox',
			'value' => qa_opt('allow_no_sub_category'),
		);
Scott committed
610

Scott committed
611 612 613
	} else
		unset($qa_content['form']['buttons']['save']);
}
Scott committed
614

Scott committed
615 616 617
if (qa_get('recalc')) {
	$qa_content['form']['ok'] = '<span id="recalc_ok">' . qa_lang_html('admin/recalc_categories') . '</span>';
	$qa_content['form']['hidden']['code_recalc'] = qa_get_form_security_code('admin/recalc');
Scott committed
618

Scott committed
619 620
	$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
621

Scott committed
622 623 624 625
	$qa_content['script_onloads'][] = array(
		"qa_recalc_click('dorecalccategories', document.getElementById('dosaveoptions'), null, 'recalc_ok');"
	);
}
Scott committed
626

Scott committed
627
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
628 629


Scott committed
630
return $qa_content;