admin-categories.php 21 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
		if (empty($errors)) {
thopd88 committed
207
			require_once QA_INCLUDE_DIR . 'app/cookies.php';
Scott committed
208 209
			if (isset($editcategory['categoryid'])) { // changing existing category
				qa_db_category_rename($editcategory['categoryid'], $inname, $inslug);
Scott committed
210

Scott committed
211
				$recalc = false;
Scott committed
212

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

222 223
				qa_report_event('cat_edit', qa_get_logged_in_userid(), qa_get_logged_in_handle(), qa_cookie_get(), array(
					'categoryid' => $editcategory['categoryid'],
thopd88 committed
224 225 226
					'parentid' => isset($inparentid)?$inparentid:null,
					'content' => isset($incontent)?$incontent:null,
					'position' => isset($inposition)?$inposition:null,
227
					'name' => $inname,
thopd88 committed
228
					'slug' => $inslug,
229 230
				));

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

Scott committed
233 234
			} else { // creating a new one
				$categoryid = qa_db_category_create($inparentid, $inname, $inslug);
Scott committed
235

Scott committed
236
				qa_db_category_set_content($categoryid, $incontent);
Scott committed
237

Scott committed
238 239
				if (isset($inposition))
					qa_db_category_set_position($categoryid, $inposition);
Scott committed
240

241 242 243
				qa_report_event('cat_new', qa_get_logged_in_userid(), qa_get_logged_in_handle(), qa_cookie_get(), array(
					'categoryid' => $categoryid,
					'parentid' => $inparentid,
thopd88 committed
244 245
					'content' => isset($incontent)?$incontent:null,
					'position' => isset($inposition)?$inposition:null,
246
					'name' => $inname,
thopd88 committed
247
					'slug' => $inslug,
248 249
				));

Scott committed
250
				qa_redirect(qa_request(), array('edit' => $inparentid, 'added' => true));
Scott committed
251 252 253
			}
		}
	}
Scott committed
254
}
Scott committed
255 256


Scott committed
257
// Prepare content for theme
Scott committed
258

Scott committed
259
$qa_content = qa_content_prepare();
Scott committed
260

Scott committed
261 262
$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
263

Scott committed
264 265 266
if ($setmissing) {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
267

Scott committed
268
		'style' => 'tall',
Scott committed
269

Scott committed
270 271 272 273 274 275
		'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
276
			),
Scott committed
277
		),
Scott committed
278

Scott committed
279 280 281 282
		'buttons' => array(
			'save' => array(
				'tags' => 'id="dosaveoptions"', // just used for qa_recalc_click()
				'label' => qa_lang_html('main/save_button'),
Scott committed
283 284
			),

Scott committed
285 286 287
			'cancel' => array(
				'tags' => 'name="docancel"',
				'label' => qa_lang_html('main/cancel_button'),
Scott committed
288
			),
Scott committed
289
		),
Scott committed
290

Scott committed
291 292 293 294 295 296 297
		'hidden' => array(
			'dosetmissing' => '1', // for IE
			'edit' => @$editcategory['categoryid'],
			'missing' => '1',
			'code' => qa_get_form_security_code('admin/categories'),
		),
	);
Scott committed
298

Scott committed
299 300
	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
301 302


Scott committed
303 304 305
} elseif (isset($editcategory)) {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
306

Scott committed
307
		'style' => 'tall',
Scott committed
308

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

Scott committed
311 312 313 314 315 316 317 318
		'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
319

Scott committed
320
			'questions' => array(),
Scott committed
321

Scott committed
322
			'delete' => array(),
Scott committed
323

Scott committed
324
			'reassign' => array(),
Scott committed
325

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

Scott committed
334 335 336 337 338 339 340 341 342
			'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
343

Scott committed
344 345 346 347
		'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
348 349
			),

Scott committed
350 351 352
			'cancel' => array(
				'tags' => 'name="docancel"',
				'label' => qa_lang_html('main/cancel_button'),
Scott committed
353
			),
Scott committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		),

		'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
374 375
		);

Scott committed
376 377 378 379 380
		$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
381

Scott committed
382 383 384 385 386 387 388
		$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
389 390 391
			unset($qa_content['form']['fields']['delete']);
			unset($qa_content['form']['fields']['reassign']);

Scott committed
392 393 394 395 396 397 398 399
		} 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
400 401
			);

Scott committed
402 403 404 405
			$qa_content['form']['fields']['reassign'] = array(
				'id' => 'reassign_display',
				'tags' => 'name="reassign"',
			);
Scott committed
406

Scott committed
407 408 409
			qa_set_up_category_field($qa_content, $qa_content['form']['fields']['reassign'], 'reassign',
				$categories, $editcategory['parentid'], true, true, null, $editcategory['categoryid']);
		}
Scott committed
410

Scott committed
411 412 413 414 415 416 417 418 419
		$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
420

Scott committed
421 422
		if ($hassubcategory && !qa_opt('allow_no_sub_category')) {
			$nosubcount = qa_db_count_categoryid_qs($editcategory['categoryid']);
Scott committed
423

Scott committed
424 425 426 427 428 429 430 431 432
			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
433

Scott committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		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
453

Scott committed
454 455
	if (!$setparent) {
		$pathhtml = qa_category_path_html($categories, @$editcategory['parentid']);
Scott committed
456

Scott committed
457 458 459 460
		if (count($categories)) {
			$qa_content['form']['fields']['parent'] = array(
				'id' => 'parent_display',
				'label' => qa_lang_html('admin/category_parent'),
Scott committed
461
				'type' => 'static',
Scott committed
462
				'value' => (strlen($pathhtml) ? $pathhtml : qa_lang_html('admin/category_top_level')),
Scott committed
463 464
			);

Scott committed
465 466 467
			$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
468

Scott committed
469 470 471 472
			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
473
			}
Scott committed
474
		}
Scott committed
475

Scott committed
476
		$positionoptions = array();
Scott committed
477

Scott committed
478 479
		$previous = null;
		$passedself = false;
Scott committed
480

Scott committed
481 482 483 484 485 486
		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
487

Scott committed
488
				$positionoptions[$category['position']] = $positionhtml;
Scott committed
489

Scott committed
490 491
				if (!strcmp($category['categoryid'], @$editcategory['categoryid']))
					$passedself = true;
Scott committed
492

Scott committed
493
				$previous = $category;
Scott committed
494
			}
Scott committed
495
		}
Scott committed
496

Scott committed
497 498
		if (isset($editcategory['position']))
			$positionvalue = $positionoptions[$editcategory['position']];
Scott committed
499

Scott committed
500 501 502 503
		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
504

Scott committed
505 506 507 508 509 510 511 512
		$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
513

Scott committed
514 515
		if (isset($editcategory['categoryid'])) {
			$catdepth = count(qa_category_path($categories, $editcategory['categoryid']));
Scott committed
516

Scott committed
517 518
			if ($catdepth < QA_CATEGORY_DEPTH) {
				$childrenhtml = '';
Scott committed
519

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

Scott committed
528 529
				if (!strlen($childrenhtml))
					$childrenhtml = qa_lang_html('admin/category_no_subs');
Scott committed
530

Scott committed
531 532
				$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
533

Scott committed
534 535 536 537 538 539 540 541
				$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
542
			}
Scott committed
543

Scott committed
544
		}
Scott committed
545
	}
Scott committed
546

Scott committed
547 548 549
} else {
	$qa_content['form'] = array(
		'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
550

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

Scott committed
553
		'style' => 'tall',
Scott committed
554

Scott committed
555 556 557 558
		'fields' => array(
			'intro' => array(
				'label' => qa_lang_html('admin/categories_introduction'),
				'type' => 'static',
Scott committed
559
			),
Scott committed
560
		),
Scott committed
561

Scott committed
562 563 564 565
		'buttons' => array(
			'save' => array(
				'tags' => 'name="dosaveoptions" id="dosaveoptions"',
				'label' => qa_lang_html('main/save_button'),
Scott committed
566 567
			),

Scott committed
568 569 570
			'add' => array(
				'tags' => 'name="doaddcategory"',
				'label' => qa_lang_html('admin/add_category_button'),
Scott committed
571
			),
Scott committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		),

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

Scott committed
597 598 599 600 601
		$qa_content['form']['fields']['nav'] = array(
			'label' => qa_lang_html('admin/top_level_categories'),
			'type' => 'static',
			'value' => $navcategoryhtml,
		);
Scott committed
602

Scott committed
603 604 605 606 607 608
		$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
609

Scott committed
610 611
		if (!qa_opt('allow_no_category')) {
			$nocatcount = qa_db_count_categoryid_qs(null);
Scott committed
612

Scott committed
613 614 615 616 617 618 619
			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
620
			}
Scott committed
621
		}
Scott committed
622

Scott committed
623 624 625 626 627 628
		$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
629

Scott committed
630 631 632
	} else
		unset($qa_content['form']['buttons']['save']);
}
Scott committed
633

Scott committed
634 635 636
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
637

Scott committed
638 639
	$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
640

Scott committed
641 642 643 644
	$qa_content['script_onloads'][] = array(
		"qa_recalc_click('dorecalccategories', document.getElementById('dosaveoptions'), null, 'recalc_ok');"
	);
}
Scott committed
645

Scott committed
646
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
647 648


Scott committed
649
return $qa_content;