qa-page-question-view.php 38.3 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

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

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	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;
	}


	function qa_page_q_load_as($question, $childposts)
/*
	Given a $question and its $childposts from the database, return a list of that question's answers
*/
	{
		$answers=array();
Scott Vivian committed
39

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

Gideon Greenspan committed
49 50
		return $answers;
	}
Scott Vivian committed
51 52


Gideon Greenspan committed
53 54 55 56 57 58 59
	function qa_page_q_load_c_follows($question, $childposts, $achildposts)
/*
	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
*/
	{
		$commentsfollows=array();
Scott Vivian committed
60

Gideon Greenspan committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		foreach ($childposts as $postid => $post)
			switch ($post['type']) {
				case 'Q': // never show follow-on Qs which have been hidden, even to admins
				case 'C':
				case 'C_HIDDEN':
				case 'C_QUEUED':
					$commentsfollows[$postid]=$post;
					break;
			}

		foreach ($achildposts as $postid => $post)
			switch ($post['type']) {
				case 'Q': // never show follow-on Qs which have been hidden, even to admins
				case 'C':
				case 'C_HIDDEN':
				case 'C_QUEUED':
					$commentsfollows[$postid]=$post;
					break;
			}
Scott Vivian committed
80

Gideon Greenspan committed
81 82 83 84 85 86 87 88 89 90 91 92 93
		return $commentsfollows;
	}


	function qa_page_q_post_rules($post, $parentpost=null, $siblingposts=null, $childposts=null)
/*
	Returns elements that can be added to $post which describe which operations the current user may perform on that
	post. This function is a key part of Q2A's logic and is ripe for overriding by plugins. Pass $post's $parentpost if
	there is one, or null otherwise. Pass an array which contains $post's siblings (i.e. other posts with the same type
	and parent) in $siblingposts and $post's children in $childposts. Both of these latter arrays can contain additional
	posts retrieved from the database, and these will be ignored.
*/
	{
Gideon Greenspan committed
94
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
95

Gideon Greenspan committed
96 97
		$userid=qa_get_logged_in_userid();
		$cookieid=qa_cookie_get();
Gideon Greenspan committed
98
		$userlevel=qa_user_level_for_post($post);
Scott Vivian committed
99

Gideon Greenspan committed
100 101 102 103 104
		$rules['isbyuser']=qa_post_is_by_user($post, $userid, $cookieid);
		$rules['queued']=(substr($post['type'], 1)=='_QUEUED');
		$rules['closed']=($post['basetype']=='Q') && (isset($post['closedbyid']) || (isset($post['selchildid']) && qa_opt('do_close_on_select')));

	//	Cache some responses to the user permission checks
Scott Vivian committed
105

Gideon Greenspan committed
106 107 108
		$permiterror_post_q=qa_user_permit_error('permit_post_q', null, $userlevel); // don't check limits here, so we can show error message
		$permiterror_post_a=qa_user_permit_error('permit_post_a', null, $userlevel);
		$permiterror_post_c=qa_user_permit_error('permit_post_c', null, $userlevel);
Gideon Greenspan committed
109 110

		$permiterror_edit=qa_user_permit_error(($post['basetype']=='Q') ? 'permit_edit_q' :
Gideon Greenspan committed
111 112 113 114 115 116
			(($post['basetype']=='A') ? 'permit_edit_a' : 'permit_edit_c'), null, $userlevel);
		$permiterror_retagcat=qa_user_permit_error('permit_retag_cat', null, $userlevel);
		$permiterror_flag=qa_user_permit_error('permit_flag', null, $userlevel);
		$permiterror_hide_show=qa_user_permit_error($rules['isbyuser'] ? null : 'permit_hide_show', null, $userlevel);
		$permiterror_close_open=qa_user_permit_error($rules['isbyuser'] ? null : 'permit_close_q', null, $userlevel);
		$permiterror_moderate=qa_user_permit_error('permit_moderate', null, $userlevel);
Scott Vivian committed
117

Gideon Greenspan committed
118
	//	General permissions
Scott Vivian committed
119

Gideon Greenspan committed
120 121
		$rules['authorlast']=((!isset($post['lastuserid'])) || ($post['lastuserid']===$post['userid']));
		$rules['viewable']=$post['hidden'] ? (!$permiterror_hide_show) : ($rules['queued'] ? ($rules['isbyuser'] || !$permiterror_moderate) : true);
Scott Vivian committed
122

Gideon Greenspan committed
123
	//	Answer, comment and edit might show the button even if the user still needs to do something (e.g. log in)
Scott Vivian committed
124

Gideon Greenspan committed
125 126 127 128
		$rules['answerbutton']=($post['type']=='Q') && ($permiterror_post_a!='level') && (!$rules['closed']) &&
			(qa_opt('allow_self_answer') || !$rules['isbyuser']);

		$rules['commentbutton']=(($post['type']=='Q') || ($post['type']=='A')) &&
Gideon Greenspan committed
129
			($permiterror_post_c!='level') && qa_opt(($post['type']=='Q') ? 'comment_on_qs' : 'comment_on_as');
Gideon Greenspan committed
130 131
		$rules['commentable']=$rules['commentbutton'] && !$permiterror_post_c;

Scott Vivian committed
132
		$rules['editbutton']=(!$post['hidden']) && (!$rules['closed']) &&
Gideon Greenspan committed
133
			($rules['isbyuser'] || (($permiterror_edit!='level') && ($permiterror_edit!='approve') && (!$rules['queued'])));
Gideon Greenspan committed
134
		$rules['editable']=$rules['editbutton'] && ($rules['isbyuser'] || !$permiterror_edit);
Scott Vivian committed
135 136

		$rules['retagcatbutton']=($post['basetype']=='Q') && (qa_using_tags() || qa_using_categories()) &&
Gideon Greenspan committed
137
			(!$post['hidden']) && ($rules['isbyuser'] || (($permiterror_retagcat!='level') && ($permiterror_retagcat!='approve')) );
Gideon Greenspan committed
138
		$rules['retagcatable']=$rules['retagcatbutton'] && ($rules['isbyuser'] || !$permiterror_retagcat);
Scott Vivian committed
139

Gideon Greenspan committed
140 141 142 143 144 145
		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 Vivian committed
146

Gideon Greenspan committed
147
		$rules['aselectable']=($post['type']=='Q') && !qa_user_permit_error($rules['isbyuser'] ? null : 'permit_select_a', null, $userlevel);
Gideon Greenspan committed
148 149

		$rules['flagbutton']=qa_opt('flagging_of_posts') && (!$rules['isbyuser']) && (!$post['hidden']) && (!$rules['queued']) &&
Gideon Greenspan committed
150 151
			(!@$post['userflag']) && ($permiterror_flag!='level') && ($permiterror_flag!='approve');
		$rules['flagtohide']=$rules['flagbutton'] && (!$permiterror_flag) && (($post['flagcount']+1)>=qa_opt('flagging_hide_after'));
Gideon Greenspan committed
152
		$rules['unflaggable']=@$post['userflag'] && (!$post['hidden']);
Gideon Greenspan committed
153
		$rules['clearflaggable']=($post['flagcount']>=(@$post['userflag'] ? 2 : 1)) && !qa_user_permit_error('permit_hide_show', null, $userlevel);
Scott Vivian committed
154

Gideon Greenspan committed
155
	//	Other actions only show the button if it's immediately possible
Scott Vivian committed
156

Gideon Greenspan committed
157 158
		$notclosedbyother=!($rules['closed'] && isset($post['closedbyid']) && !$rules['authorlast']);
		$nothiddenbyother=!($post['hidden'] && !$rules['authorlast']);
Scott Vivian committed
159

Gideon Greenspan committed
160 161
		$rules['closeable']=qa_opt('allow_close_questions') && ($post['type']=='Q') && (!$rules['closed']) && !$permiterror_close_open;
		$rules['reopenable']=$rules['closed'] && isset($post['closedbyid']) && (!$permiterror_close_open) && (!$post['hidden']) &&
Gideon Greenspan committed
162
			($notclosedbyother || !qa_user_permit_error('permit_close_q', null, $userlevel));
Gideon Greenspan committed
163 164 165
			// 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
		$rules['moderatable']=$rules['queued'] && !$permiterror_moderate;
		$rules['hideable']=(!$post['hidden']) && ($rules['isbyuser'] || !$rules['queued']) &&
Gideon Greenspan committed
166
			(!$permiterror_hide_show) && ($notclosedbyother || !qa_user_permit_error('permit_hide_show', null, $userlevel));
Gideon Greenspan committed
167
			// cannot hide a question if it was closed by someone else and you don't have global hiding permissions
Gideon Greenspan committed
168 169 170 171 172 173
		$rules['reshowimmed']=$post['hidden'] && !qa_user_permit_error('permit_hide_show', null, $userlevel);
			// means post can be reshown immediately without checking whether it needs moderation
		$rules['reshowable']=$post['hidden'] && (!$permiterror_hide_show) &&
			($rules['reshowimmed'] || ($nothiddenbyother && !$post['flagcount']));
			// cannot reshow a question if it was hidden by someone else, or if it has flags - unless you have global hide/show permissions
		$rules['deleteable']=$post['hidden'] && !qa_user_permit_error('permit_delete_hidden', null, $userlevel);
Gideon Greenspan committed
174
		$rules['claimable']=(!isset($post['userid'])) && isset($userid) && strlen(@$post['cookieid']) && (strcmp(@$post['cookieid'], $cookieid)==0) &&
Gideon Greenspan committed
175 176
			!(($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;
Scott Vivian committed
177

Gideon Greenspan committed
178
	//	Check for claims that could break rules about self answering and multiple answers
Scott Vivian committed
179 180

		if ($rules['claimable'] && ($post['basetype']=='A')) {
Gideon Greenspan committed
181 182
			if ( (!qa_opt('allow_self_answer')) && isset($parentpost) && qa_post_is_by_user($parentpost, $userid, $cookieid) )
				$rules['claimable']=false;
Scott Vivian committed
183

Gideon Greenspan committed
184 185 186 187 188
			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 Vivian committed
189

Gideon Greenspan committed
190
	//	Now make any changes based on the child posts
Scott Vivian committed
191

Gideon Greenspan committed
192 193
		if (isset($childposts))
			foreach ($childposts as $childpost)
Gideon Greenspan committed
194
				if ($childpost['parentid']==$post['postid']) {
Gideon Greenspan committed
195
					$rules['deleteable']=false;
Scott Vivian committed
196

Gideon Greenspan committed
197 198 199
					if (($childpost['basetype']=='A') && qa_post_is_by_user($childpost, $userid, $cookieid)) {
						if (!qa_opt('allow_multi_answers'))
							$rules['answerbutton']=false;
Scott Vivian committed
200

Gideon Greenspan committed
201 202 203 204
						if (!qa_opt('allow_self_answer'))
							$rules['claimable']=false;
					}
				}
Scott Vivian committed
205

Gideon Greenspan committed
206
	//	Return the resulting rules
Scott Vivian committed
207

Gideon Greenspan committed
208 209
		return $rules;
	}
Scott Vivian committed
210 211


Gideon Greenspan committed
212 213 214 215 216 217 218 219 220 221 222 223
	function qa_page_q_question_view($question, $parentquestion, $closepost, $usershtml, $formrequested)
/*
	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.
*/
	{
		$questionid=$question['postid'];
		$userid=qa_get_logged_in_userid();
		$cookieid=qa_cookie_get();
Scott Vivian committed
224

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


Gideon Greenspan committed
232
		$q_view['main_form_tags']='method="post" action="'.qa_self_html().'"';
Gideon Greenspan committed
233 234
		$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' => '');
Scott Vivian committed
235

Gideon Greenspan committed
236 237

	//	Buttons for operating on the question
Scott Vivian committed
238

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

Gideon Greenspan committed
243 244
			if ($question['editbutton'])
				$buttons['edit']=array(
Gideon Greenspan committed
245
					'tags' => 'name="q_doedit"',
Gideon Greenspan committed
246 247 248
					'label' => qa_lang_html('question/edit_button'),
					'popup' => qa_lang_html('question/edit_q_popup'),
				);
Scott Vivian committed
249

Gideon Greenspan committed
250
			$hascategories=qa_using_categories();
Scott Vivian committed
251

Gideon Greenspan committed
252 253
			if ($question['retagcatbutton'])
				$buttons['retagcat']=array(
Gideon Greenspan committed
254
					'tags' => 'name="q_doedit"',
Gideon Greenspan committed
255 256 257 258 259 260
					'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 Vivian committed
261

Gideon Greenspan committed
262 263
			if ($question['flagbutton'])
				$buttons['flag']=array(
Gideon Greenspan committed
264
					'tags' => 'name="q_doflag"'.$clicksuffix,
Gideon Greenspan committed
265 266 267 268 269 270
					'label' => qa_lang_html($question['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
					'popup' => qa_lang_html('question/flag_q_popup'),
				);

			if ($question['unflaggable'])
				$buttons['unflag']=array(
Gideon Greenspan committed
271
					'tags' => 'name="q_dounflag"'.$clicksuffix,
Gideon Greenspan committed
272 273 274
					'label' => qa_lang_html('question/unflag_button'),
					'popup' => qa_lang_html('question/unflag_popup'),
				);
Scott Vivian committed
275

Gideon Greenspan committed
276 277
			if ($question['clearflaggable'])
				$buttons['clearflags']=array(
Gideon Greenspan committed
278
					'tags' => 'name="q_doclearflags"'.$clicksuffix,
Gideon Greenspan committed
279 280 281 282 283 284
					'label' => qa_lang_html('question/clear_flags_button'),
					'popup' => qa_lang_html('question/clear_flags_popup'),
				);

			if ($question['closeable'])
				$buttons['close']=array(
Gideon Greenspan committed
285
					'tags' => 'name="q_doclose"',
Gideon Greenspan committed
286 287 288
					'label' => qa_lang_html('question/close_button'),
					'popup' => qa_lang_html('question/close_q_popup'),
				);
Scott Vivian committed
289

Gideon Greenspan committed
290 291
			if ($question['reopenable'])
				$buttons['reopen']=array(
Gideon Greenspan committed
292
					'tags' => 'name="q_doreopen"'.$clicksuffix,
Gideon Greenspan committed
293 294
					'label' => qa_lang_html('question/reopen_button'),
				);
Scott Vivian committed
295

Gideon Greenspan committed
296 297
			if ($question['moderatable']) {
				$buttons['approve']=array(
Gideon Greenspan committed
298
					'tags' => 'name="q_doapprove"'.$clicksuffix,
Gideon Greenspan committed
299 300 301 302
					'label' => qa_lang_html('question/approve_button'),
				);

				$buttons['reject']=array(
Gideon Greenspan committed
303
					'tags' => 'name="q_doreject"'.$clicksuffix,
Gideon Greenspan committed
304 305 306
					'label' => qa_lang_html('question/reject_button'),
				);
			}
Scott Vivian committed
307

Gideon Greenspan committed
308 309
			if ($question['hideable'])
				$buttons['hide']=array(
Gideon Greenspan committed
310
					'tags' => 'name="q_dohide"'.$clicksuffix,
Gideon Greenspan committed
311 312 313
					'label' => qa_lang_html('question/hide_button'),
					'popup' => qa_lang_html('question/hide_q_popup'),
				);
Scott Vivian committed
314

Gideon Greenspan committed
315 316
			if ($question['reshowable'])
				$buttons['reshow']=array(
Gideon Greenspan committed
317
					'tags' => 'name="q_doreshow"'.$clicksuffix,
Gideon Greenspan committed
318
					'label' => qa_lang_html('question/reshow_button'),
319
					'popup' => qa_lang_html('question/reshow_q_popup'),
Gideon Greenspan committed
320
				);
Scott Vivian committed
321

Gideon Greenspan committed
322 323
			if ($question['deleteable'])
				$buttons['delete']=array(
Gideon Greenspan committed
324
					'tags' => 'name="q_dodelete"'.$clicksuffix,
Gideon Greenspan committed
325 326 327
					'label' => qa_lang_html('question/delete_button'),
					'popup' => qa_lang_html('question/delete_q_popup'),
				);
Scott Vivian committed
328

Gideon Greenspan committed
329 330
			if ($question['claimable'])
				$buttons['claim']=array(
Gideon Greenspan committed
331
					'tags' => 'name="q_doclaim"'.$clicksuffix,
Gideon Greenspan committed
332 333
					'label' => qa_lang_html('question/claim_button'),
				);
Scott Vivian committed
334

Gideon Greenspan committed
335 336
			if ($question['answerbutton']) // don't show if shown by default
				$buttons['answer']=array(
Gideon Greenspan committed
337
					'tags' => 'name="q_doanswer" id="q_doanswer" onclick="return qa_toggle_element(\'anew\')"',
Gideon Greenspan committed
338 339 340
					'label' => qa_lang_html('question/answer_button'),
					'popup' => qa_lang_html('question/answer_q_popup'),
				);
Scott Vivian committed
341

Gideon Greenspan committed
342 343
			if ($question['commentbutton'])
				$buttons['comment']=array(
Gideon Greenspan committed
344
					'tags' => 'name="q_docomment" onclick="return qa_toggle_element(\'c'.$questionid.'\')"',
Gideon Greenspan committed
345 346 347
					'label' => qa_lang_html('question/comment_button'),
					'popup' => qa_lang_html('question/comment_q_popup'),
				);
Scott Vivian committed
348

Gideon Greenspan committed
349 350 351 352 353
			$q_view['form']=array(
				'style' => 'light',
				'buttons' => $buttons,
			);
		}
Scott Vivian committed
354

Gideon Greenspan committed
355 356

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

Gideon Greenspan committed
358 359 360 361 362 363 364
		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 Vivian committed
365 366


Gideon Greenspan committed
367
	//	Information about the question that this question is a duplicate of (if appropriate)
Scott Vivian committed
368

Gideon Greenspan committed
369
		if (isset($closepost)) {
Scott Vivian committed
370

Gideon Greenspan committed
371 372
			if ($closepost['basetype']=='Q') {
				$q_view['closed']=array(
373
					'state' => qa_lang_html('main/closed'),
Gideon Greenspan committed
374 375 376 377 378 379 380
					'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']),
				);

			} elseif ($closepost['type']=='NOTE') {
				$viewer=qa_load_viewer($closepost['content'], $closepost['format']);
Scott Vivian committed
381

Gideon Greenspan committed
382
				$q_view['closed']=array(
383
					'state' => qa_lang_html('main/closed'),
Gideon Greenspan committed
384 385 386 387 388 389 390
					'label' => qa_lang_html('question/closed_with_note'),
					'content' => $viewer->get_html($closepost['content'], $closepost['format'], array(
						'blockwordspreg' => qa_get_block_words_preg(),
					)),
				);
			}
		}
Scott Vivian committed
391

Gideon Greenspan committed
392 393

	//	Extra value display
Scott Vivian committed
394

Gideon Greenspan committed
395 396 397 398 399 400
		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 Vivian committed
401

Gideon Greenspan committed
402 403
		return $q_view;
	}
Scott Vivian committed
404 405


Gideon Greenspan committed
406 407 408 409 410 411 412 413 414 415 416
	function qa_page_q_answer_view($question, $answer, $isselected, $usershtml, $formrequested)
/*
	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.
*/
	{
		$answerid=$answer['postid'];
		$userid=qa_get_logged_in_userid();
		$cookieid=qa_cookie_get();
Scott Vivian committed
417

Gideon Greenspan committed
418
		$htmloptions=qa_post_html_options($answer, null, true);
Gideon Greenspan committed
419 420
		$htmloptions['isselected']=$isselected;
		$htmloptions['avatarsize']=qa_opt('avatar_q_page_a_size');
Gideon Greenspan committed
421
		$htmloptions['q_request']=qa_q_request($question['postid'], $question['title']);
Gideon Greenspan committed
422 423 424 425
		$a_view=qa_post_html_fields($answer, $userid, $cookieid, $usershtml, null, $htmloptions);

		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 Vivian committed
426

Gideon Greenspan committed
427
		$a_view['main_form_tags']='method="post" action="'.qa_self_html().'"';
Gideon Greenspan committed
428 429
		$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' => '');
Gideon Greenspan committed
430 431 432 433 434 435 436


	//	Selection/unselect buttons and others for operating on the answer

		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 Vivian committed
437

Gideon Greenspan committed
438 439
			if ($question['aselectable'] && !$answer['hidden'] && !$answer['queued']) {
				if ($isselected)
Gideon Greenspan committed
440
					$a_view['unselect_tags']='title="'.qa_lang_html('question/unselect_popup').'" name="'.$prefix.'dounselect"'.$clicksuffix;
Gideon Greenspan committed
441
				else
Gideon Greenspan committed
442
					$a_view['select_tags']='title="'.qa_lang_html('question/select_popup').'" name="'.$prefix.'doselect"'.$clicksuffix;
Gideon Greenspan committed
443
			}
Scott Vivian committed
444

Gideon Greenspan committed
445
			$buttons=array();
Scott Vivian committed
446

Gideon Greenspan committed
447 448
			if ($answer['editbutton'])
				$buttons['edit']=array(
Gideon Greenspan committed
449
					'tags' => 'name="'.$prefix.'doedit"',
Gideon Greenspan committed
450 451 452
					'label' => qa_lang_html('question/edit_button'),
					'popup' => qa_lang_html('question/edit_a_popup'),
				);
Scott Vivian committed
453

Gideon Greenspan committed
454 455
			if ($answer['flagbutton'])
				$buttons['flag']=array(
Gideon Greenspan committed
456
					'tags' => 'name="'.$prefix.'doflag"'.$clicksuffix,
Gideon Greenspan committed
457 458 459 460 461 462
					'label' => qa_lang_html($answer['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
					'popup' => qa_lang_html('question/flag_a_popup'),
				);

			if ($answer['unflaggable'])
				$buttons['unflag']=array(
Gideon Greenspan committed
463
					'tags' => 'name="'.$prefix.'dounflag"'.$clicksuffix,
Gideon Greenspan committed
464 465 466
					'label' => qa_lang_html('question/unflag_button'),
					'popup' => qa_lang_html('question/unflag_popup'),
				);
Scott Vivian committed
467

Gideon Greenspan committed
468 469
			if ($answer['clearflaggable'])
				$buttons['clearflags']=array(
Gideon Greenspan committed
470
					'tags' => 'name="'.$prefix.'doclearflags"'.$clicksuffix,
Gideon Greenspan committed
471 472 473 474 475 476
					'label' => qa_lang_html('question/clear_flags_button'),
					'popup' => qa_lang_html('question/clear_flags_popup'),
				);

			if ($answer['moderatable']) {
				$buttons['approve']=array(
Gideon Greenspan committed
477
					'tags' => 'name="'.$prefix.'doapprove"'.$clicksuffix,
Gideon Greenspan committed
478 479 480 481
					'label' => qa_lang_html('question/approve_button'),
				);

				$buttons['reject']=array(
Gideon Greenspan committed
482
					'tags' => 'name="'.$prefix.'doreject"'.$clicksuffix,
Gideon Greenspan committed
483 484 485 486 487 488
					'label' => qa_lang_html('question/reject_button'),
				);
			}

			if ($answer['hideable'])
				$buttons['hide']=array(
Gideon Greenspan committed
489
					'tags' => 'name="'.$prefix.'dohide"'.$clicksuffix,
Gideon Greenspan committed
490 491 492
					'label' => qa_lang_html('question/hide_button'),
					'popup' => qa_lang_html('question/hide_a_popup'),
				);
Scott Vivian committed
493

Gideon Greenspan committed
494 495
			if ($answer['reshowable'])
				$buttons['reshow']=array(
Gideon Greenspan committed
496
					'tags' => 'name="'.$prefix.'doreshow"'.$clicksuffix,
Gideon Greenspan committed
497
					'label' => qa_lang_html('question/reshow_button'),
498
					'popup' => qa_lang_html('question/reshow_a_popup'),
Gideon Greenspan committed
499
				);
Scott Vivian committed
500

Gideon Greenspan committed
501 502
			if ($answer['deleteable'])
				$buttons['delete']=array(
Gideon Greenspan committed
503
					'tags' => 'name="'.$prefix.'dodelete"'.$clicksuffix,
Gideon Greenspan committed
504 505 506
					'label' => qa_lang_html('question/delete_button'),
					'popup' => qa_lang_html('question/delete_a_popup'),
				);
Scott Vivian committed
507

Gideon Greenspan committed
508 509
			if ($answer['claimable'])
				$buttons['claim']=array(
Gideon Greenspan committed
510
					'tags' => 'name="'.$prefix.'doclaim"'.$clicksuffix,
Gideon Greenspan committed
511 512 513 514 515
					'label' => qa_lang_html('question/claim_button'),
				);

			if ($answer['followable'])
				$buttons['follow']=array(
Gideon Greenspan committed
516
					'tags' => 'name="'.$prefix.'dofollow"',
Gideon Greenspan committed
517 518 519 520 521 522
					'label' => qa_lang_html('question/follow_button'),
					'popup' => qa_lang_html('question/follow_a_popup'),
				);

			if ($answer['commentbutton'])
				$buttons['comment']=array(
Gideon Greenspan committed
523
					'tags' => 'name="'.$prefix.'docomment" onclick="return qa_toggle_element(\'c'.$answerid.'\')"',
Gideon Greenspan committed
524 525 526 527 528 529 530 531 532
					'label' => qa_lang_html('question/comment_button'),
					'popup' => qa_lang_html('question/comment_a_popup'),
				);

			$a_view['form']=array(
				'style' => 'light',
				'buttons' => $buttons,
			);
		}
Scott Vivian committed
533

Gideon Greenspan committed
534 535
		return $a_view;
	}
Scott Vivian committed
536 537


Gideon Greenspan committed
538
	function qa_page_q_comment_view($question, $parent, $comment, $usershtml, $formrequested)
Gideon Greenspan committed
539 540
/*
	Returns an element to add to the appropriate $qa_content[...]['c_list']['cs'] array for $comment as viewed by the
Gideon Greenspan committed
541 542 543
	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.
Gideon Greenspan committed
544 545 546 547
*/
	{
		$commentid=$comment['postid'];
		$questionid=($parent['basetype']=='Q') ? $parent['postid'] : $parent['parentid'];
Scott Vivian committed
548
		$answerid=($parent['basetype']=='Q') ? null : $parent['postid'];
Gideon Greenspan committed
549 550
		$userid=qa_get_logged_in_userid();
		$cookieid=qa_cookie_get();
Scott Vivian committed
551

Gideon Greenspan committed
552
		$htmloptions=qa_post_html_options($comment, null, true);
Gideon Greenspan committed
553
		$htmloptions['avatarsize']=qa_opt('avatar_q_page_c_size');
Gideon Greenspan committed
554
		$htmloptions['q_request']=qa_q_request($question['postid'], $question['title']);
Gideon Greenspan committed
555
		$c_view=qa_post_html_fields($comment, $userid, $cookieid, $usershtml, null, $htmloptions);
Scott Vivian committed
556

Gideon Greenspan committed
557 558 559 560 561
		if ($comment['queued'])
			$c_view['error']=$comment['isbyuser'] ? qa_lang_html('question/c_your_waiting_approval') : qa_lang_html('question/c_waiting_your_approval');


	//	Buttons for operating on this comment
Scott Vivian committed
562

Gideon Greenspan committed
563 564 565
		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);"';
Scott Vivian committed
566

Gideon Greenspan committed
567
			$buttons=array();
Scott Vivian committed
568

Gideon Greenspan committed
569 570
			if ($comment['editbutton'])
				$buttons['edit']=array(
Gideon Greenspan committed
571
					'tags' => 'name="'.$prefix.'doedit"',
Gideon Greenspan committed
572 573 574
					'label' => qa_lang_html('question/edit_button'),
					'popup' => qa_lang_html('question/edit_c_popup'),
				);
Scott Vivian committed
575

Gideon Greenspan committed
576 577
			if ($comment['flagbutton'])
				$buttons['flag']=array(
Gideon Greenspan committed
578
					'tags' => 'name="'.$prefix.'doflag"'.$clicksuffix,
Gideon Greenspan committed
579 580 581
					'label' => qa_lang_html($comment['flagtohide'] ? 'question/flag_hide_button' : 'question/flag_button'),
					'popup' => qa_lang_html('question/flag_c_popup'),
				);
Scott Vivian committed
582

Gideon Greenspan committed
583 584
			if ($comment['unflaggable'])
				$buttons['unflag']=array(
Gideon Greenspan committed
585
					'tags' => 'name="'.$prefix.'dounflag"'.$clicksuffix,
Gideon Greenspan committed
586 587 588
					'label' => qa_lang_html('question/unflag_button'),
					'popup' => qa_lang_html('question/unflag_popup'),
				);
Scott Vivian committed
589

Gideon Greenspan committed
590 591
			if ($comment['clearflaggable'])
				$buttons['clearflags']=array(
Gideon Greenspan committed
592
					'tags' => 'name="'.$prefix.'doclearflags"'.$clicksuffix,
Gideon Greenspan committed
593 594 595 596 597 598
					'label' => qa_lang_html('question/clear_flags_button'),
					'popup' => qa_lang_html('question/clear_flags_popup'),
				);

			if ($comment['moderatable']) {
				$buttons['approve']=array(
Gideon Greenspan committed
599
					'tags' => 'name="'.$prefix.'doapprove"'.$clicksuffix,
Gideon Greenspan committed
600 601 602 603
					'label' => qa_lang_html('question/approve_button'),
				);

				$buttons['reject']=array(
Gideon Greenspan committed
604
					'tags' => 'name="'.$prefix.'doreject"'.$clicksuffix,
Gideon Greenspan committed
605 606 607 608 609 610
					'label' => qa_lang_html('question/reject_button'),
				);
			}

			if ($comment['hideable'])
				$buttons['hide']=array(
Gideon Greenspan committed
611
					'tags' => 'name="'.$prefix.'dohide"'.$clicksuffix,
Gideon Greenspan committed
612 613 614
					'label' => qa_lang_html('question/hide_button'),
					'popup' => qa_lang_html('question/hide_c_popup'),
				);
Scott Vivian committed
615

Gideon Greenspan committed
616 617
			if ($comment['reshowable'])
				$buttons['reshow']=array(
Gideon Greenspan committed
618
					'tags' => 'name="'.$prefix.'doreshow"'.$clicksuffix,
Gideon Greenspan committed
619
					'label' => qa_lang_html('question/reshow_button'),
620
					'popup' => qa_lang_html('question/reshow_c_popup'),
Gideon Greenspan committed
621
				);
Scott Vivian committed
622

Gideon Greenspan committed
623 624
			if ($comment['deleteable'])
				$buttons['delete']=array(
Gideon Greenspan committed
625
					'tags' => 'name="'.$prefix.'dodelete"'.$clicksuffix,
Gideon Greenspan committed
626 627 628
					'label' => qa_lang_html('question/delete_button'),
					'popup' => qa_lang_html('question/delete_c_popup'),
				);
Scott Vivian committed
629

Gideon Greenspan committed
630 631
			if ($comment['claimable'])
				$buttons['claim']=array(
Gideon Greenspan committed
632
					'tags' => 'name="'.$prefix.'doclaim"'.$clicksuffix,
Gideon Greenspan committed
633 634
					'label' => qa_lang_html('question/claim_button'),
				);
Scott Vivian committed
635

Gideon Greenspan committed
636 637
			if ($parent['commentbutton'] && qa_opt('show_c_reply_buttons') && ($comment['type']=='C'))
				$buttons['comment']=array(
Gideon Greenspan committed
638 639
					'tags' => 'name="'.(($parent['basetype']=='Q') ? 'q' : ('a'.qa_html($parent['postid']))).
						'_docomment" onclick="return qa_toggle_element(\'c'.qa_html($parent['postid']).'\')"',
Gideon Greenspan committed
640 641 642 643 644 645 646 647 648
					'label' => qa_lang_html('question/reply_button'),
					'popup' => qa_lang_html('question/reply_c_popup'),
				);

			$c_view['form']=array(
				'style' => 'light',
				'buttons' => $buttons,
			);
		}
Scott Vivian committed
649

Gideon Greenspan committed
650 651 652 653
		return $c_view;
	}


Gideon Greenspan committed
654
	function qa_page_q_comment_follow_list($question, $parent, $commentsfollows, $alwaysfull, $usershtml, $formrequested, $formpostid)
Gideon Greenspan committed
655
/*
Gideon Greenspan committed
656 657 658 659 660 661 662
	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.
Gideon Greenspan committed
663 664 665 666 667
*/
	{
		$parentid=$parent['postid'];
		$userid=qa_get_logged_in_userid();
		$cookieid=qa_cookie_get();
Scott Vivian committed
668

Gideon Greenspan committed
669
		$commentlist=array(
Gideon Greenspan committed
670
			'tags' => 'id="c'.qa_html($parentid).'_list"',
Gideon Greenspan committed
671 672 673 674
			'cs' => array(),
		);

		$showcomments=array();
Scott Vivian committed
675

Gideon Greenspan committed
676 677 678
		foreach ($commentsfollows as $commentfollowid => $commentfollow)
			if (($commentfollow['parentid']==$parentid) && $commentfollow['viewable'] && ($commentfollowid!=$formpostid) )
				$showcomments[$commentfollowid]=$commentfollow;
Scott Vivian committed
679

Gideon Greenspan committed
680
		$countshowcomments=count($showcomments);
Scott Vivian committed
681

Gideon Greenspan committed
682 683 684 685
		if ( (!$alwaysfull) && ($countshowcomments > qa_opt('show_fewer_cs_from')) )
			$skipfirst=$countshowcomments-qa_opt('show_fewer_cs_count');
		else
			$skipfirst=0;
Scott Vivian committed
686

Gideon Greenspan committed
687 688 689 690 691
		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 Vivian committed
692

Gideon Greenspan committed
693 694 695 696 697 698
		} 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 Vivian committed
699

Gideon Greenspan committed
700 701
		if ($skipfirst>0)
			$commentlist['cs'][$parentid]=array(
Gideon Greenspan committed
702
				'url' => qa_html('?state=showcomments-'.$parentid.'&show='.$parentid.'#'.urlencode(qa_anchor($parent['basetype'], $parentid))),
Scott Vivian committed
703

Gideon Greenspan committed
704
				'expand_tags' => 'onclick="return qa_show_comments('.qa_js($question['postid']).', '.qa_js($parentid).', this);"',
Scott Vivian committed
705

Gideon Greenspan committed
706 707
				'title' => $expandtitle,
			);
Scott Vivian committed
708

Gideon Greenspan committed
709 710 711
		foreach ($showcomments as $commentfollowid => $commentfollow)
			if ($skipfirst>0)
				$skipfirst--;
Scott Vivian committed
712

Gideon Greenspan committed
713
			elseif ($commentfollow['basetype']=='C') {
Gideon Greenspan committed
714
				$commentlist['cs'][$commentfollowid]=qa_page_q_comment_view($question, $parent, $commentfollow, $usershtml, $formrequested);
Gideon Greenspan committed
715 716

			} elseif ($commentfollow['basetype']=='Q') {
Gideon Greenspan committed
717
				$htmloptions=qa_post_html_options($commentfollow);
Gideon Greenspan committed
718
				$htmloptions['avatarsize']=qa_opt('avatar_q_page_c_size');
Scott Vivian committed
719

Gideon Greenspan committed
720 721
				$commentlist['cs'][$commentfollowid]=qa_post_html_fields($commentfollow, $userid, $cookieid, $usershtml, null, $htmloptions);
			}
Scott Vivian committed
722

Gideon Greenspan committed
723 724
		if (!count($commentlist['cs']))
			$commentlist['hidden']=true;
Scott Vivian committed
725

Gideon Greenspan committed
726 727
		return $commentlist;
	}
Scott Vivian committed
728

Gideon Greenspan committed
729

Gideon Greenspan committed
730
	function qa_page_q_add_a_form(&$qa_content, $formid, $captchareason, $question, $in, $errors, $loadnow, $formrequested)
Gideon Greenspan committed
731
/*
Gideon Greenspan committed
732 733 734
	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
Gideon Greenspan committed
735
	$formrequested to true if the user explicitly requested it, as opposed being shown automatically.
Gideon Greenspan committed
736 737
*/
	{
Gideon Greenspan committed
738 739 740 741
		// 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)) {
Gideon Greenspan committed
742 743 744 745 746
			case 'login':
				$form=array(
					'title' => qa_insert_login_links(qa_lang_html('question/answer_must_login'), qa_request())
				);
				break;
Scott Vivian committed
747

Gideon Greenspan committed
748 749 750 751 752
			case 'confirm':
				$form=array(
					'title' => qa_insert_login_links(qa_lang_html('question/answer_must_confirm'), qa_request())
				);
				break;
Scott Vivian committed
753

Gideon Greenspan committed
754 755 756 757 758
			case 'approve':
				$form=array(
					'title' => qa_lang_html('question/answer_must_be_approved')
				);
				break;
Scott Vivian committed
759

Gideon Greenspan committed
760 761 762 763 764
			case 'limit':
				$form=array(
					'title' => qa_lang_html('question/answer_limit')
				);
				break;
Scott Vivian committed
765

Gideon Greenspan committed
766 767 768 769 770
			default:
				$form=array(
					'title' => qa_lang_html('users/no_permission')
				);
				break;
Scott Vivian committed
771

Gideon Greenspan committed
772 773 774
			case false:
				$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_as');
				$editor=qa_load_editor(@$in['content'], @$in['format'], $editorname);
Scott Vivian committed
775

Gideon Greenspan committed
776 777 778 779
				if (method_exists($editor, 'update_script'))
					$updatescript=$editor->update_script('a_content');
				else
					$updatescript='';
Scott Vivian committed
780

Gideon Greenspan committed
781
				$custom=qa_opt('show_custom_answer') ? trim(qa_opt('custom_answer')) : '';
Scott Vivian committed
782

Gideon Greenspan committed
783
				$form=array(
Gideon Greenspan committed
784
					'tags' => 'method="post" action="'.qa_self_html().'" name="a_form"',
Scott Vivian committed
785

Gideon Greenspan committed
786
					'title' => qa_lang_html('question/your_answer_title'),
Scott Vivian committed
787

Gideon Greenspan committed
788 789 790 791 792
					'fields' => array(
						'custom' => array(
							'type' => 'custom',
							'note' => $custom,
						),
Scott Vivian committed
793

Gideon Greenspan committed
794
						'content' => array_merge(
Gideon Greenspan committed
795
							qa_editor_load_field($editor, $qa_content, @$in['content'], @$in['format'], 'a_content', 12, $formrequested, $loadnow),
Gideon Greenspan committed
796 797 798 799 800
							array(
								'error' => qa_html(@$errors['content']),
							)
						),
					),
Scott Vivian committed
801

Gideon Greenspan committed
802 803
					'buttons' => array(
						'answer' => array(
Gideon Greenspan committed
804
							'tags' => 'onclick="'.$updatescript.' return qa_submit_answer('.qa_js($question['postid']).', this);"',
Gideon Greenspan committed
805 806 807
							'label' => qa_lang_html('question/add_answer_button'),
						),
					),
Scott Vivian committed
808

Gideon Greenspan committed
809 810 811
					'hidden' => array(
						'a_editor' => qa_html($editorname),
						'a_doadd' => '1',
Gideon Greenspan committed
812
						'code' => qa_get_form_security_code('answer-'.$question['postid']),
Gideon Greenspan committed
813 814
					),
				);
Scott Vivian committed
815

Gideon Greenspan committed
816 817 818
				if (!strlen($custom))
					unset($form['fields']['custom']);

Gideon Greenspan committed
819
				if ($formrequested || !$loadnow)
Gideon Greenspan committed
820
					$form['buttons']['cancel']=array(
Gideon Greenspan committed
821
						'tags' => 'name="docancel"',
Gideon Greenspan committed
822 823
						'label' => qa_lang_html('main/cancel_button'),
					);
Scott Vivian committed
824

Gideon Greenspan committed
825 826
				if (!qa_is_logged_in())
					qa_set_up_name_field($qa_content, $form['fields'], @$in['name'], 'a_');
Scott Vivian committed
827

Gideon Greenspan committed
828 829
				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 Vivian committed
830

Gideon Greenspan committed
831
				$onloads=array();
Scott Vivian committed
832

Gideon Greenspan committed
833 834
				if ($captchareason) {
					$captchaloadscript=qa_set_up_captcha_field($qa_content, $form['fields'], $errors, qa_captcha_reason_note($captchareason));
Scott Vivian committed
835

Gideon Greenspan committed
836
					if (strlen($captchaloadscript))
Gideon Greenspan committed
837
						$onloads[]='document.getElementById('.qa_js($formid).').qa_show=function() { '.$captchaloadscript.' };';
Gideon Greenspan committed
838 839
				}

Gideon Greenspan committed
840
				if (!$loadnow) {
Gideon Greenspan committed
841
					if (method_exists($editor, 'load_script'))
Gideon Greenspan committed
842
						$onloads[]='document.getElementById('.qa_js($formid).').qa_load=function() { '.$editor->load_script('a_content').' };';
Scott Vivian committed
843

Gideon Greenspan committed
844
					$form['buttons']['cancel']['tags'].=' onclick="return qa_toggle_element();"';
Gideon Greenspan committed
845
				}
Scott Vivian committed
846

Gideon Greenspan committed
847 848
				if (!$formrequested) {
					if (method_exists($editor, 'focus_script'))
Gideon Greenspan committed
849
						$onloads[]='document.getElementById('.qa_js($formid).').qa_focus=function() { '.$editor->focus_script('a_content').' };';
Gideon Greenspan committed
850
				}
Gideon Greenspan committed
851 852 853 854 855

				if (count($onloads))
					$qa_content['script_onloads'][]=$onloads;
				break;
		}
Scott Vivian committed
856

Gideon Greenspan committed
857
		$form['id']=$formid;
Gideon Greenspan committed
858
		$form['collapse']=!$loadnow;
Gideon Greenspan committed
859
		$form['style']='tall';
Scott Vivian committed
860

Gideon Greenspan committed
861 862
		return $form;
	}
Scott Vivian committed
863 864


Gideon Greenspan committed
865
	function qa_page_q_add_c_form(&$qa_content, $question, $parent, $formid, $captchareason, $in, $errors, $loadfocusnow)
Gideon Greenspan committed
866
/*
Gideon Greenspan committed
867 868
	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
Gideon Greenspan committed
869 870 871 872
	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.
*/
	{
Gideon Greenspan committed
873 874 875 876
		// 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)) {
Gideon Greenspan committed
877 878 879 880 881
			case 'login':
				$form=array(
					'title' => qa_insert_login_links(qa_lang_html('question/comment_must_login'), qa_request())
				);
				break;
Scott Vivian committed
882

Gideon Greenspan committed
883 884 885 886 887
			case 'confirm':
				$form=array(
					'title' => qa_insert_login_links(qa_lang_html('question/comment_must_confirm'), qa_request())
				);
				break;
Scott Vivian committed
888

Gideon Greenspan committed
889 890 891 892 893
			case 'approve':
				$form=array(
					'title' => qa_lang_html('question/comment_must_be_approved')
				);
				break;
Scott Vivian committed
894

Gideon Greenspan committed
895 896 897 898 899
			case 'limit':
				$form=array(
					'title' => qa_lang_html('question/comment_limit')
				);
				break;
Scott Vivian committed
900

Gideon Greenspan committed
901 902 903 904 905
			default:
				$form=array(
					'title' => qa_lang_html('users/no_permission')
				);
				break;
Scott Vivian committed
906

Gideon Greenspan committed
907
			case false:
Gideon Greenspan committed
908
				$prefix='c'.$parent['postid'].'_';
Scott Vivian committed
909

Gideon Greenspan committed
910 911
				$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_cs');
				$editor=qa_load_editor(@$in['content'], @$in['format'], $editorname);
Scott Vivian committed
912

Gideon Greenspan committed
913 914 915 916 917 918
				if (method_exists($editor, 'update_script'))
					$updatescript=$editor->update_script($prefix.'content');
				else
					$updatescript='';

				$custom=qa_opt('show_custom_comment') ? trim(qa_opt('custom_comment')) : '';
Scott Vivian committed
919

Gideon Greenspan committed
920
				$form=array(
Gideon Greenspan committed
921
					'tags' => 'method="post" action="'.qa_self_html().'" name="c_form_'.qa_html($parent['postid']).'"',
Scott Vivian committed
922

Gideon Greenspan committed
923
					'title' => qa_lang_html(($question['postid']==$parent['postid']) ? 'question/your_comment_q' : 'question/your_comment_a'),
Scott Vivian committed
924

Gideon Greenspan committed
925 926 927 928 929
					'fields' => array(
						'custom' => array(
							'type' => 'custom',
							'note' => $custom,
						),
Scott Vivian committed
930

Gideon Greenspan committed
931 932 933 934 935 936 937
						'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 Vivian committed
938

Gideon Greenspan committed
939 940
					'buttons' => array(
						'comment' => array(
Gideon Greenspan committed
941
							'tags' => 'onclick="'.$updatescript.' return qa_submit_comment('.qa_js($question['postid']).', '.qa_js($parent['postid']).', this);"',
Gideon Greenspan committed
942 943
							'label' => qa_lang_html('question/add_comment_button'),
						),
Scott Vivian committed
944

Gideon Greenspan committed
945
						'cancel' => array(
Gideon Greenspan committed
946
							'tags' => 'name="docancel"',
Gideon Greenspan committed
947 948 949
							'label' => qa_lang_html('main/cancel_button'),
						),
					),
Scott Vivian committed
950

Gideon Greenspan committed
951 952 953
					'hidden' => array(
						$prefix.'editor' => qa_html($editorname),
						$prefix.'doadd' => '1',
Gideon Greenspan committed
954
						$prefix.'code' => qa_get_form_security_code('comment-'.$parent['postid']),
Gideon Greenspan committed
955 956
					),
				);
Scott Vivian committed
957

Gideon Greenspan committed
958 959
				if (!strlen($custom))
					unset($form['fields']['custom']);
Scott Vivian committed
960

Gideon Greenspan committed
961 962 963
				if (!qa_is_logged_in())
					qa_set_up_name_field($qa_content, $form['fields'], @$in['name'], $prefix);

Gideon Greenspan committed
964 965
				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 Vivian committed
966

Gideon Greenspan committed
967 968
				$onloads=array();

Gideon Greenspan committed
969 970
				if ($captchareason) {
					$captchaloadscript=qa_set_up_captcha_field($qa_content, $form['fields'], $errors, qa_captcha_reason_note($captchareason));
Scott Vivian committed
971

Gideon Greenspan committed
972
					if (strlen($captchaloadscript))
Gideon Greenspan committed
973
						$onloads[]='document.getElementById('.qa_js($formid).').qa_show=function() { '.$captchaloadscript.' };';
Gideon Greenspan committed
974
				}
Scott Vivian committed
975

Gideon Greenspan committed
976 977
				if (!$loadfocusnow) {
					if (method_exists($editor, 'load_script'))
Gideon Greenspan committed
978
						$onloads[]='document.getElementById('.qa_js($formid).').qa_load=function() { '.$editor->load_script($prefix.'content').' };';
Gideon Greenspan committed
979
					if (method_exists($editor, 'focus_script'))
Gideon Greenspan committed
980
						$onloads[]='document.getElementById('.qa_js($formid).').qa_focus=function() { '.$editor->focus_script($prefix.'content').' };';
Scott Vivian committed
981

Gideon Greenspan committed
982
					$form['buttons']['cancel']['tags'].=' onclick="return qa_toggle_element()"';
Gideon Greenspan committed
983 984 985 986 987
				}

				if (count($onloads))
					$qa_content['script_onloads'][]=$onloads;
		}
Scott Vivian committed
988

Gideon Greenspan committed
989 990 991
		$form['id']=$formid;
		$form['collapse']=!$loadfocusnow;
		$form['style']='tall';
Scott Vivian committed
992

Gideon Greenspan committed
993 994 995
		return $form;
	}

Scott Vivian committed
996

Gideon Greenspan committed
997 998 999
/*
	Omit PHP closing tag to help avoid accidental output
*/