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

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	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-app-admin.php';
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-db-admin.php';

Scott Vivian committed
36

Gideon Greenspan committed
37 38 39 40 41 42 43
//	Get relevant list of categories

	$editcategoryid=qa_post_text('edit');
	if (!isset($editcategoryid))
		$editcategoryid=qa_get('edit');
	if (!isset($editcategoryid))
		$editcategoryid=qa_get('addsub');
Scott Vivian committed
44

Gideon Greenspan committed
45
	$categories=qa_db_select_with_pending(qa_db_category_nav_selectspec($editcategoryid, true, false, true));
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49 50 51

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

	if (!qa_admin_check_privileges($qa_content))
		return $qa_content;
Scott Vivian committed
52 53


Gideon Greenspan committed
54
//	Work out the appropriate state for the page
Scott Vivian committed
55

Gideon Greenspan committed
56
	$editcategory=@$categories[$editcategoryid];
Scott Vivian committed
57

Gideon Greenspan committed
58 59 60 61
	if (isset($editcategory)) {
		$parentid=qa_get('addsub');
		if (isset($parentid))
			$editcategory=array('parentid' => $parentid);
Scott Vivian committed
62

Gideon Greenspan committed
63 64 65
	} else {
		if (qa_clicked('doaddcategory'))
			$editcategory=array();
Scott Vivian committed
66

Gideon Greenspan committed
67 68 69 70 71
		elseif (qa_clicked('dosavecategory')) {
			$parentid=qa_post_text('parent');
			$editcategory=array('parentid' => strlen($parentid) ? $parentid : null);
		}
	}
Scott Vivian committed
72

Gideon Greenspan committed
73
	$setmissing=qa_post_text('missing') || qa_get('missing');
Scott Vivian committed
74

Gideon Greenspan committed
75
	$setparent=(!$setmissing) && (qa_post_text('setparent') || qa_get('setparent')) && isset($editcategory['categoryid']);
Scott Vivian committed
76

Gideon Greenspan committed
77 78 79 80 81 82 83 84
	$hassubcategory=false;
	foreach ($categories as $category)
		if (!strcmp($category['parentid'], $editcategoryid))
			$hassubcategory=true;


//	Process saving options

Gideon Greenspan committed
85 86
	$savedoptions=false;
	$securityexpired=false;
Scott Vivian committed
87

Gideon Greenspan committed
88
	if (qa_clicked('dosaveoptions')) {
Gideon Greenspan committed
89 90 91 92 93 94 95 96
		if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
			$securityexpired=true;

		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;
		}
Gideon Greenspan committed
97 98 99 100 101 102 103 104 105 106 107 108
	}


//	Process saving an old or new category

	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']));
Scott Vivian committed
109

Gideon Greenspan committed
110
	} elseif (qa_clicked('dosetmissing')) {
Gideon Greenspan committed
111 112
		if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
			$securityexpired=true;
Scott Vivian committed
113

Gideon Greenspan committed
114 115 116 117 118
		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 Vivian committed
119

Gideon Greenspan committed
120
	} elseif (qa_clicked('dosavecategory')) {
Gideon Greenspan committed
121 122
		if (!qa_check_form_security_code('admin/categories', qa_post_text('code')))
			$securityexpired=true;
Scott Vivian committed
123

Gideon Greenspan committed
124
		elseif (qa_post_text('dodelete')) {
Gideon Greenspan committed
125 126 127 128 129 130 131

			if (!$hassubcategory) {
				$inreassign=qa_get_category_field_value('reassign');
				qa_db_category_reassign($editcategory['categoryid'], $inreassign);
				qa_db_category_delete($editcategory['categoryid']);
				qa_redirect(qa_request(), array('recalc' => 1, 'edit' => $editcategory['parentid']));
			}
Scott Vivian committed
132

Gideon Greenspan committed
133 134
		} else {
			require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
135

Gideon Greenspan committed
136 137 138 139 140
			$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 Vivian committed
141

Gideon Greenspan committed
142
		//	Check the parent ID
Scott Vivian committed
143

Gideon Greenspan committed
144
			$incategories=qa_db_select_with_pending(qa_db_category_nav_selectspec($inparentid, true));
Scott Vivian committed
145

Gideon Greenspan committed
146
		//	Verify the name is legitimate for that parent ID
Scott Vivian committed
147

Gideon Greenspan committed
148 149 150 151 152 153 154 155 156 157 158 159 160
			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 Vivian committed
161

Gideon Greenspan committed
162
		//	Verify the slug is legitimate for that parent ID
Scott Vivian committed
163

Gideon Greenspan committed
164 165 166 167 168 169 170
			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 Vivian committed
171

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

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

Gideon Greenspan committed
181
				$matchcategoryid=qa_db_category_slug_to_id($inparentid, $inslug); // query against DB since MySQL ignores accents, etc...
Scott Vivian committed
182

Gideon Greenspan committed
183 184 185 186
				if (!isset($inparentid))
					$matchpage=qa_db_single_select(qa_db_page_full_selectspec($inslug, false));
				else
					$matchpage=null;
Scott Vivian committed
187

Gideon Greenspan committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
				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']);
Scott Vivian committed
202

Gideon Greenspan committed
203 204 205 206 207 208 209 210 211
				if (isset($editcategory['categoryid']) || !isset($errors['slug'])) // don't try other options if editing existing category
					break;
			}

		//	Perform appropriate database action

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

Gideon Greenspan committed
213
					$recalc=false;
Scott Vivian committed
214

Gideon Greenspan committed
215 216 217 218 219 220 221 222
					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 Vivian committed
223

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

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

Gideon Greenspan committed
229
					qa_db_category_set_content($categoryid, $incontent);
Scott Vivian committed
230

Gideon Greenspan committed
231 232
					if (isset($inposition))
						qa_db_category_set_position($categoryid, $inposition);
Scott Vivian committed
233

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


Gideon Greenspan committed
241
//	Prepare content for theme
Scott Vivian committed
242

Gideon Greenspan committed
243 244 245
	$qa_content=qa_content_prepare();

	$qa_content['title']=qa_lang_html('admin/admin_title').' - '.qa_lang_html('admin/categories_title');
Gideon Greenspan committed
246
	$qa_content['error']=$securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Scott Vivian committed
247

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

Gideon Greenspan committed
252
			'style' => 'tall',
Scott Vivian committed
253

Gideon Greenspan committed
254 255 256 257 258 259 260 261
			'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 Vivian committed
262

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

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

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

		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 Vivian committed
285 286


Gideon Greenspan committed
287 288 289
	} elseif (isset($editcategory)) {

		$qa_content['form']=array(
Gideon Greenspan committed
290
			'tags' => 'method="post" action="'.qa_path_html(qa_request()).'"',
Scott Vivian committed
291

Gideon Greenspan committed
292 293 294
			'style' => 'tall',

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

Gideon Greenspan committed
296 297 298
			'fields' => array(
				'name' => array(
					'id' => 'name_display',
Gideon Greenspan committed
299
					'tags' => 'name="name" id="name"',
Gideon Greenspan committed
300 301 302 303
					'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 Vivian committed
304

Gideon Greenspan committed
305
				'questions' => array(),
Scott Vivian committed
306

Gideon Greenspan committed
307
				'delete' => array(),
Scott Vivian committed
308

Gideon Greenspan committed
309
				'reassign' => array(),
Scott Vivian committed
310

Gideon Greenspan committed
311 312
				'slug' => array(
					'id' => 'slug_display',
Gideon Greenspan committed
313
					'tags' => 'name="slug"',
Gideon Greenspan committed
314 315 316 317
					'label' => qa_lang_html('admin/category_slug'),
					'value' => qa_html(isset($inslug) ? $inslug : @$editcategory['tags']),
					'error' => qa_html(@$errors['slug']),
				),
Scott Vivian committed
318

Gideon Greenspan committed
319 320
				'content' => array(
					'id' => 'content_display',
Gideon Greenspan committed
321
					'tags' => 'name="content"',
Gideon Greenspan committed
322 323 324 325 326 327
					'label' => qa_lang_html('admin/category_description'),
					'value' => qa_html(isset($incontent) ? $incontent : @$editcategory['content']),
					'error' => qa_html(@$errors['content']),
					'rows' => 2,
				),
			),
Scott Vivian committed
328

Gideon Greenspan committed
329 330
			'buttons' => array(
				'save' => array(
Gideon Greenspan committed
331
					'tags' => 'id="dosaveoptions"', // just used for qa_recalc_click
Gideon Greenspan committed
332 333
					'label' => qa_lang_html(isset($editcategory['categoryid']) ? 'main/save_button' : 'admin/add_category_button'),
				),
Scott Vivian committed
334

Gideon Greenspan committed
335
				'cancel' => array(
Gideon Greenspan committed
336
					'tags' => 'name="docancel"',
Gideon Greenspan committed
337 338 339
					'label' => qa_lang_html('main/cancel_button'),
				),
			),
Scott Vivian committed
340

Gideon Greenspan committed
341 342 343 344 345
			'hidden' => array(
				'dosavecategory' => '1', // for IE
				'edit' => @$editcategory['categoryid'],
				'parent' =>  @$editcategory['parentid'],
				'setparent' => (int)$setparent,
Gideon Greenspan committed
346
				'code' => qa_get_form_security_code('admin/categories'),
Gideon Greenspan committed
347 348
			),
		);
Scott Vivian committed
349 350


Gideon Greenspan committed
351 352 353 354 355
		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']);
Scott Vivian committed
356

Gideon Greenspan committed
357 358 359
			$qa_content['form']['fields']['parent']=array(
				'label' => qa_lang_html('admin/category_parent'),
			);
Scott Vivian committed
360

Gideon Greenspan committed
361
			$childdepth=qa_db_category_child_depth($editcategory['categoryid']);
Scott Vivian committed
362

Gideon Greenspan committed
363 364 365
			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 Vivian committed
366

Gideon Greenspan committed
367
			$qa_content['form']['fields']['parent']['options']['']=qa_lang_html('admin/category_top_level');
Scott Vivian committed
368

Gideon Greenspan committed
369 370 371 372 373 374 375 376 377 378
			@$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');
				unset($qa_content['form']['fields']['delete']);
				unset($qa_content['form']['fields']['reassign']);

			} else {
				$qa_content['form']['fields']['delete']=array(
Gideon Greenspan committed
379
					'tags' => 'name="dodelete" id="dodelete"',
Gideon Greenspan committed
380
					'label' =>
Gideon Greenspan committed
381 382
						'<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>',
Gideon Greenspan committed
383 384 385
					'value' => 0,
					'type' => 'checkbox',
				);
Scott Vivian committed
386

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

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

Gideon Greenspan committed
396 397 398
			$qa_content['form']['fields']['questions']=array(
				'label' => qa_lang_html('admin/total_qs'),
				'type' => 'static',
Gideon Greenspan committed
399
				'value' => '<a href="'.qa_path_html('questions/'.qa_category_path_request($categories, $editcategory['categoryid'])).'">'.
Gideon Greenspan committed
400 401 402
								( ($editcategory['qcount']==1)
									? qa_lang_html_sub('main/1_question', '1', '1')
									: qa_lang_html_sub('main/x_questions', number_format($editcategory['qcount']))
Gideon Greenspan committed
403
								).'</a>',
Gideon Greenspan committed
404 405 406 407
			);

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

Gideon Greenspan committed
409 410 411 412
				if ($nosubcount)
					$qa_content['form']['fields']['questions']['error']=
						strtr(qa_lang_html('admin/category_no_sub_error'), array(
							'^q' => number_format($nosubcount),
Gideon Greenspan committed
413 414
							'^1' => '<a href="'.qa_path_html(qa_request(), array('edit' => $editcategory['categoryid'], 'missing' => 1)).'">',
							'^2' => '</a>',
Gideon Greenspan committed
415 416
						));
			}
Scott Vivian committed
417

Gideon Greenspan committed
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']);
Scott Vivian committed
434

Gideon Greenspan committed
435 436
			$qa_content['focusid']='name';
		}
Scott Vivian committed
437

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

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

Gideon Greenspan committed
449
				$qa_content['form']['fields']['parent']['value']=
Gideon Greenspan committed
450 451
					'<a href="'.qa_path_html(qa_request(), array('edit' => @$editcategory['parentid'])).'">'.
					$qa_content['form']['fields']['parent']['value'].'</a>';
Scott Vivian committed
452

Gideon Greenspan committed
453 454
				if (isset($editcategory['categoryid']))
					$qa_content['form']['fields']['parent']['value'].=' - '.
Gideon Greenspan committed
455 456
						'<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>';
Gideon Greenspan committed
457 458 459
			}

			$positionoptions=array();
Scott Vivian committed
460

Gideon Greenspan committed
461 462
			$previous=null;
			$passedself=false;
Scott Vivian committed
463

Gideon Greenspan committed
464 465 466 467 468 469
			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 Vivian committed
470

Gideon Greenspan committed
471
					$positionoptions[$category['position']]=$positionhtml;
Scott Vivian committed
472

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

Gideon Greenspan committed
476 477
					$previous=$category;
				}
Scott Vivian committed
478

Gideon Greenspan committed
479 480
			if (isset($editcategory['position']))
				$positionvalue=$positionoptions[$editcategory['position']];
Scott Vivian committed
481

Gideon Greenspan committed
482 483 484 485
			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 Vivian committed
486

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

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

Gideon Greenspan committed
499 500
				if ($catdepth<QA_CATEGORY_DEPTH) {
					$childrenhtml='';
Scott Vivian committed
501

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

Gideon Greenspan committed
508 509
					if (!strlen($childrenhtml))
						$childrenhtml=qa_lang_html('admin/category_no_subs');
Scott Vivian committed
510

Gideon Greenspan committed
511 512
					$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 Vivian committed
513

Gideon Greenspan committed
514 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 Vivian committed
523

Gideon Greenspan committed
524 525
			}
		}
Scott Vivian committed
526

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

Gideon Greenspan committed
531
			'ok' => $savedoptions ? qa_lang_html('admin/options_saved') : null,
Scott Vivian committed
532

Gideon Greenspan committed
533
			'style' => 'tall',
Scott Vivian committed
534

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

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

Gideon Greenspan committed
548
				'add' => array(
Gideon Greenspan committed
549
					'tags' => 'name="doaddcategory"',
Gideon Greenspan committed
550
					'label' => qa_lang_html('admin/add_category_button'),
Scott Vivian committed
551
				),
Gideon Greenspan committed
552
			),
Scott Vivian committed
553

Gideon Greenspan committed
554 555
			'hidden' => array(
				'code' => qa_get_form_security_code('admin/categories'),
Scott Vivian committed
556
			),
Gideon Greenspan committed
557 558 559 560
		);

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

Gideon Greenspan committed
562 563 564 565
			$navcategoryhtml='';

			foreach ($categories as $category)
				if (!isset($category['parentid']))
Gideon Greenspan committed
566 567
					$navcategoryhtml.='<a href="'.qa_path_html('admin/categories', array('edit' => $category['categoryid'])).'">'.
						qa_html($category['title']).'</a> - '.qa_lang_html_sub('main/x_questions', $category['qcount']).'<br/>';
Gideon Greenspan committed
568 569 570 571 572 573

			$qa_content['form']['fields']['nav']=array(
				'label' => qa_lang_html('admin/top_level_categories'),
				'type' => 'static',
				'value' => $navcategoryhtml,
			);
Scott Vivian committed
574

Gideon Greenspan committed
575 576
			$qa_content['form']['fields']['allow_no_category']=array(
				'label' => qa_lang_html('options/allow_no_category'),
Gideon Greenspan committed
577
				'tags' => 'name="option_allow_no_category"',
Gideon Greenspan committed
578 579 580
				'type' => 'checkbox',
				'value' => qa_opt('allow_no_category'),
			);
Scott Vivian committed
581

Gideon Greenspan committed
582 583
			if (!qa_opt('allow_no_category')) {
				$nocatcount=qa_db_count_categoryid_qs(null);
Scott Vivian committed
584

Gideon Greenspan committed
585 586 587 588
				if ($nocatcount)
					$qa_content['form']['fields']['allow_no_category']['error']=
						strtr(qa_lang_html('admin/category_none_error'), array(
							'^q' => number_format($nocatcount),
Gideon Greenspan committed
589 590
							'^1' => '<a href="'.qa_path_html(qa_request(), array('missing' => 1)).'">',
							'^2' => '</a>',
Gideon Greenspan committed
591 592
						));
			}
Scott Vivian committed
593

Gideon Greenspan committed
594 595
			$qa_content['form']['fields']['allow_no_sub_category']=array(
				'label' => qa_lang_html('options/allow_no_sub_category'),
Gideon Greenspan committed
596
				'tags' => 'name="option_allow_no_sub_category"',
Gideon Greenspan committed
597 598 599 600 601 602 603 604 605
				'type' => 'checkbox',
				'value' => qa_opt('allow_no_sub_category'),
			);

		} else
			unset($qa_content['form']['buttons']['save']);
	}

	if (qa_get('recalc')) {
Gideon Greenspan committed
606
		$qa_content['form']['ok']='<span id="recalc_ok">'.qa_lang_html('admin/recalc_categories').'</span>';
Gideon Greenspan committed
607
		$qa_content['form']['hidden']['code_recalc']=qa_get_form_security_code('admin/recalc');
Scott Vivian committed
608

Gideon Greenspan committed
609 610
		$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
611

Gideon Greenspan committed
612
		$qa_content['script_onloads'][]=array(
Gideon Greenspan committed
613
			"qa_recalc_click('dorecalccategories', document.getElementById('dosaveoptions'), null, 'recalc_ok');"
Gideon Greenspan committed
614 615
		);
	}
Scott Vivian committed
616

Gideon Greenspan committed
617 618
	$qa_content['navigation']['sub']=qa_admin_sub_navigation();

Scott Vivian committed
619

Gideon Greenspan committed
620 621 622 623 624 625
	return $qa_content;


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