admin-categories.php 20.3 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-categories.php
	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
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
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
32 33 34 35


//	Get relevant list of categories

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

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


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

Scott committed
47 48
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
49 50 51 52


//	Work out the appropriate state for the page

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

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

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

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

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

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

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


//	Process saving options

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

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

Scott committed
90 91 92 93
	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
94
	}
Scott committed
95
}
Scott committed
96 97 98 99


//	Process saving an old or new category

Scott committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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
117

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

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

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

Scott committed
133 134 135 136 137
		$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
138 139 140

		//	Check the parent ID

Scott committed
141
		$incategories = qa_db_select_with_pending(qa_db_category_nav_selectspec($inparentid, true));
Scott committed
142 143 144

		//	Verify the name is legitimate for that parent ID

Scott committed
145 146 147 148 149 150 151 152 153 154 155 156 157
		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) {
				if (
					!strcmp($category['parentid'], $inparentid) &&
					strcmp($category['categoryid'], @$editcategory['categoryid']) &&
					qa_strtolower($category['title']) == qa_strtolower($inname)
				) {
					$errors['name'] = qa_lang('admin/category_already_used');
				}
Scott committed
158
			}
Scott committed
159
		}
Scott committed
160 161 162

		//	Verify the slug is legitimate for that parent ID

Scott committed
163 164 165 166 167 168 169
		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
170

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

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

Scott committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
			$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', '+ /');
			elseif ((!isset($inparentid)) && qa_admin_is_slug_reserved($inslug)) // only top level is a problem
				$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
206 207
		//	Perform appropriate database action

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

Scott committed
212
				$recalc = false;
Scott committed
213

Scott committed
214 215 216 217 218 219 220 221
				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
222

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

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

Scott committed
228
				qa_db_category_set_content($categoryid, $incontent);
Scott committed
229

Scott committed
230 231
				if (isset($inposition))
					qa_db_category_set_position($categoryid, $inposition);
Scott committed
232

Scott committed
233
				qa_redirect(qa_request(), array('edit' => $inparentid, 'added' => true));
Scott committed
234 235 236
			}
		}
	}
Scott committed
237
}
Scott committed
238 239 240 241


//	Prepare content for theme

Scott committed
242
$qa_content = qa_content_prepare();
Scott committed
243

Scott committed
244 245
$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
246

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

Scott committed
251
		'style' => 'tall',
Scott committed
252

Scott committed
253 254 255 256 257 258
		'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
259
			),
Scott committed
260
		),
Scott committed
261

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

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

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

Scott committed
282 283
	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
284 285


Scott committed
286
} elseif (isset($editcategory)) {
Scott committed
287

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

Scott committed
291
		'style' => 'tall',
Scott committed
292

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

Scott committed
295 296 297 298 299 300 301 302
		'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
303

Scott committed
304
			'questions' => array(),
Scott committed
305

Scott committed
306
			'delete' => array(),
Scott committed
307

Scott committed
308
			'reassign' => array(),
Scott committed
309

Scott committed
310 311 312 313 314 315
			'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
316 317
			),

Scott committed
318 319 320 321 322 323 324 325 326
			'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
327

Scott committed
328 329 330 331
		'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
332 333
			),

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

		'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
358 359
		);

Scott committed
360 361 362 363 364
		$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
365

Scott committed
366 367 368 369 370 371 372
		$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
373 374 375
			unset($qa_content['form']['fields']['delete']);
			unset($qa_content['form']['fields']['reassign']);

Scott committed
376 377 378 379 380 381 382 383
		} 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
384 385
			);

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

Scott committed
391 392 393
			qa_set_up_category_field($qa_content, $qa_content['form']['fields']['reassign'], 'reassign',
				$categories, $editcategory['parentid'], true, true, null, $editcategory['categoryid']);
		}
Scott committed
394

Scott committed
395 396 397 398 399 400 401 402 403
		$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
404

Scott committed
405 406
		if ($hassubcategory && !qa_opt('allow_no_sub_category')) {
			$nosubcount = qa_db_count_categoryid_qs($editcategory['categoryid']);
Scott committed
407

Scott committed
408 409 410 411 412 413 414 415 416
			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
417

Scott committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
		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
437

Scott committed
438 439
	if (!$setparent) {
		$pathhtml = qa_category_path_html($categories, @$editcategory['parentid']);
Scott committed
440

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

Scott committed
449 450 451
			$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
452

Scott committed
453 454 455 456
			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
457
			}
Scott committed
458
		}
Scott committed
459

Scott committed
460
		$positionoptions = array();
Scott committed
461

Scott committed
462 463
		$previous = null;
		$passedself = false;
Scott committed
464

Scott committed
465 466 467 468 469 470
		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
471

Scott committed
472
				$positionoptions[$category['position']] = $positionhtml;
Scott committed
473

Scott committed
474 475
				if (!strcmp($category['categoryid'], @$editcategory['categoryid']))
					$passedself = true;
Scott committed
476

Scott committed
477
				$previous = $category;
Scott committed
478
			}
Scott committed
479
		}
Scott committed
480

Scott committed
481 482
		if (isset($editcategory['position']))
			$positionvalue = $positionoptions[$editcategory['position']];
Scott committed
483

Scott committed
484 485 486 487
		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
488

Scott committed
489 490 491 492 493 494 495 496
		$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
497

Scott committed
498 499
		if (isset($editcategory['categoryid'])) {
			$catdepth = count(qa_category_path($categories, $editcategory['categoryid']));
Scott committed
500

Scott committed
501 502
			if ($catdepth < QA_CATEGORY_DEPTH) {
				$childrenhtml = '';
Scott committed
503

Scott committed
504 505 506 507 508 509
				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
510 511
				}

Scott committed
512 513
				if (!strlen($childrenhtml))
					$childrenhtml = qa_lang_html('admin/category_no_subs');
Scott committed
514

Scott committed
515 516
				$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
517

Scott committed
518 519 520 521 522 523 524 525
				$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
526
			}
Scott committed
527

Scott committed
528
		}
Scott committed
529
	}
Scott committed
530

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

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

Scott committed
537
		'style' => 'tall',
Scott committed
538

Scott committed
539 540 541 542
		'fields' => array(
			'intro' => array(
				'label' => qa_lang_html('admin/categories_introduction'),
				'type' => 'static',
Scott committed
543
			),
Scott committed
544
		),
Scott committed
545

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

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

		'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
580

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

Scott committed
587 588 589 590 591 592
		$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
593

Scott committed
594 595
		if (!qa_opt('allow_no_category')) {
			$nocatcount = qa_db_count_categoryid_qs(null);
Scott committed
596

Scott committed
597 598 599 600 601 602 603
			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
604
			}
Scott committed
605
		}
Scott committed
606

Scott committed
607 608 609 610 611 612
		$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
613

Scott committed
614 615 616
	} else
		unset($qa_content['form']['buttons']['save']);
}
Scott committed
617

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

Scott committed
622 623
	$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
624

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

Scott committed
630
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
631 632


Scott committed
633
return $qa_content;