question-view.php 42.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: Common functions for question page viewing, either regular or via Ajax


	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 26 27 28 29
	exit;
}


/**
 * Given a $question and its $childposts from the database, return a list of that question's answers
30 31
 * @param array $question
 * @param array $childposts
Scott committed
32 33 34 35 36 37
 * @return array
 */
function qa_page_q_load_as($question, $childposts)
{
	$answers = array();

38
	foreach ($childposts as $postid => $post) {
Scott committed
39 40 41 42 43 44 45
		switch ($post['type']) {
			case 'A':
			case 'A_HIDDEN':
			case 'A_QUEUED':
				$answers[$postid] = $post;
				break;
		}
46
	}
Scott committed
47

Scott committed
48 49 50 51 52 53 54 55
	return $answers;
}


/**
 * Given a $question, its $childposts and its answers $achildposts from the database,
 * return a list of comments or follow-on questions for that question or its answers.
 * Follow-on and duplicate questions are now returned, with their visibility determined in qa_page_q_comment_follow_list()
56 57 58
 * @param array $question
 * @param array $childposts
 * @param array $achildposts
Scott committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72
 * @param array $duplicateposts
 * @return array
 */
function qa_page_q_load_c_follows($question, $childposts, $achildposts, $duplicateposts = array())
{
	$commentsfollows = array();

	foreach ($childposts as $postid => $post) {
		switch ($post['basetype']) {
			case 'Q':
			case 'C':
				$commentsfollows[$postid] = $post;
				break;
		}
Scott committed
73 74
	}

Scott committed
75 76 77 78 79 80
	foreach ($achildposts as $postid => $post) {
		switch ($post['basetype']) {
			case 'Q':
			case 'C':
				$commentsfollows[$postid] = $post;
				break;
81
		}
Scott committed
82
	}
83

Scott committed
84 85
	foreach ($duplicateposts as $postid => $post) {
		$commentsfollows[$postid] = $post;
Scott committed
86 87
	}

Scott committed
88 89 90 91 92 93 94 95 96
	return $commentsfollows;
}


/**
 * Calculates which operations the current user may perform on a post. This function is a key part of Q2A's logic
 * and is ripe for overriding by plugins. The latter two arrays can contain additional posts retrieved from the
 * database, and these will be ignored.
 * @param array $post The question/answer/comment to check.
97 98 99
 * @param array|null $parentpost The post's parent if there is one.
 * @param array|null $siblingposts The post's siblings (i.e. those with the same type and parent as the post).
 * @param array|null $childposts The post's children (e.g. comments on answers).
Scott committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 * @return array List of elements that can be added to the post.
 */
function qa_page_q_post_rules($post, $parentpost = null, $siblingposts = null, $childposts = null)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	$userid = qa_get_logged_in_userid();
	$cookieid = qa_cookie_get();
	$userlevel = qa_user_level_for_post($post);

	$userfields = qa_get_logged_in_user_cache();
	if (!isset($userfields)) {
		$userfields = array(
			'userid' => null,
			'level' => null,
			'flags' => null,
		);
	}
Scott committed
118

Scott committed
119 120
	$rules['isbyuser'] = qa_post_is_by_user($post, $userid, $cookieid);
	$rules['closed'] = $post['basetype'] == 'Q' && (isset($post['closedbyid']) || (isset($post['selchildid']) && qa_opt('do_close_on_select')));
121

Scott committed
122
	// Cache some responses to the user permission checks
Scott committed
123

Scott committed
124 125 126
	$permiterror_post_q = qa_user_permit_error('permit_post_q', null, $userlevel, true, $userfields); // don't check limits here, so we can show error message
	$permiterror_post_a = qa_user_permit_error('permit_post_a', null, $userlevel, true, $userfields);
	$permiterror_post_c = qa_user_permit_error('permit_post_c', null, $userlevel, true, $userfields);
Scott committed
127

Scott committed
128 129 130 131 132 133
	$edit_option = $post['basetype'] == 'Q' ? 'permit_edit_q' : ($post['basetype'] == 'A' ? 'permit_edit_a' : 'permit_edit_c');
	$permiterror_edit = qa_user_permit_error($edit_option, null, $userlevel, true, $userfields);
	$permiterror_retagcat = qa_user_permit_error('permit_retag_cat', null, $userlevel, true, $userfields);
	$permiterror_flag = qa_user_permit_error('permit_flag', null, $userlevel, true, $userfields);
	$permiterror_hide_show = qa_user_permit_error('permit_hide_show', null, $userlevel, true, $userfields);
	$permiterror_hide_show_self = $rules['isbyuser'] ? qa_user_permit_error(null, null, $userlevel, true, $userfields) : $permiterror_hide_show;
134 135 136

	$close_option = $rules['isbyuser'] && qa_opt('allow_close_own_questions') ? null : 'permit_close_q';
	$permiterror_close_open = qa_user_permit_error($close_option, null, $userlevel, true, $userfields);
Scott committed
137
	$permiterror_moderate = qa_user_permit_error('permit_moderate', null, $userlevel, true, $userfields);
Scott committed
138

Scott committed
139
	// General permissions
Scott committed
140

Scott committed
141
	$rules['authorlast'] = !isset($post['lastuserid']) || $post['lastuserid'] === $post['userid'];
142
	$rules['viewable'] = $post['hidden'] ? !$permiterror_hide_show_self : ($post['queued'] ? ($rules['isbyuser'] || !$permiterror_moderate) : true);
Scott committed
143

Scott committed
144
	// Answer, comment and edit might show the button even if the user still needs to do something (e.g. log in)
Scott committed
145

Scott committed
146 147
	$rules['answerbutton'] = $post['type'] == 'Q' && $permiterror_post_a != 'level' && !$rules['closed']
		&& (qa_opt('allow_self_answer') || !$rules['isbyuser']);
Scott committed
148

Scott committed
149 150 151
	$rules['commentbutton'] = ($post['type'] == 'Q' || $post['type'] == 'A') && $permiterror_post_c != 'level'
		&& qa_opt($post['type'] == 'Q' ? 'comment_on_qs' : 'comment_on_as');
	$rules['commentable'] = $rules['commentbutton'] && !$permiterror_post_c;
Scott committed
152

Scott committed
153
	$button_errors = array('login', 'level', 'approve');
Scott committed
154

Scott committed
155
	$rules['editbutton'] = !$post['hidden'] && !$rules['closed']
156
		&& ($rules['isbyuser'] || (!in_array($permiterror_edit, $button_errors) && (!$post['queued'])));
Scott committed
157
	$rules['editable'] = $rules['editbutton'] && ($rules['isbyuser'] || !$permiterror_edit);
158

Scott committed
159 160 161
	$rules['retagcatbutton'] = $post['basetype'] == 'Q' && (qa_using_tags() || qa_using_categories())
		&& !$post['hidden'] && ($rules['isbyuser'] || !in_array($permiterror_retagcat, $button_errors));
	$rules['retagcatable'] = $rules['retagcatbutton'] && ($rules['isbyuser'] || !$permiterror_retagcat);
Scott committed
162

Scott committed
163 164 165 166 167 168 169
	if ($rules['editbutton'] && $rules['retagcatbutton']) {
		// only show one button since they lead to the same form
		if ($rules['retagcatable'] && !$rules['editable'])
			$rules['editbutton'] = false; // if we can do this without getting an error, show that as the title
		else
			$rules['retagcatbutton'] = false;
	}
Scott committed
170

Scott committed
171 172
	$rules['aselectable'] = $post['type'] == 'Q' && !qa_user_permit_error($rules['isbyuser'] ? null : 'permit_select_a', null, $userlevel, true, $userfields);

173
	$rules['flagbutton'] = qa_opt('flagging_of_posts') && !$rules['isbyuser'] && !$post['hidden'] && !$post['queued']
Scott committed
174 175 176 177 178 179 180 181 182 183
		&& !@$post['userflag'] && !in_array($permiterror_flag, $button_errors);
	$rules['flagtohide'] = $rules['flagbutton'] && !$permiterror_flag && ($post['flagcount'] + 1) >= qa_opt('flagging_hide_after');
	$rules['unflaggable'] = @$post['userflag'] && !$post['hidden'];
	$rules['clearflaggable'] = $post['flagcount'] >= (@$post['userflag'] ? 2 : 1) && !$permiterror_hide_show;

	// Other actions only show the button if it's immediately possible

	$notclosedbyother = !($rules['closed'] && isset($post['closedbyid']) && !$rules['authorlast']);
	$nothiddenbyother = !($post['hidden'] && !$rules['authorlast']);

184
	$rules['closeable'] = qa_opt('allow_close_questions') && $post['type'] == 'Q' && !$rules['closed'] && $permiterror_close_open === false;
Scott committed
185
	// cannot reopen a question if it's been hidden, or if it was closed by someone else and you don't have global closing permissions
186
	$rules['reopenable'] = $rules['closed'] && $permiterror_close_open === false && !$post['hidden']
Scott committed
187 188
		&& ($notclosedbyother || !qa_user_permit_error('permit_close_q', null, $userlevel, true, $userfields));

189
	$rules['moderatable'] = $post['queued'] && !$permiterror_moderate;
Scott committed
190
	// cannot hide a question if it was closed by someone else and you don't have global hiding permissions
191
	$rules['hideable'] = !$post['hidden'] && ($rules['isbyuser'] || !$post['queued']) && !$permiterror_hide_show_self
Scott committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		&& ($notclosedbyother || !$permiterror_hide_show);
	// means post can be reshown immediately without checking whether it needs moderation
	$rules['reshowimmed'] = $post['hidden'] && !$permiterror_hide_show;
	// cannot reshow a question if it was hidden by someone else, or if it has flags - unless you have global hide/show permissions
	$rules['reshowable'] = $post['hidden'] && (!$permiterror_hide_show_self) &&
		($rules['reshowimmed'] || ($nothiddenbyother && !$post['flagcount']));

	$rules['deleteable'] = $post['hidden'] && !qa_user_permit_error('permit_delete_hidden', null, $userlevel, true, $userfields);
	$rules['claimable'] = !isset($post['userid']) && isset($userid) && strlen(@$post['cookieid']) && (strcmp(@$post['cookieid'], $cookieid) == 0)
		&& !($post['basetype'] == 'Q' ? $permiterror_post_q : ($post['basetype'] == 'A' ? $permiterror_post_a : $permiterror_post_c));
	$rules['followable'] = $post['type'] == 'A' ? qa_opt('follow_on_as') : false;

	// Check for claims that could break rules about self answering and multiple answers

	if ($rules['claimable'] && $post['basetype'] == 'A') {
		if (!qa_opt('allow_self_answer') && isset($parentpost) && qa_post_is_by_user($parentpost, $userid, $cookieid))
			$rules['claimable'] = false;

		if (isset($siblingposts) && !qa_opt('allow_multi_answers')) {
			foreach ($siblingposts as $siblingpost) {
				if ($siblingpost['parentid'] == $post['parentid'] && $siblingpost['basetype'] == 'A' && qa_post_is_by_user($siblingpost, $userid, $cookieid))
					$rules['claimable'] = false;
			}
Scott committed
215
		}
Scott committed
216
	}
Scott committed
217

Scott committed
218
	// Now make any changes based on the child posts
Scott committed
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	// if (isset($childposts)) {
	// 	foreach ($childposts as $childpost) {
	// 		// Skip the child post if it is a note
	// 		if ($childpost['basetype'] === 'N') {
	// 			continue;
	// 		}

	// 		if ($childpost['parentid'] == $post['postid']) {
	// 			// this post has comments
	// 			$rules['deleteable'] = false;

	// 			if ($childpost['basetype'] == 'A' && qa_post_is_by_user($childpost, $userid, $cookieid)) {
	// 				if (!qa_opt('allow_multi_answers'))
	// 					$rules['answerbutton'] = false;

	// 				if (!qa_opt('allow_self_answer'))
	// 					$rules['claimable'] = false;
	// 			}
	// 		}

	// 		if ($childpost['closedbyid'] == $post['postid']) {
	// 			// other questions are closed as duplicates of this one
	// 			$rules['deleteable'] = false;
	// 		}
	// 	}
	// }
Scott committed
246

Scott committed
247 248 249 250 251 252 253 254 255 256 257 258
	// Return the resulting rules

	return $rules;
}


/**
 * Return the $qa_content['q_view'] element for $question as viewed by the current user. If this question is a
 * follow-on, pass the question for this question's parent answer in $parentquestion, otherwise null. If the question
 * is closed, pass the post used to close this question in $closepost, otherwise null. $usershtml should be an array
 * which maps userids to HTML user representations, including the question's author and (if present) last editor. If a
 * form has been explicitly requested for the page, set $formrequested to true - this will hide the buttons.
259 260 261 262 263
 * @param array $question
 * @param array|null $parentquestion
 * @param int|null $closepost
 * @param array $usershtml
 * @param bool $formrequested
Scott committed
264 265 266 267
 * @return array
 */
function qa_page_q_question_view($question, $parentquestion, $closepost, $usershtml, $formrequested)
{
268 269
	require_once QA_INCLUDE_DIR . 'app/posts.php';

Scott committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	$questionid = $question['postid'];
	$userid = qa_get_logged_in_userid();
	$cookieid = qa_cookie_get();

	$htmloptions = qa_post_html_options($question, null, true);
	$htmloptions['answersview'] = false; // answer count is displayed separately so don't show it here
	$htmloptions['avatarsize'] = qa_opt('avatar_q_page_q_size');
	$htmloptions['q_request'] = qa_q_request($question['postid'], $question['title']);
	$q_view = qa_post_html_fields($question, $userid, $cookieid, $usershtml, null, $htmloptions);


	$q_view['main_form_tags'] = 'method="post" action="' . qa_self_html() . '"';
	$q_view['voting_form_hidden'] = array('code' => qa_get_form_security_code('vote'));
	$q_view['buttons_form_hidden'] = array('code' => qa_get_form_security_code('buttons-' . $questionid), 'qa_click' => '');


	// Buttons for operating on the question

	if (!$formrequested) { // don't show if another form is currently being shown on page
		$clicksuffix = ' onclick="qa_show_waiting_after(this, false);"'; // add to operations that write to database
		$buttons = array();

		if ($question['editbutton']) {
			$buttons['edit'] = array(
				'tags' => 'name="q_doedit"',
				'label' => qa_lang_html('question/edit_button'),
				'popup' => qa_lang_html('question/edit_q_popup'),
			);
		}
Scott committed
299

Scott committed
300
		$hascategories = qa_using_categories();
Scott committed
301

Scott committed
302 303 304 305 306 307 308 309 310 311
		if ($question['retagcatbutton']) {
			$buttons['retagcat'] = array(
				'tags' => 'name="q_doedit"',
				'label' => qa_lang_html($hascategories ? 'question/recat_button' : 'question/retag_button'),
				'popup' => qa_lang_html($hascategories
					? (qa_using_tags() ? 'question/retag_cat_popup' : 'question/recat_popup')
					: 'question/retag_popup'
				),
			);
		}
Scott committed
312

Scott committed
313 314 315 316 317 318 319
		if ($question['flagbutton']) {
			$buttons['flag'] = array(
				'tags' => 'name="q_doflag"' . $clicksuffix,
				'label' => qa_lang_html($question['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
				'popup' => qa_lang_html('question/flag_q_popup'),
			);
		}
Scott committed
320

Scott committed
321 322 323 324 325 326 327
		if ($question['unflaggable']) {
			$buttons['unflag'] = array(
				'tags' => 'name="q_dounflag"' . $clicksuffix,
				'label' => qa_lang_html('question/unflag_button'),
				'popup' => qa_lang_html('question/unflag_popup'),
			);
		}
Scott committed
328

Scott committed
329 330 331 332 333 334 335
		if ($question['clearflaggable']) {
			$buttons['clearflags'] = array(
				'tags' => 'name="q_doclearflags"' . $clicksuffix,
				'label' => qa_lang_html('question/clear_flags_button'),
				'popup' => qa_lang_html('question/clear_flags_popup'),
			);
		}
Scott committed
336

Scott committed
337 338 339 340 341 342 343
		if ($question['closeable']) {
			$buttons['close'] = array(
				'tags' => 'name="q_doclose"',
				'label' => qa_lang_html('question/close_button'),
				'popup' => qa_lang_html('question/close_q_popup'),
			);
		}
Scott committed
344

Scott committed
345 346 347 348 349 350 351
		if ($question['reopenable']) {
			$buttons['reopen'] = array(
				'tags' => 'name="q_doreopen"' . $clicksuffix,
				'label' => qa_lang_html('question/reopen_button'),
				'popup' => qa_lang_html('question/reopen_q_popup'),
			);
		}
Scott committed
352

Scott committed
353 354 355 356 357 358
		if ($question['moderatable']) {
			$buttons['approve'] = array(
				'tags' => 'name="q_doapprove"' . $clicksuffix,
				'label' => qa_lang_html('question/approve_button'),
				'popup' => qa_lang_html('question/approve_q_popup'),
			);
Scott committed
359

Scott committed
360 361 362 363 364 365
			$buttons['reject'] = array(
				'tags' => 'name="q_doreject"' . $clicksuffix,
				'label' => qa_lang_html('question/reject_button'),
				'popup' => qa_lang_html('question/reject_q_popup'),
			);
		}
Scott committed
366

Scott committed
367 368 369 370 371 372 373
		if ($question['hideable']) {
			$buttons['hide'] = array(
				'tags' => 'name="q_dohide"' . $clicksuffix,
				'label' => qa_lang_html('question/hide_button'),
				'popup' => qa_lang_html('question/hide_q_popup'),
			);
		}
Scott committed
374

Scott committed
375 376 377 378 379 380 381
		if ($question['reshowable']) {
			$buttons['reshow'] = array(
				'tags' => 'name="q_doreshow"' . $clicksuffix,
				'label' => qa_lang_html('question/reshow_button'),
				'popup' => qa_lang_html('question/reshow_q_popup'),
			);
		}
Scott committed
382

Scott committed
383 384 385 386 387 388 389
		if ($question['deleteable']) {
			$buttons['delete'] = array(
				'tags' => 'name="q_dodelete"' . $clicksuffix,
				'label' => qa_lang_html('question/delete_button'),
				'popup' => qa_lang_html('question/delete_q_popup'),
			);
		}
Scott committed
390

Scott committed
391 392 393 394 395 396 397
		if ($question['claimable']) {
			$buttons['claim'] = array(
				'tags' => 'name="q_doclaim"' . $clicksuffix,
				'label' => qa_lang_html('question/claim_button'),
				'popup' => qa_lang_html('question/claim_q_popup'),
			);
		}
Scott committed
398

Scott committed
399 400 401 402 403 404
		if ($question['answerbutton']) {
			// don't show if shown by default
			$buttons['answer'] = array(
				'tags' => 'name="q_doanswer" id="q_doanswer" onclick="return qa_toggle_element(\'anew\')"',
				'label' => qa_lang_html('question/answer_button'),
				'popup' => qa_lang_html('question/answer_q_popup'),
Scott committed
405 406 407
			);
		}

Scott committed
408 409 410 411 412 413 414
		if ($question['commentbutton']) {
			$buttons['comment'] = array(
				'tags' => 'name="q_docomment" onclick="return qa_toggle_element(\'c' . $questionid . '\')"',
				'label' => qa_lang_html('question/comment_button'),
				'popup' => qa_lang_html('question/comment_q_popup'),
			);
		}
Scott committed
415

Scott committed
416 417 418 419 420
		$q_view['form'] = array(
			'style' => 'light',
			'buttons' => $buttons,
		);
	}
Scott committed
421 422


Scott committed
423
	// Information about the question of the answer that this question follows on from (or a question directly)
Scott committed
424

Scott committed
425 426 427 428 429 430 431 432
	if (isset($parentquestion)) {
		$q_view['follows'] = array(
			'label' => qa_lang_html(($question['parentid'] == $parentquestion['postid']) ? 'question/follows_q' : 'question/follows_a'),
			'title' => qa_html(qa_block_words_replace($parentquestion['title'], qa_get_block_words_preg())),
			'url' => qa_q_path_html($parentquestion['postid'], $parentquestion['title'], false,
				($question['parentid'] == $parentquestion['postid']) ? 'Q' : 'A', $question['parentid']),
		);
	}
Scott committed
433 434


Scott committed
435
	// Information about the question that this question is a duplicate of (if appropriate)
Scott committed
436

437
	if (isset($closepost) || qa_post_is_closed($question)) {
438
		if (isset($closepost['basetype']) && $closepost['basetype'] == 'Q') {
Scott committed
439 440 441 442 443 444 445 446 447
			if ($closepost['hidden']) {
				// don't show link for hidden questions
				$q_view['closed'] = array(
					'state' => qa_lang_html('main/closed'),
					'label' => qa_lang_html('main/closed'),
					'content' => '',
				);
			} else {
				$q_view['closed'] = array(
Scott committed
448
					'state' => qa_lang_html('main/closed'),
Scott committed
449 450 451
					'label' => qa_lang_html('question/closed_as_duplicate'),
					'content' => qa_html(qa_block_words_replace($closepost['title'], qa_get_block_words_preg())),
					'url' => qa_q_path_html($closepost['postid'], $closepost['title']),
Scott committed
452 453 454
				);
			}

455
		} elseif (isset($closepost['type']) && $closepost['type'] == 'NOTE') {
Scott committed
456
			$viewer = qa_load_viewer($closepost['content'], $closepost['format']);
Scott committed
457

Scott committed
458 459 460 461 462 463
			$q_view['closed'] = array(
				'state' => qa_lang_html('main/closed'),
				'label' => qa_lang_html('question/closed_with_note'),
				'content' => $viewer->get_html($closepost['content'], $closepost['format'], array(
					'blockwordspreg' => qa_get_block_words_preg(),
				)),
Scott committed
464
			);
465 466 467 468 469 470
		} else { // If closed by a selected answer due to the do_close_on_select setting being enabled
			$q_view['closed'] = array(
				'state' => qa_lang_html('main/closed'),
				'label' => qa_lang_html('main/closed'),
				'content' => '',
			);
Scott committed
471
		}
Scott committed
472 473 474
	}


Scott committed
475
	// Extra value display
Scott committed
476

Scott committed
477 478 479 480 481 482
	if (strlen(@$question['extra']) && qa_opt('extra_field_active') && qa_opt('extra_field_display')) {
		$q_view['extra'] = array(
			'label' => qa_html(qa_opt('extra_field_label')),
			'content' => qa_html(qa_block_words_replace($question['extra'], qa_get_block_words_preg())),
		);
	}
Scott committed
483 484


Scott committed
485 486
	return $q_view;
}
Scott committed
487 488


Scott committed
489 490 491 492 493
/**
 * Returns an element to add to $qa_content['a_list']['as'] for $answer as viewed by $userid and $cookieid. Pass the
 * answer's $question and whether it $isselected. $usershtml should be an array which maps userids to HTML user
 * representations, including the answer's author and (if present) last editor. If a form has been explicitly requested
 * for the page, set $formrequested to true - this will hide the buttons.
494 495 496 497 498
 * @param array $question
 * @param array $answer
 * @param bool $isselected
 * @param array $usershtml
 * @param bool $formrequested
Scott committed
499 500 501 502 503 504 505
 * @return array
 */
function qa_page_q_answer_view($question, $answer, $isselected, $usershtml, $formrequested)
{
	$answerid = $answer['postid'];
	$userid = qa_get_logged_in_userid();
	$cookieid = qa_cookie_get();
Scott committed
506

Scott committed
507 508 509 510 511
	$htmloptions = qa_post_html_options($answer, null, true);
	$htmloptions['isselected'] = $isselected;
	$htmloptions['avatarsize'] = qa_opt('avatar_q_page_a_size');
	$htmloptions['q_request'] = qa_q_request($question['postid'], $question['title']);
	$a_view = qa_post_html_fields($answer, $userid, $cookieid, $usershtml, null, $htmloptions);
Scott committed
512

Scott committed
513 514
	if ($answer['queued'])
		$a_view['error'] = $answer['isbyuser'] ? qa_lang_html('question/a_your_waiting_approval') : qa_lang_html('question/a_waiting_your_approval');
Scott committed
515

Scott committed
516 517 518
	$a_view['main_form_tags'] = 'method="post" action="' . qa_self_html() . '"';
	$a_view['voting_form_hidden'] = array('code' => qa_get_form_security_code('vote'));
	$a_view['buttons_form_hidden'] = array('code' => qa_get_form_security_code('buttons-' . $answerid), 'qa_click' => '');
Scott committed
519 520


Scott committed
521
	// Selection/unselect buttons and others for operating on the answer
Scott committed
522

Scott committed
523 524 525
	if (!$formrequested) { // don't show if another form is currently being shown on page
		$prefix = 'a' . qa_html($answerid) . '_';
		$clicksuffix = ' onclick="return qa_answer_click(' . qa_js($answerid) . ', ' . qa_js($question['postid']) . ', this);"';
Scott committed
526

Scott committed
527 528 529 530 531 532
		if ($question['aselectable'] && !$answer['hidden'] && !$answer['queued']) {
			if ($isselected)
				$a_view['unselect_tags'] = 'title="' . qa_lang_html('question/unselect_popup') . '" name="' . $prefix . 'dounselect"' . $clicksuffix;
			else
				$a_view['select_tags'] = 'title="' . qa_lang_html('question/select_popup') . '" name="' . $prefix . 'doselect"' . $clicksuffix;
		}
Scott committed
533

Scott committed
534
		$buttons = array();
Scott committed
535

Scott committed
536 537 538 539 540
		if ($answer['editbutton']) {
			$buttons['edit'] = array(
				'tags' => 'name="' . $prefix . 'doedit"',
				'label' => qa_lang_html('question/edit_button'),
				'popup' => qa_lang_html('question/edit_a_popup'),
Scott committed
541 542 543
			);
		}

Scott committed
544 545 546 547 548 549 550
		if ($answer['flagbutton']) {
			$buttons['flag'] = array(
				'tags' => 'name="' . $prefix . 'doflag"' . $clicksuffix,
				'label' => qa_lang_html($answer['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
				'popup' => qa_lang_html('question/flag_a_popup'),
			);
		}
Scott committed
551

Scott committed
552 553 554 555 556 557 558
		if ($answer['unflaggable']) {
			$buttons['unflag'] = array(
				'tags' => 'name="' . $prefix . 'dounflag"' . $clicksuffix,
				'label' => qa_lang_html('question/unflag_button'),
				'popup' => qa_lang_html('question/unflag_popup'),
			);
		}
Scott committed
559

Scott committed
560 561 562 563 564 565 566
		if ($answer['clearflaggable']) {
			$buttons['clearflags'] = array(
				'tags' => 'name="' . $prefix . 'doclearflags"' . $clicksuffix,
				'label' => qa_lang_html('question/clear_flags_button'),
				'popup' => qa_lang_html('question/clear_flags_popup'),
			);
		}
Scott committed
567

Scott committed
568 569 570 571 572 573
		if ($answer['moderatable']) {
			$buttons['approve'] = array(
				'tags' => 'name="' . $prefix . 'doapprove"' . $clicksuffix,
				'label' => qa_lang_html('question/approve_button'),
				'popup' => qa_lang_html('question/approve_a_popup'),
			);
Scott committed
574

Scott committed
575 576 577 578 579 580
			$buttons['reject'] = array(
				'tags' => 'name="' . $prefix . 'doreject"' . $clicksuffix,
				'label' => qa_lang_html('question/reject_button'),
				'popup' => qa_lang_html('question/reject_a_popup'),
			);
		}
Scott committed
581

Scott committed
582 583 584 585 586 587 588
		if ($answer['hideable']) {
			$buttons['hide'] = array(
				'tags' => 'name="' . $prefix . 'dohide"' . $clicksuffix,
				'label' => qa_lang_html('question/hide_button'),
				'popup' => qa_lang_html('question/hide_a_popup'),
			);
		}
Scott committed
589

Scott committed
590 591 592 593 594 595 596
		if ($answer['reshowable']) {
			$buttons['reshow'] = array(
				'tags' => 'name="' . $prefix . 'doreshow"' . $clicksuffix,
				'label' => qa_lang_html('question/reshow_button'),
				'popup' => qa_lang_html('question/reshow_a_popup'),
			);
		}
Scott committed
597

Scott committed
598 599 600 601 602 603 604
		if ($answer['deleteable']) {
			$buttons['delete'] = array(
				'tags' => 'name="' . $prefix . 'dodelete"' . $clicksuffix,
				'label' => qa_lang_html('question/delete_button'),
				'popup' => qa_lang_html('question/delete_a_popup'),
			);
		}
Scott committed
605

Scott committed
606 607 608 609 610 611 612
		if ($answer['claimable']) {
			$buttons['claim'] = array(
				'tags' => 'name="' . $prefix . 'doclaim"' . $clicksuffix,
				'label' => qa_lang_html('question/claim_button'),
				'popup' => qa_lang_html('question/claim_a_popup'),
			);
		}
Scott committed
613

614 615 616 617 618 619 620
		// if ($answer['followable']) {
		// 	$buttons['follow'] = array(
		// 		'tags' => 'name="' . $prefix . 'dofollow"',
		// 		'label' => qa_lang_html('question/follow_button'),
		// 		'popup' => qa_lang_html('question/follow_a_popup'),
		// 	);
		// }
Scott committed
621

Scott committed
622 623 624 625 626 627 628
		if ($answer['commentbutton']) {
			$buttons['comment'] = array(
				'tags' => 'name="' . $prefix . 'docomment" onclick="return qa_toggle_element(\'c' . $answerid . '\')"',
				'label' => qa_lang_html('question/comment_button'),
				'popup' => qa_lang_html('question/comment_a_popup'),
			);
		}
Scott committed
629

Scott committed
630 631 632 633 634
		$a_view['form'] = array(
			'style' => 'light',
			'buttons' => $buttons,
		);
	}
Scott committed
635

Scott committed
636 637 638 639 640 641 642 643 644
	return $a_view;
}


/**
 * Returns an element to add to the appropriate $qa_content[...]['c_list']['cs'] array for $comment as viewed by the
 * current user. Pass the comment's $parent post and antecedent $question. $usershtml should be an array which maps
 * userids to HTML user representations, including the comments's author and (if present) last editor. If a form has
 * been explicitly requested for the page, set $formrequested to true - this will hide the buttons.
645 646 647 648 649
 * @param array $question
 * @param array $parent
 * @param array $comment
 * @param array $usershtml
 * @param bool $formrequested
Scott committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
 * @return array
 */
function qa_page_q_comment_view($question, $parent, $comment, $usershtml, $formrequested)
{
	$commentid = $comment['postid'];
	$questionid = ($parent['basetype'] == 'Q') ? $parent['postid'] : $parent['parentid'];
	$userid = qa_get_logged_in_userid();
	$cookieid = qa_cookie_get();

	$htmloptions = qa_post_html_options($comment, null, true);
	$htmloptions['avatarsize'] = qa_opt('avatar_q_page_c_size');
	$htmloptions['q_request'] = qa_q_request($question['postid'], $question['title']);
	$c_view = qa_post_html_fields($comment, $userid, $cookieid, $usershtml, null, $htmloptions);

	if ($comment['queued'])
		$c_view['error'] = $comment['isbyuser'] ? qa_lang_html('question/c_your_waiting_approval') : qa_lang_html('question/c_waiting_your_approval');

667 668 669 670
	$c_view['main_form_tags'] = 'method="post" action="' . qa_self_html() . '"';
	$c_view['voting_form_hidden'] = array('code' => qa_get_form_security_code('vote'));
	$c_view['buttons_form_hidden'] = array('code' => qa_get_form_security_code('buttons-' . $parent['postid']), 'qa_click' => '');

Scott committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

	// Buttons for operating on this comment

	if (!$formrequested) { // don't show if another form is currently being shown on page
		$prefix = 'c' . qa_html($commentid) . '_';
		$clicksuffix = ' onclick="return qa_comment_click(' . qa_js($commentid) . ', ' . qa_js($questionid) . ', ' . qa_js($parent['postid']) . ', this);"';

		$buttons = array();

		if ($comment['editbutton']) {
			$buttons['edit'] = array(
				'tags' => 'name="' . $prefix . 'doedit"',
				'label' => qa_lang_html('question/edit_button'),
				'popup' => qa_lang_html('question/edit_c_popup'),
			);
		}
Scott committed
687

Scott committed
688 689 690 691 692 693 694
		if ($comment['flagbutton']) {
			$buttons['flag'] = array(
				'tags' => 'name="' . $prefix . 'doflag"' . $clicksuffix,
				'label' => qa_lang_html($comment['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
				'popup' => qa_lang_html('question/flag_c_popup'),
			);
		}
Scott committed
695

Scott committed
696 697 698 699 700 701 702
		if ($comment['unflaggable']) {
			$buttons['unflag'] = array(
				'tags' => 'name="' . $prefix . 'dounflag"' . $clicksuffix,
				'label' => qa_lang_html('question/unflag_button'),
				'popup' => qa_lang_html('question/unflag_popup'),
			);
		}
Scott committed
703

Scott committed
704 705 706 707 708 709 710
		if ($comment['clearflaggable']) {
			$buttons['clearflags'] = array(
				'tags' => 'name="' . $prefix . 'doclearflags"' . $clicksuffix,
				'label' => qa_lang_html('question/clear_flags_button'),
				'popup' => qa_lang_html('question/clear_flags_popup'),
			);
		}
Scott committed
711

Scott committed
712 713 714 715 716 717
		if ($comment['moderatable']) {
			$buttons['approve'] = array(
				'tags' => 'name="' . $prefix . 'doapprove"' . $clicksuffix,
				'label' => qa_lang_html('question/approve_button'),
				'popup' => qa_lang_html('question/approve_c_popup'),
			);
Scott committed
718

Scott committed
719 720 721 722 723 724
			$buttons['reject'] = array(
				'tags' => 'name="' . $prefix . 'doreject"' . $clicksuffix,
				'label' => qa_lang_html('question/reject_button'),
				'popup' => qa_lang_html('question/reject_c_popup'),
			);
		}
Scott committed
725

Scott committed
726 727 728 729 730 731 732
		if ($comment['hideable']) {
			$buttons['hide'] = array(
				'tags' => 'name="' . $prefix . 'dohide"' . $clicksuffix,
				'label' => qa_lang_html('question/hide_button'),
				'popup' => qa_lang_html('question/hide_c_popup'),
			);
		}
Scott committed
733

Scott committed
734 735 736 737 738 739 740
		if ($comment['reshowable']) {
			$buttons['reshow'] = array(
				'tags' => 'name="' . $prefix . 'doreshow"' . $clicksuffix,
				'label' => qa_lang_html('question/reshow_button'),
				'popup' => qa_lang_html('question/reshow_c_popup'),
			);
		}
Scott committed
741

Scott committed
742 743 744 745 746
		if ($comment['deleteable']) {
			$buttons['delete'] = array(
				'tags' => 'name="' . $prefix . 'dodelete"' . $clicksuffix,
				'label' => qa_lang_html('question/delete_button'),
				'popup' => qa_lang_html('question/delete_c_popup'),
Scott committed
747 748 749
			);
		}

Scott committed
750 751 752 753 754 755 756
		if ($comment['claimable']) {
			$buttons['claim'] = array(
				'tags' => 'name="' . $prefix . 'doclaim"' . $clicksuffix,
				'label' => qa_lang_html('question/claim_button'),
				'popup' => qa_lang_html('question/claim_c_popup'),
			);
		}
Scott committed
757

Félicie committed
758 759 760 761 762 763 764 765
		// if ($parent['commentbutton'] && qa_opt('show_c_reply_buttons') && $comment['type'] == 'C') {
		// 	$buttons['comment'] = array(
		// 		'tags' => 'name="' . (($parent['basetype'] == 'Q') ? 'q' : ('a' . qa_html($parent['postid']))) .
		// 			'_docomment" onclick="return qa_toggle_element(\'c' . qa_html($parent['postid']) . '\')"',
		// 		'label' => qa_lang_html('question/reply_button'),
		// 		'popup' => qa_lang_html('question/reply_c_popup'),
		// 	);
		// }
Scott committed
766

Scott committed
767 768 769
		$c_view['form'] = array(
			'style' => 'light',
			'buttons' => $buttons,
Scott committed
770
		);
Scott committed
771
	}
Scott committed
772

Scott committed
773 774 775 776 777 778 779 780 781 782 783 784
	return $c_view;
}


/**
 * Return an array for $qa_content[...]['c_list'] to display all of the comments and follow-on questions in
 * $commentsfollows which belong to post $parent with antecedent $question, as viewed by the current user. If
 * $alwaysfull then all comments will be included, otherwise the list may be shortened with a 'show previous x
 * comments' link. $usershtml should be an array which maps userids to HTML user representations, including all
 * comments' and follow on questions' authors and (if present) last editors. If a form has been explicitly requested
 * for the page, set $formrequested to true and pass the postid of the post for the form in $formpostid - this will
 * hide the buttons and remove the $formpostid comment from the list.
785 786 787 788 789 790 791
 * @param array $question
 * @param array $parent
 * @param array $commentsfollows
 * @param bool $alwaysfull
 * @param array $usershtml
 * @param bool $formrequested
 * @param int $formpostid
Scott committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
 * @return array
 */
function qa_page_q_comment_follow_list($question, $parent, $commentsfollows, $alwaysfull, $usershtml, $formrequested, $formpostid)
{
	$parentid = $parent['postid'];
	$userid = qa_get_logged_in_userid();
	$cookieid = qa_cookie_get();

	$commentlist = array(
		'tags' => 'id="c' . qa_html($parentid) . '_list"',
		'cs' => array(),
	);

	$showcomments = array();

	// $commentsfollows contains ALL comments on the question and all answers, so here we filter the comments viewable for this context
	foreach ($commentsfollows as $commentfollowid => $commentfollow) {
		$showcomment = $commentfollow['parentid'] == $parentid && $commentfollow['viewable'] && $commentfollowid != $formpostid;
		// show hidden follow-on questions only if the parent is hidden
		if ($showcomment && $commentfollow['basetype'] == 'Q' && $commentfollow['hidden']) {
			$showcomment = $parent['hidden'];
813
		}
Scott committed
814 815
		// show questions closed as duplicate of this one, only if this question is hidden
		$showduplicate = $question['hidden'] && $commentfollow['closedbyid'] == $parentid;
Scott committed
816

Scott committed
817 818
		if ($showcomment || $showduplicate) {
			$showcomments[$commentfollowid] = $commentfollow;
Scott committed
819
		}
Scott committed
820
	}
Scott committed
821

Scott committed
822
	$countshowcomments = count($showcomments);
Scott committed
823

Scott committed
824 825 826 827
	if (!$alwaysfull && $countshowcomments > qa_opt('show_fewer_cs_from'))
		$skipfirst = $countshowcomments - qa_opt('show_fewer_cs_count');
	else
		$skipfirst = 0;
Scott committed
828

Scott committed
829 830 831 832 833
	if ($skipfirst == $countshowcomments) { // showing none
		if ($skipfirst == 1)
			$expandtitle = qa_lang_html('question/show_1_comment');
		else
			$expandtitle = qa_lang_html_sub('question/show_x_comments', $skipfirst);
Scott committed
834

Scott committed
835 836 837 838 839 840
	} else {
		if ($skipfirst == 1)
			$expandtitle = qa_lang_html('question/show_1_previous_comment');
		else
			$expandtitle = qa_lang_html_sub('question/show_x_previous_comments', $skipfirst);
	}
Scott committed
841

Scott committed
842 843 844
	if ($skipfirst > 0) {
		$commentlist['cs'][$parentid] = array(
			'url' => qa_html('?state=showcomments-' . $parentid . '&show=' . $parentid . '#' . urlencode(qa_anchor($parent['basetype'], $parentid))),
Scott committed
845

Scott committed
846
			'expand_tags' => 'onclick="return qa_show_comments(' . qa_js($question['postid']) . ', ' . qa_js($parentid) . ', this);"',
Scott committed
847

Scott committed
848 849
			'title' => $expandtitle,
		);
Scott committed
850 851
	}

Scott committed
852 853 854 855 856 857 858 859
	foreach ($showcomments as $commentfollowid => $commentfollow) {
		if ($skipfirst > 0) {
			$skipfirst--;
		} elseif ($commentfollow['basetype'] == 'C') {
			$commentlist['cs'][$commentfollowid] = qa_page_q_comment_view($question, $parent, $commentfollow, $usershtml, $formrequested);
		} elseif ($commentfollow['basetype'] == 'Q') {
			$htmloptions = qa_post_html_options($commentfollow);
			$htmloptions['avatarsize'] = qa_opt('avatar_q_page_c_size');
860
			$htmloptions['voteview'] = false;
Scott committed
861

Scott committed
862 863 864
			$commentlist['cs'][$commentfollowid] = qa_post_html_fields($commentfollow, $userid, $cookieid, $usershtml, null, $htmloptions);
		}
	}
Scott committed
865

866
	if (empty($commentlist['cs']))
Scott committed
867 868 869 870 871 872 873 874 875 876 877
		$commentlist['hidden'] = true;

	return $commentlist;
}


/**
 * Return a $qa_content form for adding an answer to $question. Pass an HTML element id to use for the form in $formid
 * and the result of qa_user_captcha_reason() in $captchareason. Pass previous inputs from a submitted version of this
 * form in the array $in and resulting errors in $errors. If $loadnow is true, the form will be loaded immediately. Set
 * $formrequested to true if the user explicitly requested it, as opposed being shown automatically.
878 879 880 881 882 883 884 885
 * @param array $qa_content
 * @param string $formid
 * @param string $captchareason
 * @param array $question
 * @param array $in
 * @param array $errors
 * @param bool $loadnow
 * @param bool $formrequested
Scott committed
886 887 888 889 890 891 892 893 894 895 896 897 898
 * @return array
 */
function qa_page_q_add_a_form(&$qa_content, $formid, $captchareason, $question, $in, $errors, $loadnow, $formrequested)
{
	// The 'approve', 'login', 'confirm', 'limit', 'userblock', 'ipblock' permission errors are reported to the user here
	// The other option ('level') prevents the answer button being shown, in qa_page_q_post_rules(...)

	switch (qa_user_post_permit_error('permit_post_a', $question, QA_LIMIT_ANSWERS)) {
		case 'login':
			$form = array(
				'title' => qa_insert_login_links(qa_lang_html('question/answer_must_login'), qa_request()),
			);
			break;
Scott committed
899

Scott committed
900 901 902 903 904
		case 'confirm':
			$form = array(
				'title' => qa_insert_login_links(qa_lang_html('question/answer_must_confirm'), qa_request()),
			);
			break;
Scott committed
905

Scott committed
906 907
		case 'approve':
			$form = array(
908 909 910 911
				'title' => strtr(qa_lang_html('question/answer_must_be_approved'), array(
					'^1' => '<a href="' . qa_path_html('account') . '">',
					'^2' => '</a>',
				)),
Scott committed
912 913
			);
			break;
Scott committed
914

Scott committed
915 916 917 918 919
		case 'limit':
			$form = array(
				'title' => qa_lang_html('question/answer_limit'),
			);
			break;
Scott committed
920

Scott committed
921 922 923 924 925
		default:
			$form = array(
				'title' => qa_lang_html('users/no_permission'),
			);
			break;
Scott committed
926

Scott committed
927 928 929
		case false:
			$editorname = isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_as');
			$editor = qa_load_editor(@$in['content'], @$in['format'], $editorname);
Scott committed
930

Scott committed
931 932 933 934
			if (method_exists($editor, 'update_script'))
				$updatescript = $editor->update_script('a_content');
			else
				$updatescript = '';
Scott committed
935

Scott committed
936
			$custom = qa_opt('show_custom_answer') ? trim(qa_opt('custom_answer')) : '';
Scott committed
937

Scott committed
938 939
			$form = array(
				'tags' => 'method="post" action="' . qa_self_html() . '" name="a_form"',
Scott committed
940

Scott committed
941
				'title' => qa_lang_html('question/your_answer_title'),
Scott committed
942

Scott committed
943 944 945 946
				'fields' => array(
					'custom' => array(
						'type' => 'custom',
						'note' => $custom,
Scott committed
947 948
					),

Scott committed
949 950 951 952 953
					'content' => array_merge(
						qa_editor_load_field($editor, $qa_content, @$in['content'], @$in['format'], 'a_content', 12, $formrequested, $loadnow),
						array(
							'error' => qa_html(@$errors['content']),
						)
Scott committed
954
					),
Scott committed
955
				),
Scott committed
956

Scott committed
957 958 959 960 961 962
				'buttons' => array(
					'answer' => array(
						'tags' => 'onclick="' . $updatescript . ' return qa_submit_answer(' . qa_js($question['postid']) . ', this);"',
						'label' => qa_lang_html('question/add_answer_button'),
					),
				),
Scott committed
963

Scott committed
964 965 966 967 968 969
				'hidden' => array(
					'a_editor' => qa_html($editorname),
					'a_doadd' => '1',
					'code' => qa_get_form_security_code('answer-' . $question['postid']),
				),
			);
Scott committed
970

Scott committed
971 972
			if (!strlen($custom))
				unset($form['fields']['custom']);
Scott committed
973

Scott committed
974 975 976 977 978
			if ($formrequested || !$loadnow)
				$form['buttons']['cancel'] = array(
					'tags' => 'name="docancel"',
					'label' => qa_lang_html('main/cancel_button'),
				);
Scott committed
979

980
			if (!qa_is_logged_in() && qa_opt('allow_anonymous_naming'))
Scott committed
981
				qa_set_up_name_field($qa_content, $form['fields'], @$in['name'], 'a_');
Scott committed
982

Félicie committed
983 984
			// qa_set_up_notify_fields($qa_content, $form['fields'], 'A', qa_get_logged_in_email(),
			// 	isset($in['notify']) ? $in['notify'] : qa_opt('notify_users_default'), @$in['email'], @$errors['email'], 'a_');
Scott committed
985

Scott committed
986
			$onloads = array();
Scott committed
987

Scott committed
988 989
			if ($captchareason) {
				$captchaloadscript = qa_set_up_captcha_field($qa_content, $form['fields'], $errors, qa_captcha_reason_note($captchareason));
Scott committed
990

Scott committed
991
				if (strlen($captchaloadscript))
Scott committed
992
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_show = function() { ' . $captchaloadscript . ' };';
Scott committed
993
			}
Scott committed
994

Scott committed
995 996
			if (!$loadnow) {
				if (method_exists($editor, 'load_script'))
Scott committed
997
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_load = function() { ' . $editor->load_script('a_content') . ' };';
Scott committed
998

Scott committed
999 1000
				$form['buttons']['cancel']['tags'] .= ' onclick="return qa_toggle_element();"';
			}
Scott committed
1001

Scott committed
1002 1003
			if (!$formrequested) {
				if (method_exists($editor, 'focus_script'))
Scott committed
1004
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_focus = function() { ' . $editor->focus_script('a_content') . ' };';
Scott committed
1005
			}
Scott committed
1006

Scott committed
1007
			if (count($onloads)) {
Scott committed
1008
				$qa_content['script_onloads'][] = $onloads;
Scott committed
1009 1010
			}

Scott committed
1011
			break;
Scott committed
1012 1013
	}

Scott committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
	$form['id'] = $formid;
	$form['collapse'] = !$loadnow;
	$form['style'] = 'tall';

	return $form;
}


/**
 * Returns a $qa_content form for adding a comment to post $parent which is part of $question. Pass an HTML element id
 * to use for the form in $formid and the result of qa_user_captcha_reason() in $captchareason. Pass previous inputs
 * from a submitted version of this form in the array $in and resulting errors in $errors. If $loadfocusnow is true,
 * the form will be loaded and focused immediately.
1027 1028 1029 1030 1031 1032 1033 1034
 * @param array $qa_content
 * @param array $question
 * @param array $parent
 * @param string $formid
 * @param string $captchareason
 * @param array $in
 * @param array $errors
 * @param bool $loadfocusnow
Scott committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
 * @return array
 */
function qa_page_q_add_c_form(&$qa_content, $question, $parent, $formid, $captchareason, $in, $errors, $loadfocusnow)
{
	// The 'approve', 'login', 'confirm', 'userblock', 'ipblock' permission errors are reported to the user here
	// The other option ('level') prevents the comment button being shown, in qa_page_q_post_rules(...)

	switch (qa_user_post_permit_error('permit_post_c', $parent, QA_LIMIT_COMMENTS)) {
		case 'login':
			$form = array(
				'title' => qa_insert_login_links(qa_lang_html('question/comment_must_login'), qa_request()),
			);
			break;
Scott committed
1048

Scott committed
1049 1050 1051 1052 1053
		case 'confirm':
			$form = array(
				'title' => qa_insert_login_links(qa_lang_html('question/comment_must_confirm'), qa_request()),
			);
			break;
Scott committed
1054

Scott committed
1055 1056
		case 'approve':
			$form = array(
1057 1058 1059 1060
				'title' => strtr(qa_lang_html('question/comment_must_be_approved'), array(
					'^1' => '<a href="' . qa_path_html('account') . '">',
					'^2' => '</a>',
				)),
Scott committed
1061 1062
			);
			break;
Scott committed
1063

Scott committed
1064 1065 1066 1067 1068
		case 'limit':
			$form = array(
				'title' => qa_lang_html('question/comment_limit'),
			);
			break;
Scott committed
1069

Scott committed
1070 1071 1072 1073 1074
		default:
			$form = array(
				'title' => qa_lang_html('users/no_permission'),
			);
			break;
Scott committed
1075

Scott committed
1076 1077
		case false:
			$prefix = 'c' . $parent['postid'] . '_';
Scott committed
1078

Scott committed
1079 1080
			$editorname = isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_cs');
			$editor = qa_load_editor(@$in['content'], @$in['format'], $editorname);
Scott committed
1081

Scott committed
1082 1083 1084 1085
			if (method_exists($editor, 'update_script'))
				$updatescript = $editor->update_script($prefix . 'content');
			else
				$updatescript = '';
Scott committed
1086

Scott committed
1087
			$custom = qa_opt('show_custom_comment') ? trim(qa_opt('custom_comment')) : '';
Scott committed
1088

Scott committed
1089 1090
			$form = array(
				'tags' => 'method="post" action="' . qa_self_html() . '" name="c_form_' . qa_html($parent['postid']) . '"',
Scott committed
1091

Scott committed
1092
				'title' => qa_lang_html(($question['postid'] == $parent['postid']) ? 'question/your_comment_q' : 'question/your_comment_a'),
Scott committed
1093

Scott committed
1094 1095 1096 1097
				'fields' => array(
					'custom' => array(
						'type' => 'custom',
						'note' => $custom,
Scott committed
1098 1099
					),

Scott committed
1100 1101 1102 1103 1104
					'content' => array_merge(
						qa_editor_load_field($editor, $qa_content, @$in['content'], @$in['format'], $prefix . 'content', 4, $loadfocusnow, $loadfocusnow),
						array(
							'error' => qa_html(@$errors['content']),
						)
Scott committed
1105
					),
Scott committed
1106
				),
Scott committed
1107

Scott committed
1108 1109 1110 1111
				'buttons' => array(
					'comment' => array(
						'tags' => 'onclick="' . $updatescript . ' return qa_submit_comment(' . qa_js($question['postid']) . ', ' . qa_js($parent['postid']) . ', this);"',
						'label' => qa_lang_html('question/add_comment_button'),
Scott committed
1112 1113
					),

Scott committed
1114 1115 1116 1117 1118
					'cancel' => array(
						'tags' => 'name="docancel"',
						'label' => qa_lang_html('main/cancel_button'),
					),
				),
Scott committed
1119

Scott committed
1120 1121 1122 1123 1124 1125
				'hidden' => array(
					$prefix . 'editor' => qa_html($editorname),
					$prefix . 'doadd' => '1',
					$prefix . 'code' => qa_get_form_security_code('comment-' . $parent['postid']),
				),
			);
Scott committed
1126

Scott committed
1127 1128
			if (!strlen($custom))
				unset($form['fields']['custom']);
Scott committed
1129

1130
			if (!qa_is_logged_in() && qa_opt('allow_anonymous_naming'))
Scott committed
1131
				qa_set_up_name_field($qa_content, $form['fields'], @$in['name'], $prefix);
Scott committed
1132

Félicie committed
1133 1134
			// qa_set_up_notify_fields($qa_content, $form['fields'], 'C', qa_get_logged_in_email(),
			// 	isset($in['notify']) ? $in['notify'] : qa_opt('notify_users_default'), @$in['email'], @$errors['email'], $prefix);
Scott committed
1135

Scott committed
1136
			$onloads = array();
Scott committed
1137

Scott committed
1138 1139
			if ($captchareason) {
				$captchaloadscript = qa_set_up_captcha_field($qa_content, $form['fields'], $errors, qa_captcha_reason_note($captchareason));
Scott committed
1140

Scott committed
1141
				if (strlen($captchaloadscript))
Scott committed
1142
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_show = function() { ' . $captchaloadscript . ' };';
Scott committed
1143
			}
Scott committed
1144

Scott committed
1145 1146
			if (!$loadfocusnow) {
				if (method_exists($editor, 'load_script'))
Scott committed
1147
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_load = function() { ' . $editor->load_script($prefix . 'content') . ' };';
Scott committed
1148
				if (method_exists($editor, 'focus_script'))
Scott committed
1149
					$onloads[] = 'document.getElementById(' . qa_js($formid) . ').qa_focus = function() { ' . $editor->focus_script($prefix . 'content') . ' };';
Scott committed
1150

Scott committed
1151 1152
				$form['buttons']['cancel']['tags'] .= ' onclick="return qa_toggle_element()"';
			}
Scott committed
1153

Scott committed
1154
			if (count($onloads)) {
Scott committed
1155
				$qa_content['script_onloads'][] = $onloads;
Scott committed
1156 1157 1158
			}

			break;
Scott committed
1159 1160
	}

Scott committed
1161 1162 1163
	$form['id'] = $formid;
	$form['collapse'] = !$loadfocusnow;
	$form['style'] = 'tall';
Scott committed
1164

Scott committed
1165 1166
	return $form;
}